Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/cerberus/component/descriptor/parser/DefinitionParser.java


1   /*
2    * DefinitionParser.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.InputStream;
13  import java.io.IOException;
14  
15  import java.util.ArrayList;
16  import java.util.Collection;
17  
18  import com.aendvari.common.model.*;
19  import com.aendvari.common.model.osm.*;
20  
21  import com.aendvari.cerberus.component.descriptor.*;
22  
23  /**
24   * <p>Constructs a {@link ComponentDefinition} from the content of a {@link ModelNode}.</p>
25   *
26   * <p>The {@link ModelNode} contains the definition of a single component.</p>
27   *
28   * <p>This class only reads the content of the {@link ModelNode}, it does not attempt to verify
29   * the definition.</p>
30   *
31   * @author  Trevor Milne
32   *
33   */
34  
35  public class DefinitionParser extends DescriptorParser
36  {
37    /* Constants. */
38  
39  
40    /** Constants for descriptor element names. */
41    private interface NodeNames
42    {
43      public static String Definition      = "definition";
44  
45      public static String Name        = "name";
46      public static String Description    = "description";
47      public static String Class        = "class";
48      public static String Extends      = "extends";
49  
50      public static String Access        = "access";
51      public static String Public        = "public";
52      public static String Private      = "private";
53  
54      public static String Attribute      = "attribute";
55      public static String Required      = "required";
56      public static String Value        = "value";
57  
58      public static String Message      = "message";
59      public static String Type        = "type";
60      public static String Send        = "send";
61      public static String Receive      = "receive";
62  
63      public static String Topic        = "topic";
64      public static String Map        = "map";
65  
66      public static String Signature      = "signature";
67      public static String Property      = "property";
68  
69      public static String Component      = "component";
70    }
71  
72  
73    /* Constructors. */
74  
75  
76    /**
77     * Constructs a <code>DefinitionParser</code> instance.
78     *
79     */
80  
81    public DefinitionParser()
82    {
83    }
84  
85  
86    /* Parsing. */
87  
88  
89    /**
90     * Parses an attribute of a component definition.
91     *
92     * @param    objectNode          The {@link ModelNode} containing the attribute.
93     *
94     * @return                  A {@link ComponentAttribute} containing the attribute information.
95     *
96     */
97  
98    private ComponentAttribute parseAttribute(ModelNode objectNode)
99    {
100     // create a attribute
101     ComponentAttribute attribute = new ComponentAttribute();
102 
103     ModelNode node = objectNode.getFirstChild();
104 
105     while (node != null)
106     {
107       String nodeName = node.getNodeName();
108 
109       // process node types
110       if (nodeName.equals(NodeNames.Name))
111       {
112         attribute.setName(node.getNodeValue());
113       }
114       else
115       if (nodeName.equals(NodeNames.Description))
116       {
117         attribute.setDescription(node.getNodeValue());
118       }
119       else
120       if (nodeName.equals(NodeNames.Access))
121       {
122         String type = node.getNodeValue();
123 
124         if (type.equalsIgnoreCase(NodeNames.Public))
125           attribute.setAccess(ComponentAttribute.Access.Public);
126         else
127         if (type.equalsIgnoreCase(NodeNames.Private))
128           attribute.setAccess(ComponentAttribute.Access.Private);
129       }
130       else
131       if (nodeName.equals(NodeNames.Required))
132       {
133         attribute.setRequired(node.getNodeValue().equalsIgnoreCase("true"));
134       }
135       else
136       if (nodeName.equals(NodeNames.Value))
137       {
138         MultiPartValue value = attribute.getValue();
139         parseMultiPartValue(node, value);
140       }
141 
142       // get next child node
143       node = node.getNextSibling();
144     }
145 
146     return attribute;
147   }
148 
149   /**
150    * Parses a property of a signature.
151    *
152    * @param    objectNode          The {@link ModelNode} containing the property.
153    *
154    * @return                  A {@link SignatureProperty} containing the property information.
155    *
156    */
157 
158   private SignatureProperty parseSignatureProperty(ModelNode objectNode)
159   {
160     // create a property
161     SignatureProperty property = new SignatureProperty();
162 
163     ModelNode node = objectNode.getFirstChild();
164 
165     while (node != null)
166     {
167       String nodeName = node.getNodeName();
168 
169       // process node types
170       if (nodeName.equals(NodeNames.Name))
171       {
172         property.setName(node.getNodeValue());
173       }
174       else
175       if (nodeName.equals(NodeNames.Description))
176       {
177         property.setDescription(node.getNodeValue());
178       }
179       else
180       if (nodeName.equals(NodeNames.Required))
181       {
182         property.setRequired(node.getNodeValue().equalsIgnoreCase("true"));
183       }
184       else
185       if (nodeName.equals(NodeNames.Type))
186       {
187         property.setType(node.getNodeValue());
188       }
189 
190       // get next child node
191       node = node.getNextSibling();
192     }
193 
194     return property;
195   }
196 
197   /**
198    * Parses a signature of a message.
199    *
200    * @param    objectNode          The {@link ModelNode} containing the signature.
201    * @param    signature          The {@link MessageSignature} for the message.
202    *
203    */
204 
205   private void parseMessageSignature(ModelNode objectNode, MessageSignature signature)
206   {
207     // parse the properties of the signature
208     ModelNode node = objectNode.getFirstChild();
209 
210     while (node != null)
211     {
212       String nodeName = node.getNodeName();
213 
214       // process node types
215       if (nodeName.equals(NodeNames.Property))
216       {
217         SignatureProperty property = parseSignatureProperty(node);
218         signature.addProperty(property);
219       }
220 
221       // get next child node
222       node = node.getNextSibling();
223     }
224   }
225 
226   /**
227    * Parses a message of a component definition.
228    *
229    * @param    objectNode          The {@link ModelNode} containing the message.
230    *
231    * @return                  A {@link ComponentMessage} containing the message information.
232    *
233    */
234 
235   private ComponentMessage parseMessage(ModelNode objectNode)
236   {
237     // create a message
238     ComponentMessage message = new ComponentMessage();
239 
240     ModelNode node = objectNode.getFirstChild();
241 
242     while (node != null)
243     {
244       String nodeName = node.getNodeName();
245 
246       // process node types
247       if (nodeName.equals(NodeNames.Name))
248       {
249         message.setName(node.getNodeValue());
250       }
251       else
252       if (nodeName.equals(NodeNames.Description))
253       {
254         message.setDescription(node.getNodeValue());
255       }
256       else
257       if (nodeName.equals(NodeNames.Type))
258       {
259         String type = node.getNodeValue();
260 
261         if (type.equalsIgnoreCase(NodeNames.Send))
262           message.setType(ComponentMessage.Type.Send);
263         else
264         if (type.equalsIgnoreCase(NodeNames.Receive))
265           message.setType(ComponentMessage.Type.Receive);
266         else
267           message.setType(ComponentMessage.Type.Undefined);
268       }
269       else
270       if (nodeName.equals(NodeNames.Required))
271       {
272         message.setRequired(node.getNodeValue().equalsIgnoreCase("true"));
273       }
274       else
275       if (nodeName.equals(NodeNames.Access))
276       {
277         String type = node.getNodeValue();
278 
279         if (type.equalsIgnoreCase(NodeNames.Public))
280           message.setAccess(ComponentMessage.Access.Public);
281         else
282         if (type.equalsIgnoreCase(NodeNames.Private))
283           message.setAccess(ComponentMessage.Access.Private);
284       }
285       else
286       if (nodeName.equals(NodeNames.Topic))
287       {
288         MultiPartValue topic = message.getTopic();
289         parseMultiPartValue(node, topic);
290       }
291       else
292       if (nodeName.equals(NodeNames.Map))
293       {
294         // place mapped message name as 'topic' of message
295         message.setMapped(true);
296 
297         MultiPartValue name = message.getTopic();
298         name.addLiteral(node.getNodeValue());
299       }
300       else
301       if (nodeName.equals(NodeNames.Signature))
302       {
303         MessageSignature signature = message.getSignature();
304         parseMessageSignature(node, signature);
305       }
306 
307       // get next child node
308       node = node.getNextSibling();
309     }
310 
311     return message;
312   }
313 
314   /**
315    * Parses the given {@link ModelNode} containing a component definition. The {@link ModelNode}
316    * must be the <definition> element.
317    *
318    * @param    definitionNode        A {@link ModelNode} containing a component definition.
319    *
320    * @return                  A {@link ComponentDefinition} containing the information in the node.
321    *
322    */
323 
324   public ComponentDefinition parse(ModelNode definitionNode)
325   {
326     ComponentDefinition definition = new ComponentDefinition();
327 
328     ModelNode node = definitionNode.getFirstChild();
329 
330     while (node != null)
331     {
332       String nodeName = node.getNodeName();
333 
334       // process node types
335       if (nodeName.equals(NodeNames.Name))
336       {
337         definition.setName(node.getNodeValue());
338       }
339       else
340       if (nodeName.equals(NodeNames.Description))
341       {
342         definition.setDescription(node.getNodeValue());
343       }
344       else
345       if (nodeName.equals(NodeNames.Class))
346       {
347         definition.setBackingClass(node.getNodeValue());
348       }
349       else
350       if (nodeName.equals(NodeNames.Extends))
351       {
352         definition.addParent(node.getNodeValue());
353       }
354       else
355       if (nodeName.equals(NodeNames.Attribute))
356       {
357         definition.addAttribute(parseAttribute(node));
358       }
359       else
360       if (nodeName.equals(NodeNames.Message))
361       {
362         definition.addMessage(parseMessage(node));
363       }
364       else
365       if (nodeName.equals(NodeNames.Component))
366       {
367         InstanceParser instanceParser = new InstanceParser();
368         ComponentInstance instance = instanceParser.parse(node);
369 
370         definition.addComponent(instance);
371       }
372 
373       // get next child node
374       node = node.getNextSibling();
375     }
376 
377     return definition;
378   }
379 
380   /**
381    * Parses the content of the given <code>InputStream</code> as a component definition.
382    *
383    * @param    inputStream            The <code>InputStream</code> containing
384    *                        the component definition.
385    *
386    * @return                    A {@link ComponentDefinition} containing the
387    *                        information in the stream.
388    *
389    * @exception  ParserException          The input stream could not be parsed.
390    *
391    */
392 
393   public ComponentDefinition parse(InputStream inputStream)
394     throws ParserException
395   {
396     ModelTree modelTree = new OsmModelTree();
397 
398     try
399     {
400       // read the content of the stream
401       modelTree.loadFromStream(inputStream);
402     }
403     catch (ModelParserException exception)
404     {
405       throw new ParserException(exception);
406     }
407 
408     // parse the tree, ignore the "OsmRoot" node
409     ModelNode node = modelTree.getRootNode().getFirstChild();
410     ComponentDefinition definition = parse(node);
411 
412     return definition;
413   }
414 }
415