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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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;
ohair@286 57 import javax.xml.ws.WebServiceException;
alanb@368 58 import javax.xml.xpath.XPathFactory;
alanb@368 59 import javax.xml.xpath.XPathFactoryConfigurationException;
ohair@286 60 import java.io.IOException;
ohair@286 61 import java.io.InputStream;
ohair@286 62 import java.io.OutputStreamWriter;
ohair@286 63 import java.io.Writer;
ohair@286 64 import java.net.URL;
ohair@286 65 import java.util.ArrayList;
ohair@286 66 import java.util.Enumeration;
ohair@286 67 import java.util.Iterator;
ohair@286 68 import java.util.List;
ohair@286 69 import java.util.StringTokenizer;
alanb@368 70 import java.util.logging.Level;
alanb@368 71 import java.util.logging.Logger;
ohair@286 72
ohair@286 73 /**
ohair@286 74 * @author WS Development Team
ohair@286 75 */
ohair@286 76 public class XmlUtil {
ohair@286 77 private final static String LEXICAL_HANDLER_PROPERTY =
ohair@286 78 "http://xml.org/sax/properties/lexical-handler";
ohair@286 79
alanb@368 80 private static final Logger LOGGER = Logger.getLogger(XmlUtil.class.getName());
alanb@368 81
alanb@368 82 private static boolean globalSecureXmlProcessingEnabled;
alanb@368 83
alanb@368 84 static {
alanb@368 85 String disableSecureXmlProcessing = System.getProperty("disableSecureXmlProcessing");
alanb@368 86 globalSecureXmlProcessingEnabled = disableSecureXmlProcessing == null || !Boolean.valueOf(disableSecureXmlProcessing);
alanb@368 87 }
alanb@368 88
ohair@286 89 public static String getPrefix(String s) {
ohair@286 90 int i = s.indexOf(':');
ohair@286 91 if (i == -1)
ohair@286 92 return null;
ohair@286 93 return s.substring(0, i);
ohair@286 94 }
ohair@286 95
ohair@286 96 public static String getLocalPart(String s) {
ohair@286 97 int i = s.indexOf(':');
ohair@286 98 if (i == -1)
ohair@286 99 return s;
ohair@286 100 return s.substring(i + 1);
ohair@286 101 }
ohair@286 102
ohair@286 103
ohair@286 104
ohair@286 105 public static String getAttributeOrNull(Element e, String name) {
ohair@286 106 Attr a = e.getAttributeNode(name);
ohair@286 107 if (a == null)
ohair@286 108 return null;
ohair@286 109 return a.getValue();
ohair@286 110 }
ohair@286 111
ohair@286 112 public static String getAttributeNSOrNull(
ohair@286 113 Element e,
ohair@286 114 String name,
ohair@286 115 String nsURI) {
ohair@286 116 Attr a = e.getAttributeNodeNS(nsURI, name);
ohair@286 117 if (a == null)
ohair@286 118 return null;
ohair@286 119 return a.getValue();
ohair@286 120 }
ohair@286 121
ohair@286 122 public static String getAttributeNSOrNull(
ohair@286 123 Element e,
ohair@286 124 QName name) {
ohair@286 125 Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
ohair@286 126 if (a == null)
ohair@286 127 return null;
ohair@286 128 return a.getValue();
ohair@286 129 }
ohair@286 130
ohair@286 131 /* public static boolean matchesTagNS(Element e, String tag, String nsURI) {
ohair@286 132 try {
ohair@286 133 return e.getLocalName().equals(tag)
ohair@286 134 && e.getNamespaceURI().equals(nsURI);
ohair@286 135 } catch (NullPointerException npe) {
ohair@286 136
ohair@286 137 // localname not null since parsing would fail before here
ohair@286 138 throw new WSDLParseException(
ohair@286 139 "null.namespace.found",
ohair@286 140 e.getLocalName());
ohair@286 141 }
ohair@286 142 }
ohair@286 143
ohair@286 144 public static boolean matchesTagNS(
ohair@286 145 Element e,
ohair@286 146 javax.xml.namespace.QName name) {
ohair@286 147 try {
ohair@286 148 return e.getLocalName().equals(name.getLocalPart())
ohair@286 149 && e.getNamespaceURI().equals(name.getNamespaceURI());
ohair@286 150 } catch (NullPointerException npe) {
ohair@286 151
ohair@286 152 // localname not null since parsing would fail before here
ohair@286 153 throw new WSDLParseException(
ohair@286 154 "null.namespace.found",
ohair@286 155 e.getLocalName());
ohair@286 156 }
ohair@286 157 }*/
ohair@286 158
ohair@286 159 public static Iterator getAllChildren(Element element) {
ohair@286 160 return new NodeListIterator(element.getChildNodes());
ohair@286 161 }
ohair@286 162
ohair@286 163 public static Iterator getAllAttributes(Element element) {
ohair@286 164 return new NamedNodeMapIterator(element.getAttributes());
ohair@286 165 }
ohair@286 166
ohair@286 167 public static List<String> parseTokenList(String tokenList) {
ohair@286 168 List<String> result = new ArrayList<String>();
ohair@286 169 StringTokenizer tokenizer = new StringTokenizer(tokenList, " ");
ohair@286 170 while (tokenizer.hasMoreTokens()) {
ohair@286 171 result.add(tokenizer.nextToken());
ohair@286 172 }
ohair@286 173 return result;
ohair@286 174 }
ohair@286 175
ohair@286 176 public static String getTextForNode(Node node) {
alanb@368 177 StringBuilder sb = new StringBuilder();
ohair@286 178
ohair@286 179 NodeList children = node.getChildNodes();
ohair@286 180 if (children.getLength() == 0)
ohair@286 181 return null;
ohair@286 182
ohair@286 183 for (int i = 0; i < children.getLength(); ++i) {
ohair@286 184 Node n = children.item(i);
ohair@286 185
ohair@286 186 if (n instanceof Text)
ohair@286 187 sb.append(n.getNodeValue());
ohair@286 188 else if (n instanceof EntityReference) {
ohair@286 189 String s = getTextForNode(n);
ohair@286 190 if (s == null)
ohair@286 191 return null;
ohair@286 192 else
ohair@286 193 sb.append(s);
ohair@286 194 } else
ohair@286 195 return null;
ohair@286 196 }
ohair@286 197
ohair@286 198 return sb.toString();
ohair@286 199 }
ohair@286 200
ohair@286 201 public static InputStream getUTF8Stream(String s) {
ohair@286 202 try {
ohair@286 203 ByteArrayBuffer bab = new ByteArrayBuffer();
ohair@286 204 Writer w = new OutputStreamWriter(bab, "utf-8");
ohair@286 205 w.write(s);
ohair@286 206 w.close();
ohair@286 207 return bab.newInputStream();
ohair@286 208 } catch (IOException e) {
ohair@286 209 throw new RuntimeException("should not happen");
ohair@286 210 }
ohair@286 211 }
ohair@286 212
alanb@368 213 static final TransformerFactory transformerFactory = newTransformerFactory();
ohair@286 214
alanb@368 215 static final SAXParserFactory saxParserFactory = newSAXParserFactory(true);
ohair@286 216
ohair@286 217 static {
ohair@286 218 saxParserFactory.setNamespaceAware(true);
ohair@286 219 }
ohair@286 220
ohair@286 221 /**
ohair@286 222 * Creates a new identity transformer.
ohair@286 223 */
ohair@286 224 public static Transformer newTransformer() {
ohair@286 225 try {
ohair@286 226 return transformerFactory.newTransformer();
ohair@286 227 } catch (TransformerConfigurationException tex) {
ohair@286 228 throw new IllegalStateException("Unable to create a JAXP transformer");
ohair@286 229 }
ohair@286 230 }
ohair@286 231
ohair@286 232 /**
ohair@286 233 * Performs identity transformation.
ohair@286 234 */
ohair@286 235 public static <T extends Result>
ohair@286 236 T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
ohair@286 237 if (src instanceof StreamSource) {
ohair@286 238 // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
ohair@286 239 // is not turned on by default
ohair@286 240 StreamSource ssrc = (StreamSource) src;
ohair@286 241 TransformerHandler th = ((SAXTransformerFactory) transformerFactory).newTransformerHandler();
ohair@286 242 th.setResult(result);
ohair@286 243 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
ohair@286 244 reader.setContentHandler(th);
ohair@286 245 reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
ohair@286 246 reader.parse(toInputSource(ssrc));
ohair@286 247 } else {
ohair@286 248 newTransformer().transform(src, result);
ohair@286 249 }
ohair@286 250 return result;
ohair@286 251 }
ohair@286 252
ohair@286 253 private static InputSource toInputSource(StreamSource src) {
ohair@286 254 InputSource is = new InputSource();
ohair@286 255 is.setByteStream(src.getInputStream());
ohair@286 256 is.setCharacterStream(src.getReader());
ohair@286 257 is.setPublicId(src.getPublicId());
ohair@286 258 is.setSystemId(src.getSystemId());
ohair@286 259 return is;
ohair@286 260 }
ohair@286 261
ohair@286 262 /*
ohair@286 263 * Gets an EntityResolver using XML catalog
ohair@286 264 */
ohair@286 265 public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) {
ohair@286 266 // set up a manager
ohair@286 267 CatalogManager manager = new CatalogManager();
ohair@286 268 manager.setIgnoreMissingProperties(true);
ohair@286 269 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 270 manager.setUseStaticCatalog(false);
ohair@286 271 Catalog catalog = manager.getCatalog();
ohair@286 272 try {
ohair@286 273 if (catalogUrl != null) {
ohair@286 274 catalog.parseCatalog(catalogUrl);
ohair@286 275 }
ohair@286 276 } catch (IOException e) {
ohair@286 277 throw new ServerRtException("server.rt.err",e);
ohair@286 278 }
ohair@286 279 return workaroundCatalogResolver(catalog);
ohair@286 280 }
ohair@286 281
ohair@286 282 /**
ohair@286 283 * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml
ohair@286 284 */
ohair@286 285 public static EntityResolver createDefaultCatalogResolver() {
ohair@286 286
ohair@286 287 // set up a manager
ohair@286 288 CatalogManager manager = new CatalogManager();
ohair@286 289 manager.setIgnoreMissingProperties(true);
ohair@286 290 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 291 manager.setUseStaticCatalog(false);
ohair@286 292 // parse the catalog
ohair@286 293 ClassLoader cl = Thread.currentThread().getContextClassLoader();
ohair@286 294 Enumeration<URL> catalogEnum;
ohair@286 295 Catalog catalog = manager.getCatalog();
ohair@286 296 try {
ohair@286 297 if (cl == null) {
ohair@286 298 catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml");
ohair@286 299 } else {
ohair@286 300 catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml");
ohair@286 301 }
ohair@286 302
ohair@286 303 while(catalogEnum.hasMoreElements()) {
ohair@286 304 URL url = catalogEnum.nextElement();
ohair@286 305 catalog.parseCatalog(url);
ohair@286 306 }
ohair@286 307 } catch (IOException e) {
ohair@286 308 throw new WebServiceException(e);
ohair@286 309 }
ohair@286 310
ohair@286 311 return workaroundCatalogResolver(catalog);
ohair@286 312 }
ohair@286 313
ohair@286 314 /**
ohair@286 315 * Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when
ohair@286 316 * useStaticCatalog is false.
ohair@286 317 * This returns a CatalogResolver that uses the catalog passed as parameter.
ohair@286 318 * @param catalog
ohair@286 319 * @return CatalogResolver
ohair@286 320 */
ohair@286 321 private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) {
ohair@286 322 // set up a manager
ohair@286 323 CatalogManager manager = new CatalogManager() {
ohair@286 324 @Override
ohair@286 325 public Catalog getCatalog() {
ohair@286 326 return catalog;
ohair@286 327 }
ohair@286 328 };
ohair@286 329 manager.setIgnoreMissingProperties(true);
ohair@286 330 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 331 manager.setUseStaticCatalog(false);
ohair@286 332
ohair@286 333 return new CatalogResolver(manager);
ohair@286 334 }
ohair@286 335
ohair@286 336 /**
ohair@286 337 * {@link ErrorHandler} that always treat the error as fatal.
ohair@286 338 */
ohair@286 339 public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() {
alanb@368 340 @Override
ohair@286 341 public void warning(SAXParseException exception) {
ohair@286 342 }
ohair@286 343
alanb@368 344 @Override
ohair@286 345 public void error(SAXParseException exception) throws SAXException {
ohair@286 346 throw exception;
ohair@286 347 }
ohair@286 348
alanb@368 349 @Override
ohair@286 350 public void fatalError(SAXParseException exception) throws SAXException {
ohair@286 351 throw exception;
ohair@286 352 }
ohair@286 353 };
alanb@368 354
alanb@368 355 public static DocumentBuilderFactory newDocumentBuilderFactory() {
alanb@368 356 return newDocumentBuilderFactory(true);
alanb@368 357 }
alanb@368 358
alanb@368 359 public static DocumentBuilderFactory newDocumentBuilderFactory(boolean secureXmlProcessing) {
alanb@368 360 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
alanb@368 361 try {
alanb@368 362 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, checkGlobalOverride(secureXmlProcessing));
alanb@368 363 } catch (ParserConfigurationException e) {
alanb@368 364 LOGGER.log(Level.WARNING, "Factory [{}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } );
alanb@368 365 }
alanb@368 366 return factory;
alanb@368 367 }
alanb@368 368
alanb@368 369 public static TransformerFactory newTransformerFactory(boolean secureXmlProcessingEnabled) {
alanb@368 370 TransformerFactory factory = TransformerFactory.newInstance();
alanb@368 371 try {
alanb@368 372 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, checkGlobalOverride(secureXmlProcessingEnabled));
alanb@368 373 } catch (TransformerConfigurationException e) {
alanb@368 374 LOGGER.log(Level.WARNING, "Factory [{}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()});
alanb@368 375 }
alanb@368 376 return factory;
alanb@368 377 }
alanb@368 378
alanb@368 379 public static TransformerFactory newTransformerFactory() {
alanb@368 380 return newTransformerFactory(true);
alanb@368 381 }
alanb@368 382
alanb@368 383 public static SAXParserFactory newSAXParserFactory(boolean secureXmlProcessingEnabled) {
alanb@368 384 SAXParserFactory factory = SAXParserFactory.newInstance();
alanb@368 385 try {
alanb@368 386 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, checkGlobalOverride(secureXmlProcessingEnabled));
alanb@368 387 } catch (Exception e) {
alanb@368 388 LOGGER.log(Level.WARNING, "Factory [{}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()});
alanb@368 389 }
alanb@368 390 return factory;
alanb@368 391 }
alanb@368 392
alanb@368 393 public static XPathFactory newXPathFactory(boolean secureXmlProcessingEnabled) {
alanb@368 394 XPathFactory factory = XPathFactory.newInstance();
alanb@368 395 try {
alanb@368 396 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, checkGlobalOverride(secureXmlProcessingEnabled));
alanb@368 397 } catch (XPathFactoryConfigurationException e) {
alanb@368 398 LOGGER.log(Level.WARNING, "Factory [{}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } );
alanb@368 399 }
alanb@368 400 return factory;
alanb@368 401 }
alanb@368 402
alanb@368 403 public static XMLInputFactory newXMLInputFactory(boolean secureXmlProcessingEnabled) {
alanb@368 404 XMLInputFactory factory = XMLInputFactory.newInstance();
alanb@368 405 if (checkGlobalOverride(secureXmlProcessingEnabled)) {
alanb@368 406 // TODO-Miran: are those apppropriate defaults?
alanb@368 407 factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
alanb@368 408 factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
alanb@368 409 }
alanb@368 410 return factory;
alanb@368 411 }
alanb@368 412
alanb@368 413 private static boolean checkGlobalOverride(boolean localSecureXmlProcessingEnabled) {
alanb@368 414 return globalSecureXmlProcessingEnabled && localSecureXmlProcessingEnabled;
alanb@368 415 }
alanb@368 416
ohair@286 417 }

mercurial