KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > util > DomXml


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

16
17 package org.apache.naming.util;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.xml.sax.EntityResolver JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 // moved from jk2 config package.
34

35 /**
36  *
37  * @author Costin Manolache
38  */

39 public class DomXml {
40     String JavaDoc file;
41     String JavaDoc name;
42
43     // -------------------- Settings --------------------
44

45     /**
46      */

47     public void setFile( String JavaDoc file ) {
48         this.file=file;
49     }
50
51     /**
52      */

53     public void setName( String JavaDoc name ) {
54         this.name=name;
55     }
56
57     // -------------------- Implementation --------------------
58
Node JavaDoc domN;
59     
60     /** Return the top level node
61      */

62     public Node JavaDoc getNode() {
63         return domN;
64     }
65
66     // -------------------- ant wrapper --------------------
67

68     public void execute() {
69         try {
70             if( file== null) {
71                 log.error("No file attribute");
72                 return;
73             }
74
75             File JavaDoc docF=new File JavaDoc(file);
76
77             Document JavaDoc doc=readXml(docF);
78             if( doc == null ) return;
79
80             domN = doc.getDocumentElement();
81             if( domN==null ) {
82                 log.error("Can't find the root node");
83                 return;
84             }
85
86         } catch( Exception JavaDoc ex ) {
87             ex.printStackTrace();
88         }
89     }
90
91     private static org.apache.commons.logging.Log log=
92         org.apache.commons.logging.LogFactory.getLog( DomXml.class );
93
94     
95     // -------------------- DOM utils --------------------
96

97     /** Get the content of a node
98      */

99     public static String JavaDoc getContent(Node JavaDoc n ) {
100         if( n==null ) return null;
101         Node JavaDoc n1=n.getFirstChild();
102         // XXX Check if it's a text node
103

104         String JavaDoc s1=n1.getNodeValue();
105         return s1.trim();
106     }
107     
108     /** Get the first child
109      */

110     public static Node JavaDoc getChild( Node JavaDoc parent, String JavaDoc name ) {
111         if( parent==null ) return null;
112         Node JavaDoc first=parent.getFirstChild();
113         if( first==null ) return null;
114         for (Node JavaDoc node = first; node != null;
115              node = node.getNextSibling()) {
116             //System.out.println("getNode: " + name + " " + node.getNodeName());
117
if( name.equals( node.getNodeName() ) ) {
118                 return node;
119             }
120         }
121         return null;
122     }
123
124     /** Get the first child's content ( i.e. it's included TEXT node )
125      */

126     public static String JavaDoc getChildContent( Node JavaDoc parent, String JavaDoc name ) {
127         Node JavaDoc first=parent.getFirstChild();
128         if( first==null ) return null;
129         for (Node JavaDoc node = first; node != null;
130              node = node.getNextSibling()) {
131             //System.out.println("getNode: " + name + " " + node.getNodeName());
132
if( name.equals( node.getNodeName() ) ) {
133                 return getContent( node );
134             }
135         }
136         return null;
137     }
138
139     /** Get the node in the list of siblings
140      */

141     public static Node JavaDoc getNext( Node JavaDoc current ) {
142         Node JavaDoc first=current.getNextSibling();
143         String JavaDoc name=current.getNodeName();
144         if( first==null ) return null;
145         for (Node JavaDoc node = first; node != null;
146              node = node.getNextSibling()) {
147             //System.out.println("getNode: " + name + " " + node.getNodeName());
148
if( name.equals( node.getNodeName() ) ) {
149                 return node;
150             }
151         }
152         return null;
153     }
154
155     public static class NullResolver implements EntityResolver JavaDoc {
156         public InputSource JavaDoc resolveEntity (String JavaDoc publicId,
157                                                    String JavaDoc systemId)
158             throws SAXException JavaDoc, IOException JavaDoc
159         {
160             if( log.isTraceEnabled())
161                 log.trace("ResolveEntity: " + publicId + " " + systemId);
162             return new InputSource JavaDoc(new StringReader JavaDoc(""));
163         }
164     }
165
166     public void saveXml( Node JavaDoc n, File JavaDoc xmlF ) {
167         
168     }
169     
170     public static Document JavaDoc readXml(File JavaDoc xmlF)
171         throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc
172     {
173         if( ! xmlF.exists() ) {
174             log.error("No xml file " + xmlF );
175             return null;
176         }
177         DocumentBuilderFactory JavaDoc dbf =
178             DocumentBuilderFactory.newInstance();
179
180         dbf.setValidating(false);
181         dbf.setIgnoringComments(false);
182         dbf.setIgnoringElementContentWhitespace(true);
183         //dbf.setCoalescing(true);
184
//dbf.setExpandEntityReferences(true);
185

186         DocumentBuilder JavaDoc db = null;
187         db = dbf.newDocumentBuilder();
188         db.setEntityResolver( new NullResolver() );
189         
190         // db.setErrorHandler( new MyErrorHandler());
191

192         Document JavaDoc doc = db.parse(xmlF);
193         return doc;
194     }
195
196 }
197
Popular Tags