Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.internal.ws.wsdl.parser;
28 import com.sun.tools.internal.ws.wsdl.framework.ParseException;
29 import com.sun.xml.internal.ws.util.xml.XmlUtil;
30 import org.w3c.dom.Comment;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.Text;
35 import javax.xml.namespace.QName;
36 import java.io.File;
37 import java.net.MalformedURLException;
38 import java.net.URL;
39 import java.util.Iterator;
41 /**2
42 * Defines various utility methods.
43 *
44 * @author WS Development Team
45 */
46 public class Util {
48 public static String getRequiredAttribute(Element element, String name) {
49 String result = XmlUtil.getAttributeOrNull(element, name);
50 if (result == null)
51 fail(
52 "parsing.missingRequiredAttribute",
53 element.getTagName(),
54 name);
55 return result;
56 }
58 public static void verifyTag(Element element, String tag) {
59 if (!element.getLocalName().equals(tag))
60 fail("parsing.invalidTag", element.getTagName(), tag);
61 }
63 public static void verifyTagNS(Element element, String tag, String nsURI) {
64 if (!element.getLocalName().equals(tag)
65 || (element.getNamespaceURI() != null
66 && !element.getNamespaceURI().equals(nsURI)))
67 fail(
68 "parsing.invalidTagNS",
69 new Object[] {
70 element.getTagName(),
71 element.getNamespaceURI(),
72 tag,
73 nsURI });
74 }
76 public static void verifyTagNS(Element element, QName name) {
77 if (!isTagName(element, name))
78 fail(
79 "parsing.invalidTagNS",
80 new Object[] {
81 element.getTagName(),
82 element.getNamespaceURI(),
83 name.getLocalPart(),
84 name.getNamespaceURI()});
85 }
87 public static boolean isTagName(Element element, QName name){
88 return (element.getLocalName().equals(name.getLocalPart())
89 && (element.getNamespaceURI() != null
90 && element.getNamespaceURI().equals(name.getNamespaceURI())));
92 }
94 public static void verifyTagNSRootElement(Element element, QName name) {
95 if (!element.getLocalName().equals(name.getLocalPart())
96 || (element.getNamespaceURI() != null
97 && !element.getNamespaceURI().equals(name.getNamespaceURI())))
98 fail(
99 "parsing.incorrectRootElement",
100 new Object[] {
101 element.getTagName(),
102 element.getNamespaceURI(),
103 name.getLocalPart(),
104 name.getNamespaceURI()});
105 }
107 public static Element nextElementIgnoringCharacterContent(Iterator iter) {
108 while (iter.hasNext()) {
109 Node n = (Node) iter.next();
110 if (n instanceof Text)
111 continue;
112 if (n instanceof Comment)
113 continue;
114 if (!(n instanceof Element))
115 fail("parsing.elementExpected");
116 return (Element) n;
117 }
119 return null;
120 }
122 public static Element nextElement(Iterator iter) {
123 while (iter.hasNext()) {
124 Node n = (Node) iter.next();
125 if (n instanceof Text) {
126 Text t = (Text) n;
127 if (t.getData().trim().length() == 0)
128 continue;
129 fail("parsing.nonWhitespaceTextFound", t.getData().trim());
130 }
131 if (n instanceof Comment)
132 continue;
133 if (!(n instanceof Element))
134 fail("parsing.elementExpected");
135 return (Element) n;
136 }
138 return null;
139 }
141 public static String processSystemIdWithBase(
142 String baseSystemId,
143 String systemId) {
144 try {
145 URL base = null;
146 try {
147 base = new URL(baseSystemId);
148 } catch (MalformedURLException e) {
149 base = new File(baseSystemId).toURL();
150 }
152 try {
153 URL url = new URL(base, systemId);
154 return url.toString();
155 } catch (MalformedURLException e) {
156 fail("parsing.invalidURI", systemId);
157 }
159 } catch (MalformedURLException e) {
160 fail("parsing.invalidURI", baseSystemId);
161 }
163 return null; // keep compiler happy
164 }
166 public static void fail(String key) {
167 throw new ParseException(key);
168 }
170 public static void fail(String key, String arg) {
171 throw new ParseException(key, arg);
172 }
174 public static void fail(String key, String arg1, String arg2) {
175 throw new ParseException(key, new Object[] { arg1, arg2 });
176 }
178 public static void fail(String key, Object[] args) {
179 throw new ParseException(key, args);
180 }
181 }