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