001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.util.xml;
038:
039: import com.sun.istack.Nullable;
040: import com.sun.org.apache.xml.internal.resolver.CatalogManager;
041: import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver;
042: import com.sun.xml.ws.server.ServerRtException;
043: import com.sun.xml.ws.util.ByteArrayBuffer;
044: import org.w3c.dom.Attr;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.EntityReference;
047: import org.w3c.dom.Node;
048: import org.w3c.dom.NodeList;
049: import org.w3c.dom.Text;
050: import org.xml.sax.EntityResolver;
051: import org.xml.sax.ErrorHandler;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.SAXParseException;
054:
055: import javax.xml.namespace.QName;
056: import javax.xml.transform.Transformer;
057: import javax.xml.transform.TransformerConfigurationException;
058: import javax.xml.transform.TransformerFactory;
059: import javax.xml.ws.WebServiceException;
060: import java.io.IOException;
061: import java.io.InputStream;
062: import java.io.OutputStreamWriter;
063: import java.io.Writer;
064: import java.net.URL;
065: import java.util.ArrayList;
066: import java.util.Enumeration;
067: import java.util.Iterator;
068: import java.util.List;
069: import java.util.StringTokenizer;
070:
071: /**
072: * @author WS Development Team
073: */
074: public class XmlUtil {
075:
076: public static String getPrefix(String s) {
077: int i = s.indexOf(':');
078: if (i == -1)
079: return null;
080: return s.substring(0, i);
081: }
082:
083: public static String getLocalPart(String s) {
084: int i = s.indexOf(':');
085: if (i == -1)
086: return s;
087: return s.substring(i + 1);
088: }
089:
090: public static String getAttributeOrNull(Element e, String name) {
091: Attr a = e.getAttributeNode(name);
092: if (a == null)
093: return null;
094: return a.getValue();
095: }
096:
097: public static String getAttributeNSOrNull(Element e, String name,
098: String nsURI) {
099: Attr a = e.getAttributeNodeNS(nsURI, name);
100: if (a == null)
101: return null;
102: return a.getValue();
103: }
104:
105: public static String getAttributeNSOrNull(Element e, QName name) {
106: Attr a = e.getAttributeNodeNS(name.getNamespaceURI(), name
107: .getLocalPart());
108: if (a == null)
109: return null;
110: return a.getValue();
111: }
112:
113: /* public static boolean matchesTagNS(Element e, String tag, String nsURI) {
114: try {
115: return e.getLocalName().equals(tag)
116: && e.getNamespaceURI().equals(nsURI);
117: } catch (NullPointerException npe) {
118:
119: // localname not null since parsing would fail before here
120: throw new WSDLParseException(
121: "null.namespace.found",
122: e.getLocalName());
123: }
124: }
125:
126: public static boolean matchesTagNS(
127: Element e,
128: javax.xml.namespace.QName name) {
129: try {
130: return e.getLocalName().equals(name.getLocalPart())
131: && e.getNamespaceURI().equals(name.getNamespaceURI());
132: } catch (NullPointerException npe) {
133:
134: // localname not null since parsing would fail before here
135: throw new WSDLParseException(
136: "null.namespace.found",
137: e.getLocalName());
138: }
139: }*/
140:
141: public static Iterator getAllChildren(Element element) {
142: return new NodeListIterator(element.getChildNodes());
143: }
144:
145: public static Iterator getAllAttributes(Element element) {
146: return new NamedNodeMapIterator(element.getAttributes());
147: }
148:
149: public static List<String> parseTokenList(String tokenList) {
150: List<String> result = new ArrayList<String>();
151: StringTokenizer tokenizer = new StringTokenizer(tokenList, " ");
152: while (tokenizer.hasMoreTokens()) {
153: result.add(tokenizer.nextToken());
154: }
155: return result;
156: }
157:
158: public static String getTextForNode(Node node) {
159: StringBuffer sb = new StringBuffer();
160:
161: NodeList children = node.getChildNodes();
162: if (children.getLength() == 0)
163: return null;
164:
165: for (int i = 0; i < children.getLength(); ++i) {
166: Node n = children.item(i);
167:
168: if (n instanceof Text)
169: sb.append(n.getNodeValue());
170: else if (n instanceof EntityReference) {
171: String s = getTextForNode(n);
172: if (s == null)
173: return null;
174: else
175: sb.append(s);
176: } else
177: return null;
178: }
179:
180: return sb.toString();
181: }
182:
183: public static InputStream getUTF8Stream(String s) {
184: try {
185: ByteArrayBuffer bab = new ByteArrayBuffer();
186: Writer w = new OutputStreamWriter(bab, "utf-8");
187: w.write(s);
188: w.close();
189: return bab.newInputStream();
190: } catch (IOException e) {
191: throw new RuntimeException("should not happen");
192: }
193: }
194:
195: static final TransformerFactory transformerFactory = TransformerFactory
196: .newInstance();
197:
198: /**
199: * Creates a new identity transformer.
200: */
201: public static Transformer newTransformer() {
202: try {
203: return transformerFactory.newTransformer();
204: } catch (TransformerConfigurationException tex) {
205: throw new IllegalStateException(
206: "Unable to create a JAXP transformer");
207: }
208: }
209:
210: /*
211: * Gets an EntityResolver using XML catalog
212: */
213: public static EntityResolver createEntityResolver(@Nullable
214: URL catalogUrl) {
215: // set up a manager
216: CatalogManager manager = new CatalogManager();
217: manager.setIgnoreMissingProperties(true);
218: try {
219: if (catalogUrl != null) {
220: manager.getCatalog().parseCatalog(catalogUrl);
221: }
222: } catch (IOException e) {
223: throw new ServerRtException("server.rt.err", e);
224: }
225: return new CatalogResolver(manager);
226: }
227:
228: /**
229: * Gets a default EntityResolver for catalog at META-INF/jaxws-catalog.xml
230: */
231: public static EntityResolver createDefaultCatalogResolver() {
232:
233: // set up a manager
234: CatalogManager manager = new CatalogManager();
235: manager.setIgnoreMissingProperties(true);
236:
237: // parse the catalog
238: ClassLoader cl = Thread.currentThread().getContextClassLoader();
239: Enumeration<URL> catalogEnum;
240: try {
241: if (cl == null) {
242: catalogEnum = ClassLoader
243: .getSystemResources("META-INF/jax-ws-catalog.xml");
244: } else {
245: catalogEnum = cl
246: .getResources("META-INF/jax-ws-catalog.xml");
247: }
248:
249: while (catalogEnum.hasMoreElements()) {
250: URL url = catalogEnum.nextElement();
251: manager.getCatalog().parseCatalog(url);
252: }
253: } catch (IOException e) {
254: throw new WebServiceException(e);
255: }
256:
257: return new CatalogResolver(manager);
258: }
259:
260: /**
261: * {@link ErrorHandler} that always treat the error as fatal.
262: */
263: public static final ErrorHandler DRACONIAN_ERROR_HANDLER = new ErrorHandler() {
264: public void warning(SAXParseException exception) {
265: }
266:
267: public void error(SAXParseException exception)
268: throws SAXException {
269: throw exception;
270: }
271:
272: public void fatalError(SAXParseException exception)
273: throws SAXException {
274: throw exception;
275: }
276: };
277: }
|