1 /* |
|
2 * Copyright (c) 2008 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 * Parser for trace property files |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine.propertyfile; |
|
20 |
|
21 import java.util.HashMap; |
|
22 |
|
23 import javax.xml.parsers.DocumentBuilder; |
|
24 |
|
25 import org.w3c.dom.Document; |
|
26 import org.w3c.dom.Element; |
|
27 import org.w3c.dom.Node; |
|
28 import org.w3c.dom.NodeList; |
|
29 |
|
30 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages; |
|
31 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals; |
|
32 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.StringErrorParameters; |
|
33 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode; |
|
34 import com.nokia.tracecompiler.model.TraceCompilerException; |
|
35 import com.nokia.tracecompiler.model.TraceModel; |
|
36 import com.nokia.tracecompiler.project.ProjectFileParser; |
|
37 |
|
38 /** |
|
39 * Parser for trace property files |
|
40 * |
|
41 */ |
|
42 final class PropertyFileParser extends ProjectFileParser { |
|
43 |
|
44 /** |
|
45 * DOM document representing the property file |
|
46 */ |
|
47 private Document document; |
|
48 |
|
49 /** |
|
50 * Parsers for document elements |
|
51 */ |
|
52 private HashMap<String, PropertyFileElementParser> elementParsers = new HashMap<String, PropertyFileElementParser>(); |
|
53 |
|
54 /** |
|
55 * Document builder |
|
56 */ |
|
57 private DocumentBuilder builder; |
|
58 |
|
59 /** |
|
60 * Constructor |
|
61 * |
|
62 * @param model |
|
63 * the trace model |
|
64 * @param fileName |
|
65 * the property file name |
|
66 * @param builder |
|
67 * document builder |
|
68 * @throws TraceCompilerException |
|
69 * if parser cannot be created |
|
70 */ |
|
71 protected PropertyFileParser(TraceModel model, String fileName, |
|
72 DocumentBuilder builder) throws TraceCompilerException { |
|
73 super(model, fileName); |
|
74 this.builder = builder; |
|
75 elementParsers.put(PropertyFileConstants.ENUM_ELEMENT, |
|
76 new EnumElementParser(this)); |
|
77 elementParsers.put(PropertyFileConstants.VALUE_ELEMENT, |
|
78 new ValueElementParser()); |
|
79 elementParsers.put(PropertyFileConstants.FILE_ELEMENT, |
|
80 new FileElementParser()); |
|
81 |
|
82 } |
|
83 |
|
84 /* |
|
85 * (non-Javadoc) |
|
86 * |
|
87 * @see com.nokia.tracecompiler.project.ProjectFileParser#createParser() |
|
88 */ |
|
89 @Override |
|
90 protected void createParser() throws TraceCompilerException { |
|
91 } |
|
92 |
|
93 /* |
|
94 * (non-Javadoc) |
|
95 * |
|
96 * @see com.nokia.tracecompiler.project.ProjectFileParser#parse() |
|
97 */ |
|
98 @Override |
|
99 public void parse() throws TraceCompilerException { |
|
100 try { |
|
101 document = builder.parse(projectFile); |
|
102 Element rootElement = PropertyFileUtils.findRoot(document); |
|
103 if (rootElement != null) { |
|
104 parseChildren(model, rootElement); |
|
105 } else { |
|
106 throw new TraceCompilerException( |
|
107 TraceCompilerErrorCode.INVALID_PROJECT_FILE); |
|
108 } |
|
109 } catch (TraceCompilerException e) { |
|
110 throw e; |
|
111 } catch (Exception e) { |
|
112 throw new TraceCompilerException( |
|
113 TraceCompilerErrorCode.INVALID_PROJECT_FILE, e); |
|
114 } |
|
115 } |
|
116 |
|
117 /** |
|
118 * Parses child elements of given element |
|
119 * |
|
120 * @param owner |
|
121 * the owning object |
|
122 * @param element |
|
123 * the element |
|
124 */ |
|
125 void parseChildren(Object owner, Element element) { |
|
126 NodeList list; |
|
127 list = element.getChildNodes(); |
|
128 for (int i = 0; i < list.getLength(); i++) { |
|
129 Node node = list.item(i); |
|
130 if (node.getNodeType() == Node.ELEMENT_NODE) { |
|
131 parseElement(owner, (Element) node); |
|
132 } |
|
133 } |
|
134 } |
|
135 |
|
136 /** |
|
137 * Parses an element |
|
138 * |
|
139 * @param owner |
|
140 * the owning trace object |
|
141 * @param element |
|
142 * the element to be parsed |
|
143 */ |
|
144 private void parseElement(Object owner, Element element) { |
|
145 String name = element.getNodeName(); |
|
146 PropertyFileElementParser parser = elementParsers.get(name); |
|
147 if (parser != null) { |
|
148 try { |
|
149 parser.parse(owner, element); |
|
150 } catch (TraceCompilerException e) { |
|
151 String msg = TraceCompilerEngineErrorMessages.getErrorMessage(e); |
|
152 TraceCompilerEngineGlobals.getEvents().postWarningMessage(msg, |
|
153 e.getErrorSource()); |
|
154 } |
|
155 } else { |
|
156 postElementNotSupportedWarning(name); |
|
157 } |
|
158 } |
|
159 |
|
160 /** |
|
161 * Posts element not supported warning |
|
162 * |
|
163 * @param name |
|
164 * the element name |
|
165 */ |
|
166 private void postElementNotSupportedWarning(String name) { |
|
167 StringErrorParameters parameter = new StringErrorParameters(); |
|
168 parameter.string = name; |
|
169 String msg = TraceCompilerEngineErrorMessages.getErrorMessage( |
|
170 TraceCompilerErrorCode.PROPERTY_FILE_ELEMENT_NOT_SUPPORTED, |
|
171 parameter); |
|
172 TraceCompilerEngineGlobals.getEvents().postWarningMessage(msg, null); |
|
173 } |
|
174 |
|
175 /** |
|
176 * Gets the document representing the property file |
|
177 * |
|
178 * @return the document |
|
179 */ |
|
180 Document getDocument() { |
|
181 return document; |
|
182 } |
|
183 } |
|