1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/Util.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,181 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.wsdl.parser; 1.30 + 1.31 +import com.sun.tools.internal.ws.wsdl.framework.ParseException; 1.32 +import com.sun.xml.internal.ws.util.xml.XmlUtil; 1.33 +import org.w3c.dom.Comment; 1.34 +import org.w3c.dom.Element; 1.35 +import org.w3c.dom.Node; 1.36 +import org.w3c.dom.Text; 1.37 + 1.38 +import javax.xml.namespace.QName; 1.39 +import java.io.File; 1.40 +import java.net.MalformedURLException; 1.41 +import java.net.URL; 1.42 +import java.util.Iterator; 1.43 + 1.44 +/**2 1.45 + * Defines various utility methods. 1.46 + * 1.47 + * @author WS Development Team 1.48 + */ 1.49 +public class Util { 1.50 + 1.51 + public static String getRequiredAttribute(Element element, String name) { 1.52 + String result = XmlUtil.getAttributeOrNull(element, name); 1.53 + if (result == null) 1.54 + fail( 1.55 + "parsing.missingRequiredAttribute", 1.56 + element.getTagName(), 1.57 + name); 1.58 + return result; 1.59 + } 1.60 + 1.61 + public static void verifyTag(Element element, String tag) { 1.62 + if (!element.getLocalName().equals(tag)) 1.63 + fail("parsing.invalidTag", element.getTagName(), tag); 1.64 + } 1.65 + 1.66 + public static void verifyTagNS(Element element, String tag, String nsURI) { 1.67 + if (!element.getLocalName().equals(tag) 1.68 + || (element.getNamespaceURI() != null 1.69 + && !element.getNamespaceURI().equals(nsURI))) 1.70 + fail( 1.71 + "parsing.invalidTagNS", 1.72 + new Object[] { 1.73 + element.getTagName(), 1.74 + element.getNamespaceURI(), 1.75 + tag, 1.76 + nsURI }); 1.77 + } 1.78 + 1.79 + public static void verifyTagNS(Element element, QName name) { 1.80 + if (!isTagName(element, name)) 1.81 + fail( 1.82 + "parsing.invalidTagNS", 1.83 + new Object[] { 1.84 + element.getTagName(), 1.85 + element.getNamespaceURI(), 1.86 + name.getLocalPart(), 1.87 + name.getNamespaceURI()}); 1.88 + } 1.89 + 1.90 + public static boolean isTagName(Element element, QName name){ 1.91 + return (element.getLocalName().equals(name.getLocalPart()) 1.92 + && (element.getNamespaceURI() != null 1.93 + && element.getNamespaceURI().equals(name.getNamespaceURI()))); 1.94 + 1.95 + } 1.96 + 1.97 + public static void verifyTagNSRootElement(Element element, QName name) { 1.98 + if (!element.getLocalName().equals(name.getLocalPart()) 1.99 + || (element.getNamespaceURI() != null 1.100 + && !element.getNamespaceURI().equals(name.getNamespaceURI()))) 1.101 + fail( 1.102 + "parsing.incorrectRootElement", 1.103 + new Object[] { 1.104 + element.getTagName(), 1.105 + element.getNamespaceURI(), 1.106 + name.getLocalPart(), 1.107 + name.getNamespaceURI()}); 1.108 + } 1.109 + 1.110 + public static Element nextElementIgnoringCharacterContent(Iterator iter) { 1.111 + while (iter.hasNext()) { 1.112 + Node n = (Node) iter.next(); 1.113 + if (n instanceof Text) 1.114 + continue; 1.115 + if (n instanceof Comment) 1.116 + continue; 1.117 + if (!(n instanceof Element)) 1.118 + fail("parsing.elementExpected"); 1.119 + return (Element) n; 1.120 + } 1.121 + 1.122 + return null; 1.123 + } 1.124 + 1.125 + public static Element nextElement(Iterator iter) { 1.126 + while (iter.hasNext()) { 1.127 + Node n = (Node) iter.next(); 1.128 + if (n instanceof Text) { 1.129 + Text t = (Text) n; 1.130 + if (t.getData().trim().length() == 0) 1.131 + continue; 1.132 + fail("parsing.nonWhitespaceTextFound", t.getData().trim()); 1.133 + } 1.134 + if (n instanceof Comment) 1.135 + continue; 1.136 + if (!(n instanceof Element)) 1.137 + fail("parsing.elementExpected"); 1.138 + return (Element) n; 1.139 + } 1.140 + 1.141 + return null; 1.142 + } 1.143 + 1.144 + public static String processSystemIdWithBase( 1.145 + String baseSystemId, 1.146 + String systemId) { 1.147 + try { 1.148 + URL base = null; 1.149 + try { 1.150 + base = new URL(baseSystemId); 1.151 + } catch (MalformedURLException e) { 1.152 + base = new File(baseSystemId).toURL(); 1.153 + } 1.154 + 1.155 + try { 1.156 + URL url = new URL(base, systemId); 1.157 + return url.toString(); 1.158 + } catch (MalformedURLException e) { 1.159 + fail("parsing.invalidURI", systemId); 1.160 + } 1.161 + 1.162 + } catch (MalformedURLException e) { 1.163 + fail("parsing.invalidURI", baseSystemId); 1.164 + } 1.165 + 1.166 + return null; // keep compiler happy 1.167 + } 1.168 + 1.169 + public static void fail(String key) { 1.170 + throw new ParseException(key); 1.171 + } 1.172 + 1.173 + public static void fail(String key, String arg) { 1.174 + throw new ParseException(key, arg); 1.175 + } 1.176 + 1.177 + public static void fail(String key, String arg1, String arg2) { 1.178 + throw new ParseException(key, new Object[] { arg1, arg2 }); 1.179 + } 1.180 + 1.181 + public static void fail(String key, Object[] args) { 1.182 + throw new ParseException(key, args); 1.183 + } 1.184 +}