1   package com.mccrory.scott.base;
2   
3   ////////////////////////////////////////////////////////////////////////////////
4   // Copyright (C) 2002  Scott McCrory
5   //
6   // This program is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public License
8   // as published by the Free Software Foundation; either version 2
9   // of the License, or (at your option) any later version.
10  //
11  // This program is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  // GNU General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public License
17  // along with this program; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  ////////////////////////////////////////////////////////////////////////////////
20  
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Vector;
24  
25  import org.dom4j.Attribute;
26  import org.dom4j.Element;
27  
28  /**
29   * <P>Contains methods to make working with dom4j objects a little easier.</P>
30   *
31   * @author <a href="mailto:smccrory@users.sourceforge.net">Scott McCrory</a>.
32   * @version CVS $Id: Dom4jHelper.java,v 1.19 2002/08/04 22:04:53 smccrory Exp $
33   */
34  public class Dom4jHelper {
35  
36      /** The name of this class to log context without introspection **/
37      private static final String CLASS_NAME = "Dom4jHelper";
38  
39      /** The version of this class (filled in by CVS) **/
40      private static final String VERSION = "CVS $Revision: 1.19 $";
41  
42      /**
43       * Dom4jHelper constructor.
44       */
45      public Dom4jHelper() {
46          super();
47      }
48      
49      /**
50       * Returns a HashMap of all the attributes in the passed-in XML/Dom4J Element.
51       * Example:
52       * <pre>
53       * HashMap attribMap = Dom4jHelper.getAttributes(myElement);
54       * </pre>
55       *
56       * @return java.util.HashMap of all the attributes of the element.
57       * @param element org.dom4j.Element that you want the attributes of.
58       */
59      public static HashMap getAttributes(Element element) {
60  
61          HashMap attributes = new HashMap();
62  
63          // Iterate through all the stats program's attributes and put them
64          // into a HashMap keyed by their names.
65          for (Iterator statsProgAttr = element.attributeIterator();
66              statsProgAttr.hasNext();
67              ) {
68  
69              Attribute attribute = (Attribute) statsProgAttr.next();
70              attributes.put(attribute.getName(), attribute.getData());
71  
72          }
73          return attributes;
74      }
75      
76      /**
77       * Returns the names of all the child elements of a passed-in Element.
78       * Example:
79       * <pre>
80       * Vector childElements = Dom4jHelper.getChildElementNames(myElement);
81       * </pre>
82       *
83       * @return java.util.Vector of the names of all the child elements.
84       * @param element org.dom4j.Element that you want the child element names of.
85       */
86      public static Vector getChildElementNames(Element element) {
87  
88          Vector valueNames = new Vector();
89  
90          // Iterate through child elements to get their names
91          for (Iterator valueNamesIterator = element.elementIterator();
92              valueNamesIterator.hasNext();
93              ) {
94  
95              Element valueElement = (Element) valueNamesIterator.next();
96              valueNames.add(valueElement.getData());
97  
98          }
99  
100         return valueNames;
101     }
102     
103 }