01: package org.apache.velocity.anakia;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import java.util.ArrayList;
23: import java.util.Collection;
24: import java.util.Iterator;
25:
26: import org.jdom.Element;
27:
28: /**
29: * This class allows you to walk a tree of JDOM Element objects.
30: * It first walks the tree itself starting at the Element passed
31: * into allElements() and stores each node of the tree
32: * in a Vector which allElements() returns as a result of its
33: * execution. You can then use a #foreach in Velocity to walk
34: * over the Vector and visit each Element node. However, you can
35: * achieve the same effect by calling <code>element.selectNodes("//*")</code>.
36: *
37: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
38: * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
39: * @version $Id: TreeWalker.java 463298 2006-10-12 16:10:32Z henning $
40: */
41: public class TreeWalker {
42: /**
43: * Empty constructor
44: */
45: public TreeWalker() {
46: // Left blank
47: }
48:
49: /**
50: * Creates a new Vector and walks the Element tree.
51: *
52: * @param e the starting Element node
53: * @return Vector a vector of Element nodes
54: */
55: public NodeList allElements(Element e) {
56: ArrayList theElements = new ArrayList();
57: treeWalk(e, theElements);
58: return new NodeList(theElements, false);
59: }
60:
61: /**
62: * A recursive method to walk the Element tree.
63: * @param Element the current Element
64: */
65: private final void treeWalk(Element e, Collection theElements) {
66: for (Iterator i = e.getChildren().iterator(); i.hasNext();) {
67: Element child = (Element) i.next();
68: theElements.add(child);
69: treeWalk(child, theElements);
70: }
71: }
72: }
|