1 package org.apache.velocity.anakia; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "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 /** 44 * Empty constructor 45 */ 46 public TreeWalker() 47 { 48 // Left blank 49 } 50 51 /** 52 * Creates a new Vector and walks the Element tree. 53 * 54 * @param e the starting Element node 55 * @return Vector a vector of Element nodes 56 */ 57 public NodeList allElements(Element e) 58 { 59 ArrayList theElements = new ArrayList(); 60 treeWalk (e, theElements); 61 return new NodeList(theElements, false); 62 } 63 64 /** 65 * A recursive method to walk the Element tree. 66 * @param Element the current Element 67 */ 68 private final void treeWalk(Element e, Collection theElements ) 69 { 70 for (Iterator i=e.getChildren().iterator(); i.hasNext(); ) 71 { 72 Element child = (Element)i.next(); 73 theElements.add(child); 74 treeWalk(child, theElements); 75 } 76 } 77 }