KickJava   Java API By Example, From Geeks To Geeks.

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


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.OMContainer;
19 import org.apache.axis2.om.OMException;
20 import org.apache.axis2.om.OMNode;
21 import org.apache.axis2.om.OMXMLParserWrapper;
22
23 /**
24  * Class OMNodeImpl
25  */

26 public abstract class OMNodeImpl implements OMNode {
27     /**
28      * Field parent
29      */

30     protected OMContainer parent;
31
32     /**
33      * Field nextSibling
34      */

35     protected OMNodeImpl nextSibling;
36
37     /**
38      * Field previousSibling
39      */

40     protected OMNodeImpl previousSibling;
41     /**
42      * Field builder
43      */

44     protected OMXMLParserWrapper builder;
45
46     /**
47      * Field done
48      */

49     protected boolean done = false;
50
51     /**
52      * Field nodeType
53      */

54     protected int nodeType;
55
56     /**
57      * Constructor OMNodeImpl
58      */

59     public OMNodeImpl() {
60     }
61
62     /**
63      * For a node to exist there must be a parent
64      *
65      * @param parent
66      */

67     public OMNodeImpl(OMContainer parent) {
68         //if ((parent != null) && (parent.getType() == OMNode.ELEMENT_NODE)) {
69
//Comment by Jaya:
70
//OMContainer is only implemented by OMElement and OMDocument which are
71
//quite well deemed to act as parents, so checking the type of parent
72
//is not necessary.
73
if ((parent != null)) {
74             this.parent = parent;
75             parent.addChild(this);
76         }
77     }
78
79     /**
80      * This method should return the immediate parent of the node.
81      * Parent is always an Element
82      *
83      * @return
84      * @throws org.apache.axis2.om.OMException
85      * @throws OMException
86      */

87     public OMContainer getParent() throws OMException {
88         return parent;
89     }
90
91     /**
92      * Method setParent
93      *
94      * @param element
95      */

96     public void setParent(OMContainer element) {
97
98         if ((this.parent) == element) {
99             return;
100         }
101
102         //If we are asked to assign a new parent in place
103
//of an existing one. We should detach this node
104
//from the aegis of previous parent.
105
if (this.parent != null) {
106             this.detach();
107         }
108         this.parent = element;
109     }
110
111     /**
112      * This will give the next sibling. This can be an OMAttribute for OMAttribute or OMText or OMELement for others.
113      *
114      * @return
115      * @throws org.apache.axis2.om.OMException
116      * @throws OMException
117      */

118     public OMNode getNextSibling() throws OMException {
119         if ((nextSibling == null) && (parent != null) && !parent.isComplete()) {
120             parent.buildNext();
121         }
122         return nextSibling;
123     }
124
125     /**
126      * Method setNextSibling
127      *
128      * @param node
129      */

130     public void setNextSibling(OMNode node) {
131         this.nextSibling = (OMNodeImpl) node;
132     }
133
134
135     /**
136      * this will indicate whether parser has parsed this information item completely or not.
137      * If somethings info are not available in the item, one has to check this attribute to make sure that, this
138      * item has been parsed completely or not.
139      *
140      * @return
141      */

142     public boolean isComplete() {
143         return done;
144     }
145
146     /**
147      * Method setComplete
148      *
149      * @param state
150      */

151     public void setComplete(boolean state) {
152         this.done = state;
153     }
154
155     /**
156      * This will remove this information item and its children, from the model completely
157      *
158      * @throws org.apache.axis2.om.OMException
159      * @throws OMException
160      */

161     public OMNode detach() throws OMException {
162         if (parent == null) {
163             throw new OMException("Elements that doesn't have a parent can not be detached");
164         }
165         OMNodeImpl nextSibling = (OMNodeImpl) getNextSibling();
166         if (previousSibling == null) {
167             parent.setFirstChild(nextSibling);
168         } else {
169             getPreviousSibling().setNextSibling(nextSibling);
170         }
171         if (nextSibling != null) {
172             nextSibling.setPreviousSibling(getPreviousSibling());
173         }
174         this.parent = null;
175         return this;
176     }
177
178     /**
179      * This will insert a sibling just after the current information item.
180      *
181      * @param sibling
182      * @throws org.apache.axis2.om.OMException
183      * @throws OMException
184      */

185     public void insertSiblingAfter(OMNode sibling) throws OMException {
186         if (parent == null) {
187             throw new OMException();
188         }
189         sibling.setParent(parent);
190         if (sibling instanceof OMNodeImpl) {
191             OMNodeImpl siblingImpl = (OMNodeImpl) sibling;
192             if (nextSibling == null) {
193                 getNextSibling();
194             }
195             siblingImpl.setPreviousSibling(this);
196             if (nextSibling != null) {
197                 nextSibling.setPreviousSibling(sibling);
198             }
199             sibling.setNextSibling(nextSibling);
200             nextSibling = siblingImpl;
201         }
202     }
203
204     /**
205      * This will insert a sibling just before the current information item
206      *
207      * @param sibling
208      * @throws org.apache.axis2.om.OMException
209      * @throws OMException
210      */

211     public void insertSiblingBefore(OMNode sibling) throws OMException {
212         if (parent == null) {
213             throw new OMException();
214         }
215         sibling.setParent(parent);
216         if (sibling instanceof OMNodeImpl) {
217             OMNodeImpl siblingImpl = (OMNodeImpl) sibling;
218             siblingImpl.setPreviousSibling(previousSibling);
219             siblingImpl.setNextSibling(this);
220             if (previousSibling == null) {
221                 parent.setFirstChild(siblingImpl);
222             } else {
223                 previousSibling.setNextSibling(siblingImpl);
224             }
225             previousSibling = siblingImpl;
226         }
227     }
228
229     /**
230      * This is to get the type of node, as this is the super class of all the nodes
231      *
232      * @return
233      * @throws org.apache.axis2.om.OMException
234      * @throws OMException
235      */

236     public int getType() throws OMException {
237         return nodeType;
238     }
239
240     /**
241      * Method setType
242      *
243      * @param nodeType
244      * @throws OMException
245      */

246     public void setType(int nodeType) throws OMException {
247         this.nodeType = nodeType;
248     }
249
250     /**
251      * Method getPreviousSibling
252      *
253      * @return
254      */

255     public OMNode getPreviousSibling() {
256         return previousSibling;
257     }
258
259     /**
260      * Method setPreviousSibling
261      *
262      * @param previousSibling
263      */

264     public void setPreviousSibling(OMNode previousSibling) {
265         this.previousSibling = (OMNodeImpl) previousSibling;
266     }
267
268
269     /**
270      * This will completely parse this node and build the object structure in the memory.
271      * However a programmatically created node will have done set to true by default and will cause
272      * populateyourself not to work properly!
273      *
274      * @throws OMException
275      */

276     public void build() throws OMException {
277         while (!done) {
278             builder.next();
279         }
280     }
281
282
283 }
284
Popular Tags