1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.xml; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.w3c.dom.Document; |
12 |
| import org.w3c.dom.Element; |
13 |
| import org.w3c.dom.Node; |
14 |
| import org.w3c.dom.NodeList; |
15 |
| import org.w3c.dom.Text; |
16 |
| import org.xml.sax.InputSource; |
17 |
| import org.xml.sax.SAXException; |
18 |
| import org.xml.sax.SAXParseException; |
19 |
| |
20 |
| import javax.xml.parsers.DocumentBuilder; |
21 |
| import javax.xml.parsers.DocumentBuilderFactory; |
22 |
| import java.io.ByteArrayInputStream; |
23 |
| import java.io.IOException; |
24 |
| import java.io.InputStream; |
25 |
| import java.util.Properties; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class XmlHelper |
33 |
| { |
34 |
| private static Log log = LogFactory.getLog(XmlHelper.class); |
35 |
| public static final String ROOT = "mbean"; |
36 |
| public static final String ATTR = "attribute"; |
37 |
| public static final String CONFIG_ATTR = "config"; |
38 |
| public static final String NAME = "name"; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
3027
| public static String getTagContents(Element elem, String myName, String tagName, String attributeName)
|
50 |
| { |
51 |
3027
| NodeList list = elem.getElementsByTagName(tagName);
|
52 |
| |
53 |
3027
| for (int s = 0; s < list.getLength(); s++)
|
54 |
| { |
55 |
9420
| org.w3c.dom.Node node = list.item(s);
|
56 |
9420
| if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
|
57 |
0
| continue;
|
58 |
| |
59 |
9420
| Element element = (Element) node;
|
60 |
9420
| String name = element.getAttribute(attributeName);
|
61 |
9420
| if (name.equals(myName))
|
62 |
| { |
63 |
2357
| return getElementContent(element, true);
|
64 |
| } |
65 |
| } |
66 |
670
| return null;
|
67 |
| } |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
1801
| public static String getAttributeValue(Element elem, String tagName, String attributeName)
|
77 |
| { |
78 |
1801
| NodeList list = elem.getElementsByTagName(tagName);
|
79 |
| |
80 |
1801
| for (int s = 0; s < list.getLength(); s++)
|
81 |
| { |
82 |
17
| org.w3c.dom.Node node = list.item(s);
|
83 |
17
| if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
|
84 |
0
| continue;
|
85 |
| |
86 |
17
| Element element = (Element) node;
|
87 |
17
| return element.getAttribute(attributeName);
|
88 |
| |
89 |
| } |
90 |
1784
| return null;
|
91 |
| } |
92 |
| |
93 |
1721
| public static Element getConfigSubElement(Element element)
|
94 |
| { |
95 |
1721
| return getSubElement(element, CONFIG_ATTR);
|
96 |
| } |
97 |
| |
98 |
1721
| public static Element getSubElement(Element element, String subElementName)
|
99 |
| { |
100 |
1721
| NodeList nl = element.getChildNodes();
|
101 |
3442
| for (int i = 0; i < nl.getLength(); i++)
|
102 |
| { |
103 |
3442
| Node node = nl.item(i);
|
104 |
3442
| if (node.getNodeType() == Node.ELEMENT_NODE && subElementName.equals(((Element) node).getTagName()))
|
105 |
| { |
106 |
1721
| return (Element) node;
|
107 |
| } |
108 |
| } |
109 |
| |
110 |
0
| if (log.isDebugEnabled()) log.debug("getSubElement(): Does not exist for " + subElementName);
|
111 |
0
| return null;
|
112 |
| } |
113 |
| |
114 |
29288
| public static String getElementContent(Element element, boolean trim)
|
115 |
| { |
116 |
29288
| NodeList nl = element.getChildNodes();
|
117 |
29288
| String attributeText = "";
|
118 |
29288
| for (int i = 0; i < nl.getLength(); i++)
|
119 |
| { |
120 |
32730
| Node n = nl.item(i);
|
121 |
32730
| if (n instanceof Text)
|
122 |
| { |
123 |
31009
| attributeText += ((Text) n).getData();
|
124 |
| } |
125 |
| } |
126 |
29288
| if (trim)
|
127 |
29288
| attributeText = attributeText.trim();
|
128 |
29288
| return attributeText;
|
129 |
| } |
130 |
| |
131 |
19121
| public static String readStringContents(Element element, String tagName)
|
132 |
| { |
133 |
19121
| NodeList nodes = element.getElementsByTagName(tagName);
|
134 |
19121
| if (nodes.getLength() > 0)
|
135 |
| { |
136 |
14599
| Node node = nodes.item(0);
|
137 |
14599
| Element ne = (Element) node;
|
138 |
14599
| NodeList nl2 = ne.getChildNodes();
|
139 |
14599
| Node node2 = nl2.item(0);
|
140 |
14599
| if (node2 != null)
|
141 |
| { |
142 |
13393
| String value = node2.getNodeValue();
|
143 |
13393
| if (value == null)
|
144 |
0
| return "";
|
145 |
13393
| return value.trim();
|
146 |
| } |
147 |
| else |
148 |
| { |
149 |
1206
| return "";
|
150 |
| } |
151 |
| } |
152 |
| else |
153 |
| { |
154 |
4522
| return "";
|
155 |
| } |
156 |
| } |
157 |
| |
158 |
1969
| public static String escapeBackslashes(String value)
|
159 |
| { |
160 |
1969
| StringBuffer buf = new StringBuffer(value);
|
161 |
1969
| for (int looper = 0; looper < buf.length(); looper++)
|
162 |
| { |
163 |
71934
| char curr = buf.charAt(looper);
|
164 |
71934
| char next = 0;
|
165 |
71934
| if (looper + 1 < buf.length())
|
166 |
70971
| next = buf.charAt(looper + 1);
|
167 |
| |
168 |
71934
| if (curr == '\\')
|
169 |
| { |
170 |
0
| if (next != '\\')
|
171 |
| { |
172 |
0
| buf.insert(looper, '\\');
|
173 |
| } |
174 |
0
| looper++;
|
175 |
| } |
176 |
| } |
177 |
1969
| return buf.toString();
|
178 |
| } |
179 |
| |
180 |
1969
| public static Properties readPropertiesContents(Element element, String tagName) throws IOException
|
181 |
| { |
182 |
1969
| String stringContents = readStringContents(element, tagName);
|
183 |
0
| if (stringContents == null) return new Properties();
|
184 |
| |
185 |
1969
| stringContents = escapeBackslashes(stringContents);
|
186 |
1969
| ByteArrayInputStream is = new ByteArrayInputStream(stringContents.trim().getBytes("ISO8859_1"));
|
187 |
1969
| Properties properties = new Properties();
|
188 |
1969
| properties.load(is);
|
189 |
1969
| is.close();
|
190 |
1969
| return properties;
|
191 |
| } |
192 |
| |
193 |
3616
| public static boolean readBooleanContents(Element element, String tagName)
|
194 |
| { |
195 |
3616
| return readBooleanContents(element, tagName, false);
|
196 |
| } |
197 |
| |
198 |
13123
| public static boolean readBooleanContents(Element element, String tagName, boolean defaultValue)
|
199 |
| { |
200 |
13123
| String val = readStringContents(element, tagName);
|
201 |
13123
| if (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false"))
|
202 |
| { |
203 |
| |
204 |
8836
| return Boolean.valueOf(val);
|
205 |
| |
206 |
| } |
207 |
4287
| return defaultValue;
|
208 |
| } |
209 |
| |
210 |
1224
| public static Element stringToElement(String xml) throws Exception
|
211 |
| { |
212 |
1224
| ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("utf8"));
|
213 |
1224
| DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
214 |
1224
| Document d = builder.parse(bais);
|
215 |
1224
| bais.close();
|
216 |
1224
| return d.getDocumentElement();
|
217 |
| } |
218 |
| |
219 |
1017
| public static Element getDocumentRoot(InputStream is)
|
220 |
| { |
221 |
1017
| Document doc = null;
|
222 |
1017
| try
|
223 |
| { |
224 |
1017
| InputSource xmlInp = new InputSource(is);
|
225 |
| |
226 |
1017
| DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
227 |
1017
| DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
|
228 |
1017
| doc = parser.parse(xmlInp);
|
229 |
1017
| Element root = doc.getDocumentElement();
|
230 |
1017
| root.normalize();
|
231 |
1017
| return root;
|
232 |
| } |
233 |
| catch (SAXParseException err) |
234 |
| { |
235 |
0
| log.error("Configurator SAXParse error", err);
|
236 |
| } |
237 |
| catch (SAXException e) |
238 |
| { |
239 |
0
| log.error("Configurator SAX error", e);
|
240 |
| } |
241 |
| catch (Exception pce) |
242 |
| { |
243 |
0
| log.error("Configurator general error", pce);
|
244 |
| } |
245 |
0
| return null;
|
246 |
| } |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
1801
| public static boolean readBooleanAttribute(Element elem, String tagName, String attributeName, boolean defaultValue)
|
257 |
| { |
258 |
1801
| String val = getAttributeValue(elem, tagName, attributeName);
|
259 |
1801
| if (val != null)
|
260 |
| { |
261 |
17
| if (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false"))
|
262 |
| { |
263 |
| |
264 |
| |
265 |
15
| return Boolean.valueOf(val);
|
266 |
| } |
267 |
| } |
268 |
| |
269 |
1786
| return defaultValue;
|
270 |
| } |
271 |
| |
272 |
| } |