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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/xml/XmlUtil.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,340 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.util.xml;
    1.30 +
    1.31 +import com.sun.istack.internal.Nullable;
    1.32 +import com.sun.org.apache.xml.internal.resolver.Catalog;
    1.33 +import com.sun.org.apache.xml.internal.resolver.CatalogManager;
    1.34 +import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;
    1.35 +import com.sun.xml.internal.ws.server.ServerRtException;
    1.36 +import com.sun.xml.internal.ws.util.ByteArrayBuffer;
    1.37 +import org.w3c.dom.Attr;
    1.38 +import org.w3c.dom.Element;
    1.39 +import org.w3c.dom.EntityReference;
    1.40 +import org.w3c.dom.Node;
    1.41 +import org.w3c.dom.NodeList;
    1.42 +import org.w3c.dom.Text;
    1.43 +import org.xml.sax.EntityResolver;
    1.44 +import org.xml.sax.ErrorHandler;
    1.45 +import org.xml.sax.SAXException;
    1.46 +import org.xml.sax.SAXParseException;
    1.47 +import org.xml.sax.XMLReader;
    1.48 +import org.xml.sax.InputSource;
    1.49 +
    1.50 +import javax.xml.namespace.QName;
    1.51 +import javax.xml.parsers.ParserConfigurationException;
    1.52 +import javax.xml.parsers.SAXParserFactory;
    1.53 +import javax.xml.transform.Result;
    1.54 +import javax.xml.transform.Source;
    1.55 +import javax.xml.transform.Transformer;
    1.56 +import javax.xml.transform.TransformerConfigurationException;
    1.57 +import javax.xml.transform.TransformerException;
    1.58 +import javax.xml.transform.TransformerFactory;
    1.59 +import javax.xml.transform.sax.SAXTransformerFactory;
    1.60 +import javax.xml.transform.sax.TransformerHandler;
    1.61 +import javax.xml.transform.stream.StreamSource;
    1.62 +import javax.xml.ws.WebServiceException;
    1.63 +import java.io.IOException;
    1.64 +import java.io.InputStream;
    1.65 +import java.io.OutputStreamWriter;
    1.66 +import java.io.Writer;
    1.67 +import java.net.URL;
    1.68 +import java.util.ArrayList;
    1.69 +import java.util.Enumeration;
    1.70 +import java.util.Iterator;
    1.71 +import java.util.List;
    1.72 +import java.util.StringTokenizer;
    1.73 +
    1.74 +/**
    1.75 + * @author WS Development Team
    1.76 + */
    1.77 +public class XmlUtil {
    1.78 +    private final static String LEXICAL_HANDLER_PROPERTY =
    1.79 +        "http://xml.org/sax/properties/lexical-handler";
    1.80 +
    1.81 +    public static String getPrefix(String s) {
    1.82 +        int i = s.indexOf(':');
    1.83 +        if (i == -1)
    1.84 +            return null;
    1.85 +        return s.substring(0, i);
    1.86 +    }
    1.87 +
    1.88 +    public static String getLocalPart(String s) {
    1.89 +        int i = s.indexOf(':');
    1.90 +        if (i == -1)
    1.91 +            return s;
    1.92 +        return s.substring(i + 1);
    1.93 +    }
    1.94 +
    1.95 +
    1.96 +
    1.97 +    public static String getAttributeOrNull(Element e, String name) {
    1.98 +        Attr a = e.getAttributeNode(name);
    1.99 +        if (a == null)
   1.100 +            return null;
   1.101 +        return a.getValue();
   1.102 +    }
   1.103 +
   1.104 +    public static String getAttributeNSOrNull(
   1.105 +        Element e,
   1.106 +        String name,
   1.107 +        String nsURI) {
   1.108 +        Attr a = e.getAttributeNodeNS(nsURI, name);
   1.109 +        if (a == null)
   1.110 +            return null;
   1.111 +        return a.getValue();
   1.112 +    }
   1.113 +
   1.114 +    public static String getAttributeNSOrNull(
   1.115 +        Element e,
   1.116 +        QName name) {
   1.117 +        Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
   1.118 +        if (a == null)
   1.119 +            return null;
   1.120 +        return a.getValue();
   1.121 +    }
   1.122 +
   1.123 +/*    public static boolean matchesTagNS(Element e, String tag, String nsURI) {
   1.124 +        try {
   1.125 +            return e.getLocalName().equals(tag)
   1.126 +                && e.getNamespaceURI().equals(nsURI);
   1.127 +        } catch (NullPointerException npe) {
   1.128 +
   1.129 +            // localname not null since parsing would fail before here
   1.130 +            throw new WSDLParseException(
   1.131 +                "null.namespace.found",
   1.132 +                e.getLocalName());
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    public static boolean matchesTagNS(
   1.137 +        Element e,
   1.138 +        javax.xml.namespace.QName name) {
   1.139 +        try {
   1.140 +            return e.getLocalName().equals(name.getLocalPart())
   1.141 +                && e.getNamespaceURI().equals(name.getNamespaceURI());
   1.142 +        } catch (NullPointerException npe) {
   1.143 +
   1.144 +            // localname not null since parsing would fail before here
   1.145 +            throw new WSDLParseException(
   1.146 +                "null.namespace.found",
   1.147 +                e.getLocalName());
   1.148 +        }
   1.149 +    }*/
   1.150 +
   1.151 +    public static Iterator getAllChildren(Element element) {
   1.152 +        return new NodeListIterator(element.getChildNodes());
   1.153 +    }
   1.154 +
   1.155 +    public static Iterator getAllAttributes(Element element) {
   1.156 +        return new NamedNodeMapIterator(element.getAttributes());
   1.157 +    }
   1.158 +
   1.159 +    public static List<String> parseTokenList(String tokenList) {
   1.160 +        List<String> result = new ArrayList<String>();
   1.161 +        StringTokenizer tokenizer = new StringTokenizer(tokenList, " ");
   1.162 +        while (tokenizer.hasMoreTokens()) {
   1.163 +            result.add(tokenizer.nextToken());
   1.164 +        }
   1.165 +        return result;
   1.166 +    }
   1.167 +
   1.168 +    public static String getTextForNode(Node node) {
   1.169 +        StringBuffer sb = new StringBuffer();
   1.170 +
   1.171 +        NodeList children = node.getChildNodes();
   1.172 +        if (children.getLength() == 0)
   1.173 +            return null;
   1.174 +
   1.175 +        for (int i = 0; i < children.getLength(); ++i) {
   1.176 +            Node n = children.item(i);
   1.177 +
   1.178 +            if (n instanceof Text)
   1.179 +                sb.append(n.getNodeValue());
   1.180 +            else if (n instanceof EntityReference) {
   1.181 +                String s = getTextForNode(n);
   1.182 +                if (s == null)
   1.183 +                    return null;
   1.184 +                else
   1.185 +                    sb.append(s);
   1.186 +            } else
   1.187 +                return null;
   1.188 +        }
   1.189 +
   1.190 +        return sb.toString();
   1.191 +    }
   1.192 +
   1.193 +    public static InputStream getUTF8Stream(String s) {
   1.194 +        try {
   1.195 +            ByteArrayBuffer bab = new ByteArrayBuffer();
   1.196 +            Writer w = new OutputStreamWriter(bab, "utf-8");
   1.197 +            w.write(s);
   1.198 +            w.close();
   1.199 +            return bab.newInputStream();
   1.200 +        } catch (IOException e) {
   1.201 +            throw new RuntimeException("should not happen");
   1.202 +        }
   1.203 +    }
   1.204 +
   1.205 +    static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
   1.206 +
   1.207 +    static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
   1.208 +
   1.209 +    static {
   1.210 +        saxParserFactory.setNamespaceAware(true);
   1.211 +    }
   1.212 +
   1.213 +    /**
   1.214 +     * Creates a new identity transformer.
   1.215 +     */
   1.216 +    public static Transformer newTransformer() {
   1.217 +        try {
   1.218 +            return transformerFactory.newTransformer();
   1.219 +        } catch (TransformerConfigurationException tex) {
   1.220 +            throw new IllegalStateException("Unable to create a JAXP transformer");
   1.221 +        }
   1.222 +    }
   1.223 +
   1.224 +    /**
   1.225 +     * Performs identity transformation.
   1.226 +     */
   1.227 +    public static <T extends Result>
   1.228 +    T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
   1.229 +        if (src instanceof StreamSource) {
   1.230 +            // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
   1.231 +            // is not turned on by default
   1.232 +            StreamSource ssrc = (StreamSource) src;
   1.233 +            TransformerHandler th = ((SAXTransformerFactory) transformerFactory).newTransformerHandler();
   1.234 +            th.setResult(result);
   1.235 +            XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
   1.236 +            reader.setContentHandler(th);
   1.237 +            reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
   1.238 +            reader.parse(toInputSource(ssrc));
   1.239 +        } else {
   1.240 +            newTransformer().transform(src, result);
   1.241 +        }
   1.242 +        return result;
   1.243 +    }
   1.244 +
   1.245 +    private static InputSource toInputSource(StreamSource src) {
   1.246 +        InputSource is = new InputSource();
   1.247 +        is.setByteStream(src.getInputStream());
   1.248 +        is.setCharacterStream(src.getReader());
   1.249 +        is.setPublicId(src.getPublicId());
   1.250 +        is.setSystemId(src.getSystemId());
   1.251 +        return is;
   1.252 +    }
   1.253 +
   1.254 +    /*
   1.255 +    * Gets an EntityResolver using XML catalog
   1.256 +    */
   1.257 +     public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) {
   1.258 +        // set up a manager
   1.259 +        CatalogManager manager = new CatalogManager();
   1.260 +        manager.setIgnoreMissingProperties(true);
   1.261 +        // Using static catalog may  result in to sharing of the catalog by multiple apps running in a container
   1.262 +        manager.setUseStaticCatalog(false);
   1.263 +        Catalog catalog = manager.getCatalog();
   1.264 +        try {
   1.265 +            if (catalogUrl != null) {
   1.266 +                catalog.parseCatalog(catalogUrl);
   1.267 +            }
   1.268 +        } catch (IOException e) {
   1.269 +            throw new ServerRtException("server.rt.err",e);
   1.270 +        }
   1.271 +        return workaroundCatalogResolver(catalog);
   1.272 +    }
   1.273 +
   1.274 +    /**
   1.275 +     * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml
   1.276 +     */
   1.277 +    public static EntityResolver createDefaultCatalogResolver() {
   1.278 +
   1.279 +        // set up a manager
   1.280 +        CatalogManager manager = new CatalogManager();
   1.281 +        manager.setIgnoreMissingProperties(true);
   1.282 +        // Using static catalog may  result in to sharing of the catalog by multiple apps running in a container
   1.283 +        manager.setUseStaticCatalog(false);
   1.284 +        // parse the catalog
   1.285 +        ClassLoader cl = Thread.currentThread().getContextClassLoader();
   1.286 +        Enumeration<URL> catalogEnum;
   1.287 +        Catalog catalog = manager.getCatalog();
   1.288 +        try {
   1.289 +            if (cl == null) {
   1.290 +                catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml");
   1.291 +            } else {
   1.292 +                catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml");
   1.293 +            }
   1.294 +
   1.295 +            while(catalogEnum.hasMoreElements()) {
   1.296 +                URL url = catalogEnum.nextElement();
   1.297 +                catalog.parseCatalog(url);
   1.298 +            }
   1.299 +        } catch (IOException e) {
   1.300 +            throw new WebServiceException(e);
   1.301 +        }
   1.302 +
   1.303 +        return workaroundCatalogResolver(catalog);
   1.304 +    }
   1.305 +
   1.306 +    /**
   1.307 +     *  Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when
   1.308 +     *  useStaticCatalog is false.
   1.309 +     *  This returns a CatalogResolver that uses the catalog passed as parameter.
   1.310 +     * @param catalog
   1.311 +     * @return  CatalogResolver
   1.312 +     */
   1.313 +    private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) {
   1.314 +        // set up a manager
   1.315 +        CatalogManager manager = new CatalogManager() {
   1.316 +            @Override
   1.317 +            public Catalog getCatalog() {
   1.318 +                return catalog;
   1.319 +            }
   1.320 +        };
   1.321 +        manager.setIgnoreMissingProperties(true);
   1.322 +        // Using static catalog may  result in to sharing of the catalog by multiple apps running in a container
   1.323 +        manager.setUseStaticCatalog(false);
   1.324 +
   1.325 +        return new CatalogResolver(manager);
   1.326 +    }
   1.327 +
   1.328 +    /**
   1.329 +     * {@link ErrorHandler} that always treat the error as fatal.
   1.330 +     */
   1.331 +    public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() {
   1.332 +        public void warning(SAXParseException exception) {
   1.333 +        }
   1.334 +
   1.335 +        public void error(SAXParseException exception) throws SAXException {
   1.336 +            throw exception;
   1.337 +        }
   1.338 +
   1.339 +        public void fatalError(SAXParseException exception) throws SAXException {
   1.340 +            throw exception;
   1.341 +        }
   1.342 +    };
   1.343 +}

mercurial