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 * Represents a location of a trace within a source document |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine; |
|
20 |
|
21 import java.util.ArrayList; |
|
22 import java.util.Collections; |
|
23 import java.util.Iterator; |
|
24 import java.util.List; |
|
25 |
|
26 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode; |
|
27 import com.nokia.tracecompiler.engine.source.SourceParserResult; |
|
28 import com.nokia.tracecompiler.engine.source.SourceParserRule; |
|
29 import com.nokia.tracecompiler.engine.source.SourceProperties; |
|
30 import com.nokia.tracecompiler.model.Trace; |
|
31 import com.nokia.tracecompiler.model.TraceCompilerErrorParameters; |
|
32 import com.nokia.tracecompiler.model.TraceCompilerException; |
|
33 import com.nokia.tracecompiler.model.TraceObject; |
|
34 import com.nokia.tracecompiler.source.SourceConstants; |
|
35 import com.nokia.tracecompiler.source.SourceLocation; |
|
36 import com.nokia.tracecompiler.source.SourceLocationListener; |
|
37 |
|
38 /** |
|
39 * Represents a location of a trace within a source document. The locations of a |
|
40 * document are updated by TraceLocationUpdater when document is changed |
|
41 * |
|
42 */ |
|
43 public class TraceLocation extends SourceLocation implements LocationProperties { |
|
44 |
|
45 /** |
|
46 * Source which owns this location |
|
47 */ |
|
48 private SourceProperties source; |
|
49 |
|
50 /** |
|
51 * Name of the trace as parsed from source |
|
52 */ |
|
53 private String originalName; |
|
54 |
|
55 /** |
|
56 * Name after conversion to valid trace name |
|
57 */ |
|
58 private String convertedName; |
|
59 |
|
60 /** |
|
61 * Text of the trace as parsed from source |
|
62 */ |
|
63 private String traceText; |
|
64 |
|
65 /** |
|
66 * The tag of the parser that found this trace location |
|
67 */ |
|
68 private String tag; |
|
69 |
|
70 /** |
|
71 * Content changed flag |
|
72 */ |
|
73 private boolean contentChanged; |
|
74 |
|
75 /** |
|
76 * Name changed flag |
|
77 */ |
|
78 private boolean nameChanged; |
|
79 |
|
80 /** |
|
81 * The location list |
|
82 */ |
|
83 private TraceLocationList list; |
|
84 |
|
85 /** |
|
86 * The parameters |
|
87 */ |
|
88 private List<String> parameters; |
|
89 |
|
90 /** |
|
91 * Parser-specific data associated with this location |
|
92 */ |
|
93 private List<String> parserData; |
|
94 |
|
95 /** |
|
96 * Rule which defines how the parameters found from source are interpreted |
|
97 */ |
|
98 private SourceParserRule parserRule; |
|
99 |
|
100 /** |
|
101 * Flag, which determines if this location has changed after last convert |
|
102 * operation |
|
103 */ |
|
104 private boolean changedAfterConvert = true; |
|
105 |
|
106 /** |
|
107 * Last notified validity code |
|
108 */ |
|
109 private TraceCompilerErrorCode notifiedValidity = TraceCompilerErrorCode.OK; |
|
110 |
|
111 /** |
|
112 * Last notified validity parameters |
|
113 */ |
|
114 private TraceCompilerErrorParameters notifiedValidityParameters; |
|
115 |
|
116 /** |
|
117 * Parser error code |
|
118 */ |
|
119 private TraceCompilerErrorCode parserErrorCode = TraceCompilerErrorCode.OK; |
|
120 |
|
121 /** |
|
122 * Parser error parameters |
|
123 */ |
|
124 private TraceCompilerErrorParameters parserErrorParameters; |
|
125 |
|
126 /** |
|
127 * Converter error code |
|
128 */ |
|
129 private TraceCompilerErrorCode converterErrorCode = TraceCompilerErrorCode.OK; |
|
130 |
|
131 /** |
|
132 * Converter error parameters |
|
133 */ |
|
134 private TraceCompilerErrorParameters converterErrorParameters; |
|
135 |
|
136 /** |
|
137 * Creates a new location |
|
138 * |
|
139 * @param source |
|
140 * the source where the location is associated |
|
141 * @param offset |
|
142 * the offset to the trace within the source document |
|
143 * @param length |
|
144 * the length of the trace |
|
145 */ |
|
146 public TraceLocation(SourceProperties source, int offset, int length) { |
|
147 super(source.getSourceParser(), offset, length); |
|
148 this.source = source; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Sets the content changed flag. If <i>changed</i> is false this also sets |
|
153 * the name changed flag to false |
|
154 * |
|
155 * @param changed |
|
156 * the new changed flag |
|
157 */ |
|
158 public void setContentChanged(boolean changed) { |
|
159 this.contentChanged = changed; |
|
160 if (!changed) { |
|
161 nameChanged = false; |
|
162 } |
|
163 } |
|
164 |
|
165 /** |
|
166 * Checks if the content has changed. |
|
167 * |
|
168 * @return the content changed flag |
|
169 */ |
|
170 public boolean isContentChanged() { |
|
171 return contentChanged; |
|
172 } |
|
173 |
|
174 /** |
|
175 * Checks if the name has changed. |
|
176 * |
|
177 * @return the name changed flag |
|
178 */ |
|
179 public boolean isNameChanged() { |
|
180 return contentChanged && nameChanged; |
|
181 } |
|
182 |
|
183 /** |
|
184 * Gets the trace this location is associated to or null if unrelated |
|
185 * |
|
186 * @return the trace |
|
187 */ |
|
188 public Trace getTrace() { |
|
189 Trace retval = null; |
|
190 if (list != null) { |
|
191 TraceObject object = list.getOwner(); |
|
192 if (object instanceof Trace) { |
|
193 retval = (Trace) object; |
|
194 } |
|
195 } |
|
196 return retval; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Gets the name of the trace as parsed from source |
|
201 * |
|
202 * @return the name of the trace |
|
203 */ |
|
204 public String getOriginalName() { |
|
205 return originalName; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Gets the name of the trace after conversion to valid name |
|
210 * |
|
211 * @return the name of the trace |
|
212 */ |
|
213 public String getConvertedName() { |
|
214 return convertedName; |
|
215 } |
|
216 |
|
217 /** |
|
218 * Gets the text of the trace |
|
219 * |
|
220 * @return the text of the trace |
|
221 */ |
|
222 public String getTraceText() { |
|
223 return traceText; |
|
224 } |
|
225 |
|
226 /** |
|
227 * Returns the source properties this location belongs to |
|
228 * |
|
229 * @return source properties |
|
230 */ |
|
231 public SourceProperties getSource() { |
|
232 return source; |
|
233 } |
|
234 |
|
235 /** |
|
236 * Sets the location list which owns this location |
|
237 * |
|
238 * @param list |
|
239 * the location list |
|
240 */ |
|
241 public void setLocationList(TraceLocationList list) { |
|
242 this.list = list; |
|
243 } |
|
244 |
|
245 /** |
|
246 * Gets the location list this location belongs to |
|
247 * |
|
248 * @return the list |
|
249 */ |
|
250 public TraceLocationList getLocationList() { |
|
251 return list; |
|
252 } |
|
253 |
|
254 /** |
|
255 * Sets the parser rule that will be used to parse the data of this |
|
256 * location. Must be called before setData |
|
257 * |
|
258 * @param rule |
|
259 * the rule used to parse the data |
|
260 */ |
|
261 public void setParserRule(SourceParserRule rule) { |
|
262 parserRule = rule; |
|
263 } |
|
264 |
|
265 /** |
|
266 * Gets the parser rule that found this location |
|
267 * |
|
268 * @return the parser rule |
|
269 */ |
|
270 public SourceParserRule getParserRule() { |
|
271 return parserRule; |
|
272 } |
|
273 |
|
274 /** |
|
275 * Sets the trace tag that was found from source |
|
276 * |
|
277 * @param tag |
|
278 * the trace tag |
|
279 */ |
|
280 public void setTag(String tag) { |
|
281 this.tag = tag; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Gets the trace tag |
|
286 * |
|
287 * @return the tag |
|
288 */ |
|
289 public String getTag() { |
|
290 return tag; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Sets the trace data |
|
295 * |
|
296 * @param list |
|
297 * the list of parameters |
|
298 */ |
|
299 public void setData(ArrayList<String> list) { |
|
300 try { |
|
301 SourceParserResult result = parserRule.parseParameters(tag, list); |
|
302 setData(result.originalName, result.convertedName, |
|
303 result.traceText, result.parameters, result.parserData); |
|
304 parserRule.getLocationParser().processNewLocation(this); |
|
305 parserErrorCode = TraceCompilerErrorCode.OK; |
|
306 parserErrorParameters = null; |
|
307 converterErrorCode = TraceCompilerErrorCode.OK; |
|
308 converterErrorParameters = null; |
|
309 } catch (TraceCompilerException e) { |
|
310 parserErrorCode = (TraceCompilerErrorCode) e.getErrorCode(); |
|
311 parserErrorParameters = e.getErrorParameters(); |
|
312 } |
|
313 } |
|
314 |
|
315 /** |
|
316 * Sets the trace name and parameter list |
|
317 * |
|
318 * @param originalName |
|
319 * the name parsed from source |
|
320 * @param convertedName |
|
321 * the name after conversion |
|
322 * @param traceText |
|
323 * the text parsed from source |
|
324 * @param parameters |
|
325 * the list of parameters parsed from source |
|
326 * @param parserData |
|
327 * list of parser-specific data |
|
328 */ |
|
329 private void setData(String originalName, String convertedName, |
|
330 String traceText, List<String> parameters, List<String> parserData) { |
|
331 if (!convertedName.equals(this.convertedName)) { |
|
332 this.convertedName = convertedName; |
|
333 nameChanged = true; |
|
334 } |
|
335 this.originalName = originalName; |
|
336 //remove line feed if the trace text spans multiple lines. |
|
337 if (traceText != null ) { |
|
338 this.traceText = traceText.replaceAll(SourceConstants.LINE_FEED, ""); //$NON-NLS-1$ //$NON-NLS-2$ |
|
339 } |
|
340 this.parameters = parameters; |
|
341 this.parserData = parserData; |
|
342 } |
|
343 |
|
344 /** |
|
345 * Removes the parameter at given index |
|
346 * |
|
347 * @param index |
|
348 * the index |
|
349 */ |
|
350 public void removeParameterAt(int index) { |
|
351 // NOTE: Does not cause validity callbacks |
|
352 // This is currently only used temporarily when removing a parameter |
|
353 // from trace. This is needed to correctly update the source code |
|
354 if (parameters != null && index >= 0 && index < parameters.size()) { |
|
355 parameters.remove(index); |
|
356 } |
|
357 } |
|
358 |
|
359 /** |
|
360 * Gets the number of parameters found from the source code |
|
361 * |
|
362 * @return the number of parameters |
|
363 */ |
|
364 public int getParameterCount() { |
|
365 return parameters != null ? parameters.size() : 0; |
|
366 } |
|
367 |
|
368 /** |
|
369 * Gets the parameter at given index |
|
370 * |
|
371 * @param index |
|
372 * the parameter index |
|
373 * @return the parameter at the index |
|
374 */ |
|
375 public String getParameter(int index) { |
|
376 return parameters.get(index); |
|
377 } |
|
378 |
|
379 /** |
|
380 * Gets the parameters |
|
381 * |
|
382 * @return iterator over the parameters |
|
383 */ |
|
384 public Iterator<String> getParameters() { |
|
385 List<String> list = null; |
|
386 if (parameters != null) { |
|
387 list = parameters; |
|
388 } else { |
|
389 list = Collections.emptyList(); |
|
390 } |
|
391 return list.iterator(); |
|
392 } |
|
393 |
|
394 /** |
|
395 * Gets the parser-specific data |
|
396 * |
|
397 * @return the parser data |
|
398 */ |
|
399 public List<String> getParserData() { |
|
400 return parserData; |
|
401 } |
|
402 |
|
403 /** |
|
404 * Gets the current location validity code. |
|
405 * |
|
406 * @return the validity code |
|
407 */ |
|
408 public TraceCompilerErrorCode getValidityCode() { |
|
409 return notifiedValidity; |
|
410 } |
|
411 |
|
412 /** |
|
413 * Gets the parameters associated with the current location validity code. |
|
414 * |
|
415 * @return the validity code |
|
416 */ |
|
417 public TraceCompilerErrorParameters getValidityParameters() { |
|
418 return notifiedValidityParameters; |
|
419 } |
|
420 |
|
421 /** |
|
422 * Creates a notification if validity has changed. This is initially called |
|
423 * from the location list when a location is added to it and after that from |
|
424 * notifyLocationChanged. |
|
425 */ |
|
426 void runValidityCheck() { |
|
427 TraceCompilerErrorCode code; |
|
428 TraceCompilerErrorParameters parameters; |
|
429 if (parserErrorCode != TraceCompilerErrorCode.OK) { |
|
430 code = parserErrorCode; |
|
431 parameters = parserErrorParameters; |
|
432 } else if (converterErrorCode != TraceCompilerErrorCode.OK) { |
|
433 code = converterErrorCode; |
|
434 parameters = converterErrorParameters; |
|
435 } else { |
|
436 code = TraceCompilerErrorCode.TRACE_DOES_NOT_EXIST; |
|
437 parameters = null; |
|
438 if (parserRule != null) { |
|
439 code = parserRule.getLocationParser().checkLocationValidity( |
|
440 this); |
|
441 } |
|
442 } |
|
443 if (code != notifiedValidity) { |
|
444 notifiedValidity = code; |
|
445 notifiedValidityParameters = parameters; |
|
446 Iterator<SourceLocationListener> itr = getListeners(); |
|
447 while (itr.hasNext()) { |
|
448 SourceLocationListener listener = itr.next(); |
|
449 if (listener instanceof SourceLocationListener) { |
|
450 ((SourceLocationListener) listener) |
|
451 .locationValidityChanged(this); |
|
452 } |
|
453 } |
|
454 } |
|
455 } |
|
456 |
|
457 /** |
|
458 * Sets the converter error code and runs the validity check to notify |
|
459 * listeners about change in error code |
|
460 * |
|
461 * @param errorCode |
|
462 * the new error code |
|
463 * @param parameters |
|
464 * the error parameters |
|
465 */ |
|
466 void setConverterErrorCode(TraceCompilerErrorCode errorCode, |
|
467 TraceCompilerErrorParameters parameters) { |
|
468 converterErrorCode = errorCode; |
|
469 converterErrorParameters = parameters; |
|
470 runValidityCheck(); |
|
471 } |
|
472 |
|
473 /** |
|
474 * Flag, which determines if the location has changed since last convert |
|
475 * operation |
|
476 * |
|
477 * @return the flag |
|
478 */ |
|
479 public boolean hasChangedAfterConvert() { |
|
480 return changedAfterConvert; |
|
481 } |
|
482 |
|
483 /** |
|
484 * Called when the location has been converted. Sets the changed after |
|
485 * convert flag to false |
|
486 */ |
|
487 public void locationConverted() { |
|
488 changedAfterConvert = false; |
|
489 } |
|
490 |
|
491 } |
|