Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
890916ec | 2 | * Copyright (c) 2010, 2014 Ericsson |
be222f56 PT |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
890916ec | 11 | * Matthew Khouzam - Add support for default parsers |
be222f56 PT |
12 | *******************************************************************************/ |
13 | ||
47aafe74 | 14 | package org.eclipse.linuxtools.tmf.core.parsers.custom; |
be222f56 | 15 | |
be222f56 PT |
16 | import java.io.File; |
17 | import java.io.FileWriter; | |
18 | import java.io.IOException; | |
19 | import java.io.StringWriter; | |
20 | import java.util.ArrayList; | |
890916ec MK |
21 | import java.util.Arrays; |
22 | import java.util.Comparator; | |
be222f56 PT |
23 | import java.util.List; |
24 | import java.util.Map; | |
890916ec MK |
25 | import java.util.Set; |
26 | import java.util.TreeSet; | |
be222f56 PT |
27 | import java.util.regex.Pattern; |
28 | import java.util.regex.PatternSyntaxException; | |
29 | ||
30 | import javax.xml.parsers.DocumentBuilder; | |
31 | import javax.xml.parsers.DocumentBuilderFactory; | |
32 | import javax.xml.parsers.ParserConfigurationException; | |
33 | import javax.xml.transform.OutputKeys; | |
34 | import javax.xml.transform.Transformer; | |
35 | import javax.xml.transform.TransformerConfigurationException; | |
36 | import javax.xml.transform.TransformerException; | |
37 | import javax.xml.transform.TransformerFactory; | |
38 | import javax.xml.transform.TransformerFactoryConfigurationError; | |
39 | import javax.xml.transform.dom.DOMSource; | |
40 | import javax.xml.transform.stream.StreamResult; | |
41 | ||
890916ec | 42 | import org.eclipse.core.runtime.Platform; |
47aafe74 AM |
43 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
44 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; | |
be222f56 PT |
45 | import org.w3c.dom.Document; |
46 | import org.w3c.dom.Element; | |
47 | import org.w3c.dom.Node; | |
48 | import org.w3c.dom.NodeList; | |
be222f56 | 49 | import org.xml.sax.SAXException; |
be222f56 | 50 | |
a0a88f65 AM |
51 | /** |
52 | * Trace definition for custom text traces. | |
53 | * | |
54 | * @author Patrick Tassé | |
47aafe74 | 55 | * @since 3.0 |
a0a88f65 | 56 | */ |
be222f56 PT |
57 | public class CustomTxtTraceDefinition extends CustomTraceDefinition { |
58 | ||
a0a88f65 AM |
59 | /** Input lines */ |
60 | public List<InputLine> inputs; | |
61 | ||
890916ec MK |
62 | /** File name of the default definition file */ |
63 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_DEFAULT_FILE_NAME = "custom_txt_default_parsers.xml"; //$NON-NLS-1$ | |
a0a88f65 | 64 | /** File name of the definition file */ |
be222f56 | 65 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME = "custom_txt_parsers.xml"; //$NON-NLS-1$ |
a0a88f65 | 66 | |
890916ec MK |
67 | /** Path of the definition file */ |
68 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_DEFAULT_PATH_NAME = | |
cb1cf0e8 MK |
69 | Platform.getInstallLocation().getURL().getPath() + "templates/org.eclipse.linuxtools.tmf.core/" + //$NON-NLS-1$ |
70 | CUSTOM_TXT_TRACE_DEFINITIONS_DEFAULT_FILE_NAME; | |
a0a88f65 | 71 | /** Path of the definition file */ |
be222f56 | 72 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME = |
890916ec | 73 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME).toString(); |
be222f56 | 74 | |
cb1cf0e8 MK |
75 | /** |
76 | * Legacy path to the XML definitions file (in the UI plug-in) TODO Remove | |
77 | * once we feel the transition phase is over. | |
78 | */ | |
03573754 AM |
79 | private static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME_LEGACY = |
80 | Activator.getDefault().getStateLocation().removeLastSegments(1).addTrailingSeparator() | |
81 | .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$ | |
82 | .append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME).toString(); | |
83 | ||
be222f56 PT |
84 | private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomTxtTraceDefinition_definitionRootElement; |
85 | private static final String DEFINITION_ELEMENT = Messages.CustomTxtTraceDefinition_definition; | |
332527a4 | 86 | private static final String CATEGORY_ATTRIBUTE = Messages.CustomTxtTraceDefinition_category; |
be222f56 PT |
87 | private static final String NAME_ATTRIBUTE = Messages.CustomTxtTraceDefinition_name; |
88 | private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomTxtTraceDefinition_timestampOutputFormat; | |
89 | private static final String INPUT_LINE_ELEMENT = Messages.CustomTxtTraceDefinition_inputLine; | |
90 | private static final String CARDINALITY_ELEMENT = Messages.CustomTxtTraceDefinition_cardinality; | |
91 | private static final String MIN_ATTRIBUTE = Messages.CustomTxtTraceDefinition_min; | |
92 | private static final String MAX_ATTRIBUTE = Messages.CustomTxtTraceDefinition_max; | |
93 | private static final String REGEX_ELEMENT = Messages.CustomTxtTraceDefinition_regEx; | |
94 | private static final String INPUT_DATA_ELEMENT = Messages.CustomTxtTraceDefinition_inputData; | |
95 | private static final String ACTION_ATTRIBUTE = Messages.CustomTxtTraceDefinition_action; | |
96 | private static final String FORMAT_ATTRIBUTE = Messages.CustomTxtTraceDefinition_format; | |
97 | private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomTxtTraceDefinition_outputColumn; | |
98 | ||
a0a88f65 AM |
99 | /** |
100 | * Default constructor. | |
101 | */ | |
be222f56 | 102 | public CustomTxtTraceDefinition() { |
332527a4 | 103 | this(TmfTraceType.CUSTOM_TXT_CATEGORY, "", new ArrayList<InputLine>(0), new ArrayList<OutputColumn>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
104 | } |
105 | ||
a0a88f65 AM |
106 | /** |
107 | * Full constructor. | |
108 | * | |
332527a4 | 109 | * @param traceType |
a0a88f65 AM |
110 | * Name of the trace type |
111 | * @param inputs | |
112 | * List of inputs | |
113 | * @param outputs | |
114 | * List of output columns | |
115 | * @param timeStampOutputFormat | |
116 | * The timestamp format to use | |
332527a4 | 117 | * @deprecated Use {@link #CustomTxtTraceDefinition(String, String, List, List, String)} |
a0a88f65 | 118 | */ |
332527a4 PT |
119 | @Deprecated |
120 | public CustomTxtTraceDefinition(String traceType, List<InputLine> inputs, | |
a0a88f65 | 121 | List<OutputColumn> outputs, String timeStampOutputFormat) { |
332527a4 PT |
122 | this.definitionName = traceType; |
123 | this.inputs = inputs; | |
124 | this.outputs = outputs; | |
125 | this.timeStampOutputFormat = timeStampOutputFormat; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Full constructor. | |
130 | * | |
131 | * @param category | |
132 | * Category of the trace type | |
133 | * @param traceType | |
134 | * Name of the trace type | |
135 | * @param inputs | |
136 | * List of inputs | |
137 | * @param outputs | |
138 | * List of output columns | |
139 | * @param timeStampOutputFormat | |
140 | * The timestamp format to use | |
141 | * @since 3.1 | |
142 | */ | |
143 | public CustomTxtTraceDefinition(String category, String traceType, List<InputLine> inputs, | |
144 | List<OutputColumn> outputs, String timeStampOutputFormat) { | |
145 | this.categoryName = category; | |
146 | this.definitionName = traceType; | |
be222f56 PT |
147 | this.inputs = inputs; |
148 | this.outputs = outputs; | |
149 | this.timeStampOutputFormat = timeStampOutputFormat; | |
150 | } | |
151 | ||
a0a88f65 AM |
152 | /** |
153 | * Wrapper to store a line of the log file | |
154 | */ | |
be222f56 | 155 | public static class InputLine { |
a0a88f65 AM |
156 | |
157 | /** Data columns of this line */ | |
be222f56 | 158 | public List<InputData> columns; |
a0a88f65 AM |
159 | |
160 | /** Cardinality of this line (see {@link Cardinality}) */ | |
be222f56 | 161 | public Cardinality cardinality; |
a0a88f65 AM |
162 | |
163 | /** Parent line */ | |
be222f56 | 164 | public InputLine parentInput; |
a0a88f65 AM |
165 | |
166 | /** Level of this line */ | |
be222f56 | 167 | public int level; |
a0a88f65 AM |
168 | |
169 | /** Next input line in the file */ | |
be222f56 | 170 | public InputLine nextInput; |
a0a88f65 AM |
171 | |
172 | /** Children of this line (if one "event" spans many lines) */ | |
be222f56 PT |
173 | public List<InputLine> childrenInputs; |
174 | ||
a0a88f65 AM |
175 | private String regex; |
176 | private Pattern pattern; | |
177 | ||
178 | /** | |
179 | * Default (empty) constructor. | |
180 | */ | |
cb1cf0e8 MK |
181 | public InputLine() { |
182 | } | |
be222f56 | 183 | |
a0a88f65 AM |
184 | /** |
185 | * Constructor. | |
186 | * | |
cb1cf0e8 MK |
187 | * @param cardinality |
188 | * Cardinality of this line. | |
189 | * @param regex | |
190 | * Regex | |
191 | * @param columns | |
192 | * Columns to use | |
a0a88f65 | 193 | */ |
be222f56 PT |
194 | public InputLine(Cardinality cardinality, String regex, List<InputData> columns) { |
195 | this.cardinality = cardinality; | |
196 | this.regex = regex; | |
197 | this.columns = columns; | |
198 | } | |
199 | ||
a0a88f65 AM |
200 | /** |
201 | * Set the regex of this input line | |
202 | * | |
203 | * @param regex | |
204 | * Regex to set | |
205 | */ | |
be222f56 PT |
206 | public void setRegex(String regex) { |
207 | this.regex = regex; | |
208 | this.pattern = null; | |
209 | } | |
210 | ||
a0a88f65 AM |
211 | /** |
212 | * Get the current regex | |
213 | * | |
214 | * @return The current regex | |
215 | */ | |
be222f56 PT |
216 | public String getRegex() { |
217 | return regex; | |
218 | } | |
219 | ||
a0a88f65 AM |
220 | /** |
221 | * Get the Pattern object of this line's regex | |
222 | * | |
223 | * @return The Pattern | |
224 | * @throws PatternSyntaxException | |
225 | * If the regex does not parse correctly | |
226 | */ | |
be222f56 PT |
227 | public Pattern getPattern() throws PatternSyntaxException { |
228 | if (pattern == null) { | |
229 | pattern = Pattern.compile(regex); | |
230 | } | |
231 | return pattern; | |
232 | } | |
233 | ||
a0a88f65 AM |
234 | /** |
235 | * Add a child line to this line. | |
236 | * | |
237 | * @param input | |
238 | * The child input line | |
239 | */ | |
be222f56 PT |
240 | public void addChild(InputLine input) { |
241 | if (childrenInputs == null) { | |
507b1336 | 242 | childrenInputs = new ArrayList<>(1); |
be222f56 PT |
243 | } else if (childrenInputs.size() > 0) { |
244 | InputLine last = childrenInputs.get(childrenInputs.size() - 1); | |
245 | last.nextInput = input; | |
246 | } | |
247 | childrenInputs.add(input); | |
248 | input.parentInput = this; | |
249 | input.level = this.level + 1; | |
250 | } | |
251 | ||
a0a88f65 AM |
252 | /** |
253 | * Set the next input line. | |
254 | * | |
255 | * @param input | |
256 | * The next input line | |
257 | */ | |
be222f56 PT |
258 | public void addNext(InputLine input) { |
259 | if (parentInput != null) { | |
260 | int index = parentInput.childrenInputs.indexOf(this); | |
261 | parentInput.childrenInputs.add(index + 1, input); | |
262 | InputLine next = nextInput; | |
263 | nextInput = input; | |
264 | input.nextInput = next; | |
265 | } | |
266 | input.parentInput = this.parentInput; | |
267 | input.level = this.level; | |
268 | } | |
269 | ||
a0a88f65 AM |
270 | /** |
271 | * Move this line up in its parent's children. | |
272 | */ | |
be222f56 PT |
273 | public void moveUp() { |
274 | if (parentInput != null) { | |
275 | int index = parentInput.childrenInputs.indexOf(this); | |
276 | if (index > 0) { | |
890916ec | 277 | parentInput.childrenInputs.add(index - 1, parentInput.childrenInputs.remove(index)); |
be222f56 PT |
278 | parentInput.childrenInputs.get(index).nextInput = nextInput; |
279 | nextInput = parentInput.childrenInputs.get(index); | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
a0a88f65 AM |
284 | /** |
285 | * Move this line down in its parent's children. | |
286 | */ | |
be222f56 PT |
287 | public void moveDown() { |
288 | if (parentInput != null) { | |
289 | int index = parentInput.childrenInputs.indexOf(this); | |
290 | if (index < parentInput.childrenInputs.size() - 1) { | |
890916ec | 291 | parentInput.childrenInputs.add(index + 1, parentInput.childrenInputs.remove(index)); |
be222f56 PT |
292 | nextInput = parentInput.childrenInputs.get(index).nextInput; |
293 | parentInput.childrenInputs.get(index).nextInput = this; | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
a0a88f65 AM |
298 | /** |
299 | * Add a data column to this line | |
300 | * | |
301 | * @param column | |
302 | * The column to add | |
303 | */ | |
be222f56 PT |
304 | public void addColumn(InputData column) { |
305 | if (columns == null) { | |
507b1336 | 306 | columns = new ArrayList<>(1); |
be222f56 PT |
307 | } |
308 | columns.add(column); | |
309 | } | |
310 | ||
a0a88f65 AM |
311 | /** |
312 | * Get the next input lines. | |
313 | * | |
314 | * @param countMap | |
315 | * The map of line "sets". | |
316 | * @return The next list of lines. | |
317 | */ | |
be222f56 | 318 | public List<InputLine> getNextInputs(Map<InputLine, Integer> countMap) { |
507b1336 | 319 | List<InputLine> nextInputs = new ArrayList<>(); |
be222f56 PT |
320 | InputLine next = nextInput; |
321 | while (next != null) { | |
322 | nextInputs.add(next); | |
323 | if (next.cardinality.min > 0) { | |
324 | return nextInputs; | |
325 | } | |
326 | next = next.nextInput; | |
327 | } | |
328 | if (parentInput != null && parentInput.level > 0) { | |
329 | int parentCount = countMap.get(parentInput); | |
330 | if (parentCount < parentInput.getMaxCount()) { | |
331 | nextInputs.add(parentInput); | |
332 | } | |
333 | if (parentCount < parentInput.getMinCount()) { | |
334 | return nextInputs; | |
335 | } | |
336 | nextInputs.addAll(parentInput.getNextInputs(countMap)); | |
337 | } | |
338 | return nextInputs; | |
339 | } | |
340 | ||
a0a88f65 AM |
341 | /** |
342 | * Get the minimum possible amount of entries. | |
343 | * | |
344 | * @return The minimum | |
345 | */ | |
be222f56 PT |
346 | public int getMinCount() { |
347 | return cardinality.min; | |
348 | } | |
349 | ||
a0a88f65 AM |
350 | /** |
351 | * Get the maximum possible amount of entries. | |
352 | * | |
353 | * @return The maximum | |
354 | */ | |
be222f56 PT |
355 | public int getMaxCount() { |
356 | return cardinality.max; | |
357 | } | |
358 | ||
359 | @Override | |
360 | public String toString() { | |
361 | return regex + " " + cardinality; //$NON-NLS-1$ | |
362 | } | |
be222f56 PT |
363 | } |
364 | ||
a0a88f65 AM |
365 | /** |
366 | * Data column for input lines. | |
367 | */ | |
be222f56 | 368 | public static class InputData { |
a0a88f65 AM |
369 | |
370 | /** Name of this column */ | |
be222f56 | 371 | public String name; |
a0a88f65 AM |
372 | |
373 | /** Action id */ | |
be222f56 | 374 | public int action; |
a0a88f65 AM |
375 | |
376 | /** Format */ | |
be222f56 PT |
377 | public String format; |
378 | ||
a0a88f65 AM |
379 | /** |
380 | * Default (empty) constructor | |
381 | */ | |
cb1cf0e8 MK |
382 | public InputData() { |
383 | } | |
be222f56 | 384 | |
a0a88f65 AM |
385 | /** |
386 | * Full constructor | |
387 | * | |
cb1cf0e8 MK |
388 | * @param name |
389 | * Name | |
390 | * @param action | |
391 | * Action | |
392 | * @param format | |
393 | * Format | |
a0a88f65 | 394 | */ |
be222f56 PT |
395 | public InputData(String name, int action, String format) { |
396 | this.name = name; | |
397 | this.action = action; | |
398 | this.format = format; | |
399 | } | |
400 | ||
a0a88f65 AM |
401 | /** |
402 | * Constructor with default format | |
403 | * | |
cb1cf0e8 MK |
404 | * @param name |
405 | * Name | |
406 | * @param action | |
407 | * Action | |
a0a88f65 | 408 | */ |
be222f56 PT |
409 | public InputData(String name, int action) { |
410 | this.name = name; | |
411 | this.action = action; | |
412 | } | |
413 | } | |
414 | ||
a0a88f65 AM |
415 | /** |
416 | * Input line cardinality | |
417 | */ | |
be222f56 | 418 | public static class Cardinality { |
a0a88f65 AM |
419 | |
420 | /** Representation of infinity */ | |
be222f56 | 421 | public final static int INF = Integer.MAX_VALUE; |
a0a88f65 AM |
422 | |
423 | /** Preset for [1, 1] */ | |
be222f56 | 424 | public final static Cardinality ONE = new Cardinality(1, 1); |
a0a88f65 AM |
425 | |
426 | /** Preset for [1, inf] */ | |
be222f56 | 427 | public final static Cardinality ONE_OR_MORE = new Cardinality(1, INF); |
a0a88f65 AM |
428 | |
429 | /** Preset for [0, 1] */ | |
be222f56 | 430 | public final static Cardinality ZERO_OR_ONE = new Cardinality(0, 1); |
a0a88f65 AM |
431 | |
432 | /** Preset for [0, inf] */ | |
be222f56 PT |
433 | public final static Cardinality ZERO_OR_MORE = new Cardinality(0, INF); |
434 | ||
435 | private final int min; | |
436 | private final int max; | |
437 | ||
a0a88f65 AM |
438 | /** |
439 | * Constructor. | |
440 | * | |
441 | * @param min | |
442 | * Minimum | |
443 | * @param max | |
444 | * Maximum | |
445 | */ | |
be222f56 PT |
446 | public Cardinality(int min, int max) { |
447 | this.min = min; | |
448 | this.max = max; | |
449 | } | |
450 | ||
890916ec | 451 | @Override |
be222f56 | 452 | public String toString() { |
0126a8ca | 453 | return "(" + (min >= 0 ? min : "?") + ',' + (max == INF ? "\u221E" : (max >= 0 ? max : "?")) + ')'; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
be222f56 PT |
454 | } |
455 | ||
be222f56 PT |
456 | @Override |
457 | public int hashCode() { | |
458 | final int prime = 31; | |
459 | int result = 1; | |
460 | result = prime * result + max; | |
461 | result = prime * result + min; | |
462 | return result; | |
463 | } | |
464 | ||
be222f56 PT |
465 | @Override |
466 | public boolean equals(Object obj) { | |
467 | if (this == obj) { | |
468 | return true; | |
469 | } | |
470 | if (obj == null) { | |
471 | return false; | |
472 | } | |
473 | if (!(obj instanceof Cardinality)) { | |
474 | return false; | |
475 | } | |
476 | Cardinality other = (Cardinality) obj; | |
477 | return (this.min == other.min && this.max == other.max); | |
478 | } | |
479 | } | |
480 | ||
481 | @Override | |
890916ec | 482 | public void save() { |
be222f56 PT |
483 | save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME); |
484 | } | |
485 | ||
486 | @Override | |
a0a88f65 | 487 | public void save(String path) { |
be222f56 PT |
488 | try { |
489 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
490 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
491 | ||
492 | // The following allows xml parsing without access to the dtd | |
cb1cf0e8 | 493 | db.setEntityResolver(createEmptyEntityResolver()); |
be222f56 PT |
494 | |
495 | // The following catches xml parsing exceptions | |
cb1cf0e8 | 496 | db.setErrorHandler(createErrorHandler()); |
be222f56 PT |
497 | |
498 | Document doc = null; | |
499 | File file = new File(path); | |
500 | if (file.canRead()) { | |
501 | doc = db.parse(file); | |
890916ec | 502 | if (!doc.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) { |
be222f56 PT |
503 | return; |
504 | } | |
505 | } else { | |
506 | doc = db.newDocument(); | |
507 | Node node = doc.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT); | |
508 | doc.appendChild(node); | |
509 | } | |
510 | ||
511 | Element root = doc.getDocumentElement(); | |
512 | ||
332527a4 PT |
513 | Element oldDefinitionElement = findDefinitionElement(root, categoryName, definitionName); |
514 | if (oldDefinitionElement != null) { | |
515 | root.removeChild(oldDefinitionElement); | |
be222f56 PT |
516 | } |
517 | Element definitionElement = doc.createElement(DEFINITION_ELEMENT); | |
518 | root.appendChild(definitionElement); | |
332527a4 | 519 | definitionElement.setAttribute(CATEGORY_ATTRIBUTE, categoryName); |
be222f56 PT |
520 | definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName); |
521 | ||
522 | Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT); | |
523 | definitionElement.appendChild(formatElement); | |
524 | formatElement.appendChild(doc.createTextNode(timeStampOutputFormat)); | |
525 | ||
526 | if (inputs != null) { | |
527 | for (InputLine inputLine : inputs) { | |
528 | definitionElement.appendChild(createInputLineElement(inputLine, doc)); | |
529 | } | |
530 | } | |
531 | ||
532 | if (outputs != null) { | |
533 | for (OutputColumn output : outputs) { | |
534 | Element outputColumnElement = doc.createElement(OUTPUT_COLUMN_ELEMENT); | |
535 | definitionElement.appendChild(outputColumnElement); | |
536 | outputColumnElement.setAttribute(NAME_ATTRIBUTE, output.name); | |
537 | } | |
538 | } | |
539 | ||
540 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
541 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ | |
542 | ||
890916ec | 543 | // initialize StreamResult with File object to save to file |
be222f56 PT |
544 | StreamResult result = new StreamResult(new StringWriter()); |
545 | DOMSource source = new DOMSource(doc); | |
546 | transformer.transform(source, result); | |
547 | String xmlString = result.getWriter().toString(); | |
548 | ||
507b1336 AM |
549 | try (FileWriter writer = new FileWriter(file);) { |
550 | writer.write(xmlString); | |
551 | } | |
52885aeb | 552 | |
4b3b667b | 553 | TmfTraceType.addCustomTraceType(CustomTxtTrace.class, categoryName, definitionName); |
52885aeb | 554 | |
be222f56 | 555 | } catch (ParserConfigurationException e) { |
47aafe74 | 556 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 557 | } catch (TransformerConfigurationException e) { |
47aafe74 | 558 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 559 | } catch (TransformerFactoryConfigurationError e) { |
47aafe74 | 560 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 561 | } catch (TransformerException e) { |
47aafe74 | 562 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 563 | } catch (IOException e) { |
47aafe74 | 564 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 565 | } catch (SAXException e) { |
47aafe74 | 566 | Activator.logError("Error saving CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 PT |
567 | } |
568 | } | |
569 | ||
570 | private Element createInputLineElement(InputLine inputLine, Document doc) { | |
571 | Element inputLineElement = doc.createElement(INPUT_LINE_ELEMENT); | |
572 | ||
573 | Element cardinalityElement = doc.createElement(CARDINALITY_ELEMENT); | |
574 | inputLineElement.appendChild(cardinalityElement); | |
575 | cardinalityElement.setAttribute(MIN_ATTRIBUTE, Integer.toString(inputLine.cardinality.min)); | |
576 | cardinalityElement.setAttribute(MAX_ATTRIBUTE, Integer.toString(inputLine.cardinality.max)); | |
577 | ||
578 | Element regexElement = doc.createElement(REGEX_ELEMENT); | |
579 | inputLineElement.appendChild(regexElement); | |
580 | regexElement.appendChild(doc.createTextNode(inputLine.regex)); | |
581 | ||
582 | if (inputLine.columns != null) { | |
583 | for (InputData inputData : inputLine.columns) { | |
584 | Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT); | |
585 | inputLineElement.appendChild(inputDataElement); | |
586 | inputDataElement.setAttribute(NAME_ATTRIBUTE, inputData.name); | |
587 | inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(inputData.action)); | |
588 | if (inputData.format != null) { | |
589 | inputDataElement.setAttribute(FORMAT_ATTRIBUTE, inputData.format); | |
590 | } | |
591 | } | |
592 | } | |
593 | ||
594 | if (inputLine.childrenInputs != null) { | |
595 | for (InputLine childInputLine : inputLine.childrenInputs) { | |
596 | inputLineElement.appendChild(createInputLineElement(childInputLine, doc)); | |
597 | } | |
598 | } | |
599 | ||
600 | return inputLineElement; | |
601 | } | |
602 | ||
a0a88f65 AM |
603 | /** |
604 | * Load the default text trace definitions file. | |
605 | * | |
606 | * @return The loaded trace definitions | |
607 | */ | |
be222f56 | 608 | public static CustomTxtTraceDefinition[] loadAll() { |
03573754 AM |
609 | File defaultFile = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME); |
610 | File legacyFile = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME_LEGACY); | |
611 | ||
612 | /* | |
613 | * If there is no file at the expected location, check the legacy | |
614 | * location instead. | |
615 | */ | |
616 | if (!defaultFile.exists() && legacyFile.exists()) { | |
617 | CustomTxtTraceDefinition[] oldDefs = loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME_LEGACY); | |
618 | for (CustomTxtTraceDefinition def : oldDefs) { | |
619 | /* Save in the new location */ | |
620 | def.save(); | |
621 | } | |
622 | } | |
623 | ||
890916ec MK |
624 | Set<CustomTxtTraceDefinition> defs = new TreeSet<>(new Comparator<CustomTxtTraceDefinition>() { |
625 | @Override | |
626 | public int compare(CustomTxtTraceDefinition o1, CustomTxtTraceDefinition o2) { | |
c22ca172 PT |
627 | int result = o1.categoryName.compareTo(o2.categoryName); |
628 | if (result != 0) { | |
629 | return result; | |
630 | } | |
890916ec MK |
631 | return o1.definitionName.compareTo(o2.definitionName); |
632 | } | |
633 | }); | |
634 | defs.addAll(Arrays.asList(loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME))); | |
635 | defs.addAll(Arrays.asList(loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_DEFAULT_PATH_NAME))); | |
636 | return defs.toArray(new CustomTxtTraceDefinition[0]); | |
637 | ||
be222f56 PT |
638 | } |
639 | ||
a0a88f65 AM |
640 | /** |
641 | * Load a specific text trace definition file. | |
642 | * | |
cb1cf0e8 MK |
643 | * @param path |
644 | * The path to the file to load | |
a0a88f65 AM |
645 | * @return The loaded trace definitions |
646 | */ | |
be222f56 PT |
647 | public static CustomTxtTraceDefinition[] loadAll(String path) { |
648 | try { | |
649 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
650 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
651 | ||
652 | // The following allows xml parsing without access to the dtd | |
cb1cf0e8 | 653 | db.setEntityResolver(createEmptyEntityResolver()); |
be222f56 PT |
654 | |
655 | // The following catches xml parsing exceptions | |
cb1cf0e8 | 656 | db.setErrorHandler(createErrorHandler()); |
be222f56 PT |
657 | |
658 | File file = new File(path); | |
659 | if (!file.canRead()) { | |
660 | return new CustomTxtTraceDefinition[0]; | |
661 | } | |
662 | Document doc = db.parse(file); | |
663 | ||
664 | Element root = doc.getDocumentElement(); | |
890916ec | 665 | if (!root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) { |
be222f56 PT |
666 | return new CustomTxtTraceDefinition[0]; |
667 | } | |
668 | ||
507b1336 | 669 | ArrayList<CustomTxtTraceDefinition> defList = new ArrayList<>(); |
be222f56 PT |
670 | NodeList nodeList = root.getChildNodes(); |
671 | for (int i = 0; i < nodeList.getLength(); i++) { | |
672 | Node node = nodeList.item(i); | |
673 | if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) { | |
674 | CustomTxtTraceDefinition def = extractDefinition((Element) node); | |
675 | if (def != null) { | |
676 | defList.add(def); | |
677 | } | |
678 | } | |
679 | } | |
680 | return defList.toArray(new CustomTxtTraceDefinition[0]); | |
681 | } catch (ParserConfigurationException e) { | |
47aafe74 | 682 | Activator.logError("Error loading all in CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 683 | } catch (SAXException e) { |
47aafe74 | 684 | Activator.logError("Error loading all in CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 | 685 | } catch (IOException e) { |
47aafe74 | 686 | Activator.logError("Error loading all in CustomTxtTraceDefinition: path=" + path, e); //$NON-NLS-1$ |
be222f56 PT |
687 | } |
688 | return new CustomTxtTraceDefinition[0]; | |
689 | } | |
690 | ||
a0a88f65 AM |
691 | /** |
692 | * Load a single definition. | |
693 | * | |
cb1cf0e8 MK |
694 | * @param definitionName |
695 | * Name of the definition to load | |
a0a88f65 | 696 | * @return The loaded trace definition |
332527a4 | 697 | * @deprecated Use {@link #load(String, String)} |
a0a88f65 | 698 | */ |
332527a4 | 699 | @Deprecated |
be222f56 | 700 | public static CustomTxtTraceDefinition load(String definitionName) { |
332527a4 PT |
701 | return load(TmfTraceType.CUSTOM_TXT_CATEGORY, definitionName); |
702 | } | |
703 | ||
704 | /** | |
705 | * Load a single definition. | |
706 | * | |
707 | * @param categoryName | |
708 | * Category of the definition to load | |
709 | * @param definitionName | |
710 | * Name of the definition to load | |
711 | * @return The loaded trace definition | |
712 | * @since 3.1 | |
713 | */ | |
714 | public static CustomTxtTraceDefinition load(String categoryName, String definitionName) { | |
be222f56 PT |
715 | try { |
716 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
717 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
718 | ||
719 | // The following allows xml parsing without access to the dtd | |
cb1cf0e8 | 720 | db.setEntityResolver(createEmptyEntityResolver()); |
be222f56 PT |
721 | |
722 | // The following catches xml parsing exceptions | |
cb1cf0e8 | 723 | db.setErrorHandler(createErrorHandler()); |
be222f56 | 724 | |
332527a4 | 725 | CustomTxtTraceDefinition value = lookupDefinition(categoryName, definitionName, db, CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME); |
890916ec | 726 | if (value == null) { |
332527a4 | 727 | return lookupDefinition(categoryName, definitionName, db, CUSTOM_TXT_TRACE_DEFINITIONS_DEFAULT_PATH_NAME); |
be222f56 | 728 | } |
890916ec | 729 | return value; |
cb1cf0e8 | 730 | } catch (ParserConfigurationException | SAXException | IOException e) { |
890916ec MK |
731 | Activator.logError("Error loading CustomTxtTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$ |
732 | } | |
733 | return null; | |
734 | } | |
be222f56 | 735 | |
332527a4 | 736 | private static CustomTxtTraceDefinition lookupDefinition(String categoryName, String definitionName, DocumentBuilder db, String source) throws SAXException, IOException { |
890916ec MK |
737 | File file = new File(source); |
738 | if (!file.exists()) { | |
739 | return null; | |
740 | } | |
741 | Document doc = db.parse(file); | |
742 | ||
743 | Element root = doc.getDocumentElement(); | |
744 | if (!root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) { | |
745 | return null; | |
746 | } | |
747 | ||
332527a4 PT |
748 | Element definitionElement = findDefinitionElement(root, categoryName, definitionName); |
749 | if (definitionElement != null) { | |
750 | return extractDefinition(definitionElement); | |
751 | } | |
752 | return null; | |
753 | } | |
754 | ||
755 | private static Element findDefinitionElement(Element root, String categoryName, String definitionName) { | |
890916ec MK |
756 | NodeList nodeList = root.getChildNodes(); |
757 | for (int i = 0; i < nodeList.getLength(); i++) { | |
758 | Node node = nodeList.item(i); | |
332527a4 PT |
759 | if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) { |
760 | Element element = (Element) node; | |
761 | String categoryAttribute = element.getAttribute(CATEGORY_ATTRIBUTE); | |
762 | if (categoryAttribute.isEmpty()) { | |
763 | categoryAttribute = TmfTraceType.CUSTOM_TXT_CATEGORY; | |
764 | } | |
765 | String nameAttribute = element.getAttribute(NAME_ATTRIBUTE); | |
766 | if (categoryName.equals(categoryAttribute) && | |
767 | definitionName.equals(nameAttribute)) { | |
768 | return element; | |
769 | } | |
be222f56 | 770 | } |
be222f56 PT |
771 | } |
772 | return null; | |
773 | } | |
774 | ||
a0a88f65 AM |
775 | /** |
776 | * Get the definition from a definition element. | |
777 | * | |
cb1cf0e8 MK |
778 | * @param definitionElement |
779 | * The Element to extract from | |
a0a88f65 AM |
780 | * @return The loaded trace definition |
781 | */ | |
be222f56 PT |
782 | public static CustomTxtTraceDefinition extractDefinition(Element definitionElement) { |
783 | CustomTxtTraceDefinition def = new CustomTxtTraceDefinition(); | |
784 | ||
332527a4 PT |
785 | def.categoryName = definitionElement.getAttribute(CATEGORY_ATTRIBUTE); |
786 | if (def.categoryName.isEmpty()) { | |
787 | def.categoryName = TmfTraceType.CUSTOM_TXT_CATEGORY; | |
788 | } | |
be222f56 | 789 | def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE); |
332527a4 | 790 | if (def.definitionName.isEmpty()) { |
be222f56 PT |
791 | return null; |
792 | } | |
793 | ||
794 | NodeList nodeList = definitionElement.getChildNodes(); | |
795 | for (int i = 0; i < nodeList.getLength(); i++) { | |
796 | Node node = nodeList.item(i); | |
797 | String nodeName = node.getNodeName(); | |
798 | if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) { | |
799 | Element formatElement = (Element) node; | |
800 | def.timeStampOutputFormat = formatElement.getTextContent(); | |
801 | } else if (nodeName.equals(INPUT_LINE_ELEMENT)) { | |
802 | InputLine inputLine = extractInputLine((Element) node); | |
803 | if (inputLine != null) { | |
804 | def.inputs.add(inputLine); | |
805 | } | |
806 | } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) { | |
807 | Element outputColumnElement = (Element) node; | |
808 | OutputColumn outputColumn = new OutputColumn(); | |
809 | outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE); | |
810 | def.outputs.add(outputColumn); | |
811 | } | |
812 | } | |
813 | return def; | |
814 | } | |
815 | ||
816 | private static InputLine extractInputLine(Element inputLineElement) { | |
817 | InputLine inputLine = new InputLine(); | |
818 | NodeList nodeList = inputLineElement.getChildNodes(); | |
819 | for (int i = 0; i < nodeList.getLength(); i++) { | |
820 | Node node = nodeList.item(i); | |
821 | String nodeName = node.getNodeName(); | |
822 | if (nodeName.equals(CARDINALITY_ELEMENT)) { | |
823 | Element cardinalityElement = (Element) node; | |
824 | try { | |
825 | int min = Integer.parseInt(cardinalityElement.getAttribute(MIN_ATTRIBUTE)); | |
826 | int max = Integer.parseInt(cardinalityElement.getAttribute(MAX_ATTRIBUTE)); | |
827 | inputLine.cardinality = new Cardinality(min, max); | |
828 | } catch (NumberFormatException e) { | |
829 | return null; | |
830 | } | |
831 | } else if (nodeName.equals(REGEX_ELEMENT)) { | |
832 | Element regexElement = (Element) node; | |
833 | inputLine.regex = regexElement.getTextContent(); | |
834 | } else if (nodeName.equals(INPUT_DATA_ELEMENT)) { | |
835 | Element inputDataElement = (Element) node; | |
836 | InputData inputData = new InputData(); | |
837 | inputData.name = inputDataElement.getAttribute(NAME_ATTRIBUTE); | |
838 | inputData.action = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE)); | |
839 | inputData.format = inputDataElement.getAttribute(FORMAT_ATTRIBUTE); | |
840 | inputLine.addColumn(inputData); | |
841 | } else if (nodeName.equals(INPUT_LINE_ELEMENT)) { | |
842 | Element childInputLineElement = (Element) node; | |
843 | InputLine childInputLine = extractInputLine(childInputLineElement); | |
844 | if (childInputLine != null) { | |
845 | inputLine.addChild(childInputLine); | |
846 | } | |
847 | } | |
848 | } | |
849 | return inputLine; | |
850 | } | |
851 | ||
a0a88f65 AM |
852 | /** |
853 | * Delete a definition from the currently loaded ones. | |
854 | * | |
cb1cf0e8 MK |
855 | * @param definitionName |
856 | * The name of the definition to delete | |
332527a4 | 857 | * @deprecated Use {@link #delete(String, String)} |
a0a88f65 | 858 | */ |
332527a4 | 859 | @Deprecated |
be222f56 | 860 | public static void delete(String definitionName) { |
332527a4 PT |
861 | delete(TmfTraceType.CUSTOM_TXT_CATEGORY, definitionName); |
862 | } | |
863 | ||
864 | /** | |
865 | * Delete a definition from the currently loaded ones. | |
866 | * | |
867 | * @param categoryName | |
868 | * The category of the definition to delete | |
869 | * @param definitionName | |
870 | * The name of the definition to delete | |
871 | * @since 3.1 | |
872 | */ | |
873 | public static void delete(String categoryName, String definitionName) { | |
be222f56 PT |
874 | try { |
875 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
876 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
877 | ||
878 | // The following allows xml parsing without access to the dtd | |
cb1cf0e8 | 879 | db.setEntityResolver(createEmptyEntityResolver()); |
be222f56 PT |
880 | |
881 | // The following catches xml parsing exceptions | |
cb1cf0e8 | 882 | db.setErrorHandler(createErrorHandler()); |
be222f56 PT |
883 | |
884 | File file = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME); | |
885 | Document doc = db.parse(file); | |
886 | ||
887 | Element root = doc.getDocumentElement(); | |
890916ec | 888 | if (!root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) { |
be222f56 PT |
889 | return; |
890 | } | |
891 | ||
332527a4 PT |
892 | Element definitionElement = findDefinitionElement(root, categoryName, definitionName); |
893 | if (definitionElement != null) { | |
894 | root.removeChild(definitionElement); | |
be222f56 PT |
895 | } |
896 | ||
897 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
898 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ | |
899 | ||
890916ec | 900 | // initialize StreamResult with File object to save to file |
be222f56 PT |
901 | StreamResult result = new StreamResult(new StringWriter()); |
902 | DOMSource source = new DOMSource(doc); | |
903 | transformer.transform(source, result); | |
904 | String xmlString = result.getWriter().toString(); | |
905 | ||
507b1336 AM |
906 | try (FileWriter writer = new FileWriter(file);) { |
907 | writer.write(xmlString); | |
908 | } | |
52885aeb | 909 | |
4b3b667b | 910 | TmfTraceType.removeCustomTraceType(CustomTxtTrace.class, categoryName, definitionName); |
0621dbae | 911 | // Check if default definition needs to be reloaded |
4b3b667b | 912 | TmfTraceType.addCustomTraceType(CustomTxtTrace.class, categoryName, definitionName); |
52885aeb | 913 | |
cb1cf0e8 | 914 | } catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | TransformerException e) { |
47aafe74 | 915 | Activator.logError("Error deleting CustomTxtTraceDefinition: definitionName=" + definitionName, e); //$NON-NLS-1$ |
be222f56 PT |
916 | } |
917 | } | |
918 | } |