View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.util;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URL;
26  import java.util.List;
27  import java.util.Vector;
28  
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  
33  import net.sf.jour.log.Logger;
34  
35  import org.w3c.dom.Document;
36  import org.w3c.dom.NamedNodeMap;
37  import org.w3c.dom.Node;
38  import org.w3c.dom.NodeList;
39  import org.xml.sax.SAXException;
40  
41  /**
42   * TODO Add docs
43   * Need this Class so runtime would not depend on JAXB.
44   * 
45   * Created on 04.12.2004
46   *
47   * Contributing Author(s):
48   *
49   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
50   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
51   *
52   * @author vlads
53   * @version $Revision: 51 $ ($Author: vlads $) $Date: 2007-10-11 10:43:56 -0400 (Thu, 11 Oct 2007) $
54   */
55  
56  public class ConfigFileUtil extends FileUtil {
57  
58  	protected static final Logger log = Logger.getLogger();
59  	
60  	public static Document loadDocument(InputStream stream) throws ParserConfigurationException, SAXException, IOException {
61  		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62          factory.setIgnoringComments(true);
63          factory.setValidating(false);
64          DocumentBuilder builder = factory.newDocumentBuilder();
65          builder.setErrorHandler(null);
66          return builder.parse(stream);
67  	}
68  	
69      public static Document loadDocument(URL location) throws ParserConfigurationException, SAXException, IOException {
70      	InputStream configStream = null;
71      	try {
72      		try {
73      			configStream = location.openStream();
74              } catch (IOException e) {
75              	throw new Error("resource not found " + location);
76      		} 
77      		return loadDocument(configStream);
78  		} finally {
79  			FileUtil.closeQuietly(configStream);
80          }
81      }
82      
83      public static InputStream loadFile(String fileName) {
84          URL location = FileUtil.getFile(fileName);
85          if (location != null) {
86              try {
87                  FileUtil.log.info("Using config file " + location);
88                  return location.openStream(); 
89              } catch (Exception e) {
90                  FileUtil.log.error("Error reading " + fileName, e);
91                  return null;
92              }
93          } else {
94              FileUtil.log.error("Config file not found: " + fileName);
95              return null;
96          }
97      }
98      
99      public static Node getFirstElement(Document doc, String tagname) {
100         NodeList list = doc.getElementsByTagName(tagname);
101         if (list.getLength() == 0) {
102             return null;
103         }
104         return list.item(0);
105     }
106     
107     public static Node getChildNode(Node node, String tagName) {
108         if (node == null) {
109             return null;
110         }
111         NodeList children =  node.getChildNodes();  
112         for (int j = 0, cnt = children.getLength(); j < cnt; j++) {
113             Node child = children.item(j);
114             if (child != null) {
115                 String nodeName = child.getNodeName();
116                 if (nodeName != null && nodeName.equals(tagName)) {
117                     return child;
118                 }
119             }
120         }
121         return null;
122     }
123     
124     public static Node[] getChildNodes(Node node, String tagName) {
125         if (node == null) {
126             return null;
127         }
128         List ret = new Vector();
129         NodeList children =  node.getChildNodes();  
130         for (int j = 0, cnt = children.getLength(); j < cnt; j++) {
131             Node child = children.item(j);
132             if (child != null) {
133                 String nodeName = child.getNodeName();
134                 if (nodeName != null && nodeName.equals(tagName)) {
135                     ret.add(child);
136                 }
137             }
138         }
139         return (Node[])ret.toArray(new Node[ret.size()]);
140     }
141     
142     public static String getNodeValue(Node node, String tagName) {
143         if (node == null) {
144             return null;
145         }
146         NodeList children =  node.getChildNodes();  
147         for (int j = 0, cnt = children.getLength(); j < cnt; j++) {
148             Node child = children.item(j);
149             if (child != null) {
150                 String nodeName = child.getNodeName();
151                 if (nodeName != null && nodeName.equals(tagName)) {
152                     Node firstChild = child.getFirstChild();
153                     if (firstChild != null) {
154                         String nodeValue = firstChild.getNodeValue();
155                         if (nodeValue != null) {
156                             return nodeValue;
157                         }
158                     }
159                 }
160             }
161         }
162         return null;
163     }
164     
165     public static String getNodeAttribute(Node node, String tagName) {
166     	NamedNodeMap nodeAttrs = node.getAttributes();
167     	if (nodeAttrs == null) {
168     		return null;
169     	}
170     	Node value = nodeAttrs.getNamedItem(tagName);
171     	if (value != null) {
172     		return value.getNodeValue();
173     	} else {
174     		return null;
175     	}
176     }
177     
178     public static boolean getNodeAttribute(Node node, String tagName, boolean defaultValue) {
179     	String value = getNodeAttribute(node, tagName);
180     	if (value == null) {
181     		return defaultValue;
182     	}
183     	return Boolean.valueOf(value).booleanValue();
184     }
185 }