Debian Codegraph @

scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java

raw
1 /*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19 package com.sun.star.script.framework.container;
20
21 import com.sun.star.script.framework.log.LogUtils;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import javax.xml.XMLConstants;
35
36 import org.w3c.dom.Document;
37
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.SAXParseException;
41
42 public class XMLParserFactory {
43
44 private static XMLParser parser = null;
45 private static String officedtdurl = null;
46
47 private XMLParserFactory() {}
48
49 public static synchronized XMLParser getParser() {
50 if (parser == null)
51 parser = new DefaultParser();
52
53 return parser;
54 }
55
56 public static void setOfficeDTDURL(String url) {
57 officedtdurl = url;
58 }
59
60 private static class DefaultParser implements XMLParser {
61
62 private final DocumentBuilderFactory factory;
63
64 public DefaultParser() {
65 factory = DocumentBuilderFactory.newInstance();
66
67 try {
68 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
69 } catch (ParserConfigurationException e) {
70 LogUtils.DEBUG(LogUtils.getTrace(e));
71 }
72
73 try {
74 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
75 } catch (ParserConfigurationException e) {
76 LogUtils.DEBUG(LogUtils.getTrace(e));
77 }
78
79 try {
80 factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
81 } catch (ParserConfigurationException e) {
82 LogUtils.DEBUG(LogUtils.getTrace(e));
83 }
84
85 try {
86 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
87 } catch (ParserConfigurationException e) {
88 LogUtils.DEBUG(LogUtils.getTrace(e));
89 }
90 }
91
92 public Document parse(InputStream inputStream) throws IOException {
93
94 Document result = null;
95
96 try {
97 DocumentBuilder builder = factory.newDocumentBuilder();
98 InputSource is = new InputSource(inputStream);
99
100 if (officedtdurl != null) {
101 is.setSystemId(officedtdurl);
102 }
103
104 result = builder.parse(is);
105 } catch (SAXParseException ex1) {
106 IOException ex2 = new IOException();
107 ex2.initCause(ex1);
108 throw ex2;
109 } catch (SAXException ex1) {
110 IOException ex2 = new IOException();
111 ex2.initCause(ex1);
112 throw ex2;
113 } catch (ParserConfigurationException ex1) {
114 IOException ex2 = new IOException();
115 ex2.initCause(ex1);
116 throw ex2;
117 }
118
119 return result;
120 }
121
122 public void write(Document doc, OutputStream out) throws IOException {
123 try {
124 TransformerFactory transformerFactory = TransformerFactory.newInstance();
125 transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
126 transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
127 transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
128 transformerFactory.newTransformer().transform(
129 new DOMSource(doc), new StreamResult(out));
130 } catch (TransformerException ex1) {
131 IOException ex2 = new IOException();
132 ex2.initCause(ex1);
133 throw ex2;
134 }
135 }
136 }
137 }