src/share/jaxws_classes/com/sun/xml/internal/ws/util/xml/XmlUtil.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 397
b99d7e355d4b
child 515
6cd506508147
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial