| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.io.Reader; |
| 7 |
import java.io.StringReader; |
| 8 |
import java.io.StringWriter; |
| 9 |
import java.io.UncheckedIOException; |
| 10 |
import java.io.Writer; |
| 11 |
import java.nio.file.Files; |
| 12 |
import java.nio.file.Path; |
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import javax.xml.namespace.QName; |
| 19 |
import javax.xml.parsers.DocumentBuilder; |
| 20 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 21 |
import javax.xml.parsers.ParserConfigurationException; |
| 22 |
import javax.xml.transform.Transformer; |
| 23 |
import javax.xml.transform.TransformerConfigurationException; |
| 24 |
import javax.xml.transform.TransformerException; |
| 25 |
import javax.xml.transform.TransformerFactory; |
| 26 |
import javax.xml.transform.TransformerFactoryConfigurationError; |
| 27 |
import javax.xml.transform.dom.DOMSource; |
| 28 |
import javax.xml.transform.stream.StreamResult; |
| 29 |
|
| 30 |
import org.w3c.dom.Document; |
| 31 |
import org.w3c.dom.Element; |
| 32 |
import org.w3c.dom.Node; |
| 33 |
import org.w3c.dom.NodeList; |
| 34 |
import org.xml.sax.InputSource; |
| 35 |
import org.xml.sax.SAXException; |
| 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 |
public final class XmlUtils { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static Document createDocument() { |
| 76 |
try { |
| 77 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 78 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 79 |
return builder.newDocument(); |
| 80 |
} catch (ParserConfigurationException e) { |
| 81 |
|
| 82 |
throw new RuntimeException(e); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static Document toDocument(String xml) throws SAXException { |
| 93 |
try { |
| 94 |
return toDocument(new StringReader(xml)); |
| 95 |
} catch (IOException e) { |
| 96 |
|
| 97 |
throw new UncheckedIOException(e); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static Document toDocument(Path file) throws SAXException, IOException { |
| 109 |
try (InputStream in = new BufferedInputStream(Files.newInputStream(file))) { |
| 110 |
return XmlUtils.toDocument(in); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static Document toDocument(InputStream in) throws SAXException, IOException { |
| 122 |
return toDocument(new InputSource(in)); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public static Document toDocument(Reader reader) throws SAXException, IOException { |
| 145 |
return toDocument(new InputSource(reader)); |
| 146 |
} |
| 147 |
|
| 148 |
private static Document toDocument(InputSource in) throws SAXException, IOException { |
| 149 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 150 |
factory.setNamespaceAware(true); |
| 151 |
factory.setIgnoringComments(true); |
| 152 |
applyXXEProtection(factory); |
| 153 |
|
| 154 |
DocumentBuilder builder; |
| 155 |
try { |
| 156 |
builder = factory.newDocumentBuilder(); |
| 157 |
} catch (ParserConfigurationException e) { |
| 158 |
|
| 159 |
throw new RuntimeException(e); |
| 160 |
} |
| 161 |
|
| 162 |
return builder.parse(in); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static void applyXXEProtection(DocumentBuilderFactory factory) { |
| 174 |
Map<String, Boolean> features = new HashMap<>(); |
| 175 |
features.put("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 176 |
features.put("http://xml.org/sax/features/external-general-entities", false); |
| 177 |
features.put("http://xml.org/sax/features/external-parameter-entities", false); |
| 178 |
features.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); |
| 179 |
|
| 180 |
for (Map.Entry<String, Boolean> entry : features.entrySet()) { |
| 181 |
String feature = entry.getKey(); |
| 182 |
Boolean value = entry.getValue(); |
| 183 |
try { |
| 184 |
factory.setFeature(feature, value); |
| 185 |
} catch (ParserConfigurationException e) { |
| 186 |
|
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
factory.setXIncludeAware(false); |
| 191 |
factory.setExpandEntityReferences(false); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public static void applyXXEProtection(TransformerFactory factory) { |
| 203 |
|
| 204 |
String[] attributes = { |
| 205 |
|
| 206 |
"http://javax.xml.XMLConstants/property/accessExternalDTD", |
| 207 |
|
| 208 |
|
| 209 |
"http://javax.xml.XMLConstants/property/accessExternalStylesheet" |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
for (String attribute : attributes) { |
| 214 |
try { |
| 215 |
factory.setAttribute(attribute, ""); |
| 216 |
} catch (IllegalArgumentException e) { |
| 217 |
|
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public static String toString(Node node) { |
| 228 |
return toString(node, new HashMap<>()); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public static String toString(Node node, Map<String, String> outputProperties) { |
| 238 |
try { |
| 239 |
StringWriter writer = new StringWriter(); |
| 240 |
toWriter(node, writer, outputProperties); |
| 241 |
return writer.toString(); |
| 242 |
} catch (TransformerException e) { |
| 243 |
|
| 244 |
throw new RuntimeException(e); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public static void toWriter(Node node, Writer writer) throws TransformerException { |
| 255 |
toWriter(node, writer, new HashMap<>()); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public static void toWriter(Node node, Writer writer, Map<String, String> outputProperties) throws TransformerException { |
| 266 |
Transformer transformer; |
| 267 |
try { |
| 268 |
transformer = TransformerFactory.newInstance().newTransformer(); |
| 269 |
} catch (TransformerConfigurationException e) { |
| 270 |
|
| 271 |
throw new RuntimeException(e); |
| 272 |
} catch (TransformerFactoryConfigurationError e) { |
| 273 |
|
| 274 |
throw new RuntimeException(e); |
| 275 |
} |
| 276 |
|
| 277 |
assignOutputProperties(transformer, outputProperties); |
| 278 |
|
| 279 |
DOMSource source = new DOMSource(node); |
| 280 |
StreamResult result = new StreamResult(writer); |
| 281 |
transformer.transform(source, result); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public static void assignOutputProperties(Transformer transformer, Map<String, String> outputProperties) { |
| 291 |
for (Map.Entry<String, String> property : outputProperties.entrySet()) { |
| 292 |
try { |
| 293 |
transformer.setOutputProperty(property.getKey(), property.getValue()); |
| 294 |
} catch (IllegalArgumentException e) { |
| 295 |
|
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public static List<Element> toElementList(NodeList nodeList) { |
| 306 |
List<Element> elements = new ArrayList<>(); |
| 307 |
for (int i = 0; i < nodeList.getLength(); i++) { |
| 308 |
Node node = nodeList.item(i); |
| 309 |
if (node instanceof Element) { |
| 310 |
elements.add((Element) node); |
| 311 |
} |
| 312 |
} |
| 313 |
return elements; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public static Element getFirstChildElement(Element parent) { |
| 322 |
return getFirstChildElement((Node) parent); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private static Element getFirstChildElement(Node parent) { |
| 331 |
NodeList nodeList = parent.getChildNodes(); |
| 332 |
for (int i = 0; i < nodeList.getLength(); i++) { |
| 333 |
Node node = nodeList.item(i); |
| 334 |
if (node instanceof Element) { |
| 335 |
return (Element) node; |
| 336 |
} |
| 337 |
} |
| 338 |
return null; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static boolean hasQName(Node node, QName qname) { |
| 348 |
return qname.getNamespaceURI().equals(node.getNamespaceURI()) && qname.getLocalPart().equals(node.getLocalName()); |
| 349 |
} |
| 350 |
|
| 351 |
private XmlUtils() { |
| 352 |
|
| 353 |
} |
| 354 |
} |