325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.wsdl.parser;
325N/A
325N/Aimport com.sun.tools.internal.ws.wsdl.framework.ParseException;
325N/Aimport com.sun.xml.internal.ws.util.xml.XmlUtil;
325N/Aimport org.w3c.dom.Comment;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.w3c.dom.Node;
325N/Aimport org.w3c.dom.Text;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/Aimport java.io.File;
325N/Aimport java.net.MalformedURLException;
325N/Aimport java.net.URL;
325N/Aimport java.util.Iterator;
325N/A
325N/A/**2
325N/A * Defines various utility methods.
325N/A *
325N/A * @author WS Development Team
325N/A */
325N/Apublic class Util {
325N/A
325N/A public static String getRequiredAttribute(Element element, String name) {
325N/A String result = XmlUtil.getAttributeOrNull(element, name);
325N/A if (result == null)
325N/A fail(
325N/A "parsing.missingRequiredAttribute",
325N/A element.getTagName(),
325N/A name);
325N/A return result;
325N/A }
325N/A
325N/A public static void verifyTag(Element element, String tag) {
325N/A if (!element.getLocalName().equals(tag))
325N/A fail("parsing.invalidTag", element.getTagName(), tag);
325N/A }
325N/A
325N/A public static void verifyTagNS(Element element, String tag, String nsURI) {
325N/A if (!element.getLocalName().equals(tag)
325N/A || (element.getNamespaceURI() != null
325N/A && !element.getNamespaceURI().equals(nsURI)))
325N/A fail(
325N/A "parsing.invalidTagNS",
325N/A new Object[] {
325N/A element.getTagName(),
325N/A element.getNamespaceURI(),
325N/A tag,
325N/A nsURI });
325N/A }
325N/A
325N/A public static void verifyTagNS(Element element, QName name) {
325N/A if (!isTagName(element, name))
325N/A fail(
325N/A "parsing.invalidTagNS",
325N/A new Object[] {
325N/A element.getTagName(),
325N/A element.getNamespaceURI(),
325N/A name.getLocalPart(),
325N/A name.getNamespaceURI()});
325N/A }
325N/A
325N/A public static boolean isTagName(Element element, QName name){
325N/A return (element.getLocalName().equals(name.getLocalPart())
325N/A && (element.getNamespaceURI() != null
325N/A && element.getNamespaceURI().equals(name.getNamespaceURI())));
325N/A
325N/A }
325N/A
325N/A public static void verifyTagNSRootElement(Element element, QName name) {
325N/A if (!element.getLocalName().equals(name.getLocalPart())
325N/A || (element.getNamespaceURI() != null
325N/A && !element.getNamespaceURI().equals(name.getNamespaceURI())))
325N/A fail(
325N/A "parsing.incorrectRootElement",
325N/A new Object[] {
325N/A element.getTagName(),
325N/A element.getNamespaceURI(),
325N/A name.getLocalPart(),
325N/A name.getNamespaceURI()});
325N/A }
325N/A
325N/A public static Element nextElementIgnoringCharacterContent(Iterator iter) {
325N/A while (iter.hasNext()) {
325N/A Node n = (Node) iter.next();
325N/A if (n instanceof Text)
325N/A continue;
325N/A if (n instanceof Comment)
325N/A continue;
325N/A if (!(n instanceof Element))
325N/A fail("parsing.elementExpected");
325N/A return (Element) n;
325N/A }
325N/A
325N/A return null;
325N/A }
325N/A
325N/A public static Element nextElement(Iterator iter) {
325N/A while (iter.hasNext()) {
325N/A Node n = (Node) iter.next();
325N/A if (n instanceof Text) {
325N/A Text t = (Text) n;
325N/A if (t.getData().trim().length() == 0)
325N/A continue;
325N/A fail("parsing.nonWhitespaceTextFound", t.getData().trim());
325N/A }
325N/A if (n instanceof Comment)
325N/A continue;
325N/A if (!(n instanceof Element))
325N/A fail("parsing.elementExpected");
325N/A return (Element) n;
325N/A }
325N/A
325N/A return null;
325N/A }
325N/A
325N/A public static String processSystemIdWithBase(
325N/A String baseSystemId,
325N/A String systemId) {
325N/A try {
325N/A URL base = null;
325N/A try {
325N/A base = new URL(baseSystemId);
325N/A } catch (MalformedURLException e) {
325N/A base = new File(baseSystemId).toURL();
325N/A }
325N/A
325N/A try {
325N/A URL url = new URL(base, systemId);
325N/A return url.toString();
325N/A } catch (MalformedURLException e) {
325N/A fail("parsing.invalidURI", systemId);
325N/A }
325N/A
325N/A } catch (MalformedURLException e) {
325N/A fail("parsing.invalidURI", baseSystemId);
325N/A }
325N/A
325N/A return null; // keep compiler happy
325N/A }
325N/A
325N/A public static void fail(String key) {
325N/A throw new ParseException(key);
325N/A }
325N/A
325N/A public static void fail(String key, String arg) {
325N/A throw new ParseException(key, arg);
325N/A }
325N/A
325N/A public static void fail(String key, String arg1, String arg2) {
325N/A throw new ParseException(key, new Object[] { arg1, arg2 });
325N/A }
325N/A
325N/A public static void fail(String key, Object[] args) {
325N/A throw new ParseException(key, args);
325N/A }
325N/A}