KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > xml > DomParseUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.xml;
25
26 import java.util.*;
27 import org.xml.sax.*;
28 import org.w3c.dom.*;
29 import com.mchange.v1.util.DebugUtils;
30
31 public final class DomParseUtils
32 {
33     final static boolean DEBUG = true;
34
35     /**
36      * @return null if child doesn't exist.
37      */

38     public static String JavaDoc allTextFromUniqueChild(Element elem, String JavaDoc childTagName)
39     throws DOMException
40     { return allTextFromUniqueChild( elem, childTagName, false ); }
41
42     /**
43      * @return null if child doesn't exist.
44      */

45            public static String JavaDoc allTextFromUniqueChild(Element elem, String JavaDoc childTagName, boolean trim)
46     throws DOMException
47     {
48     Element uniqueChild = uniqueChildByTagName( elem, childTagName );
49     if (uniqueChild == null)
50         return null;
51     else
52         return DomParseUtils.allTextFromElement( uniqueChild, trim );
53     }
54
55     public static Element uniqueChild(Element elem, String JavaDoc childTagName) throws DOMException
56     { return uniqueChildByTagName( elem, childTagName); }
57
58     /**
59      * @deprecated use uniqueChild(Element elem, String childTagName)
60      */

61     public static Element uniqueChildByTagName(Element elem, String JavaDoc childTagName) throws DOMException
62     {
63     NodeList nl = elem.getElementsByTagName(childTagName);
64     int len = nl.getLength();
65     if (DEBUG)
66         DebugUtils.myAssert( len <= 1 ,
67                  "There is more than one (" + len + ") child with tag name: " +
68                  childTagName + "!!!" );
69     return (len == 1 ? (Element) nl.item( 0 ) : null);
70     }
71
72     public static String JavaDoc allText(Element elem) throws DOMException
73     { return allTextFromElement( elem ); }
74
75     public static String JavaDoc allText(Element elem, boolean trim) throws DOMException
76     { return allTextFromElement( elem, trim ); }
77
78     /** @deprecated use allText(Element elem) */
79     public static String JavaDoc allTextFromElement(Element elem) throws DOMException
80     { return allTextFromElement( elem, false); }
81
82     /** @deprecated use allText(Element elem, boolean trim) */
83     public static String JavaDoc allTextFromElement(Element elem, boolean trim) throws DOMException
84     {
85     StringBuffer JavaDoc textBuf = new StringBuffer JavaDoc();
86     NodeList nl = elem.getChildNodes();
87     for (int j = 0, len = nl.getLength(); j < len; ++j)
88         {
89         Node node = nl.item(j);
90         if (node instanceof Text) //includes Text and CDATA!
91
textBuf.append(node.getNodeValue());
92         }
93     String JavaDoc out = textBuf.toString();
94     return ( trim ? out.trim() : out );
95     }
96
97     public static String JavaDoc[] allTextFromImmediateChildElements( Element parent, String JavaDoc tagName )
98     throws DOMException
99     { return allTextFromImmediateChildElements( parent, tagName, false ); }
100
101     public static String JavaDoc[] allTextFromImmediateChildElements( Element parent, String JavaDoc tagName, boolean trim )
102     throws DOMException
103     {
104     NodeList nl = immediateChildElementsByTagName( parent, tagName );
105     int len = nl.getLength();
106     String JavaDoc[] out = new String JavaDoc[ len ];
107     for (int i = 0; i < len; ++i)
108         out[i] = allText( (Element) nl.item(i), trim );
109     return out;
110     }
111
112
113     public static NodeList immediateChildElementsByTagName( Element parent, String JavaDoc tagName )
114     throws DOMException
115     { return getImmediateChildElementsByTagName( parent, tagName ); }
116
117     /**
118      * @deprecated use immediateChildrenByTagName( Element parent, String tagName )
119      */

120     public static NodeList getImmediateChildElementsByTagName( Element parent, String JavaDoc tagName )
121     throws DOMException
122     {
123     final List nodes = new ArrayList();
124     for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
125         if (child instanceof Element && ((Element) child).getTagName().equals(tagName))
126         nodes.add(child);
127     return new NodeList()
128         {
129         public int getLength()
130         { return nodes.size(); }
131
132         public Node item( int i )
133         { return (Node) nodes.get( i ); }
134         };
135     }
136
137     public static String JavaDoc allTextFromUniqueImmediateChild(Element elem, String JavaDoc childTagName)
138     throws DOMException
139     {
140     Element uniqueChild = uniqueImmediateChildByTagName( elem, childTagName );
141     if (uniqueChild == null)
142         return null;
143     return DomParseUtils.allTextFromElement( uniqueChild );
144     }
145
146     public static Element uniqueImmediateChild(Element elem, String JavaDoc childTagName)
147     throws DOMException
148     { return uniqueImmediateChildByTagName( elem, childTagName); }
149
150     /**
151      * @deprecated use uniqueImmediateChild(Element elem, String childTagName)
152      */

153     public static Element uniqueImmediateChildByTagName(Element elem, String JavaDoc childTagName)
154     throws DOMException
155     {
156     NodeList nl = getImmediateChildElementsByTagName(elem, childTagName);
157     int len = nl.getLength();
158     if (DEBUG)
159         DebugUtils.myAssert( len <= 1 ,
160                  "There is more than one (" + len + ") child with tag name: " +
161                  childTagName + "!!!" );
162     return (len == 1 ? (Element) nl.item( 0 ) : null);
163     }
164
165     /**
166      * @deprecated use Element.getAttribute(String val)
167      */

168     public static String JavaDoc attrValFromElement(Element element, String JavaDoc attrName)
169     throws DOMException
170     {
171     Attr attr = element.getAttributeNode( attrName );
172     return (attr == null ? null : attr.getValue());
173     }
174
175     private DomParseUtils()
176     {}
177 }
178
179
180
Popular Tags