001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.parser;
027:
028: import java.io.File;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.util.Iterator;
032:
033: import javax.xml.namespace.QName;
034:
035: import org.w3c.dom.Comment;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.Text;
039:
040: import com.sun.tools.internal.ws.wsdl.framework.ParseException;
041: import com.sun.xml.internal.ws.util.xml.XmlUtil;
042:
043: /**2
044: * Defines various utility methods.
045: *
046: * @author WS Development Team
047: */
048: public class Util {
049:
050: public static String getRequiredAttribute(Element element,
051: String name) {
052: String result = XmlUtil.getAttributeOrNull(element, name);
053: if (result == null)
054: fail("parsing.missingRequiredAttribute", element
055: .getTagName(), name);
056: return result;
057: }
058:
059: public static void verifyTag(Element element, String tag) {
060: if (!element.getLocalName().equals(tag))
061: fail("parsing.invalidTag", element.getTagName(), tag);
062: }
063:
064: public static void verifyTagNS(Element element, String tag,
065: String nsURI) {
066: if (!element.getLocalName().equals(tag)
067: || (element.getNamespaceURI() != null && !element
068: .getNamespaceURI().equals(nsURI)))
069: fail("parsing.invalidTagNS", new Object[] {
070: element.getTagName(), element.getNamespaceURI(),
071: tag, nsURI });
072: }
073:
074: public static void verifyTagNS(Element element, QName name) {
075: if (!element.getLocalName().equals(name.getLocalPart())
076: || (element.getNamespaceURI() != null && !element
077: .getNamespaceURI().equals(
078: name.getNamespaceURI())))
079: fail("parsing.invalidTagNS", new Object[] {
080: element.getTagName(), element.getNamespaceURI(),
081: name.getLocalPart(), name.getNamespaceURI() });
082: }
083:
084: public static void verifyTagNSRootElement(Element element,
085: QName name) {
086: if (!element.getLocalName().equals(name.getLocalPart())
087: || (element.getNamespaceURI() != null && !element
088: .getNamespaceURI().equals(
089: name.getNamespaceURI())))
090: fail("parsing.incorrectRootElement", new Object[] {
091: element.getTagName(), element.getNamespaceURI(),
092: name.getLocalPart(), name.getNamespaceURI() });
093: }
094:
095: public static Element nextElementIgnoringCharacterContent(
096: Iterator iter) {
097: while (iter.hasNext()) {
098: Node n = (Node) iter.next();
099: if (n instanceof Text)
100: continue;
101: if (n instanceof Comment)
102: continue;
103: if (!(n instanceof Element))
104: fail("parsing.elementExpected");
105: return (Element) n;
106: }
107:
108: return null;
109: }
110:
111: public static Element nextElement(Iterator iter) {
112: while (iter.hasNext()) {
113: Node n = (Node) iter.next();
114: if (n instanceof Text) {
115: Text t = (Text) n;
116: if (t.getData().trim().length() == 0)
117: continue;
118: fail("parsing.nonWhitespaceTextFound", t.getData()
119: .trim());
120: }
121: if (n instanceof Comment)
122: continue;
123: if (!(n instanceof Element))
124: fail("parsing.elementExpected");
125: return (Element) n;
126: }
127:
128: return null;
129: }
130:
131: public static String processSystemIdWithBase(String baseSystemId,
132: String systemId) {
133: try {
134: URL base = null;
135: try {
136: base = new URL(baseSystemId);
137: } catch (MalformedURLException e) {
138: base = new File(baseSystemId).toURL();
139: }
140:
141: try {
142: URL url = new URL(base, systemId);
143: return url.toString();
144: } catch (MalformedURLException e) {
145: fail("parsing.invalidURI", systemId);
146: }
147:
148: } catch (MalformedURLException e) {
149: fail("parsing.invalidURI", baseSystemId);
150: }
151:
152: return null; // keep compiler happy
153: }
154:
155: public static void fail(String key) {
156: throw new ParseException(key);
157: }
158:
159: public static void fail(String key, String arg) {
160: throw new ParseException(key, arg);
161: }
162:
163: public static void fail(String key, String arg1, String arg2) {
164: throw new ParseException(key, new Object[] { arg1, arg2 });
165: }
166:
167: public static void fail(String key, Object[] args) {
168: throw new ParseException(key, args);
169: }
170: }
|