1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48
49
50
51
52
53
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 }