forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlHelper.java
More file actions
173 lines (154 loc) · 5.73 KB
/
XmlHelper.java
File metadata and controls
173 lines (154 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.bridge.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class XmlHelper {
protected static Logger logger = Logger.getLogger(XmlHelper.class);
public static Document parse(String xmlContent) throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream(xmlContent.getBytes("UTF-8"));
return parse(is);
}
public static Document parse(File file) throws IOException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
return parser.parse(file);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (SAXException e) {
throw new IOException(e);
}
}
public static Document parse(InputStream is) throws IOException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
InputSource in = new InputSource(is);
return parser.parse(in);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (SAXException e) {
throw new IOException(e);
}
}
public static Document newDocument() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder builder;
try {
builder = dbf.newDocumentBuilder();
Document document = builder.newDocument();
return document;
} catch (ParserConfigurationException e) {
logger.error("Unexpected exception " + e.getMessage(), e);
}
return null;
}
public static Node getRootNode(Document doc) {
NodeList l = doc.getChildNodes();
if(l != null && l.getLength() == 1)
return l.item(0);
return null;
}
public static Node getChildNode(Node parentNode, String childElementName) {
NodeList l = parentNode.getChildNodes();
for(int i = 0; i < l.getLength(); i++) {
Node node = l.item(i);
if(node.getNodeName().equals(childElementName))
return node;
}
return null;
}
public static String getChildNodeTextContent(Node parentNode, String childElementName) {
Node node = getChildNode(parentNode, childElementName);
if(node != null)
return node.getTextContent();
return null;
}
public static String getAttribute(Node node, String name) {
NamedNodeMap attributes = node.getAttributes();
Node attrNode = attributes.getNamedItem(name);
if(attrNode != null)
return attrNode.getNodeValue();
return null;
}
public static String toXML(Node node) {
if (node != null) {
Transformer transformer = newTransformer();
try {
StringWriter sw = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
} catch (TransformerException e) {
logger.error("Unexpected exception " + e.getMessage(), e);
}
}
return StringHelper.EMPTY_STRING;
}
public static Transformer newTransformer() {
return newTransformer("UTF-8", false);
}
public static Transformer newTransformer(String encoding, boolean indent) {
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, encoding);
properties.setProperty(OutputKeys.METHOD, "XML");
properties.setProperty(OutputKeys.VERSION, "1.0");
if(indent)
properties.setProperty(OutputKeys.INDENT, "YES");
else
properties.setProperty(OutputKeys.INDENT, "NO");
transformer.setOutputProperties(properties);
return transformer;
} catch (TransformerConfigurationException e) {
logger.error("Unexpected exception " + e.getMessage(), e);
}
return null;
}
}