|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Base class for locations |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.source; |
|
20 |
|
21 /** |
|
22 * Base class for locations |
|
23 * |
|
24 */ |
|
25 public class SourceLocationBase { |
|
26 |
|
27 /** |
|
28 * Source parser |
|
29 */ |
|
30 private SourceParser parser; |
|
31 |
|
32 /** |
|
33 * Position abstraction |
|
34 */ |
|
35 private SourceLocationInterface position; |
|
36 |
|
37 /** |
|
38 * Line number is cached and updated when changes occur |
|
39 */ |
|
40 private int currentLine = -1; |
|
41 |
|
42 /** |
|
43 * Constructor |
|
44 * |
|
45 * @param parser |
|
46 * the source parser |
|
47 * @param offset |
|
48 * the offset to the location |
|
49 */ |
|
50 protected SourceLocationBase(SourceParser parser, int offset) { |
|
51 this.parser = parser; |
|
52 position = parser.getDocumentFramework() |
|
53 .createLocation(this, offset, 0); |
|
54 } |
|
55 |
|
56 /** |
|
57 * Constructor |
|
58 * |
|
59 * @param parser |
|
60 * the source parser |
|
61 * @param offset |
|
62 * the offset to the location |
|
63 * @param length |
|
64 * the location length |
|
65 */ |
|
66 protected SourceLocationBase(SourceParser parser, int offset, int length) { |
|
67 this.parser = parser; |
|
68 position = parser.getDocumentFramework().createLocation(this, offset, |
|
69 length); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Gets the offset |
|
74 * |
|
75 * @return offset |
|
76 */ |
|
77 public final int getOffset() { |
|
78 return position.getOffset(); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Sets the offset |
|
83 * |
|
84 * @param offset |
|
85 * new offset |
|
86 */ |
|
87 public final void setOffset(int offset) { |
|
88 position.setOffset(offset); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Gets the length |
|
93 * |
|
94 * @return length |
|
95 */ |
|
96 public final int getLength() { |
|
97 return position.getLength(); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Sets the length |
|
102 * |
|
103 * @param length |
|
104 * the length |
|
105 */ |
|
106 public final void setLength(int length) { |
|
107 position.setLength(length); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Returns deleted flag |
|
112 * |
|
113 * @return the flag |
|
114 */ |
|
115 public final boolean isDeleted() { |
|
116 return position.isDeleted(); |
|
117 } |
|
118 |
|
119 /** |
|
120 * Sets the deleted flag |
|
121 */ |
|
122 public final void delete() { |
|
123 position.delete(); |
|
124 } |
|
125 |
|
126 /** |
|
127 * Gets the location interface |
|
128 * |
|
129 * @return the location interface |
|
130 */ |
|
131 final SourceLocationInterface getLocation() { |
|
132 return position; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Gets the source parser |
|
137 * |
|
138 * @return the parser |
|
139 */ |
|
140 public SourceParser getParser() { |
|
141 return parser; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Resets the source parser |
|
146 */ |
|
147 protected void resetParser() { |
|
148 parser = null; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Gets the line number of this location |
|
153 * |
|
154 * @return line number |
|
155 */ |
|
156 public int getLineNumber() { |
|
157 // Line number is set to -1 when notifyUpdate is called |
|
158 if (parser != null) { |
|
159 if (currentLine == -1) { |
|
160 currentLine = parser.getLineNumber(getOffset()); |
|
161 } |
|
162 } else { |
|
163 currentLine = -1; |
|
164 } |
|
165 return currentLine; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Resets the line number |
|
170 */ |
|
171 protected void resetLineNumber() { |
|
172 currentLine = -1; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Gets the source file name |
|
177 * |
|
178 * @return the file name |
|
179 */ |
|
180 public String getFileName() { |
|
181 String retval = null; |
|
182 if (parser != null) { |
|
183 SourceDocumentInterface owner = parser.getSource(); |
|
184 if (owner != null) { |
|
185 SourcePropertyProvider propertyProvider = owner |
|
186 .getPropertyProvider(); |
|
187 if (propertyProvider != null) { |
|
188 retval = propertyProvider.getFileName(); |
|
189 } |
|
190 } |
|
191 } |
|
192 return retval; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Gets the source file path |
|
197 * |
|
198 * @return the path |
|
199 */ |
|
200 public String getFilePath() { |
|
201 String retval = null; |
|
202 if (parser != null) { |
|
203 SourceDocumentInterface owner = parser.getSource(); |
|
204 if (owner != null) { |
|
205 SourcePropertyProvider propertyProvider = owner |
|
206 .getPropertyProvider(); |
|
207 if (propertyProvider != null) { |
|
208 retval = propertyProvider.getFilePath(); |
|
209 } |
|
210 } |
|
211 } |
|
212 return retval; |
|
213 } |
|
214 |
|
215 } |