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/InstanceParser.java


1   /*
2    * InstanceParser.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 ComponentInstance} from the content of a {@link ModelNode}.</p>
25   *
26   * <p>The {@link ModelNode} contains a single component instance.</p>
27   *
28   * <p>This class only reads the content of the {@link ModelNode}, it does not attempt to verify the data.</p>
29   *
30   * @author  Trevor Milne
31   *
32   */
33  
34  public class InstanceParser extends DescriptorParser
35  {
36    /* Constants. */
37  
38  
39    /** Constants for descriptor element names. */
40    private interface NodeNames
41    {
42      public static String Component      = "component";
43  
44      public static String Name        = "name";
45      public static String Definition      = "definition";
46  
47      public static String Attribute      = "attribute";
48      public static String Constant      = "constant";
49      public static String Value        = "value";
50  
51      public static String Message      = "message";
52      public static String Topic        = "topic";
53      public static String Map        = "map";
54    }
55  
56  
57    /* Constructors. */
58  
59  
60    /**
61     * Constructs a <code>InstanceParser</code> instance.
62     *
63     */
64  
65    public InstanceParser()
66    {
67    }
68  
69  
70    /* Parsing. */
71  
72  
73    /**
74     * Parses an attribute of a component instance.
75     *
76     * @param    objectNode          The {@link ModelNode} containing the attribute.
77     *
78     * @return                  A {@link ComponentAttribute} containing the attribute information.
79     *
80     */
81  
82    private ComponentAttribute parseAttribute(ModelNode objectNode)
83    {
84      // create a attribute
85      ComponentAttribute attribute = new ComponentAttribute();
86  
87      ModelNode node = objectNode.getFirstChild();
88  
89      while (node != null)
90      {
91        String nodeName = node.getNodeName();
92  
93        // process node types
94        if (nodeName.equals(NodeNames.Name))
95        {
96          attribute.setName(node.getNodeValue());
97        }
98        else
99        if (nodeName.equals(NodeNames.Value))
100       {
101         MultiPartValue value = attribute.getValue();
102         parseMultiPartValue(node, value);
103       }
104 
105       // get next child node
106       node = node.getNextSibling();
107     }
108 
109     return attribute;
110   }
111 
112   /**
113    * Parses a message of a component instance.
114    *
115    * @param    objectNode          The {@link ModelNode} containing the message.
116    *
117    * @return                  A {@link ComponentMessage} containing the message information.
118    *
119    */
120 
121   private ComponentMessage parseMessage(ModelNode objectNode)
122   {
123     // create a message
124     ComponentMessage message = new ComponentMessage();
125 
126     ModelNode node = objectNode.getFirstChild();
127 
128     while (node != null)
129     {
130       String nodeName = node.getNodeName();
131 
132       // process node types
133       if (nodeName.equals(NodeNames.Name))
134       {
135         message.setName(node.getNodeValue());
136       }
137       else
138       if (nodeName.equals(NodeNames.Topic))
139       {
140         MultiPartValue topic = message.getTopic();
141         parseMultiPartValue(node, topic);
142       }
143       else
144       if (nodeName.equals(NodeNames.Map))
145       {
146         // place mapped message name as 'topic' of message
147         message.setMapped(true);
148 
149         MultiPartValue name = message.getTopic();
150         name.addLiteral(node.getNodeValue());
151       }
152 
153       // get next child node
154       node = node.getNextSibling();
155     }
156 
157     return message;
158   }
159 
160   /**
161    * Parses the given {@link ModelNode} containing a component instance. The {@link ModelNode}
162    * must be the <instance> element.
163    *
164    * @param    instanceNode          A {@link ModelNode} containing a component instance.
165    *
166    * @return                    A {@link ComponentInstance} containing the information in the node.
167    *
168    */
169 
170   public ComponentInstance parse(ModelNode instanceNode)
171   {
172     ComponentInstance instance = new ComponentInstance();
173 
174     ModelNode node = instanceNode.getFirstChild();
175 
176     while (node != null)
177     {
178       String nodeName = node.getNodeName();
179 
180       // process node types
181       if (nodeName.equals(NodeNames.Name))
182       {
183         instance.setName(node.getNodeValue());
184       }
185       else
186       if (nodeName.equals(NodeNames.Definition))
187       {
188         instance.setDefinition(node.getNodeValue());
189       }
190       else
191       if (nodeName.equals(NodeNames.Message))
192       {
193         instance.addMessage(parseMessage(node));
194       }
195       else
196       if (nodeName.equals(NodeNames.Attribute))
197       {
198         instance.addAttribute(parseAttribute(node));
199       }
200 
201       // get next child node
202       node = node.getNextSibling();
203     }
204 
205     return instance;
206   }
207 
208   /**
209    * Parses the content of the given <code>InputStream</code> as a component instance.
210    *
211    * @param    inputStream            The <code>InputStream</code> containing the component instance.
212    *
213    * @return                    A {@link ComponentInstance} containing the information in the stream.
214    *
215    * @exception  ParserException          The input stream could not be parsed.
216    *
217    */
218 
219   public ComponentInstance parse(InputStream inputStream)
220     throws ParserException
221   {
222     ModelTree modelTree = new OsmModelTree();
223 
224     try
225     {
226       // read the content of the stream
227       modelTree.loadFromStream(inputStream);
228     }
229     catch (ModelParserException exception)
230     {
231       throw new ParserException(exception);
232     }
233 
234     // parse the tree, ignore the "OsmRoot" node
235     ModelNode node = modelTree.getRootNode().getFirstChild();
236     ComponentInstance instance = parse(node);
237 
238     return instance;
239   }
240 }
241