Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.util.xml; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 29 | import com.sun.org.apache.xml.internal.resolver.Catalog; |
aoqi@0 | 30 | import com.sun.org.apache.xml.internal.resolver.CatalogManager; |
aoqi@0 | 31 | import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.server.ServerRtException; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.util.ByteArrayBuffer; |
aoqi@0 | 34 | import org.w3c.dom.Attr; |
aoqi@0 | 35 | import org.w3c.dom.Element; |
aoqi@0 | 36 | import org.w3c.dom.EntityReference; |
aoqi@0 | 37 | import org.w3c.dom.Node; |
aoqi@0 | 38 | import org.w3c.dom.NodeList; |
aoqi@0 | 39 | import org.w3c.dom.Text; |
aoqi@0 | 40 | import org.xml.sax.*; |
aoqi@0 | 41 | |
aoqi@0 | 42 | import javax.xml.XMLConstants; |
aoqi@0 | 43 | import javax.xml.namespace.QName; |
aoqi@0 | 44 | import javax.xml.parsers.DocumentBuilderFactory; |
aoqi@0 | 45 | import javax.xml.parsers.ParserConfigurationException; |
aoqi@0 | 46 | import javax.xml.parsers.SAXParserFactory; |
aoqi@0 | 47 | import javax.xml.stream.XMLInputFactory; |
aoqi@0 | 48 | import javax.xml.transform.Result; |
aoqi@0 | 49 | import javax.xml.transform.Source; |
aoqi@0 | 50 | import javax.xml.transform.Transformer; |
aoqi@0 | 51 | import javax.xml.transform.TransformerConfigurationException; |
aoqi@0 | 52 | import javax.xml.transform.TransformerException; |
aoqi@0 | 53 | import javax.xml.transform.TransformerFactory; |
aoqi@0 | 54 | import javax.xml.transform.sax.SAXTransformerFactory; |
aoqi@0 | 55 | import javax.xml.transform.sax.TransformerHandler; |
aoqi@0 | 56 | import javax.xml.transform.stream.StreamSource; |
aoqi@0 | 57 | import javax.xml.validation.SchemaFactory; |
aoqi@0 | 58 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 59 | import javax.xml.xpath.XPathFactory; |
aoqi@0 | 60 | import javax.xml.xpath.XPathFactoryConfigurationException; |
aoqi@0 | 61 | import java.io.IOException; |
aoqi@0 | 62 | import java.io.InputStream; |
aoqi@0 | 63 | import java.io.OutputStreamWriter; |
aoqi@0 | 64 | import java.io.Writer; |
aoqi@0 | 65 | import java.net.URL; |
mkos@721 | 66 | import java.security.AccessController; |
mkos@721 | 67 | import java.security.PrivilegedAction; |
aoqi@0 | 68 | import java.util.ArrayList; |
aoqi@0 | 69 | import java.util.Enumeration; |
aoqi@0 | 70 | import java.util.Iterator; |
aoqi@0 | 71 | import java.util.List; |
aoqi@0 | 72 | import java.util.StringTokenizer; |
aoqi@0 | 73 | import java.util.logging.Level; |
aoqi@0 | 74 | import java.util.logging.Logger; |
aoqi@0 | 75 | |
aoqi@0 | 76 | /** |
aoqi@0 | 77 | * @author WS Development Team |
aoqi@0 | 78 | */ |
aoqi@0 | 79 | public class XmlUtil { |
aoqi@0 | 80 | |
aoqi@0 | 81 | // not in older JDK, so must be duplicated here, otherwise javax.xml.XMLConstants should be used |
aoqi@0 | 82 | private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema"; |
aoqi@0 | 83 | |
aoqi@0 | 84 | private final static String LEXICAL_HANDLER_PROPERTY = |
aoqi@0 | 85 | "http://xml.org/sax/properties/lexical-handler"; |
aoqi@0 | 86 | |
aoqi@0 | 87 | private static final Logger LOGGER = Logger.getLogger(XmlUtil.class.getName()); |
aoqi@0 | 88 | |
mkos@721 | 89 | private static final String DISABLE_XML_SECURITY = "com.sun.xml.internal.ws.disableXmlSecurity"; |
aoqi@0 | 90 | |
mkos@721 | 91 | private static boolean XML_SECURITY_DISABLED = AccessController.doPrivileged( |
mkos@721 | 92 | new PrivilegedAction<Boolean>() { |
mkos@721 | 93 | @Override |
mkos@721 | 94 | public Boolean run() { |
mkos@721 | 95 | return Boolean.getBoolean(DISABLE_XML_SECURITY); |
mkos@721 | 96 | } |
mkos@721 | 97 | } |
mkos@721 | 98 | ); |
aoqi@0 | 99 | |
aoqi@0 | 100 | public static String getPrefix(String s) { |
aoqi@0 | 101 | int i = s.indexOf(':'); |
aoqi@0 | 102 | if (i == -1) |
aoqi@0 | 103 | return null; |
aoqi@0 | 104 | return s.substring(0, i); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | public static String getLocalPart(String s) { |
aoqi@0 | 108 | int i = s.indexOf(':'); |
aoqi@0 | 109 | if (i == -1) |
aoqi@0 | 110 | return s; |
aoqi@0 | 111 | return s.substring(i + 1); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | |
aoqi@0 | 115 | |
aoqi@0 | 116 | public static String getAttributeOrNull(Element e, String name) { |
aoqi@0 | 117 | Attr a = e.getAttributeNode(name); |
aoqi@0 | 118 | if (a == null) |
aoqi@0 | 119 | return null; |
aoqi@0 | 120 | return a.getValue(); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | public static String getAttributeNSOrNull( |
aoqi@0 | 124 | Element e, |
aoqi@0 | 125 | String name, |
aoqi@0 | 126 | String nsURI) { |
aoqi@0 | 127 | Attr a = e.getAttributeNodeNS(nsURI, name); |
aoqi@0 | 128 | if (a == null) |
aoqi@0 | 129 | return null; |
aoqi@0 | 130 | return a.getValue(); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | public static String getAttributeNSOrNull( |
aoqi@0 | 134 | Element e, |
aoqi@0 | 135 | QName name) { |
aoqi@0 | 136 | Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart()); |
aoqi@0 | 137 | if (a == null) |
aoqi@0 | 138 | return null; |
aoqi@0 | 139 | return a.getValue(); |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | /* public static boolean matchesTagNS(Element e, String tag, String nsURI) { |
aoqi@0 | 143 | try { |
aoqi@0 | 144 | return e.getLocalName().equals(tag) |
aoqi@0 | 145 | && e.getNamespaceURI().equals(nsURI); |
aoqi@0 | 146 | } catch (NullPointerException npe) { |
aoqi@0 | 147 | |
aoqi@0 | 148 | // localname not null since parsing would fail before here |
aoqi@0 | 149 | throw new WSDLParseException( |
aoqi@0 | 150 | "null.namespace.found", |
aoqi@0 | 151 | e.getLocalName()); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | public static boolean matchesTagNS( |
aoqi@0 | 156 | Element e, |
aoqi@0 | 157 | javax.xml.namespace.QName name) { |
aoqi@0 | 158 | try { |
aoqi@0 | 159 | return e.getLocalName().equals(name.getLocalPart()) |
aoqi@0 | 160 | && e.getNamespaceURI().equals(name.getNamespaceURI()); |
aoqi@0 | 161 | } catch (NullPointerException npe) { |
aoqi@0 | 162 | |
aoqi@0 | 163 | // localname not null since parsing would fail before here |
aoqi@0 | 164 | throw new WSDLParseException( |
aoqi@0 | 165 | "null.namespace.found", |
aoqi@0 | 166 | e.getLocalName()); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | }*/ |
aoqi@0 | 169 | |
aoqi@0 | 170 | public static Iterator getAllChildren(Element element) { |
aoqi@0 | 171 | return new NodeListIterator(element.getChildNodes()); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | public static Iterator getAllAttributes(Element element) { |
aoqi@0 | 175 | return new NamedNodeMapIterator(element.getAttributes()); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | public static List<String> parseTokenList(String tokenList) { |
aoqi@0 | 179 | List<String> result = new ArrayList<String>(); |
aoqi@0 | 180 | StringTokenizer tokenizer = new StringTokenizer(tokenList, " "); |
aoqi@0 | 181 | while (tokenizer.hasMoreTokens()) { |
aoqi@0 | 182 | result.add(tokenizer.nextToken()); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | return result; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | public static String getTextForNode(Node node) { |
aoqi@0 | 188 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 189 | |
aoqi@0 | 190 | NodeList children = node.getChildNodes(); |
aoqi@0 | 191 | if (children.getLength() == 0) |
aoqi@0 | 192 | return null; |
aoqi@0 | 193 | |
aoqi@0 | 194 | for (int i = 0; i < children.getLength(); ++i) { |
aoqi@0 | 195 | Node n = children.item(i); |
aoqi@0 | 196 | |
aoqi@0 | 197 | if (n instanceof Text) |
aoqi@0 | 198 | sb.append(n.getNodeValue()); |
aoqi@0 | 199 | else if (n instanceof EntityReference) { |
aoqi@0 | 200 | String s = getTextForNode(n); |
aoqi@0 | 201 | if (s == null) |
aoqi@0 | 202 | return null; |
aoqi@0 | 203 | else |
aoqi@0 | 204 | sb.append(s); |
aoqi@0 | 205 | } else |
aoqi@0 | 206 | return null; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | return sb.toString(); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | public static InputStream getUTF8Stream(String s) { |
aoqi@0 | 213 | try { |
aoqi@0 | 214 | ByteArrayBuffer bab = new ByteArrayBuffer(); |
aoqi@0 | 215 | Writer w = new OutputStreamWriter(bab, "utf-8"); |
aoqi@0 | 216 | w.write(s); |
aoqi@0 | 217 | w.close(); |
aoqi@0 | 218 | return bab.newInputStream(); |
aoqi@0 | 219 | } catch (IOException e) { |
aoqi@0 | 220 | throw new RuntimeException("should not happen"); |
aoqi@0 | 221 | } |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | static final ContextClassloaderLocal<TransformerFactory> transformerFactory = new ContextClassloaderLocal<TransformerFactory>() { |
aoqi@0 | 225 | @Override |
aoqi@0 | 226 | protected TransformerFactory initialValue() throws Exception { |
aoqi@0 | 227 | return TransformerFactory.newInstance(); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | }; |
aoqi@0 | 230 | |
aoqi@0 | 231 | static final ContextClassloaderLocal<SAXParserFactory> saxParserFactory = new ContextClassloaderLocal<SAXParserFactory>() { |
aoqi@0 | 232 | @Override |
aoqi@0 | 233 | protected SAXParserFactory initialValue() throws Exception { |
aoqi@0 | 234 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
aoqi@0 | 235 | factory.setNamespaceAware(true); |
aoqi@0 | 236 | return factory; |
aoqi@0 | 237 | } |
aoqi@0 | 238 | }; |
aoqi@0 | 239 | |
aoqi@0 | 240 | /** |
aoqi@0 | 241 | * Creates a new identity transformer. |
aoqi@0 | 242 | */ |
aoqi@0 | 243 | public static Transformer newTransformer() { |
aoqi@0 | 244 | try { |
aoqi@0 | 245 | return transformerFactory.get().newTransformer(); |
aoqi@0 | 246 | } catch (TransformerConfigurationException tex) { |
aoqi@0 | 247 | throw new IllegalStateException("Unable to create a JAXP transformer"); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | /** |
aoqi@0 | 252 | * Performs identity transformation. |
aoqi@0 | 253 | */ |
aoqi@0 | 254 | public static <T extends Result> |
aoqi@0 | 255 | T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException { |
aoqi@0 | 256 | if (src instanceof StreamSource) { |
aoqi@0 | 257 | // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing |
aoqi@0 | 258 | // is not turned on by default |
aoqi@0 | 259 | StreamSource ssrc = (StreamSource) src; |
aoqi@0 | 260 | TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler(); |
aoqi@0 | 261 | th.setResult(result); |
aoqi@0 | 262 | XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader(); |
aoqi@0 | 263 | reader.setContentHandler(th); |
aoqi@0 | 264 | reader.setProperty(LEXICAL_HANDLER_PROPERTY, th); |
aoqi@0 | 265 | reader.parse(toInputSource(ssrc)); |
aoqi@0 | 266 | } else { |
aoqi@0 | 267 | newTransformer().transform(src, result); |
aoqi@0 | 268 | } |
aoqi@0 | 269 | return result; |
aoqi@0 | 270 | } |
aoqi@0 | 271 | |
aoqi@0 | 272 | private static InputSource toInputSource(StreamSource src) { |
aoqi@0 | 273 | InputSource is = new InputSource(); |
aoqi@0 | 274 | is.setByteStream(src.getInputStream()); |
aoqi@0 | 275 | is.setCharacterStream(src.getReader()); |
aoqi@0 | 276 | is.setPublicId(src.getPublicId()); |
aoqi@0 | 277 | is.setSystemId(src.getSystemId()); |
aoqi@0 | 278 | return is; |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | /* |
aoqi@0 | 282 | * Gets an EntityResolver using XML catalog |
aoqi@0 | 283 | */ |
aoqi@0 | 284 | public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) { |
aoqi@0 | 285 | // set up a manager |
aoqi@0 | 286 | CatalogManager manager = new CatalogManager(); |
aoqi@0 | 287 | manager.setIgnoreMissingProperties(true); |
aoqi@0 | 288 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
aoqi@0 | 289 | manager.setUseStaticCatalog(false); |
aoqi@0 | 290 | Catalog catalog = manager.getCatalog(); |
aoqi@0 | 291 | try { |
aoqi@0 | 292 | if (catalogUrl != null) { |
aoqi@0 | 293 | catalog.parseCatalog(catalogUrl); |
aoqi@0 | 294 | } |
aoqi@0 | 295 | } catch (IOException e) { |
aoqi@0 | 296 | throw new ServerRtException("server.rt.err",e); |
aoqi@0 | 297 | } |
aoqi@0 | 298 | return workaroundCatalogResolver(catalog); |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | /** |
aoqi@0 | 302 | * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml |
aoqi@0 | 303 | */ |
aoqi@0 | 304 | public static EntityResolver createDefaultCatalogResolver() { |
aoqi@0 | 305 | |
aoqi@0 | 306 | // set up a manager |
aoqi@0 | 307 | CatalogManager manager = new CatalogManager(); |
aoqi@0 | 308 | manager.setIgnoreMissingProperties(true); |
aoqi@0 | 309 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
aoqi@0 | 310 | manager.setUseStaticCatalog(false); |
aoqi@0 | 311 | // parse the catalog |
aoqi@0 | 312 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
aoqi@0 | 313 | Enumeration<URL> catalogEnum; |
aoqi@0 | 314 | Catalog catalog = manager.getCatalog(); |
aoqi@0 | 315 | try { |
aoqi@0 | 316 | if (cl == null) { |
aoqi@0 | 317 | catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml"); |
aoqi@0 | 318 | } else { |
aoqi@0 | 319 | catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml"); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | while(catalogEnum.hasMoreElements()) { |
aoqi@0 | 323 | URL url = catalogEnum.nextElement(); |
aoqi@0 | 324 | catalog.parseCatalog(url); |
aoqi@0 | 325 | } |
aoqi@0 | 326 | } catch (IOException e) { |
aoqi@0 | 327 | throw new WebServiceException(e); |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | return workaroundCatalogResolver(catalog); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | |
aoqi@0 | 333 | /** |
aoqi@0 | 334 | * Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when |
aoqi@0 | 335 | * useStaticCatalog is false. |
aoqi@0 | 336 | * This returns a CatalogResolver that uses the catalog passed as parameter. |
aoqi@0 | 337 | * @param catalog |
aoqi@0 | 338 | * @return CatalogResolver |
aoqi@0 | 339 | */ |
aoqi@0 | 340 | private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) { |
aoqi@0 | 341 | // set up a manager |
aoqi@0 | 342 | CatalogManager manager = new CatalogManager() { |
aoqi@0 | 343 | @Override |
aoqi@0 | 344 | public Catalog getCatalog() { |
aoqi@0 | 345 | return catalog; |
aoqi@0 | 346 | } |
aoqi@0 | 347 | }; |
aoqi@0 | 348 | manager.setIgnoreMissingProperties(true); |
aoqi@0 | 349 | // Using static catalog may result in to sharing of the catalog by multiple apps running in a container |
aoqi@0 | 350 | manager.setUseStaticCatalog(false); |
aoqi@0 | 351 | |
aoqi@0 | 352 | return new CatalogResolver(manager); |
aoqi@0 | 353 | } |
aoqi@0 | 354 | |
aoqi@0 | 355 | /** |
aoqi@0 | 356 | * {@link ErrorHandler} that always treat the error as fatal. |
aoqi@0 | 357 | */ |
aoqi@0 | 358 | public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() { |
aoqi@0 | 359 | @Override |
aoqi@0 | 360 | public void warning(SAXParseException exception) { |
aoqi@0 | 361 | } |
aoqi@0 | 362 | |
aoqi@0 | 363 | @Override |
aoqi@0 | 364 | public void error(SAXParseException exception) throws SAXException { |
aoqi@0 | 365 | throw exception; |
aoqi@0 | 366 | } |
aoqi@0 | 367 | |
aoqi@0 | 368 | @Override |
aoqi@0 | 369 | public void fatalError(SAXParseException exception) throws SAXException { |
aoqi@0 | 370 | throw exception; |
aoqi@0 | 371 | } |
aoqi@0 | 372 | }; |
aoqi@0 | 373 | |
aoqi@0 | 374 | public static DocumentBuilderFactory newDocumentBuilderFactory() { |
aoqi@0 | 375 | return newDocumentBuilderFactory(true); |
aoqi@0 | 376 | } |
aoqi@0 | 377 | |
aoqi@0 | 378 | public static DocumentBuilderFactory newDocumentBuilderFactory(boolean secureXmlProcessing) { |
aoqi@0 | 379 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
aoqi@0 | 380 | try { |
aoqi@0 | 381 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessing)); |
aoqi@0 | 382 | } catch (ParserConfigurationException e) { |
aoqi@0 | 383 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); |
aoqi@0 | 384 | } |
aoqi@0 | 385 | return factory; |
aoqi@0 | 386 | } |
aoqi@0 | 387 | |
aoqi@0 | 388 | public static TransformerFactory newTransformerFactory(boolean secureXmlProcessingEnabled) { |
aoqi@0 | 389 | TransformerFactory factory = TransformerFactory.newInstance(); |
aoqi@0 | 390 | try { |
aoqi@0 | 391 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
aoqi@0 | 392 | } catch (TransformerConfigurationException e) { |
aoqi@0 | 393 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()}); |
aoqi@0 | 394 | } |
aoqi@0 | 395 | return factory; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | public static TransformerFactory newTransformerFactory() { |
aoqi@0 | 399 | return newTransformerFactory(true); |
aoqi@0 | 400 | } |
aoqi@0 | 401 | |
aoqi@0 | 402 | public static SAXParserFactory newSAXParserFactory(boolean secureXmlProcessingEnabled) { |
aoqi@0 | 403 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
aoqi@0 | 404 | try { |
aoqi@0 | 405 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
aoqi@0 | 406 | } catch (Exception e) { |
aoqi@0 | 407 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()}); |
aoqi@0 | 408 | } |
aoqi@0 | 409 | return factory; |
aoqi@0 | 410 | } |
aoqi@0 | 411 | |
aoqi@0 | 412 | public static XPathFactory newXPathFactory(boolean secureXmlProcessingEnabled) { |
aoqi@0 | 413 | XPathFactory factory = XPathFactory.newInstance(); |
aoqi@0 | 414 | try { |
aoqi@0 | 415 | factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled)); |
aoqi@0 | 416 | } catch (XPathFactoryConfigurationException e) { |
aoqi@0 | 417 | LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } ); |
aoqi@0 | 418 | } |
aoqi@0 | 419 | return factory; |
aoqi@0 | 420 | } |
aoqi@0 | 421 | |
aoqi@0 | 422 | public static XMLInputFactory newXMLInputFactory(boolean secureXmlProcessingEnabled) { |
aoqi@0 | 423 | XMLInputFactory factory = XMLInputFactory.newInstance(); |
aoqi@0 | 424 | if (isXMLSecurityDisabled(secureXmlProcessingEnabled)) { |
aoqi@0 | 425 | // TODO-Miran: are those apppropriate defaults? |
aoqi@0 | 426 | factory.setProperty(XMLInputFactory.SUPPORT_DTD, false); |
aoqi@0 | 427 | factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); |
aoqi@0 | 428 | } |
aoqi@0 | 429 | return factory; |
aoqi@0 | 430 | } |
aoqi@0 | 431 | |
aoqi@0 | 432 | private static boolean isXMLSecurityDisabled(boolean runtimeDisabled) { |
aoqi@0 | 433 | return XML_SECURITY_DISABLED || runtimeDisabled; |
aoqi@0 | 434 | } |
aoqi@0 | 435 | |
aoqi@0 | 436 | public static SchemaFactory allowExternalAccess(SchemaFactory sf, String value, boolean disableSecureProcessing) { |
aoqi@0 | 437 | |
aoqi@0 | 438 | // if xml security (feature secure processing) disabled, nothing to do, no restrictions applied |
aoqi@0 | 439 | if (isXMLSecurityDisabled(disableSecureProcessing)) { |
aoqi@0 | 440 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 441 | LOGGER.log(Level.FINE, "Xml Security disabled, no JAXP xsd external access configuration necessary."); |
aoqi@0 | 442 | } |
aoqi@0 | 443 | return sf; |
aoqi@0 | 444 | } |
aoqi@0 | 445 | |
aoqi@0 | 446 | if (System.getProperty("javax.xml.accessExternalSchema") != null) { |
aoqi@0 | 447 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 448 | LOGGER.log(Level.FINE, "Detected explicitly JAXP configuration, no JAXP xsd external access configuration necessary."); |
aoqi@0 | 449 | } |
aoqi@0 | 450 | return sf; |
aoqi@0 | 451 | } |
aoqi@0 | 452 | |
aoqi@0 | 453 | try { |
aoqi@0 | 454 | sf.setProperty(ACCESS_EXTERNAL_SCHEMA, value); |
aoqi@0 | 455 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 456 | LOGGER.log(Level.FINE, "Property \"{0}\" is supported and has been successfully set by used JAXP implementation.", new Object[]{ACCESS_EXTERNAL_SCHEMA}); |
aoqi@0 | 457 | } |
aoqi@0 | 458 | } catch (SAXException ignored) { |
aoqi@0 | 459 | // nothing to do; support depends on version JDK or SAX implementation |
aoqi@0 | 460 | if (LOGGER.isLoggable(Level.CONFIG)) { |
aoqi@0 | 461 | LOGGER.log(Level.CONFIG, "Property \"{0}\" is not supported by used JAXP implementation.", new Object[]{ACCESS_EXTERNAL_SCHEMA}); |
aoqi@0 | 462 | } |
aoqi@0 | 463 | } |
aoqi@0 | 464 | return sf; |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | } |