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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, 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;
ohair@286 40 import org.xml.sax.EntityResolver;
ohair@286 41 import org.xml.sax.ErrorHandler;
ohair@286 42 import org.xml.sax.SAXException;
ohair@286 43 import org.xml.sax.SAXParseException;
ohair@286 44 import org.xml.sax.XMLReader;
ohair@286 45 import org.xml.sax.InputSource;
ohair@286 46
ohair@286 47 import javax.xml.namespace.QName;
ohair@286 48 import javax.xml.parsers.ParserConfigurationException;
ohair@286 49 import javax.xml.parsers.SAXParserFactory;
ohair@286 50 import javax.xml.transform.Result;
ohair@286 51 import javax.xml.transform.Source;
ohair@286 52 import javax.xml.transform.Transformer;
ohair@286 53 import javax.xml.transform.TransformerConfigurationException;
ohair@286 54 import javax.xml.transform.TransformerException;
ohair@286 55 import javax.xml.transform.TransformerFactory;
ohair@286 56 import javax.xml.transform.sax.SAXTransformerFactory;
ohair@286 57 import javax.xml.transform.sax.TransformerHandler;
ohair@286 58 import javax.xml.transform.stream.StreamSource;
ohair@286 59 import javax.xml.ws.WebServiceException;
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;
ohair@286 70
ohair@286 71 /**
ohair@286 72 * @author WS Development Team
ohair@286 73 */
ohair@286 74 public class XmlUtil {
ohair@286 75 private final static String LEXICAL_HANDLER_PROPERTY =
ohair@286 76 "http://xml.org/sax/properties/lexical-handler";
ohair@286 77
ohair@286 78 public static String getPrefix(String s) {
ohair@286 79 int i = s.indexOf(':');
ohair@286 80 if (i == -1)
ohair@286 81 return null;
ohair@286 82 return s.substring(0, i);
ohair@286 83 }
ohair@286 84
ohair@286 85 public static String getLocalPart(String s) {
ohair@286 86 int i = s.indexOf(':');
ohair@286 87 if (i == -1)
ohair@286 88 return s;
ohair@286 89 return s.substring(i + 1);
ohair@286 90 }
ohair@286 91
ohair@286 92
ohair@286 93
ohair@286 94 public static String getAttributeOrNull(Element e, String name) {
ohair@286 95 Attr a = e.getAttributeNode(name);
ohair@286 96 if (a == null)
ohair@286 97 return null;
ohair@286 98 return a.getValue();
ohair@286 99 }
ohair@286 100
ohair@286 101 public static String getAttributeNSOrNull(
ohair@286 102 Element e,
ohair@286 103 String name,
ohair@286 104 String nsURI) {
ohair@286 105 Attr a = e.getAttributeNodeNS(nsURI, name);
ohair@286 106 if (a == null)
ohair@286 107 return null;
ohair@286 108 return a.getValue();
ohair@286 109 }
ohair@286 110
ohair@286 111 public static String getAttributeNSOrNull(
ohair@286 112 Element e,
ohair@286 113 QName name) {
ohair@286 114 Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
ohair@286 115 if (a == null)
ohair@286 116 return null;
ohair@286 117 return a.getValue();
ohair@286 118 }
ohair@286 119
ohair@286 120 /* public static boolean matchesTagNS(Element e, String tag, String nsURI) {
ohair@286 121 try {
ohair@286 122 return e.getLocalName().equals(tag)
ohair@286 123 && e.getNamespaceURI().equals(nsURI);
ohair@286 124 } catch (NullPointerException npe) {
ohair@286 125
ohair@286 126 // localname not null since parsing would fail before here
ohair@286 127 throw new WSDLParseException(
ohair@286 128 "null.namespace.found",
ohair@286 129 e.getLocalName());
ohair@286 130 }
ohair@286 131 }
ohair@286 132
ohair@286 133 public static boolean matchesTagNS(
ohair@286 134 Element e,
ohair@286 135 javax.xml.namespace.QName name) {
ohair@286 136 try {
ohair@286 137 return e.getLocalName().equals(name.getLocalPart())
ohair@286 138 && e.getNamespaceURI().equals(name.getNamespaceURI());
ohair@286 139 } catch (NullPointerException npe) {
ohair@286 140
ohair@286 141 // localname not null since parsing would fail before here
ohair@286 142 throw new WSDLParseException(
ohair@286 143 "null.namespace.found",
ohair@286 144 e.getLocalName());
ohair@286 145 }
ohair@286 146 }*/
ohair@286 147
ohair@286 148 public static Iterator getAllChildren(Element element) {
ohair@286 149 return new NodeListIterator(element.getChildNodes());
ohair@286 150 }
ohair@286 151
ohair@286 152 public static Iterator getAllAttributes(Element element) {
ohair@286 153 return new NamedNodeMapIterator(element.getAttributes());
ohair@286 154 }
ohair@286 155
ohair@286 156 public static List<String> parseTokenList(String tokenList) {
ohair@286 157 List<String> result = new ArrayList<String>();
ohair@286 158 StringTokenizer tokenizer = new StringTokenizer(tokenList, " ");
ohair@286 159 while (tokenizer.hasMoreTokens()) {
ohair@286 160 result.add(tokenizer.nextToken());
ohair@286 161 }
ohair@286 162 return result;
ohair@286 163 }
ohair@286 164
ohair@286 165 public static String getTextForNode(Node node) {
ohair@286 166 StringBuffer sb = new StringBuffer();
ohair@286 167
ohair@286 168 NodeList children = node.getChildNodes();
ohair@286 169 if (children.getLength() == 0)
ohair@286 170 return null;
ohair@286 171
ohair@286 172 for (int i = 0; i < children.getLength(); ++i) {
ohair@286 173 Node n = children.item(i);
ohair@286 174
ohair@286 175 if (n instanceof Text)
ohair@286 176 sb.append(n.getNodeValue());
ohair@286 177 else if (n instanceof EntityReference) {
ohair@286 178 String s = getTextForNode(n);
ohair@286 179 if (s == null)
ohair@286 180 return null;
ohair@286 181 else
ohair@286 182 sb.append(s);
ohair@286 183 } else
ohair@286 184 return null;
ohair@286 185 }
ohair@286 186
ohair@286 187 return sb.toString();
ohair@286 188 }
ohair@286 189
ohair@286 190 public static InputStream getUTF8Stream(String s) {
ohair@286 191 try {
ohair@286 192 ByteArrayBuffer bab = new ByteArrayBuffer();
ohair@286 193 Writer w = new OutputStreamWriter(bab, "utf-8");
ohair@286 194 w.write(s);
ohair@286 195 w.close();
ohair@286 196 return bab.newInputStream();
ohair@286 197 } catch (IOException e) {
ohair@286 198 throw new RuntimeException("should not happen");
ohair@286 199 }
ohair@286 200 }
ohair@286 201
ohair@286 202 static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
ohair@286 203
ohair@286 204 static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
ohair@286 205
ohair@286 206 static {
ohair@286 207 saxParserFactory.setNamespaceAware(true);
ohair@286 208 }
ohair@286 209
ohair@286 210 /**
ohair@286 211 * Creates a new identity transformer.
ohair@286 212 */
ohair@286 213 public static Transformer newTransformer() {
ohair@286 214 try {
ohair@286 215 return transformerFactory.newTransformer();
ohair@286 216 } catch (TransformerConfigurationException tex) {
ohair@286 217 throw new IllegalStateException("Unable to create a JAXP transformer");
ohair@286 218 }
ohair@286 219 }
ohair@286 220
ohair@286 221 /**
ohair@286 222 * Performs identity transformation.
ohair@286 223 */
ohair@286 224 public static <T extends Result>
ohair@286 225 T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
ohair@286 226 if (src instanceof StreamSource) {
ohair@286 227 // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
ohair@286 228 // is not turned on by default
ohair@286 229 StreamSource ssrc = (StreamSource) src;
ohair@286 230 TransformerHandler th = ((SAXTransformerFactory) transformerFactory).newTransformerHandler();
ohair@286 231 th.setResult(result);
ohair@286 232 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
ohair@286 233 reader.setContentHandler(th);
ohair@286 234 reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
ohair@286 235 reader.parse(toInputSource(ssrc));
ohair@286 236 } else {
ohair@286 237 newTransformer().transform(src, result);
ohair@286 238 }
ohair@286 239 return result;
ohair@286 240 }
ohair@286 241
ohair@286 242 private static InputSource toInputSource(StreamSource src) {
ohair@286 243 InputSource is = new InputSource();
ohair@286 244 is.setByteStream(src.getInputStream());
ohair@286 245 is.setCharacterStream(src.getReader());
ohair@286 246 is.setPublicId(src.getPublicId());
ohair@286 247 is.setSystemId(src.getSystemId());
ohair@286 248 return is;
ohair@286 249 }
ohair@286 250
ohair@286 251 /*
ohair@286 252 * Gets an EntityResolver using XML catalog
ohair@286 253 */
ohair@286 254 public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) {
ohair@286 255 // set up a manager
ohair@286 256 CatalogManager manager = new CatalogManager();
ohair@286 257 manager.setIgnoreMissingProperties(true);
ohair@286 258 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 259 manager.setUseStaticCatalog(false);
ohair@286 260 Catalog catalog = manager.getCatalog();
ohair@286 261 try {
ohair@286 262 if (catalogUrl != null) {
ohair@286 263 catalog.parseCatalog(catalogUrl);
ohair@286 264 }
ohair@286 265 } catch (IOException e) {
ohair@286 266 throw new ServerRtException("server.rt.err",e);
ohair@286 267 }
ohair@286 268 return workaroundCatalogResolver(catalog);
ohair@286 269 }
ohair@286 270
ohair@286 271 /**
ohair@286 272 * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml
ohair@286 273 */
ohair@286 274 public static EntityResolver createDefaultCatalogResolver() {
ohair@286 275
ohair@286 276 // set up a manager
ohair@286 277 CatalogManager manager = new CatalogManager();
ohair@286 278 manager.setIgnoreMissingProperties(true);
ohair@286 279 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 280 manager.setUseStaticCatalog(false);
ohair@286 281 // parse the catalog
ohair@286 282 ClassLoader cl = Thread.currentThread().getContextClassLoader();
ohair@286 283 Enumeration<URL> catalogEnum;
ohair@286 284 Catalog catalog = manager.getCatalog();
ohair@286 285 try {
ohair@286 286 if (cl == null) {
ohair@286 287 catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml");
ohair@286 288 } else {
ohair@286 289 catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml");
ohair@286 290 }
ohair@286 291
ohair@286 292 while(catalogEnum.hasMoreElements()) {
ohair@286 293 URL url = catalogEnum.nextElement();
ohair@286 294 catalog.parseCatalog(url);
ohair@286 295 }
ohair@286 296 } catch (IOException e) {
ohair@286 297 throw new WebServiceException(e);
ohair@286 298 }
ohair@286 299
ohair@286 300 return workaroundCatalogResolver(catalog);
ohair@286 301 }
ohair@286 302
ohair@286 303 /**
ohair@286 304 * Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when
ohair@286 305 * useStaticCatalog is false.
ohair@286 306 * This returns a CatalogResolver that uses the catalog passed as parameter.
ohair@286 307 * @param catalog
ohair@286 308 * @return CatalogResolver
ohair@286 309 */
ohair@286 310 private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) {
ohair@286 311 // set up a manager
ohair@286 312 CatalogManager manager = new CatalogManager() {
ohair@286 313 @Override
ohair@286 314 public Catalog getCatalog() {
ohair@286 315 return catalog;
ohair@286 316 }
ohair@286 317 };
ohair@286 318 manager.setIgnoreMissingProperties(true);
ohair@286 319 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
ohair@286 320 manager.setUseStaticCatalog(false);
ohair@286 321
ohair@286 322 return new CatalogResolver(manager);
ohair@286 323 }
ohair@286 324
ohair@286 325 /**
ohair@286 326 * {@link ErrorHandler} that always treat the error as fatal.
ohair@286 327 */
ohair@286 328 public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() {
ohair@286 329 public void warning(SAXParseException exception) {
ohair@286 330 }
ohair@286 331
ohair@286 332 public void error(SAXParseException exception) throws SAXException {
ohair@286 333 throw exception;
ohair@286 334 }
ohair@286 335
ohair@286 336 public void fatalError(SAXParseException exception) throws SAXException {
ohair@286 337 throw exception;
ohair@286 338 }
ohair@286 339 };
ohair@286 340 }

mercurial