Source code: com/aendvari/cerberus/component/descriptor/parser/AssemblyParser.java
1 /*
2 * AssemblyParser.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.cerberus.component.descriptor.parser;
11
12 import java.io.FileInputStream;
13 import java.io.InputStream;
14 import java.io.IOException;
15
16 import java.net.URL;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20
21 import com.aendvari.common.model.*;
22 import com.aendvari.common.model.osm.*;
23
24 import com.aendvari.common.util.ResourceLoader;
25
26 import com.aendvari.cerberus.component.descriptor.*;
27
28 /**
29 * <p>Constructs a {@link AssemblyDescriptor} from the content of a {@link ModelNode}.</p>
30 *
31 * <p>The {@link ModelNode} contains an assembly descriptor.</p>
32 *
33 * <p>This class only reads the content of the {@link ModelNode}, it does not attempt to verify the data.</p>
34 *
35 * @author Trevor Milne
36 *
37 */
38
39 public class AssemblyParser extends DescriptorParser
40 {
41 /** Parses component definitions. */
42 protected DefinitionParser definitionParser;
43
44 /** Parses component instances. */
45 protected InstanceParser instanceParser;
46
47 /* Constants. */
48
49
50 /** Constants for descriptor element names. */
51 private interface NodeNames
52 {
53 public static String Assembly = "assembly";
54
55 public static String Include = "include";
56 public static String File = "file";
57 public static String Url = "url";
58 public static String Resource = "resource";
59
60 public static String Definition = "definition";
61 public static String Component = "component";
62 }
63
64
65 /* Constructors. */
66
67
68 /**
69 * Constructs a <code>AssemblyParser</code> instance.
70 *
71 */
72
73 public AssemblyParser()
74 {
75 definitionParser = new DefinitionParser();
76 instanceParser = new InstanceParser();
77 }
78
79
80 /* Parsing. */
81
82
83 /**
84 * Parses an include into the assembly descriptor. The content of the included descriptor
85 * is placed into the supplied assembly descriptor.
86 *
87 * @param objectNode The {@link ModelNode} containing the include.
88 * @param assembly The {@link AssemblyDescriptor} to store the information.
89 *
90 * @exception ParserException A parser error occured.
91 *
92 */
93
94 private void parseInclude(ModelNode objectNode, AssemblyDescriptor assembly)
95 throws ParserException
96 {
97 // create a message
98 ComponentMessage message = new ComponentMessage();
99
100 ModelNode node = objectNode.getFirstChild();
101
102 while (node != null)
103 {
104 String nodeName = node.getNodeName();
105
106 // process node types
107 if (nodeName.equals(NodeNames.File))
108 {
109 // open local file
110 String path = node.getNodeValue();
111
112 try
113 {
114 FileInputStream input = new FileInputStream(path);
115
116 parseInclude(input, assembly);
117
118 input.close();
119 }
120 catch (IOException exception)
121 {
122 throw new ParserException("include=" + path, exception);
123 }
124 catch (ParserException exception)
125 {
126 exception.addMessage("include=" + path);
127 throw exception;
128 }
129 }
130 else
131 if (nodeName.equals(NodeNames.Url))
132 {
133 URL url = null;
134
135 try
136 {
137 // open URL resource
138 url = new URL(node.getNodeValue());
139
140 InputStream input = url.openStream();
141
142 parseInclude(input, assembly);
143
144 input.close();
145 }
146 catch (IOException exception)
147 {
148 throw new ParserException("include=" + url, exception);
149 }
150 catch (ParserException exception)
151 {
152 exception.addMessage("include=" + url);
153 throw exception;
154 }
155 }
156 else
157 if (nodeName.equals(NodeNames.Resource))
158 {
159 // open class resource
160 String resource = node.getNodeValue();
161
162 try
163 {
164 InputStream input = ResourceLoader.getResourceAsStream(resource);
165
166 parseInclude(input, assembly);
167
168 input.close();
169 }
170 catch (IOException exception)
171 {
172 throw new ParserException("include=" + resource, exception);
173 }
174 catch (ParserException exception)
175 {
176 exception.addMessage("include=" + resource);
177 throw exception;
178 }
179 }
180
181 // get next child node
182 node = node.getNextSibling();
183 }
184 }
185
186 /**
187 * Parses the given {@link ModelNode} containing a assembly descriptor. The {@link ModelNode}
188 * must be the <assembly> element.
189 *
190 * @param assemblyNode An {@link ModelNode} containing an assembly descriptor.
191 * @param assembly The {@link AssemblyDescriptor} to store the information into.
192 *
193 * @exception ParserException A parser error occured.
194 *
195 */
196
197 private void parseAssembly(ModelNode assemblyNode, AssemblyDescriptor assembly)
198 throws ParserException
199 {
200 ModelNode node = assemblyNode.getFirstChild();
201
202 while (node != null)
203 {
204 String nodeName = node.getNodeName();
205
206 // process node types
207 if (nodeName.equals(NodeNames.Include))
208 {
209 parseInclude(node, assembly);
210 }
211 else
212 if (nodeName.equals(NodeNames.Definition))
213 {
214 assembly.addDefinition(definitionParser.parse(node));
215 }
216 else
217 if (nodeName.equals(NodeNames.Component))
218 {
219 assembly.addComponent(instanceParser.parse(node));
220 }
221
222 // get next child node
223 node = node.getNextSibling();
224 }
225 }
226
227 /**
228 * Parses the content of the given <code>InputStream</code> as a descriptor.
229 * The top level Node of the document must be a <assembly>, <descriptor>, or
230 * <component> element.
231 *
232 * @param inputStream The <code>InputStream</code> containing the assembly descriptor.
233 * @param assembly The {@link AssemblyDescriptor} to store the information into.
234 *
235 * @exception ParserException A parser error occured.
236 *
237 */
238
239 private void parseInclude(InputStream inputStream, AssemblyDescriptor assembly)
240 throws ParserException
241 {
242 ModelTree modelTree = new OsmModelTree();
243
244 try
245 {
246 // parse stream
247 modelTree.loadFromStream(inputStream);
248 }
249 catch (ModelParserException exception)
250 {
251 throw new ParserException(exception);
252 }
253
254 // parse the tree, ignore the "OsmRoot" node
255 ModelNode node = modelTree.getRootNode().getFirstChild();
256
257 String nodeName = node.getNodeName();
258
259 // process node types
260 if (nodeName.equals(NodeNames.Assembly))
261 {
262 parseAssembly(node, assembly);
263 }
264 else
265 if (nodeName.equals(NodeNames.Definition))
266 {
267 assembly.addDefinition(definitionParser.parse(node));
268 }
269 else
270 if (nodeName.equals(NodeNames.Component))
271 {
272 assembly.addComponent(instanceParser.parse(node));
273 }
274 }
275
276 /**
277 * Parses the given {@link ModelNode} containing a assembly descriptor. The {@link ModelNode}
278 * must be the <assembly> element.
279 *
280 * @param assemblyNode A {@link ModelNode} containing an assembly descriptor.
281 *
282 * @return A {@link AssemblyDescriptor} containing the information in the DOM.
283 *
284 * @exception ParserException A parser error occured.
285 *
286 */
287
288 public AssemblyDescriptor parse(ModelNode assemblyNode)
289 throws ParserException
290 {
291 AssemblyDescriptor assembly = new AssemblyDescriptor();
292
293 parseAssembly(assemblyNode, assembly);
294
295 return assembly;
296 }
297
298 /**
299 * Parses the content of the given <code>InputStream</code> as an assembly descriptor.
300 *
301 * @param inputStream The <code>InputStream</code> containing the assembly descriptor.
302 *
303 * @return A {@link AssemblyDescriptor} containing the information in the DOM.
304 *
305 * @exception ParserException A parser error occured.
306 *
307 */
308
309 public AssemblyDescriptor parse(InputStream inputStream)
310 throws ParserException
311 {
312 AssemblyDescriptor assembly = new AssemblyDescriptor();
313 ModelTree modelTree = new OsmModelTree();
314
315 try
316 {
317 // parse stream
318 modelTree.loadFromStream(inputStream);
319 }
320 catch (ModelParserException exception)
321 {
322 throw new ParserException(exception);
323 }
324
325 // parse the tree, ignore the "OsmRoot" node
326 ModelNode node = modelTree.getRootNode().getFirstChild();
327 parseAssembly(node, assembly);
328
329 return assembly;
330 }
331 }
332