KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > DomUtil


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

17 package org.apache.tomcat.util;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.transform.OutputKeys JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.TransformerFactory JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.stream.StreamResult JavaDoc;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.NamedNodeMap JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.xml.sax.EntityResolver JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41
42 /**
43  * Few simple utils to read DOM
44  *
45  * @author Costin Manolache
46  */

47 public class DomUtil {
48     private static org.apache.commons.logging.Log log=
49         org.apache.commons.logging.LogFactory.getLog( DomUtil.class );
50
51     // -------------------- DOM utils --------------------
52

53     /** Get the trimed text content of a node or null if there is no text
54      */

55     public static String JavaDoc getContent(Node JavaDoc n ) {
56         if( n==null ) return null;
57         Node JavaDoc n1=DomUtil.getChild(n, Node.TEXT_NODE);
58
59         if( n1==null ) return null;
60
61         String JavaDoc s1=n1.getNodeValue();
62         return s1.trim();
63     }
64
65     /** Get the first element child.
66      * @param parent lookup direct childs
67      * @param name name of the element. If null return the first element.
68      */

69     public static Node JavaDoc getChild( Node JavaDoc parent, String JavaDoc name ) {
70         if( parent==null ) return null;
71         Node JavaDoc first=parent.getFirstChild();
72         if( first==null ) return null;
73
74         for (Node JavaDoc node = first; node != null;
75              node = node.getNextSibling()) {
76             //System.out.println("getNode: " + name + " " + node.getNodeName());
77
if( node.getNodeType()!=Node.ELEMENT_NODE)
78                 continue;
79             if( name != null &&
80                 name.equals( node.getNodeName() ) ) {
81                 return node;
82             }
83             if( name == null ) {
84                 return node;
85             }
86         }
87         return null;
88     }
89
90     public static String JavaDoc getAttribute(Node JavaDoc element, String JavaDoc attName ) {
91         NamedNodeMap JavaDoc attrs=element.getAttributes();
92         if( attrs==null ) return null;
93         Node JavaDoc attN=attrs.getNamedItem(attName);
94         if( attN==null ) return null;
95         return attN.getNodeValue();
96     }
97
98     public static void setAttribute(Node JavaDoc node, String JavaDoc attName, String JavaDoc val) {
99         NamedNodeMap JavaDoc attributes=node.getAttributes();
100         Node JavaDoc attNode=node.getOwnerDocument().createAttribute(attName);
101         attNode.setNodeValue( val );
102         attributes.setNamedItem(attNode);
103     }
104     
105     public static void removeAttribute( Node JavaDoc node, String JavaDoc attName ) {
106         NamedNodeMap JavaDoc attributes=node.getAttributes();
107         attributes.removeNamedItem(attName);
108     }
109     
110     
111     /** Set or replace the text value
112      */

113     public static void setText(Node JavaDoc node, String JavaDoc val) {
114         Node JavaDoc chld=DomUtil.getChild(node, Node.TEXT_NODE);
115         if( chld == null ) {
116             Node JavaDoc textN=node.getOwnerDocument().createTextNode(val);
117             node.appendChild(textN);
118             return;
119         }
120         // change the value
121
chld.setNodeValue(val);
122     }
123
124     /** Find the first direct child with a given attribute.
125      * @param parent
126      * @param elemName name of the element, or null for any
127      * @param attName attribute we're looking for
128      * @param attVal attribute value or null if we just want any
129      */

130     public static Node JavaDoc findChildWithAtt(Node JavaDoc parent, String JavaDoc elemName,
131                                         String JavaDoc attName, String JavaDoc attVal) {
132         
133         Node JavaDoc child=DomUtil.getChild(parent, Node.ELEMENT_NODE);
134         if( attVal== null ) {
135             while( child!= null &&
136                     ( elemName==null || elemName.equals( child.getNodeName())) &&
137                     DomUtil.getAttribute(child, attName) != null ) {
138                 child=getNext(child, elemName, Node.ELEMENT_NODE );
139             }
140         } else {
141             while( child!= null &&
142                     ( elemName==null || elemName.equals( child.getNodeName())) &&
143                     ! attVal.equals( DomUtil.getAttribute(child, attName)) ) {
144                 child=getNext(child, elemName, Node.ELEMENT_NODE );
145             }
146         }
147         return child;
148     }
149     
150
151     /** Get the first child's content ( ie it's included TEXT node ).
152      */

153     public static String JavaDoc getChildContent( Node JavaDoc parent, String JavaDoc name ) {
154         Node JavaDoc first=parent.getFirstChild();
155         if( first==null ) return null;
156         for (Node JavaDoc node = first; node != null;
157              node = node.getNextSibling()) {
158             //System.out.println("getNode: " + name + " " + node.getNodeName());
159
if( name.equals( node.getNodeName() ) ) {
160                 return getContent( node );
161             }
162         }
163         return null;
164     }
165
166     /** Get the first direct child with a given type
167      */

168     public static Node JavaDoc getChild( Node JavaDoc parent, int type ) {
169         Node JavaDoc n=parent.getFirstChild();
170         while( n!=null && type != n.getNodeType() ) {
171             n=n.getNextSibling();
172         }
173         if( n==null ) return null;
174         return n;
175     }
176
177     /** Get the next sibling with the same name and type
178      */

179     public static Node JavaDoc getNext( Node JavaDoc current ) {
180         String JavaDoc name=current.getNodeName();
181         int type=current.getNodeType();
182         return getNext( current, name, type);
183     }
184
185     /** Return the next sibling with a given name and type
186      */

187     public static Node JavaDoc getNext( Node JavaDoc current, String JavaDoc name, int type) {
188         Node JavaDoc first=current.getNextSibling();
189         if( first==null ) return null;
190
191         for (Node JavaDoc node = first; node != null;
192              node = node.getNextSibling()) {
193             
194             if( type >= 0 && node.getNodeType() != type ) continue;
195             //System.out.println("getNode: " + name + " " + node.getNodeName());
196
if( name==null )
197                 return node;
198             if( name.equals( node.getNodeName() ) ) {
199                 return node;
200             }
201         }
202         return null;
203     }
204
205     public static class NullResolver implements EntityResolver JavaDoc {
206         public InputSource JavaDoc resolveEntity (String JavaDoc publicId,
207                                                    String JavaDoc systemId)
208             throws SAXException JavaDoc, IOException JavaDoc
209         {
210             if( log.isTraceEnabled())
211                 log.trace("ResolveEntity: " + publicId + " " + systemId);
212             return new InputSource JavaDoc(new StringReader JavaDoc(""));
213         }
214     }
215
216     public static void setAttributes( Object JavaDoc o, Node JavaDoc parent)
217     {
218         NamedNodeMap JavaDoc attrs=parent.getAttributes();
219         if( attrs==null ) return;
220
221         for (int i=0; i<attrs.getLength(); i++ ) {
222             Node JavaDoc n=attrs.item(i);
223             String JavaDoc name=n.getNodeName();
224             String JavaDoc value=n.getNodeValue();
225
226             if( log.isTraceEnabled() )
227                 log.trace("Attribute " + parent.getNodeName() + " " +
228                             name + "=" + value);
229             try {
230                 IntrospectionUtils.setProperty(o, name, value);
231             } catch( Exception JavaDoc ex ) {
232                 ex.printStackTrace();
233             }
234         }
235     }
236
237     /** Read XML as DOM.
238      */

239     public static Document JavaDoc readXml(InputStream JavaDoc is)
240         throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc
241     {
242         DocumentBuilderFactory JavaDoc dbf =
243             DocumentBuilderFactory.newInstance();
244
245         dbf.setValidating(false);
246         dbf.setIgnoringComments(false);
247         dbf.setIgnoringElementContentWhitespace(true);
248         //dbf.setCoalescing(true);
249
//dbf.setExpandEntityReferences(true);
250

251         DocumentBuilder JavaDoc db = null;
252         db = dbf.newDocumentBuilder();
253         db.setEntityResolver( new NullResolver() );
254
255         // db.setErrorHandler( new MyErrorHandler());
256

257         Document JavaDoc doc = db.parse(is);
258         return doc;
259     }
260
261     public static void writeXml( Node JavaDoc n, OutputStream JavaDoc os )
262             throws TransformerException JavaDoc
263     {
264         TransformerFactory JavaDoc tf=TransformerFactory.newInstance();
265         //identity
266
Transformer JavaDoc t=tf.newTransformer();
267         t.setOutputProperty(OutputKeys.INDENT, "yes");
268         t.transform(new DOMSource JavaDoc( n ), new StreamResult JavaDoc( os ));
269     }
270 }
271
Popular Tags