001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.util;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.StringReader;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Node;
029: import org.xml.sax.EntityResolver;
030: import org.xml.sax.InputSource;
031: import org.xml.sax.SAXException;
032:
033: // moved from jk2 config package.
034:
035: /**
036: *
037: * @author Costin Manolache
038: */
039: public class DomXml {
040: String file;
041: String name;
042:
043: // -------------------- Settings --------------------
044:
045: /**
046: */
047: public void setFile(String file) {
048: this .file = file;
049: }
050:
051: /**
052: */
053: public void setName(String name) {
054: this .name = name;
055: }
056:
057: // -------------------- Implementation --------------------
058: Node domN;
059:
060: /** Return the top level node
061: */
062: public Node getNode() {
063: return domN;
064: }
065:
066: // -------------------- ant wrapper --------------------
067:
068: public void execute() {
069: try {
070: if (file == null) {
071: log.error("No file attribute");
072: return;
073: }
074:
075: File docF = new File(file);
076:
077: Document doc = readXml(docF);
078: if (doc == null)
079: return;
080:
081: domN = doc.getDocumentElement();
082: if (domN == null) {
083: log.error("Can't find the root node");
084: return;
085: }
086:
087: } catch (Exception ex) {
088: ex.printStackTrace();
089: }
090: }
091:
092: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
093: .getLog(DomXml.class);
094:
095: // -------------------- DOM utils --------------------
096:
097: /** Get the content of a node
098: */
099: public static String getContent(Node n) {
100: if (n == null)
101: return null;
102: Node n1 = n.getFirstChild();
103: // XXX Check if it's a text node
104:
105: String s1 = n1.getNodeValue();
106: return s1.trim();
107: }
108:
109: /** Get the first child
110: */
111: public static Node getChild(Node parent, String name) {
112: if (parent == null)
113: return null;
114: Node first = parent.getFirstChild();
115: if (first == null)
116: return null;
117: for (Node node = first; node != null; node = node
118: .getNextSibling()) {
119: //System.out.println("getNode: " + name + " " + node.getNodeName());
120: if (name.equals(node.getNodeName())) {
121: return node;
122: }
123: }
124: return null;
125: }
126:
127: /** Get the first child's content ( i.e. it's included TEXT node )
128: */
129: public static String getChildContent(Node parent, String name) {
130: Node first = parent.getFirstChild();
131: if (first == null)
132: return null;
133: for (Node node = first; node != null; node = node
134: .getNextSibling()) {
135: //System.out.println("getNode: " + name + " " + node.getNodeName());
136: if (name.equals(node.getNodeName())) {
137: return getContent(node);
138: }
139: }
140: return null;
141: }
142:
143: /** Get the node in the list of siblings
144: */
145: public static Node getNext(Node current) {
146: Node first = current.getNextSibling();
147: String name = current.getNodeName();
148: if (first == null)
149: return null;
150: for (Node node = first; node != null; node = node
151: .getNextSibling()) {
152: //System.out.println("getNode: " + name + " " + node.getNodeName());
153: if (name.equals(node.getNodeName())) {
154: return node;
155: }
156: }
157: return null;
158: }
159:
160: public static class NullResolver implements EntityResolver {
161: public InputSource resolveEntity(String publicId,
162: String systemId) throws SAXException, IOException {
163: if (log.isTraceEnabled())
164: log
165: .trace("ResolveEntity: " + publicId + " "
166: + systemId);
167: return new InputSource(new StringReader(""));
168: }
169: }
170:
171: public void saveXml(Node n, File xmlF) {
172:
173: }
174:
175: public static Document readXml(File xmlF) throws SAXException,
176: IOException, ParserConfigurationException {
177: if (!xmlF.exists()) {
178: log.error("No xml file " + xmlF);
179: return null;
180: }
181: DocumentBuilderFactory dbf = DocumentBuilderFactory
182: .newInstance();
183:
184: dbf.setValidating(false);
185: dbf.setIgnoringComments(false);
186: dbf.setIgnoringElementContentWhitespace(true);
187: //dbf.setCoalescing(true);
188: //dbf.setExpandEntityReferences(true);
189:
190: DocumentBuilder db = null;
191: db = dbf.newDocumentBuilder();
192: db.setEntityResolver(new NullResolver());
193:
194: // db.setErrorHandler( new MyErrorHandler());
195:
196: Document doc = db.parse(xmlF);
197: return doc;
198: }
199:
200: }
|