Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.ws.util.xml;
28 import com.sun.istack.internal.Nullable;
29 import com.sun.org.apache.xml.internal.resolver.Catalog;
30 import com.sun.org.apache.xml.internal.resolver.CatalogManager;
31 import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;
32 import com.sun.xml.internal.ws.server.ServerRtException;
33 import com.sun.xml.internal.ws.util.ByteArrayBuffer;
34 import org.w3c.dom.Attr;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.EntityReference;
37 import org.w3c.dom.Node;
38 import org.w3c.dom.NodeList;
39 import org.w3c.dom.Text;
40 import org.xml.sax.EntityResolver;
41 import org.xml.sax.ErrorHandler;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.SAXParseException;
44 import org.xml.sax.XMLReader;
45 import org.xml.sax.InputSource;
47 import javax.xml.namespace.QName;
48 import javax.xml.parsers.ParserConfigurationException;
49 import javax.xml.parsers.SAXParserFactory;
50 import javax.xml.transform.Result;
51 import javax.xml.transform.Source;
52 import javax.xml.transform.Transformer;
53 import javax.xml.transform.TransformerConfigurationException;
54 import javax.xml.transform.TransformerException;
55 import javax.xml.transform.TransformerFactory;
56 import javax.xml.transform.sax.SAXTransformerFactory;
57 import javax.xml.transform.sax.TransformerHandler;
58 import javax.xml.transform.stream.StreamSource;
59 import javax.xml.ws.WebServiceException;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.io.OutputStreamWriter;
63 import java.io.Writer;
64 import java.net.URL;
65 import java.util.ArrayList;
66 import java.util.Enumeration;
67 import java.util.Iterator;
68 import java.util.List;
69 import java.util.StringTokenizer;
71 /**
72 * @author WS Development Team
73 */
74 public class XmlUtil {
75 private final static String LEXICAL_HANDLER_PROPERTY =
76 "http://xml.org/sax/properties/lexical-handler";
78 public static String getPrefix(String s) {
79 int i = s.indexOf(':');
80 if (i == -1)
81 return null;
82 return s.substring(0, i);
83 }
85 public static String getLocalPart(String s) {
86 int i = s.indexOf(':');
87 if (i == -1)
88 return s;
89 return s.substring(i + 1);
90 }
94 public static String getAttributeOrNull(Element e, String name) {
95 Attr a = e.getAttributeNode(name);
96 if (a == null)
97 return null;
98 return a.getValue();
99 }
101 public static String getAttributeNSOrNull(
102 Element e,
103 String name,
104 String nsURI) {
105 Attr a = e.getAttributeNodeNS(nsURI, name);
106 if (a == null)
107 return null;
108 return a.getValue();
109 }
111 public static String getAttributeNSOrNull(
112 Element e,
113 QName name) {
114 Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name.getLocalPart());
115 if (a == null)
116 return null;
117 return a.getValue();
118 }
120 /* public static boolean matchesTagNS(Element e, String tag, String nsURI) {
121 try {
122 return e.getLocalName().equals(tag)
123 && e.getNamespaceURI().equals(nsURI);
124 } catch (NullPointerException npe) {
126 // localname not null since parsing would fail before here
127 throw new WSDLParseException(
128 "null.namespace.found",
129 e.getLocalName());
130 }
131 }
133 public static boolean matchesTagNS(
134 Element e,
135 javax.xml.namespace.QName name) {
136 try {
137 return e.getLocalName().equals(name.getLocalPart())
138 && e.getNamespaceURI().equals(name.getNamespaceURI());
139 } catch (NullPointerException npe) {
141 // localname not null since parsing would fail before here
142 throw new WSDLParseException(
143 "null.namespace.found",
144 e.getLocalName());
145 }
146 }*/
148 public static Iterator getAllChildren(Element element) {
149 return new NodeListIterator(element.getChildNodes());
150 }
152 public static Iterator getAllAttributes(Element element) {
153 return new NamedNodeMapIterator(element.getAttributes());
154 }
156 public static List<String> parseTokenList(String tokenList) {
157 List<String> result = new ArrayList<String>();
158 StringTokenizer tokenizer = new StringTokenizer(tokenList, " ");
159 while (tokenizer.hasMoreTokens()) {
160 result.add(tokenizer.nextToken());
161 }
162 return result;
163 }
165 public static String getTextForNode(Node node) {
166 StringBuffer sb = new StringBuffer();
168 NodeList children = node.getChildNodes();
169 if (children.getLength() == 0)
170 return null;
172 for (int i = 0; i < children.getLength(); ++i) {
173 Node n = children.item(i);
175 if (n instanceof Text)
176 sb.append(n.getNodeValue());
177 else if (n instanceof EntityReference) {
178 String s = getTextForNode(n);
179 if (s == null)
180 return null;
181 else
182 sb.append(s);
183 } else
184 return null;
185 }
187 return sb.toString();
188 }
190 public static InputStream getUTF8Stream(String s) {
191 try {
192 ByteArrayBuffer bab = new ByteArrayBuffer();
193 Writer w = new OutputStreamWriter(bab, "utf-8");
194 w.write(s);
195 w.close();
196 return bab.newInputStream();
197 } catch (IOException e) {
198 throw new RuntimeException("should not happen");
199 }
200 }
202 static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
204 static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
206 static {
207 saxParserFactory.setNamespaceAware(true);
208 }
210 /**
211 * Creates a new identity transformer.
212 */
213 public static Transformer newTransformer() {
214 try {
215 return transformerFactory.newTransformer();
216 } catch (TransformerConfigurationException tex) {
217 throw new IllegalStateException("Unable to create a JAXP transformer");
218 }
219 }
221 /**
222 * Performs identity transformation.
223 */
224 public static <T extends Result>
225 T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
226 if (src instanceof StreamSource) {
227 // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
228 // is not turned on by default
229 StreamSource ssrc = (StreamSource) src;
230 TransformerHandler th = ((SAXTransformerFactory) transformerFactory).newTransformerHandler();
231 th.setResult(result);
232 XMLReader reader = saxParserFactory.newSAXParser().getXMLReader();
233 reader.setContentHandler(th);
234 reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
235 reader.parse(toInputSource(ssrc));
236 } else {
237 newTransformer().transform(src, result);
238 }
239 return result;
240 }
242 private static InputSource toInputSource(StreamSource src) {
243 InputSource is = new InputSource();
244 is.setByteStream(src.getInputStream());
245 is.setCharacterStream(src.getReader());
246 is.setPublicId(src.getPublicId());
247 is.setSystemId(src.getSystemId());
248 return is;
249 }
251 /*
252 * Gets an EntityResolver using XML catalog
253 */
254 public static EntityResolver createEntityResolver(@Nullable URL catalogUrl) {
255 // set up a manager
256 CatalogManager manager = new CatalogManager();
257 manager.setIgnoreMissingProperties(true);
258 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
259 manager.setUseStaticCatalog(false);
260 Catalog catalog = manager.getCatalog();
261 try {
262 if (catalogUrl != null) {
263 catalog.parseCatalog(catalogUrl);
264 }
265 } catch (IOException e) {
266 throw new ServerRtException("server.rt.err",e);
267 }
268 return workaroundCatalogResolver(catalog);
269 }
271 /**
272 * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml
273 */
274 public static EntityResolver createDefaultCatalogResolver() {
276 // set up a manager
277 CatalogManager manager = new CatalogManager();
278 manager.setIgnoreMissingProperties(true);
279 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
280 manager.setUseStaticCatalog(false);
281 // parse the catalog
282 ClassLoader cl = Thread.currentThread().getContextClassLoader();
283 Enumeration<URL> catalogEnum;
284 Catalog catalog = manager.getCatalog();
285 try {
286 if (cl == null) {
287 catalogEnum = ClassLoader.getSystemResources("META-INF/jax-ws-catalog.xml");
288 } else {
289 catalogEnum = cl.getResources("META-INF/jax-ws-catalog.xml");
290 }
292 while(catalogEnum.hasMoreElements()) {
293 URL url = catalogEnum.nextElement();
294 catalog.parseCatalog(url);
295 }
296 } catch (IOException e) {
297 throw new WebServiceException(e);
298 }
300 return workaroundCatalogResolver(catalog);
301 }
303 /**
304 * Default CatalogResolver implementation is broken as it depends on CatalogManager.getCatalog() which will always create a new one when
305 * useStaticCatalog is false.
306 * This returns a CatalogResolver that uses the catalog passed as parameter.
307 * @param catalog
308 * @return CatalogResolver
309 */
310 private static CatalogResolver workaroundCatalogResolver(final Catalog catalog) {
311 // set up a manager
312 CatalogManager manager = new CatalogManager() {
313 @Override
314 public Catalog getCatalog() {
315 return catalog;
316 }
317 };
318 manager.setIgnoreMissingProperties(true);
319 // Using static catalog may result in to sharing of the catalog by multiple apps running in a container
320 manager.setUseStaticCatalog(false);
322 return new CatalogResolver(manager);
323 }
325 /**
326 * {@link ErrorHandler} that always treat the error as fatal.
327 */
328 public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() {
329 public void warning(SAXParseException exception) {
330 }
332 public void error(SAXParseException exception) throws SAXException {
333 throw exception;
334 }
336 public void fatalError(SAXParseException exception) throws SAXException {
337 throw exception;
338 }
339 };
340 }