KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > anakia > TreeWalker


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

18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.jdom.Element;
24
25 /**
26  * This class allows you to walk a tree of JDOM Element objects.
27  * It first walks the tree itself starting at the Element passed
28  * into allElements() and stores each node of the tree
29  * in a Vector which allElements() returns as a result of its
30  * execution. You can then use a #foreach in Velocity to walk
31  * over the Vector and visit each Element node. However, you can
32  * achieve the same effect by calling <code>element.selectNodes("//*")</code>.
33  *
34  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
35  * @author <a HREF="mailto:szegedia@freemail.hu">Attila Szegedi</a>
36  * @version $Id: TreeWalker.java,v 1.6.4.1 2004/03/03 23:22:04 geirm Exp $
37  */

38 public class TreeWalker
39 {
40     /**
41      * Empty constructor
42      */

43     public TreeWalker()
44     {
45         // Left blank
46
}
47     
48     /**
49      * Creates a new Vector and walks the Element tree.
50      *
51      * @param Element the starting Element node
52      * @return Vector a vector of Element nodes
53      */

54     public NodeList allElements(Element e)
55     {
56         ArrayList JavaDoc theElements = new ArrayList JavaDoc();
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 JavaDoc theElements )
66     {
67         for (Iterator JavaDoc i=e.getChildren().iterator(); i.hasNext(); )
68         {
69             Element child = (Element)i.next();
70             theElements.add(child);
71             treeWalk(child, theElements);
72         }
73     }
74 }
75
Popular Tags