KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > OMDocument


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;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.om.impl.llom.traverse.OMChildrenIterator;
20 import org.apache.axis2.om.impl.llom.traverse.OMChildrenQNameIterator;
21
22 import javax.xml.namespace.QName JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Class OMDocument
27  */

28 public class OMDocument implements OMContainer {
29     /**
30      * Field rootElement
31      */

32     private OMElement rootElement;
33     
34     /**
35      * Field firstChild
36      */

37     protected OMNode firstChild;
38     
39     /**
40      * Field lastChild
41      */

42     private OMNode lastChild;
43     
44     /**
45      * Field done
46      */

47     protected boolean done = false;
48
49     /**
50      * Field parserWrapper
51      */

52     private OMXMLParserWrapper parserWrapper;
53
54     /**
55      * @param rootElement
56      * @param parserWrapper
57      */

58     public OMDocument(OMElement rootElement, OMXMLParserWrapper parserWrapper) {
59         this.rootElement = rootElement;
60         this.parserWrapper = parserWrapper;
61     }
62
63     /**
64      * @param parserWrapper
65      */

66     public OMDocument(OMXMLParserWrapper parserWrapper) {
67         this.parserWrapper = parserWrapper;
68     }
69
70     /**
71      * Method getRootElement
72      *
73      * @return
74      */

75     public OMElement getRootElement() {
76         while (rootElement == null) {
77             parserWrapper.next();
78         }
79         return rootElement;
80     }
81
82     /**
83      * Method setRootElement
84      *
85      * @param rootElement
86      */

87     public void setRootElement(OMElement rootElement) {
88         this.rootElement = rootElement;
89     }
90     
91     /**
92      * this will indicate whether parser has parsed this information item completely or not.
93      * If somethings info are not available in the item, one has to check this attribute to make sure that, this
94      * item has been parsed completely or not.
95      *
96      * @return
97      */

98     public boolean isComplete() {
99         return done;
100     }
101
102     /**
103      * Method setComplete
104      *
105      * @param state
106      */

107     public void setComplete(boolean state) {
108         this.done = state;
109     }
110     
111     /**
112      * This will force the parser to proceed, if parser has not yet finished with the XML input
113      */

114     public void buildNext() {
115         parserWrapper.next();
116     }
117
118     /**
119      * This will add child to the element. One can decide whether he append the child or he adds to the
120      * front of the children list
121      *
122      * @param child
123      */

124     public void addChild(OMNode child) {
125         addChild((OMNodeImpl) child);
126     }
127     
128     /**
129      * Method addChild
130      *
131      * @param child
132      */

133     private void addChild(OMNodeImpl child) {
134         if (firstChild == null) {
135             firstChild = child;
136             child.setPreviousSibling(null);
137         } else {
138             child.setPreviousSibling(lastChild);
139             lastChild.setNextSibling(child);
140         }
141         child.setNextSibling(null);
142         child.setParent(this);
143         lastChild = child;
144
145     }
146
147     /**
148      * This returns a collection of this element.
149      * Children can be of types OMElement, OMText.
150      *
151      * @return
152      */

153     public Iterator getChildren() {
154         return new OMChildrenIterator(getFirstChild());
155     }
156
157     /**
158      * This will search for children with a given QName and will return an iterator to traverse through
159      * the OMNodes.
160      * This QName can contain any combination of prefix, localname and URI
161      *
162      * @param elementQName
163      * @return
164      * @throws org.apache.axis2.om.OMException
165      * @throws OMException
166      */

167     public Iterator getChildrenWithName(QName JavaDoc elementQName) throws OMException {
168         return new OMChildrenQNameIterator((OMNodeImpl) getFirstChild(),
169                 elementQName);
170     }
171
172     /**
173      * Method getFirstChild
174      *
175      * @return
176      */

177     public OMNode getFirstChild() {
178         while ((firstChild == null) && !done) {
179             buildNext();
180         }
181         return firstChild;
182     }
183
184     /**
185      * Method getFirstChildWithName
186      *
187      * @param elementQName
188      * @return
189      * @throws OMException
190      */

191     public OMElement getFirstChildWithName(QName JavaDoc elementQName) throws OMException {
192         OMChildrenQNameIterator omChildrenQNameIterator =
193                 new OMChildrenQNameIterator((OMNodeImpl) getFirstChild(),
194                         elementQName);
195         OMNode omNode = null;
196         if (omChildrenQNameIterator.hasNext()) {
197             omNode = (OMNode) omChildrenQNameIterator.next();
198         }
199
200         return ((omNode != null) && (OMNode.ELEMENT_NODE == omNode.getType())) ? (OMElement) omNode : null;
201
202     }
203
204     /**
205      * Method setFirstChild
206      *
207      * @param firstChild
208      */

209     public void setFirstChild(OMNode firstChild) {
210         this.firstChild = firstChild;
211     }
212 }
213
Popular Tags