KickJava   Java API By Example, From Geeks To Geeks.

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


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.OMDocument;
20 import org.apache.axis2.soap.SOAPEnvelope;
21
22 import javax.xml.stream.XMLStreamConstants;
23 import javax.xml.stream.XMLStreamReader;
24
25 /**
26  * This will construct an OM without using SOAP specific classes like SOAPEnvelope, SOAPHeader, SOAPHeaderBlock and SOAPBody.
27  * And this will habe the Document concept also.
28  */

29 public class StAXOMBuilder extends StAXBuilder{
30     /**
31      * Field document
32      */

33     protected OMDocument document;
34
35      /**
36      * Constructor StAXOMBuilder
37      *
38      * @param ombuilderFactory
39      * @param parser
40      */

41     public StAXOMBuilder(OMFactory ombuilderFactory, XMLStreamReader parser) {
42         super(ombuilderFactory, parser);
43         document = new OMDocument(this);
44         omfactory = OMAbstractFactory.getOMFactory();
45     }
46
47     /**
48      * Constructor StAXOMBuilder
49      *
50      * @param parser
51      */

52     public StAXOMBuilder(XMLStreamReader parser) {
53         super(parser);
54         document = new OMDocument(this);
55         omfactory = OMAbstractFactory.getOMFactory();
56     }
57
58     /**
59      * Method createOMElement
60      *
61      * @return
62      * @throws OMException
63      */

64     protected OMNode createOMElement() throws OMException {
65         OMElement node;
66         String JavaDoc elementName = parser.getLocalName();
67         if (lastNode == null) {
68             node = omfactory.createOMElement(elementName, null, null, this);
69             document.setRootElement(node);
70         } else if (lastNode.isComplete()) {
71             node = omfactory.createOMElement(elementName, null,
72                     lastNode.getParent(), this);
73             lastNode.setNextSibling(node);
74             node.setPreviousSibling(lastNode);
75         } else {
76             OMElement e = (OMElement) lastNode;
77             node = omfactory.createOMElement(elementName, null,
78                     (OMElement) lastNode, this);
79             e.setFirstChild(node);
80         }
81
82         // create the namespaces
83
processNamespaceData(node, false);
84
85         // fill in the attributes
86
processAttributes(node);
87         return node;
88     }
89
90     /**
91      * Method getOMEnvelope
92      *
93      * @return
94      * @throws OMException
95      */

96     public SOAPEnvelope getOMEnvelope() throws OMException {
97         throw new UnsupportedOperationException JavaDoc(); // TODO implement this
98
}
99
100     /**
101      * Method next
102      *
103      * @return
104      * @throws OMException
105      */

106     public int next() throws OMException {
107         try {
108             if (done) {
109                 throw new OMException();
110             }
111             ///////////////////////////////////
112
// int token = parser.getEventType();
113
//////////////////////////////////
114

115             int token = parser.next();
116             if (!cache) {
117                 return token;
118             }
119             switch (token) {
120                 case XMLStreamConstants.START_ELEMENT:
121                     lastNode = createOMElement();
122                     break;
123                 case XMLStreamConstants.START_DOCUMENT:
124                     //Don't do anything in the start document event
125
//We've already assumed that start document has passed!
126
//document = new OMDocument(this);
127
break;
128                 case XMLStreamConstants.CHARACTERS:
129                     lastNode = createOMText();
130                     break;
131                 case XMLStreamConstants.END_ELEMENT:
132                     if (lastNode.isComplete()) {
133                         OMElement parent = (OMElement)lastNode.getParent();
134                         parent.setComplete(true);
135                         lastNode = parent;
136                     } else {
137                         OMElement e = (OMElement) lastNode;
138                         e.setComplete(true);
139                     }
140                     break;
141                 case XMLStreamConstants.END_DOCUMENT:
142                     done = true;
143                     break;
144                 case XMLStreamConstants.SPACE:
145                     next();
146                     break;
147                 default :
148                     throw new OMException();
149             }
150             ////////////////////
151
// if (!done) parser.next();
152
///////////////////
153
return token;
154         } catch (OMException e) {
155             throw e;
156         } catch (Exception JavaDoc e) {
157             throw new OMException(e);
158         }
159     }
160
161     /**
162      * Method getDocumentElement
163      *
164      * @return
165      */

166     public OMElement getDocumentElement() {
167         return document.getRootElement();
168     }
169
170     /**
171      * Method processNamespaceData
172      *
173      * @param node
174      * @param isSOAPElement
175      */

176     protected void processNamespaceData(OMElement node, boolean isSOAPElement) {
177           int namespaceCount = parser.getNamespaceCount();
178         for (int i = 0; i < namespaceCount; i++) {
179             node.declareNamespace(parser.getNamespaceURI(i),
180                     parser.getNamespacePrefix(i));
181         }
182
183         // set the own namespace
184
String JavaDoc namespaceURI = parser.getNamespaceURI();
185         String JavaDoc prefix = parser.getPrefix();
186         OMNamespace namespace = null;
187         if (!"".equals(namespaceURI)) {
188             if (prefix == null) {
189                 // this means, this elements has a default namespace or it has inherited a default namespace from its parent
190
namespace = node.findNamespace(namespaceURI, "");
191                 if (namespace == null) {
192                     namespace = node.declareNamespace(namespaceURI, "");
193                 }
194             } else {
195                 namespace = node.findNamespace(namespaceURI, prefix);
196                 if(namespace == null){
197                     node.setNamespace(omfactory.createOMNamespace(namespaceURI, prefix));
198                 }else{
199                     node.setNamespace(namespace);
200                 }
201             }
202
203         } else {
204             // What to do if namespace URI is not available
205
}
206     }
207 // int namespaceCount = parser.getNamespaceCount();
208
// for (int i = 0; i < namespaceCount; i++) {
209
// node.declareNamespace(parser.getNamespaceURI(i),
210
// parser.getNamespacePrefix(i));
211
// }
212
//
213
// // set the own namespace
214
// OMNamespace namespace =
215
// node.findNamespace(parser.getNamespaceURI(),
216
// parser.getPrefix());
217
// node.setNamespace(namespace);
218
// }
219
}
220
Popular Tags