KickJava   Java API By Example, From Geeks To Geeks.

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


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

17  package org.apache.axis2.om.impl.llom;
18
19 import org.apache.axis2.om.OMContainer;
20 import org.apache.axis2.om.OMElement;
21 import org.apache.axis2.om.OMNode;
22
23  /**
24   * Refer to the testClass to find out how to use
25   * features like isNavigable, isComplete and step
26   */

27  public class OMNavigator {
28      /**
29       * Field node
30       */

31      protected OMNode node;
32
33      /**
34       * Field visited
35       */

36      private boolean visited;
37
38      /**
39       * Field next
40       */

41      private OMNode next;
42
43      // root is the starting element. Once the navigator comes back to the
44
// root, the traversal is terminated
45

46      /**
47       * Field root
48       */

49      private OMNode root;
50
51      /**
52       * Field backtracked
53       */

54      private boolean backtracked;
55
56      // flags that tell the status of the navigator
57

58      /**
59       * Field end
60       */

61      private boolean end = false;
62
63      /**
64       * Field start
65       */

66      private boolean start = true;
67
68      /**
69       * Constructor OMNavigator
70       */

71      public OMNavigator() {
72      }
73
74      /**
75       * Constructor OMNavigator
76       *
77       * @param node
78       */

79      public OMNavigator(OMNode node) {
80          init(node);
81      }
82
83      /**
84       * Method init
85       *
86       * @param node
87       */

88      public void init(OMNode node) {
89          next = node;
90          root = node;
91          backtracked = false;
92      }
93
94      /**
95       * get the next node
96       *
97       * @return OMnode in the sequence of preorder traversal. Note however that an element node is
98       * treated slightly diffrently. Once the element is passed it returns the same element in the
99       * next encounter as well
100       */

101      public OMNode next() {
102          if (next == null) {
103              return null;
104          }
105          node = next;
106          visited = backtracked;
107          backtracked = false;
108          updateNextNode();
109
110          // set the starting and ending flags
111
if (root.equals(node)) {
112              if (!start) {
113                  end = true;
114              } else {
115                  start = false;
116              }
117          }
118          return node;
119      }
120
121      /**
122       * Private method to encapsulate the searching logic
123       */

124      private void updateNextNode() {
125          if ((next instanceof OMElement) && !visited) {
126              OMElementImpl e = (OMElementImpl) next;
127              if (e.firstChild != null) {
128                  next = e.firstChild;
129              } else if (e.isComplete()) {
130                  backtracked = true;
131              } else {
132                  next = null;
133              }
134          } else {
135              OMNode nextSibling = ((OMNodeImpl) next).nextSibling;
136              //OMNode parent = next.getParent();
137
OMContainer parent = next.getParent();
138              if (nextSibling != null) {
139                  next = nextSibling;
140              } else if ((parent != null) && parent.isComplete()) {
141                  next = (OMNodeImpl)parent;
142                  backtracked = true;
143              } else {
144                  next = null;
145              }
146          }
147      }
148
149      /**
150       * Method visited
151       *
152       * @return
153       */

154      public boolean visited() {
155          return visited;
156      }
157
158      /**
159       * This is a very special method. This allows the navigator to step
160       * once it has reached the existing om. At this point the isNavigable
161       * method will return false but the isComplete method may return false
162       * which means that the navigating the given element is not complete but
163       * the navigator cannot proceed
164       */

165      public void step() {
166          if (!end) {
167              next = node;
168              updateNextNode();
169          }
170      }
171
172      /**
173       * the navigable status
174       *
175       * @return
176       */

177      public boolean isNavigable() {
178          if (end) {
179              return false;
180          } else {
181              return !(next == null);
182          }
183      }
184
185      /**
186       * The completed status
187       *
188       * @return
189       */

190      public boolean isCompleted() {
191          return end;
192      }
193  }
194
Popular Tags