Commit | Line | Data |
---|---|---|
c3c5c786 FC |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Patrick Tasse - Initial API and implementation\r | |
11 | *******************************************************************************/\r | |
12 | \r | |
13 | package org.eclipse.linuxtools.tmf.ui.parsers.custom;\r | |
14 | \r | |
15 | import java.io.ByteArrayInputStream;\r | |
16 | import java.io.File;\r | |
17 | import java.io.FileWriter;\r | |
18 | import java.io.IOException;\r | |
19 | import java.io.StringWriter;\r | |
20 | import java.util.ArrayList;\r | |
21 | import java.util.List;\r | |
22 | import java.util.Map;\r | |
23 | import java.util.regex.Pattern;\r | |
24 | import java.util.regex.PatternSyntaxException;\r | |
25 | \r | |
26 | import javax.xml.parsers.DocumentBuilder;\r | |
27 | import javax.xml.parsers.DocumentBuilderFactory;\r | |
28 | import javax.xml.parsers.ParserConfigurationException;\r | |
29 | import javax.xml.transform.OutputKeys;\r | |
30 | import javax.xml.transform.Transformer;\r | |
31 | import javax.xml.transform.TransformerConfigurationException;\r | |
32 | import javax.xml.transform.TransformerException;\r | |
33 | import javax.xml.transform.TransformerFactory;\r | |
34 | import javax.xml.transform.TransformerFactoryConfigurationError;\r | |
35 | import javax.xml.transform.dom.DOMSource;\r | |
36 | import javax.xml.transform.stream.StreamResult;\r | |
37 | \r | |
38 | import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;\r | |
39 | import org.w3c.dom.Document;\r | |
40 | import org.w3c.dom.Element;\r | |
41 | import org.w3c.dom.Node;\r | |
42 | import org.w3c.dom.NodeList;\r | |
43 | import org.xml.sax.EntityResolver;\r | |
44 | import org.xml.sax.ErrorHandler;\r | |
45 | import org.xml.sax.InputSource;\r | |
46 | import org.xml.sax.SAXException;\r | |
47 | import org.xml.sax.SAXParseException;\r | |
48 | \r | |
49 | public class CustomTxtTraceDefinition extends CustomTraceDefinition {\r | |
50 | \r | |
3b38ea61 | 51 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME = "custom_txt_parsers.xml"; //$NON-NLS-1$\r |
c3c5c786 FC |
52 | protected static final String CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME =\r |
53 | TmfUiPlugin.getDefault().getStateLocation().addTrailingSeparator().append(CUSTOM_TXT_TRACE_DEFINITIONS_FILE_NAME).toString();\r | |
54 | \r | |
3b38ea61 FC |
55 | private static final String CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT = Messages.CustomTxtTraceDefinition_definitionRootElement;\r |
56 | private static final String DEFINITION_ELEMENT = Messages.CustomTxtTraceDefinition_definition;\r | |
57 | private static final String NAME_ATTRIBUTE = Messages.CustomTxtTraceDefinition_name;\r | |
58 | private static final String TIME_STAMP_OUTPUT_FORMAT_ELEMENT = Messages.CustomTxtTraceDefinition_timestampOutputFormat;\r | |
59 | private static final String INPUT_LINE_ELEMENT = Messages.CustomTxtTraceDefinition_inputLine;\r | |
60 | private static final String CARDINALITY_ELEMENT = Messages.CustomTxtTraceDefinition_cardinality;\r | |
61 | private static final String MIN_ATTRIBUTE = Messages.CustomTxtTraceDefinition_min;\r | |
62 | private static final String MAX_ATTRIBUTE = Messages.CustomTxtTraceDefinition_max;\r | |
63 | private static final String REGEX_ELEMENT = Messages.CustomTxtTraceDefinition_regEx;\r | |
64 | private static final String INPUT_DATA_ELEMENT = Messages.CustomTxtTraceDefinition_inputData;\r | |
65 | private static final String ACTION_ATTRIBUTE = Messages.CustomTxtTraceDefinition_action;\r | |
66 | private static final String FORMAT_ATTRIBUTE = Messages.CustomTxtTraceDefinition_format;\r | |
67 | private static final String OUTPUT_COLUMN_ELEMENT = Messages.CustomTxtTraceDefinition_outputColumn;\r | |
c3c5c786 FC |
68 | \r |
69 | public List<InputLine> inputs;\r | |
70 | \r | |
71 | public CustomTxtTraceDefinition() {\r | |
3b38ea61 | 72 | this("", new ArrayList<InputLine>(0), new ArrayList<OutputColumn>(0), ""); //$NON-NLS-1$ //$NON-NLS-2$\r |
c3c5c786 FC |
73 | };\r |
74 | \r | |
75 | public CustomTxtTraceDefinition(String logtype, List<InputLine> inputs, List<OutputColumn> outputs, String timeStampOutputFormat) {\r | |
76 | this.definitionName = logtype;\r | |
77 | this.inputs = inputs;\r | |
78 | this.outputs = outputs;\r | |
79 | this.timeStampOutputFormat = timeStampOutputFormat;\r | |
80 | }\r | |
81 | \r | |
82 | public static class InputLine {\r | |
83 | public List<InputData> columns;\r | |
84 | public Cardinality cardinality;\r | |
85 | private String regex;\r | |
86 | private Pattern pattern;\r | |
87 | public InputLine parentInput;\r | |
88 | public int level;\r | |
89 | public InputLine nextInput;\r | |
90 | public List<InputLine> childrenInputs;\r | |
91 | \r | |
92 | public InputLine() {};\r | |
93 | \r | |
94 | public InputLine(Cardinality cardinality, String regex, List<InputData> columns) {\r | |
95 | this.cardinality = cardinality;\r | |
96 | this.regex = regex;\r | |
97 | this.columns = columns;\r | |
98 | }\r | |
99 | \r | |
100 | public void setRegex(String regex) {\r | |
101 | this.regex = regex;\r | |
102 | this.pattern = null;\r | |
103 | }\r | |
104 | \r | |
105 | public String getRegex() {\r | |
106 | return regex;\r | |
107 | }\r | |
108 | \r | |
109 | public Pattern getPattern() throws PatternSyntaxException {\r | |
110 | if (pattern == null) {\r | |
111 | pattern = Pattern.compile(regex);\r | |
112 | }\r | |
113 | return pattern;\r | |
114 | }\r | |
115 | \r | |
116 | public void addChild(InputLine input) {\r | |
117 | if (childrenInputs == null) {\r | |
118 | childrenInputs = new ArrayList<InputLine>(1);\r | |
119 | } else if (childrenInputs.size() > 0) {\r | |
120 | InputLine last = childrenInputs.get(childrenInputs.size() - 1);\r | |
121 | last.nextInput = input;\r | |
122 | }\r | |
123 | childrenInputs.add(input);\r | |
124 | input.parentInput = this;\r | |
125 | input.level = this.level + 1;\r | |
126 | }\r | |
127 | \r | |
128 | public void addNext(InputLine input) {\r | |
129 | if (parentInput != null) {\r | |
130 | int index = parentInput.childrenInputs.indexOf(this);\r | |
131 | parentInput.childrenInputs.add(index + 1, input);\r | |
132 | InputLine next = nextInput;\r | |
133 | nextInput = input;\r | |
134 | input.nextInput = next;\r | |
135 | }\r | |
136 | input.parentInput = this.parentInput;\r | |
137 | input.level = this.level;\r | |
138 | }\r | |
139 | \r | |
140 | public void moveUp() {\r | |
141 | if (parentInput != null) {\r | |
142 | int index = parentInput.childrenInputs.indexOf(this);\r | |
143 | if (index > 0) {\r | |
144 | parentInput.childrenInputs.add(index - 1 , parentInput.childrenInputs.remove(index));\r | |
145 | parentInput.childrenInputs.get(index).nextInput = nextInput;\r | |
146 | nextInput = parentInput.childrenInputs.get(index);\r | |
147 | }\r | |
148 | }\r | |
149 | }\r | |
150 | \r | |
151 | public void moveDown() {\r | |
152 | if (parentInput != null) {\r | |
153 | int index = parentInput.childrenInputs.indexOf(this);\r | |
154 | if (index < parentInput.childrenInputs.size() - 1) {\r | |
155 | parentInput.childrenInputs.add(index + 1 , parentInput.childrenInputs.remove(index));\r | |
156 | nextInput = parentInput.childrenInputs.get(index).nextInput;\r | |
157 | parentInput.childrenInputs.get(index).nextInput = this;\r | |
158 | }\r | |
159 | }\r | |
160 | }\r | |
161 | \r | |
162 | public void addColumn(InputData column) {\r | |
163 | if (columns == null) {\r | |
164 | columns = new ArrayList<InputData>(1);\r | |
165 | }\r | |
166 | columns.add(column);\r | |
167 | }\r | |
168 | \r | |
169 | public List<InputLine> getNextInputs(Map<InputLine, Integer> countMap) {\r | |
170 | List<InputLine> nextInputs = new ArrayList<InputLine>();\r | |
171 | InputLine next = nextInput;\r | |
172 | while (next != null) {\r | |
173 | nextInputs.add(next);\r | |
174 | if (next.cardinality.min > 0) {\r | |
175 | return nextInputs;\r | |
176 | }\r | |
177 | next = next.nextInput;\r | |
178 | }\r | |
179 | if (parentInput != null && parentInput.level > 0) {\r | |
180 | int parentCount = countMap.get(parentInput);\r | |
181 | if (parentCount < parentInput.getMaxCount()) {\r | |
182 | nextInputs.add(parentInput);\r | |
183 | }\r | |
184 | if (parentCount < parentInput.getMinCount()) {\r | |
185 | return nextInputs;\r | |
186 | }\r | |
187 | nextInputs.addAll(parentInput.getNextInputs(countMap));\r | |
188 | }\r | |
189 | return nextInputs;\r | |
190 | }\r | |
191 | \r | |
192 | public int getMinCount() {\r | |
193 | return cardinality.min;\r | |
194 | }\r | |
195 | \r | |
196 | public int getMaxCount() {\r | |
197 | return cardinality.max;\r | |
198 | }\r | |
199 | \r | |
200 | @Override\r | |
201 | public String toString() {\r | |
3b38ea61 | 202 | return regex + " " + cardinality; //$NON-NLS-1$\r |
c3c5c786 FC |
203 | }\r |
204 | \r | |
205 | }\r | |
206 | \r | |
207 | public static class InputData {\r | |
208 | public String name;\r | |
209 | public int action;\r | |
210 | public String format;\r | |
211 | \r | |
212 | public InputData() {};\r | |
213 | \r | |
214 | public InputData(String name, int action, String format) {\r | |
215 | this.name = name;\r | |
216 | this.action = action;\r | |
217 | this.format = format;\r | |
218 | }\r | |
219 | \r | |
220 | public InputData(String name, int action) {\r | |
221 | this.name = name;\r | |
222 | this.action = action;\r | |
223 | }\r | |
224 | }\r | |
225 | \r | |
226 | public static class Cardinality {\r | |
227 | public final static int INF = Integer.MAX_VALUE;\r | |
228 | public final static Cardinality ONE = new Cardinality(1, 1);\r | |
229 | public final static Cardinality ONE_OR_MORE = new Cardinality(1, INF);\r | |
230 | public final static Cardinality ZERO_OR_ONE = new Cardinality(0, 1);\r | |
231 | public final static Cardinality ZERO_OR_MORE = new Cardinality(0, INF); \r | |
232 | \r | |
233 | private int min;\r | |
234 | private int max;\r | |
235 | \r | |
236 | public Cardinality(int min, int max) {\r | |
237 | this.min = min;\r | |
238 | this.max = max;\r | |
239 | }\r | |
240 | \r | |
3b38ea61 | 241 | @Override\r |
c3c5c786 | 242 | public String toString() {\r |
3b38ea61 | 243 | return "(" + (min >= 0 ? min : "?") + "," + (max == INF ? "\u221E" : (max >= 0 ? max : "?")) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$\r |
c3c5c786 FC |
244 | }\r |
245 | \r | |
246 | @Override\r | |
247 | public boolean equals(Object obj) {\r | |
248 | if (!(obj instanceof Cardinality)) return false;\r | |
249 | Cardinality other = (Cardinality) obj;\r | |
250 | return (this.min == other.min && this.max == other.max);\r | |
251 | }\r | |
252 | }\r | |
253 | \r | |
9ccc6d01 FC |
254 | @Override\r |
255 | public void save() {\r | |
c3c5c786 FC |
256 | save(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);\r |
257 | }\r | |
258 | \r | |
9ccc6d01 FC |
259 | @Override\r |
260 | public void save(String path) {\r | |
c3c5c786 FC |
261 | try {\r |
262 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
263 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
264 | \r | |
265 | // The following allows xml parsing without access to the dtd\r | |
266 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 | 267 | @Override\r |
c3c5c786 | 268 | public InputSource resolveEntity (String publicId, String systemId) {\r |
3b38ea61 | 269 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
270 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
271 | return new InputSource(bais);\r | |
272 | }\r | |
273 | };\r | |
274 | db.setEntityResolver(resolver);\r | |
275 | \r | |
276 | // The following catches xml parsing exceptions\r | |
277 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 | 278 | @Override\r |
c3c5c786 | 279 | public void error(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 280 | @Override\r |
c3c5c786 | 281 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 282 | @Override\r |
c3c5c786 FC |
283 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r |
284 | throw saxparseexception;\r | |
285 | }});\r | |
286 | \r | |
287 | Document doc = null;\r | |
288 | File file = new File(path);\r | |
289 | if (file.canRead()) {\r | |
290 | doc = db.parse(file);\r | |
291 | if (! doc.getDocumentElement().getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
292 | return;\r | |
293 | }\r | |
294 | } else {\r | |
295 | doc = db.newDocument();\r | |
296 | Node node = doc.createElement(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT);\r | |
297 | doc.appendChild(node);\r | |
298 | }\r | |
299 | \r | |
300 | Element root = doc.getDocumentElement();\r | |
301 | \r | |
302 | NodeList nodeList = root.getChildNodes();\r | |
303 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
304 | Node node = nodeList.item(i);\r | |
305 | if (node instanceof Element &&\r | |
306 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
307 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
308 | root.removeChild(node);\r | |
309 | }\r | |
310 | }\r | |
311 | Element definitionElement = doc.createElement(DEFINITION_ELEMENT);\r | |
312 | root.appendChild(definitionElement);\r | |
313 | definitionElement.setAttribute(NAME_ATTRIBUTE, definitionName);\r | |
314 | \r | |
315 | Element formatElement = doc.createElement(TIME_STAMP_OUTPUT_FORMAT_ELEMENT);\r | |
316 | definitionElement.appendChild(formatElement);\r | |
317 | formatElement.appendChild(doc.createTextNode(timeStampOutputFormat));\r | |
318 | \r | |
319 | if (inputs != null) {\r | |
320 | for (InputLine inputLine : inputs) {\r | |
321 | definitionElement.appendChild(createInputLineElement(inputLine, doc));\r | |
322 | }\r | |
323 | }\r | |
324 | \r | |
325 | if (outputs != null) {\r | |
326 | for (OutputColumn output : outputs) {\r | |
327 | Element outputColumnElement = doc.createElement(OUTPUT_COLUMN_ELEMENT);\r | |
328 | definitionElement.appendChild(outputColumnElement);\r | |
329 | outputColumnElement.setAttribute(NAME_ATTRIBUTE, output.name);\r | |
330 | }\r | |
331 | }\r | |
332 | \r | |
333 | Transformer transformer = TransformerFactory.newInstance().newTransformer();\r | |
3b38ea61 | 334 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$\r |
c3c5c786 FC |
335 | \r |
336 | //initialize StreamResult with File object to save to file\r | |
337 | StreamResult result = new StreamResult(new StringWriter());\r | |
338 | DOMSource source = new DOMSource(doc);\r | |
339 | transformer.transform(source, result);\r | |
340 | String xmlString = result.getWriter().toString();\r | |
341 | \r | |
342 | FileWriter writer = new FileWriter(file);\r | |
343 | writer.write(xmlString);\r | |
344 | writer.close();\r | |
345 | } catch (ParserConfigurationException e) {\r | |
346 | e.printStackTrace();\r | |
347 | } catch (TransformerConfigurationException e) {\r | |
348 | e.printStackTrace();\r | |
349 | } catch (TransformerFactoryConfigurationError e) {\r | |
350 | e.printStackTrace();\r | |
351 | } catch (TransformerException e) {\r | |
352 | e.printStackTrace();\r | |
353 | } catch (IOException e) {\r | |
354 | e.printStackTrace();\r | |
355 | } catch (SAXException e) {\r | |
356 | e.printStackTrace();\r | |
357 | }\r | |
358 | }\r | |
359 | \r | |
360 | private Element createInputLineElement(InputLine inputLine, Document doc) {\r | |
361 | Element inputLineElement = doc.createElement(INPUT_LINE_ELEMENT);\r | |
362 | \r | |
363 | Element cardinalityElement = doc.createElement(CARDINALITY_ELEMENT);\r | |
364 | inputLineElement.appendChild(cardinalityElement);\r | |
365 | cardinalityElement.setAttribute(MIN_ATTRIBUTE, Integer.toString(inputLine.cardinality.min));\r | |
366 | cardinalityElement.setAttribute(MAX_ATTRIBUTE, Integer.toString(inputLine.cardinality.max));\r | |
367 | \r | |
368 | Element regexElement = doc.createElement(REGEX_ELEMENT);\r | |
369 | inputLineElement.appendChild(regexElement);\r | |
370 | regexElement.appendChild(doc.createTextNode(inputLine.regex));\r | |
371 | \r | |
372 | if (inputLine.columns != null) {\r | |
373 | for (InputData inputData : inputLine.columns) {\r | |
374 | Element inputDataElement = doc.createElement(INPUT_DATA_ELEMENT);\r | |
375 | inputLineElement.appendChild(inputDataElement);\r | |
376 | inputDataElement.setAttribute(NAME_ATTRIBUTE, inputData.name);\r | |
377 | inputDataElement.setAttribute(ACTION_ATTRIBUTE, Integer.toString(inputData.action));\r | |
378 | if (inputData.format != null) {\r | |
379 | inputDataElement.setAttribute(FORMAT_ATTRIBUTE, inputData.format);\r | |
380 | }\r | |
381 | }\r | |
382 | }\r | |
383 | \r | |
384 | if (inputLine.childrenInputs != null) {\r | |
385 | for (InputLine childInputLine : inputLine.childrenInputs) {\r | |
386 | inputLineElement.appendChild(createInputLineElement(childInputLine, doc));\r | |
387 | }\r | |
388 | }\r | |
389 | \r | |
390 | return inputLineElement;\r | |
391 | }\r | |
392 | \r | |
393 | public static CustomTxtTraceDefinition[] loadAll() {\r | |
394 | return loadAll(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);\r | |
395 | }\r | |
396 | \r | |
397 | public static CustomTxtTraceDefinition[] loadAll(String path) {\r | |
398 | try {\r | |
399 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
400 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
401 | \r | |
402 | // The following allows xml parsing without access to the dtd\r | |
403 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 | 404 | @Override\r |
c3c5c786 | 405 | public InputSource resolveEntity (String publicId, String systemId) {\r |
3b38ea61 | 406 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
407 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
408 | return new InputSource(bais);\r | |
409 | }\r | |
410 | };\r | |
411 | db.setEntityResolver(resolver);\r | |
412 | \r | |
413 | // The following catches xml parsing exceptions\r | |
414 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 | 415 | @Override\r |
c3c5c786 | 416 | public void error(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 417 | @Override\r |
c3c5c786 | 418 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 419 | @Override\r |
c3c5c786 FC |
420 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r |
421 | throw saxparseexception;\r | |
422 | }});\r | |
423 | \r | |
424 | File file = new File(path);\r | |
425 | if (!file.canRead()) {\r | |
426 | return new CustomTxtTraceDefinition[0];\r | |
427 | }\r | |
428 | Document doc = db.parse(file);\r | |
429 | \r | |
430 | Element root = doc.getDocumentElement();\r | |
431 | if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
432 | return new CustomTxtTraceDefinition[0];\r | |
433 | }\r | |
434 | \r | |
435 | ArrayList<CustomTxtTraceDefinition> defList = new ArrayList<CustomTxtTraceDefinition>();\r | |
436 | NodeList nodeList = root.getChildNodes();\r | |
437 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
438 | Node node = nodeList.item(i);\r | |
439 | if (node instanceof Element && node.getNodeName().equals(DEFINITION_ELEMENT)) {\r | |
440 | CustomTxtTraceDefinition def = extractDefinition((Element) node);\r | |
441 | if (def != null) {\r | |
442 | defList.add(def);\r | |
443 | }\r | |
444 | }\r | |
445 | }\r | |
446 | return defList.toArray(new CustomTxtTraceDefinition[0]);\r | |
447 | } catch (ParserConfigurationException e) {\r | |
448 | e.printStackTrace();\r | |
449 | } catch (SAXException e) {\r | |
450 | e.printStackTrace();\r | |
451 | } catch (IOException e) {\r | |
452 | e.printStackTrace();\r | |
453 | }\r | |
454 | return new CustomTxtTraceDefinition[0];\r | |
455 | }\r | |
456 | \r | |
457 | public static CustomTxtTraceDefinition load(String definitionName) {\r | |
458 | try {\r | |
459 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
460 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
461 | \r | |
462 | // The following allows xml parsing without access to the dtd\r | |
463 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 | 464 | @Override\r |
c3c5c786 | 465 | public InputSource resolveEntity (String publicId, String systemId) {\r |
3b38ea61 | 466 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
467 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
468 | return new InputSource(bais);\r | |
469 | }\r | |
470 | };\r | |
471 | db.setEntityResolver(resolver);\r | |
472 | \r | |
473 | // The following catches xml parsing exceptions\r | |
474 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 | 475 | @Override\r |
c3c5c786 | 476 | public void error(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 477 | @Override\r |
c3c5c786 | 478 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 479 | @Override\r |
c3c5c786 FC |
480 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r |
481 | throw saxparseexception;\r | |
482 | }});\r | |
483 | \r | |
484 | File file = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);\r | |
485 | Document doc = db.parse(file);\r | |
486 | \r | |
487 | Element root = doc.getDocumentElement();\r | |
488 | if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
489 | return null;\r | |
490 | }\r | |
491 | \r | |
492 | NodeList nodeList = root.getChildNodes();\r | |
493 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
494 | Node node = nodeList.item(i);\r | |
495 | if (node instanceof Element &&\r | |
496 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
497 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
498 | return extractDefinition((Element) node);\r | |
499 | }\r | |
500 | }\r | |
501 | } catch (ParserConfigurationException e) {\r | |
502 | e.printStackTrace();\r | |
503 | } catch (SAXException e) {\r | |
504 | e.printStackTrace();\r | |
505 | } catch (IOException e) {\r | |
506 | e.printStackTrace();\r | |
507 | }\r | |
508 | return null;\r | |
509 | }\r | |
510 | \r | |
511 | public static CustomTxtTraceDefinition extractDefinition(Element definitionElement) {\r | |
512 | CustomTxtTraceDefinition def = new CustomTxtTraceDefinition();\r | |
513 | \r | |
514 | def.definitionName = definitionElement.getAttribute(NAME_ATTRIBUTE);\r | |
515 | if (def.definitionName == null) return null;\r | |
516 | \r | |
517 | NodeList nodeList = definitionElement.getChildNodes();\r | |
518 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
519 | Node node = nodeList.item(i);\r | |
520 | String nodeName = node.getNodeName();\r | |
521 | if (nodeName.equals(TIME_STAMP_OUTPUT_FORMAT_ELEMENT)) {\r | |
522 | Element formatElement = (Element) node;\r | |
523 | def.timeStampOutputFormat = formatElement.getTextContent();\r | |
524 | } else if (nodeName.equals(INPUT_LINE_ELEMENT)) {\r | |
525 | InputLine inputLine = extractInputLine((Element) node);\r | |
526 | if (inputLine != null) {\r | |
527 | def.inputs.add(inputLine);\r | |
528 | }\r | |
529 | } else if (nodeName.equals(OUTPUT_COLUMN_ELEMENT)) {\r | |
530 | Element outputColumnElement = (Element) node;\r | |
531 | OutputColumn outputColumn = new OutputColumn();\r | |
532 | outputColumn.name = outputColumnElement.getAttribute(NAME_ATTRIBUTE);\r | |
533 | def.outputs.add(outputColumn);\r | |
534 | }\r | |
535 | }\r | |
536 | return def;\r | |
537 | }\r | |
538 | \r | |
539 | private static InputLine extractInputLine(Element inputLineElement) {\r | |
540 | InputLine inputLine = new InputLine();\r | |
541 | NodeList nodeList = inputLineElement.getChildNodes();\r | |
542 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
543 | Node node = nodeList.item(i);\r | |
544 | String nodeName = node.getNodeName();\r | |
545 | if (nodeName.equals(CARDINALITY_ELEMENT)) {\r | |
546 | Element cardinalityElement = (Element) node;\r | |
547 | try {\r | |
548 | int min = Integer.parseInt(cardinalityElement.getAttribute(MIN_ATTRIBUTE));\r | |
549 | int max = Integer.parseInt(cardinalityElement.getAttribute(MAX_ATTRIBUTE));\r | |
550 | inputLine.cardinality = new Cardinality(min, max);\r | |
551 | } catch (NumberFormatException e) {\r | |
552 | return null;\r | |
553 | }\r | |
554 | } else if (nodeName.equals(REGEX_ELEMENT)) {\r | |
555 | Element regexElement = (Element) node;\r | |
556 | inputLine.regex = regexElement.getTextContent();\r | |
557 | } else if (nodeName.equals(INPUT_DATA_ELEMENT)) {\r | |
558 | Element inputDataElement = (Element) node;\r | |
559 | InputData inputData = new InputData();\r | |
560 | inputData.name = inputDataElement.getAttribute(NAME_ATTRIBUTE);\r | |
561 | inputData.action = Integer.parseInt(inputDataElement.getAttribute(ACTION_ATTRIBUTE));\r | |
562 | inputData.format = inputDataElement.getAttribute(FORMAT_ATTRIBUTE);\r | |
563 | inputLine.addColumn(inputData);\r | |
564 | } else if (nodeName.equals(INPUT_LINE_ELEMENT)) {\r | |
565 | Element childInputLineElement = (Element) node;\r | |
566 | InputLine childInputLine = extractInputLine(childInputLineElement);\r | |
567 | if (childInputLine != null) {\r | |
568 | inputLine.addChild(childInputLine);\r | |
569 | }\r | |
570 | }\r | |
571 | }\r | |
572 | return inputLine;\r | |
573 | }\r | |
574 | \r | |
575 | public static void delete(String definitionName) {\r | |
576 | try {\r | |
577 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\r | |
578 | DocumentBuilder db = dbf.newDocumentBuilder();\r | |
579 | \r | |
580 | // The following allows xml parsing without access to the dtd\r | |
581 | EntityResolver resolver = new EntityResolver () {\r | |
d4011df2 | 582 | @Override\r |
c3c5c786 | 583 | public InputSource resolveEntity (String publicId, String systemId) {\r |
3b38ea61 | 584 | String empty = ""; //$NON-NLS-1$\r |
c3c5c786 FC |
585 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());\r |
586 | return new InputSource(bais);\r | |
587 | }\r | |
588 | };\r | |
589 | db.setEntityResolver(resolver);\r | |
590 | \r | |
591 | // The following catches xml parsing exceptions\r | |
592 | db.setErrorHandler(new ErrorHandler(){\r | |
d4011df2 | 593 | @Override\r |
c3c5c786 | 594 | public void error(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 595 | @Override\r |
c3c5c786 | 596 | public void warning(SAXParseException saxparseexception) throws SAXException {}\r |
d4011df2 | 597 | @Override\r |
c3c5c786 FC |
598 | public void fatalError(SAXParseException saxparseexception) throws SAXException {\r |
599 | throw saxparseexception;\r | |
600 | }});\r | |
601 | \r | |
602 | File file = new File(CUSTOM_TXT_TRACE_DEFINITIONS_PATH_NAME);\r | |
603 | Document doc = db.parse(file);\r | |
604 | \r | |
605 | Element root = doc.getDocumentElement();\r | |
606 | if (! root.getNodeName().equals(CUSTOM_TXT_TRACE_DEFINITION_ROOT_ELEMENT)) {\r | |
607 | return;\r | |
608 | }\r | |
609 | \r | |
610 | NodeList nodeList = root.getChildNodes();\r | |
611 | for (int i = 0; i < nodeList.getLength(); i++) {\r | |
612 | Node node = nodeList.item(i);\r | |
613 | if (node instanceof Element &&\r | |
614 | node.getNodeName().equals(DEFINITION_ELEMENT) &&\r | |
615 | definitionName.equals(((Element) node).getAttribute(NAME_ATTRIBUTE))) {\r | |
616 | root.removeChild(node);\r | |
617 | }\r | |
618 | }\r | |
619 | \r | |
620 | Transformer transformer = TransformerFactory.newInstance().newTransformer();\r | |
3b38ea61 | 621 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$\r |
c3c5c786 FC |
622 | \r |
623 | //initialize StreamResult with File object to save to file\r | |
624 | StreamResult result = new StreamResult(new StringWriter());\r | |
625 | DOMSource source = new DOMSource(doc);\r | |
626 | transformer.transform(source, result);\r | |
627 | String xmlString = result.getWriter().toString();\r | |
628 | \r | |
629 | FileWriter writer = new FileWriter(file);\r | |
630 | writer.write(xmlString);\r | |
631 | writer.close();\r | |
632 | } catch (ParserConfigurationException e) {\r | |
633 | e.printStackTrace();\r | |
634 | } catch (SAXException e) {\r | |
635 | e.printStackTrace();\r | |
636 | } catch (IOException e) {\r | |
637 | e.printStackTrace();\r | |
638 | } catch (TransformerConfigurationException e) {\r | |
639 | e.printStackTrace();\r | |
640 | } catch (TransformerFactoryConfigurationError e) {\r | |
641 | e.printStackTrace();\r | |
642 | } catch (TransformerException e) {\r | |
643 | e.printStackTrace();\r | |
644 | }\r | |
645 | }\r | |
646 | }\r |