|
1 /* |
|
2 * Copyright (c) 2007-2010 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 * Metadata file Importer class |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.metafile; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 import java.util.Map; |
|
24 |
|
25 import javax.xml.parsers.DocumentBuilder; |
|
26 import javax.xml.parsers.DocumentBuilderFactory; |
|
27 import javax.xml.parsers.ParserConfigurationException; |
|
28 |
|
29 import org.w3c.dom.Attr; |
|
30 import org.w3c.dom.Document; |
|
31 import org.w3c.dom.Element; |
|
32 import org.w3c.dom.NamedNodeMap; |
|
33 import org.w3c.dom.Node; |
|
34 import org.w3c.dom.NodeList; |
|
35 import org.xml.sax.SAXException; |
|
36 |
|
37 /** |
|
38 * Metadata file Importer class |
|
39 */ |
|
40 public class MetaFileXMLImporter { |
|
41 |
|
42 /** |
|
43 * File path where to import |
|
44 */ |
|
45 private final String filePath; |
|
46 |
|
47 /** |
|
48 * Constructor |
|
49 * |
|
50 * @param filePath |
|
51 * file name where to export data |
|
52 */ |
|
53 public MetaFileXMLImporter(String filePath) { |
|
54 this.filePath = filePath; |
|
55 } |
|
56 |
|
57 /** |
|
58 * Imports trace comment data |
|
59 * |
|
60 * @param traceComments |
|
61 * trace comments map |
|
62 * @return true if everything went fine |
|
63 */ |
|
64 public boolean importData(Map<Integer, String> traceComments) { |
|
65 boolean success = true; |
|
66 File file = new File(filePath); |
|
67 if (file.exists()) { |
|
68 Document doc = getDocument(); |
|
69 |
|
70 // Get root node of trace comment rules |
|
71 NodeList list = doc |
|
72 .getElementsByTagName(MetaFileXMLConstants.TRACECOMMENTS_TAG); |
|
73 Node parent = list.item(0); |
|
74 |
|
75 // Go trough the comments |
|
76 if (parent != null) { |
|
77 NodeList comments = parent.getChildNodes(); |
|
78 if (comments != null) { |
|
79 |
|
80 for (int i = 0; i < comments.getLength(); i++) { |
|
81 Node node = comments.item(i); |
|
82 if (node instanceof Element) { |
|
83 |
|
84 // Get line number |
|
85 NamedNodeMap itemAttributes = node.getAttributes(); |
|
86 Attr lineAttr = (Attr) itemAttributes |
|
87 .getNamedItem(MetaFileXMLConstants.LINE_TAG); |
|
88 String line = lineAttr.getValue(); |
|
89 |
|
90 // Get comment |
|
91 String comment = node.getTextContent(); |
|
92 |
|
93 // Insert to the comments map |
|
94 traceComments.put(Integer.valueOf(line), comment); |
|
95 } |
|
96 } |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 return success; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Gets DOM document for the file path |
|
106 * |
|
107 * @return document |
|
108 */ |
|
109 protected Document getDocument() { |
|
110 DocumentBuilderFactory docFactory = DocumentBuilderFactory |
|
111 .newInstance(); |
|
112 DocumentBuilder docBuilder = null; |
|
113 try { |
|
114 docBuilder = docFactory.newDocumentBuilder(); |
|
115 } catch (ParserConfigurationException e) { |
|
116 e.printStackTrace(); |
|
117 } |
|
118 |
|
119 // Get the document |
|
120 Document doc = null; |
|
121 try { |
|
122 if (docBuilder != null) { |
|
123 File file = new File(filePath); |
|
124 if (file.exists()) { |
|
125 doc = docBuilder.parse(filePath); |
|
126 } |
|
127 } |
|
128 } catch (SAXException e1) { |
|
129 e1.printStackTrace(); |
|
130 |
|
131 } catch (IOException e1) { |
|
132 e1.printStackTrace(); |
|
133 } |
|
134 return doc; |
|
135 } |
|
136 } |