1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: DomUtil.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.dom;
27   
28   import java.util.ArrayList;
29   import java.util.Iterator;
30   
31   import org.w3c.dom.*;
32   
33   /**
34    * @author michaelh
35    *
36    * To change this generated comment edit the template variable "typecomment":
37    * Window>Preferences>Java>Templates.
38    * To enable and disable the creation of type comments go to
39    * Window>Preferences>Java>Code Generation.
40    */
41   public class DomUtil
42   {
43       public static NodeList getChildElements(Node node)
44       {
45           return getChildNodesOfType(node, Node.ELEMENT_NODE);
46       }
47   
48       public static NodeList getChildElementsByTagName(Node node, String tagName)
49       {
50           ArrayList list = getChildNodesOfTypeAsList(node, Node.ELEMENT_NODE);
51           Iterator elements = list.iterator();
52   
53           list = new ArrayList();
54   
55           while (elements.hasNext())
56           {
57               Element element = (Element) elements.next();
58   
59               if (element.getTagName().equals(tagName))
60               {
61                   list.add(element);
62               }
63           }
64   
65           return new DomUtil().new NodeListImpl(list);
66       }
67   
68       public static Node getFirstChildElement(Node node)
69       {
70           return getFirstChildNodeOfType(node, Node.ELEMENT_NODE);
71       }
72   
73       public static Node getFirstChildNodeOfType(Node node, int type)
74       {
75           NodeList list = node.getChildNodes();
76   
77           node = null;
78   
79           int length = list.getLength();
80   
81           for (int i = 0; i < length; i++)
82           {
83               node = list.item(i);
84   
85               if (node.getNodeType() == type)
86               {
87                   break;
88               }
89           }
90   
91           return node;
92       }
93   
94       public static NodeList getChildNodesOfType(Node node, short type)
95       {
96           ArrayList list = getChildNodesOfTypeAsList(node, type);
97   
98           return new DomUtil().new NodeListImpl(list);
99       }
100  
101      private static ArrayList getChildNodesOfTypeAsList(Node node, short type)
102      {
103          ArrayList list = new ArrayList();
104          NodeList nodes = node.getChildNodes();
105  
106          if (nodes != null)
107          {
108              int length = nodes.getLength();
109  
110              for (int i = 0; i < length; i++)
111              {
112                  node = nodes.item(i);
113  
114                  if (node.getNodeType() == type)
115                  {
116                      list.add(node);
117                  }
118              }
119          }
120  
121          return list;
122      }
123  
124      public class NodeListImpl
125          implements NodeList
126      {
127          private ArrayList list;
128  
129          public NodeListImpl(ArrayList al)
130          {
131              list = al;
132          }
133  
134          public int getLength()
135          {
136              return list.size();
137          }
138  
139          public Node item(int index)
140          {
141              return (Node) list.get(index);
142          }
143      }
144  }