KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > builder > StAXBuilder


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om.impl.llom.builder;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.om.impl.llom.OMElementImpl;
20 import org.apache.axis2.om.impl.llom.OMNodeImpl;
21
22 import javax.xml.stream.XMLStreamConstants;
23 import javax.xml.stream.XMLStreamReader;
24
25 /**
26  * OM should be able to built from any data source. And the model it builds may be a SOAP specific one
27  * or just an XML model. This class will give some common functionality of OM Building from StAX.
28  */

29 public abstract class StAXBuilder implements OMXMLParserWrapper {
30
31     /**
32      * Field parser
33      */

34     protected XMLStreamReader parser;
35
36     /**
37      * Field omfactory
38      */

39     protected OMFactory omfactory;
40
41     /**
42      * Field lastNode
43      */

44     protected OMNode lastNode;
45
46     // returns the state of completion
47

48     /**
49      * Field done
50      */

51     protected boolean done = false;
52
53     // keeps the state of the cache
54

55     /**
56      * Field cache
57      */

58     protected boolean cache = true;
59
60     // keeps the state of the parser access. if the parser is
61
// accessed atleast once,this flag will be set
62

63     /**
64      * Field parserAccessed
65      */

66     protected boolean parserAccessed = false;
67
68     /**
69      * Constructor StAXBuilder
70      *
71      * @param ombuilderFactory
72      * @param parser
73      */

74     protected StAXBuilder(OMFactory ombuilderFactory, XMLStreamReader parser) {
75         this.parser = parser;
76         omfactory = ombuilderFactory;
77     }
78
79     /**
80      * Constructor StAXBuilder
81      *
82      * @param parser
83      */

84     protected StAXBuilder(XMLStreamReader parser) {
85         this(OMAbstractFactory.getOMFactory(), parser);
86     }
87
88     /**
89      * Method setOmbuilderFactory
90      *
91      * @param ombuilderFactory
92      */

93     public void setOmbuilderFactory(OMFactory ombuilderFactory) {
94         this.omfactory = ombuilderFactory;
95     }
96
97     /**
98      * Method processNamespaceData
99      *
100      * @param node
101      * @param isSOAPElement
102      */

103     protected abstract void processNamespaceData(OMElement node,
104                                                  boolean isSOAPElement);
105
106     // since the behaviors are different when it comes to namespaces
107
// this must be implemented differently
108

109     /**
110      * Method processAttributes
111      *
112      * @param node
113      */

114     protected void processAttributes(OMElement node) {
115         int attribCount = parser.getAttributeCount();
116         for (int i = 0; i < attribCount; i++) {
117             OMNamespace ns = null;
118             String JavaDoc uri = parser.getAttributeNamespace(i);
119             if (uri.hashCode() != 0) {
120                 ns = node.findNamespace(uri,
121                         parser.getAttributePrefix(i));
122             }
123
124             // todo if the attributes are supposed to namespace qualified all the time
125
// todo then this should throw an exception here
126
node.addAttribute(parser.getAttributeLocalName(i),
127                     parser.getAttributeValue(i), ns);
128         }
129     }
130
131     /**
132      * Method createOMText
133      *
134      * @return
135      * @throws OMException
136      */

137     protected OMNode createOMText() throws OMException {
138         if (lastNode == null) {
139             throw new OMException();
140         }
141         OMNode node;
142         if (lastNode.isComplete()) {
143             node = omfactory.createText((OMElement)lastNode.getParent(), parser.getText());
144             lastNode.setNextSibling(node);
145             node.setPreviousSibling(lastNode);
146         } else {
147             OMElement e = (OMElement) lastNode;
148             node = omfactory.createText(e, parser.getText());
149             e.setFirstChild(node);
150         }
151         return node;
152     }
153
154     /**
155      * Method reset
156      *
157      * @param node
158      * @throws OMException
159      */

160     public void reset(OMNode node) throws OMException {
161         lastNode = null;
162     }
163
164     /**
165      * Method discard
166      *
167      * @param el
168      * @throws OMException
169      */

170     public void discard(OMElement el) throws OMException {
171         OMElementImpl elementImpl = null;
172         if (el instanceof OMElementImpl) {
173             elementImpl = (OMElementImpl) el;
174         } else {
175             throw new OMException();
176         }
177         if (elementImpl.isComplete() || !cache) {
178             throw new OMException();
179         }
180         try {
181             cache = false;
182             do {
183                 while (parser.next() != XMLStreamConstants.END_ELEMENT) ;
184
185                 // TODO:
186
} while (!parser.getName().equals(elementImpl.getQName()));
187             lastNode = (OMNodeImpl) elementImpl.getPreviousSibling();
188             if (lastNode != null) {
189                 lastNode.setNextSibling(null);
190             } else {
191                 OMElement parent = (OMElement)elementImpl.getParent();
192                 if (parent == null) {
193                     throw new OMException();
194                 }
195                 parent.setFirstChild(null);
196                 lastNode = parent;
197             }
198             cache = true;
199         } catch (OMException e) {
200             throw e;
201         } catch (Exception JavaDoc e) {
202             throw new OMException(e);
203         }
204     }
205
206     /**
207      * Method getText
208      *
209      * @return
210      * @throws OMException
211      */

212     public String JavaDoc getText() throws OMException {
213         return parser.getText();
214     }
215
216     /**
217      * Method getNamespace
218      *
219      * @return
220      * @throws OMException
221      */

222     public String JavaDoc getNamespace() throws OMException {
223         return parser.getNamespaceURI();
224     }
225
226     /**
227      * Method getNamespaceCount
228      *
229      * @return
230      * @throws OMException
231      */

232     public int getNamespaceCount() throws OMException {
233         try {
234             return parser.getNamespaceCount();
235         } catch (Exception JavaDoc e) {
236             throw new OMException(e);
237         }
238     }
239
240     /**
241      * Method getNamespacePrefix
242      *
243      * @param index
244      * @return
245      * @throws OMException
246      */

247     public String JavaDoc getNamespacePrefix(int index) throws OMException {
248         try {
249             return parser.getNamespacePrefix(index);
250         } catch (Exception JavaDoc e) {
251             throw new OMException(e);
252         }
253     }
254
255     /**
256      * Method getNamespaceUri
257      *
258      * @param index
259      * @return
260      * @throws OMException
261      */

262     public String JavaDoc getNamespaceUri(int index) throws OMException {
263         try {
264             return parser.getNamespaceURI(index);
265         } catch (Exception JavaDoc e) {
266             throw new OMException(e);
267         }
268     }
269
270     /**
271      * Method setCache
272      *
273      * @param b
274      */

275     public void setCache(boolean b) {
276         if (parserAccessed && b) {
277             throw new UnsupportedOperationException JavaDoc(
278                     "parser accessed. cannot set cache");
279         }
280         cache = b;
281     }
282
283     /**
284      * Method getName
285      *
286      * @return
287      * @throws OMException
288      */

289     public String JavaDoc getName() throws OMException {
290         return parser.getLocalName();
291     }
292
293     /**
294      * Method getPrefix
295      *
296      * @return
297      * @throws OMException
298      */

299     public String JavaDoc getPrefix() throws OMException {
300         return parser.getPrefix();
301     }
302
303     /**
304      * Method getAttributeCount
305      *
306      * @return
307      * @throws OMException
308      */

309     public int getAttributeCount() throws OMException {
310         return parser.getAttributeCount();
311     }
312
313     /**
314      * Method getAttributeNamespace
315      *
316      * @param arg
317      * @return
318      * @throws OMException
319      */

320     public String JavaDoc getAttributeNamespace(int arg) throws OMException {
321         return parser.getAttributeNamespace(arg);
322     }
323
324     /**
325      * Method getAttributeName
326      *
327      * @param arg
328      * @return
329      * @throws OMException
330      */

331     public String JavaDoc getAttributeName(int arg) throws OMException {
332         return parser.getAttributeNamespace(arg);
333     }
334
335     /**
336      * Method getAttributePrefix
337      *
338      * @param arg
339      * @return
340      * @throws OMException
341      */

342     public String JavaDoc getAttributePrefix(int arg) throws OMException {
343         return parser.getAttributeNamespace(arg);
344     }
345
346     /**
347      * Method getParser
348      *
349      * @return
350      */

351     public Object JavaDoc getParser() {
352         if (!cache) {
353             parserAccessed = true;
354             return parser;
355         } else {
356             throw new UnsupportedOperationException JavaDoc(
357                     "cache must be switched off to access the parser");
358         }
359     }
360
361     /**
362      * Method isCompleted
363      *
364      * @return
365      */

366     public boolean isCompleted() {
367         return done;
368     }
369
370     /**
371      * This method will be called with the XMLStreamConstants.START_ELEMENT event
372      *
373      * @return
374      * @throws OMException
375      */

376     protected abstract OMNode createOMElement() throws OMException;
377
378     /**
379      * This should proceed the parser one step further, if parser is not completed yet.
380      * If this has been called whist parser is done, then throw an OMException.
381      * If the cache is set to false, then should be return the event, *without* building the OM tree.
382      * If the cache is set to true, then this should handle all the events within this, and should build
383      * the object structure appropriately and return the event.
384      *
385      * @return
386      * @throws OMException
387      */

388     public abstract int next() throws OMException;
389
390     /**
391      * @return
392      */

393     public short getBuilderType() {
394         return OMConstants.PULL_TYPE_BUILDER;
395     }
396
397     /**
398      * Method registerExternalContentHandler
399      *
400      * @param obj
401      */

402     public void registerExternalContentHandler(Object JavaDoc obj) {
403         throw new UnsupportedOperationException JavaDoc();
404     }
405
406     /**
407      * Method getRegisteredContentHandler
408      *
409      * @return
410      */

411     public Object JavaDoc getRegisteredContentHandler() {
412         throw new UnsupportedOperationException JavaDoc();
413     }
414 }
415
Popular Tags