180 return root;
181 }
182
183 /**
184 * Fetches the current depth of element tree.
185 *
186 * @return the depth.
187 */
188 public int depth() {
189 if (elementStack == null) {
190 return 0;
191 }
192 return elementStack.size();
193 }
194
195
196 /**
197 * Fetches the current Element.
198 *
199 * @return element on top of the stack or
200 * <code>null</code> if the root element is <code>null</code>
201 */
202 public Element current() {
203
204 if (elementStack == null) {
205 return first();
206 }
207
208 /*
209 get a handle to the element on top of the stack.
210 */
211 if (! elementStack.empty()) {
212 StackItem item = elementStack.peek();
213 Element elem = item.getElement();
214 int index = item.getIndex();
215 // self reference
216 if (index == -1) {
217 return elem;
218 }
219 // return the child at location "index".
220 return elem.getElement(index);
221 }
222 return null;
223 }
224
225
226 /**
227 * Fetches the next Element. The strategy
228 * used to locate the next element is
229 * a depth-first search.
230 *
231 * @return the next element or <code>null</code>
232 * at the end of the list.
233 */
234 public Element next() {
235
236 /* if current() has not been invoked
237 and next is invoked, the very first
238 element will be returned. */
239 if (elementStack == null) {
240 return first();
241 }
242
243 // no more elements
244 if (elementStack.isEmpty()) {
245 return null;
246 }
247
248 // get a handle to the element on top of the stack
249
250 StackItem item = elementStack.peek();
251 Element elem = item.getElement();
271 elementStack.pop();
272 if (!elementStack.isEmpty()) {
273 /* Increment the child index for the item that
274 is now on top of the stack. */
275 StackItem top = elementStack.peek();
276 top.incrementIndex();
277 /* We now want to return its next child, therefore
278 call next() recursively. */
279 return next();
280 }
281 }
282 return null;
283 }
284
285
286 /**
287 * Fetches the previous Element. If however the current
288 * element is the last element, or the current element
289 * is null, then null is returned.
290 *
291 * @return previous <code>Element</code> if available
292 *
293 */
294 public Element previous() {
295
296 int stackSize;
297 if (elementStack == null || (stackSize = elementStack.size()) == 0) {
298 return null;
299 }
300
301 // get a handle to the element on top of the stack
302 //
303 StackItem item = elementStack.peek();
304 Element elem = item.getElement();
305 int index = item.getIndex();
306
307 if (index > 0) {
308 /* return child at previous index. */
309 return getDeepestLeaf(elem.getElement(--index));
310 } else if (index == 0) {
311 /* this implies that current is the element's
318 return null;
319 }
320 /* We need to return either the item
321 below the top item or one of the
322 former's children. */
323 StackItem top = elementStack.pop();
324 item = elementStack.peek();
325
326 // restore the top item.
327 elementStack.push(top);
328 elem = item.getElement();
329 index = item.getIndex();
330 return ((index == -1) ? elem : getDeepestLeaf(elem.getElement
331 (index)));
332 }
333 // should never get here.
334 return null;
335 }
336
337 /**
338 * Returns the last child of <code>parent</code> that is a leaf. If the
339 * last child is a not a leaf, this method is called with the last child.
340 */
341 private Element getDeepestLeaf(Element parent) {
342 if (parent.isLeaf()) {
343 return parent;
344 }
345 int childCount = parent.getElementCount();
346 if (childCount == 0) {
347 return parent;
348 }
349 return getDeepestLeaf(parent.getElement(childCount - 1));
350 }
351
352 /*
353 Iterates through the element tree and prints
354 out each element and its attributes.
355 */
356 private void dumpTree() {
357
358 Element elem;
|
180 return root;
181 }
182
183 /**
184 * Fetches the current depth of element tree.
185 *
186 * @return the depth.
187 */
188 public int depth() {
189 if (elementStack == null) {
190 return 0;
191 }
192 return elementStack.size();
193 }
194
195
196 /**
197 * Fetches the current Element.
198 *
199 * @return element on top of the stack or
200 * {@code null} if the root element is {@code null}
201 */
202 public Element current() {
203
204 if (elementStack == null) {
205 return first();
206 }
207
208 /*
209 get a handle to the element on top of the stack.
210 */
211 if (! elementStack.empty()) {
212 StackItem item = elementStack.peek();
213 Element elem = item.getElement();
214 int index = item.getIndex();
215 // self reference
216 if (index == -1) {
217 return elem;
218 }
219 // return the child at location "index".
220 return elem.getElement(index);
221 }
222 return null;
223 }
224
225
226 /**
227 * Fetches the next Element. The strategy
228 * used to locate the next element is
229 * a depth-first search.
230 *
231 * @return the next element or {@code null}
232 * at the end of the list.
233 */
234 public Element next() {
235
236 /* if current() has not been invoked
237 and next is invoked, the very first
238 element will be returned. */
239 if (elementStack == null) {
240 return first();
241 }
242
243 // no more elements
244 if (elementStack.isEmpty()) {
245 return null;
246 }
247
248 // get a handle to the element on top of the stack
249
250 StackItem item = elementStack.peek();
251 Element elem = item.getElement();
271 elementStack.pop();
272 if (!elementStack.isEmpty()) {
273 /* Increment the child index for the item that
274 is now on top of the stack. */
275 StackItem top = elementStack.peek();
276 top.incrementIndex();
277 /* We now want to return its next child, therefore
278 call next() recursively. */
279 return next();
280 }
281 }
282 return null;
283 }
284
285
286 /**
287 * Fetches the previous Element. If however the current
288 * element is the last element, or the current element
289 * is null, then null is returned.
290 *
291 * @return previous {@code Element} if available
292 *
293 */
294 public Element previous() {
295
296 int stackSize;
297 if (elementStack == null || (stackSize = elementStack.size()) == 0) {
298 return null;
299 }
300
301 // get a handle to the element on top of the stack
302 //
303 StackItem item = elementStack.peek();
304 Element elem = item.getElement();
305 int index = item.getIndex();
306
307 if (index > 0) {
308 /* return child at previous index. */
309 return getDeepestLeaf(elem.getElement(--index));
310 } else if (index == 0) {
311 /* this implies that current is the element's
318 return null;
319 }
320 /* We need to return either the item
321 below the top item or one of the
322 former's children. */
323 StackItem top = elementStack.pop();
324 item = elementStack.peek();
325
326 // restore the top item.
327 elementStack.push(top);
328 elem = item.getElement();
329 index = item.getIndex();
330 return ((index == -1) ? elem : getDeepestLeaf(elem.getElement
331 (index)));
332 }
333 // should never get here.
334 return null;
335 }
336
337 /**
338 * Returns the last child of {@code parent} that is a leaf. If the
339 * last child is a not a leaf, this method is called with the last child.
340 */
341 private Element getDeepestLeaf(Element parent) {
342 if (parent.isLeaf()) {
343 return parent;
344 }
345 int childCount = parent.getElementCount();
346 if (childCount == 0) {
347 return parent;
348 }
349 return getDeepestLeaf(parent.getElement(childCount - 1));
350 }
351
352 /*
353 Iterates through the element tree and prints
354 out each element and its attributes.
355 */
356 private void dumpTree() {
357
358 Element elem;
|