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.tools.ws.wsdl.parser;
038:
039: import com.sun.tools.ws.wsdl.framework.ParseException;
040: import com.sun.xml.ws.util.xml.XmlUtil;
041: import org.w3c.dom.Comment;
042: import org.w3c.dom.Element;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.Text;
045:
046: import javax.xml.namespace.QName;
047: import java.io.File;
048: import java.net.MalformedURLException;
049: import java.net.URL;
050: import java.util.Iterator;
051:
052: /**2
053: * Defines various utility methods.
054: *
055: * @author WS Development Team
056: */
057: public class Util {
058:
059: public static String getRequiredAttribute(Element element,
060: String name) {
061: String result = XmlUtil.getAttributeOrNull(element, name);
062: if (result == null)
063: fail("parsing.missingRequiredAttribute", element
064: .getTagName(), name);
065: return result;
066: }
067:
068: public static void verifyTag(Element element, String tag) {
069: if (!element.getLocalName().equals(tag))
070: fail("parsing.invalidTag", element.getTagName(), tag);
071: }
072:
073: public static void verifyTagNS(Element element, String tag,
074: String nsURI) {
075: if (!element.getLocalName().equals(tag)
076: || (element.getNamespaceURI() != null && !element
077: .getNamespaceURI().equals(nsURI)))
078: fail("parsing.invalidTagNS", new Object[] {
079: element.getTagName(), element.getNamespaceURI(),
080: tag, nsURI });
081: }
082:
083: public static void verifyTagNS(Element element, QName name) {
084: if (!isTagName(element, name))
085: fail("parsing.invalidTagNS", new Object[] {
086: element.getTagName(), element.getNamespaceURI(),
087: name.getLocalPart(), name.getNamespaceURI() });
088: }
089:
090: public static boolean isTagName(Element element, QName name) {
091: return (element.getLocalName().equals(name.getLocalPart()) && (element
092: .getNamespaceURI() != null && element.getNamespaceURI()
093: .equals(name.getNamespaceURI())));
094:
095: }
096:
097: public static void verifyTagNSRootElement(Element element,
098: QName name) {
099: if (!element.getLocalName().equals(name.getLocalPart())
100: || (element.getNamespaceURI() != null && !element
101: .getNamespaceURI().equals(
102: name.getNamespaceURI())))
103: fail("parsing.incorrectRootElement", new Object[] {
104: element.getTagName(), element.getNamespaceURI(),
105: name.getLocalPart(), name.getNamespaceURI() });
106: }
107:
108: public static Element nextElementIgnoringCharacterContent(
109: Iterator iter) {
110: while (iter.hasNext()) {
111: Node n = (Node) iter.next();
112: if (n instanceof Text)
113: continue;
114: if (n instanceof Comment)
115: continue;
116: if (!(n instanceof Element))
117: fail("parsing.elementExpected");
118: return (Element) n;
119: }
120:
121: return null;
122: }
123:
124: public static Element nextElement(Iterator iter) {
125: while (iter.hasNext()) {
126: Node n = (Node) iter.next();
127: if (n instanceof Text) {
128: Text t = (Text) n;
129: if (t.getData().trim().length() == 0)
130: continue;
131: fail("parsing.nonWhitespaceTextFound", t.getData()
132: .trim());
133: }
134: if (n instanceof Comment)
135: continue;
136: if (!(n instanceof Element))
137: fail("parsing.elementExpected");
138: return (Element) n;
139: }
140:
141: return null;
142: }
143:
144: public static String processSystemIdWithBase(String baseSystemId,
145: String systemId) {
146: try {
147: URL base = null;
148: try {
149: base = new URL(baseSystemId);
150: } catch (MalformedURLException e) {
151: base = new File(baseSystemId).toURL();
152: }
153:
154: try {
155: URL url = new URL(base, systemId);
156: return url.toString();
157: } catch (MalformedURLException e) {
158: fail("parsing.invalidURI", systemId);
159: }
160:
161: } catch (MalformedURLException e) {
162: fail("parsing.invalidURI", baseSystemId);
163: }
164:
165: return null; // keep compiler happy
166: }
167:
168: public static void fail(String key) {
169: throw new ParseException(key);
170: }
171:
172: public static void fail(String key, String arg) {
173: throw new ParseException(key, arg);
174: }
175:
176: public static void fail(String key, String arg1, String arg2) {
177: throw new ParseException(key, new Object[] { arg1, arg2 });
178: }
179:
180: public static void fail(String key, Object[] args) {
181: throw new ParseException(key, args);
182: }
183: }
|