2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.wsdl.validator.visitor;
45
* Copied from castor validation utils.
49
public class ValidationUtils {
56
private ValidationUtils() {
66
* Checks the given character to determine if it is a valid
67
* CombiningChar as defined by the W3C XML 1.0 Recommendation
68
* @return true if the given character is a CombiningChar
70
public static boolean isCombiningChar(char ch) {
72
//-- NOTE: THIS METHOD IS NOT COMPLETE
76
} //-- isCombiningChar
79
* @param ch the character to check
80
* @return true if the given character is a digit
82
public static boolean isDigit(char ch) {
83
return Character.isDigit(ch);
88
* @param ch the character to check
89
* @return true if the given character is a letter
91
public static boolean isLetter(char ch) {
92
return Character.isLetter(ch);
96
* Checks the characters of the given String to determine if they
97
* syntactically match the production of an NCName as defined
98
* by the W3C XML Namespaces recommendation
99
* @param str the String to check
100
* @return true if the given String follows the Syntax of an NCName
102
public static boolean isNCName(String str) {
104
if ((str == null) || (str.length() == 0)) return false;
107
char[] chars = str.toCharArray();
111
//-- make sure String starts with a letter or '_'
112
if ((!isLetter(ch)) && (ch != '_'))
115
for (int i = 1; i < chars.length; i++) {
116
if (!isNCNameChar(chars[i])) return false;
122
* Checks the the given character to determine if it is
123
* a valid NCNameChar as defined by the W3C XML
124
* Namespaces recommendation
125
* @param ch the char to check
126
* @return true if the given char is an NCNameChar
128
public static boolean isNCNameChar(char ch) {
129
if (isLetter(ch) || isDigit(ch)) return true;
130
if (isExtender(ch) || isCombiningChar(ch)) return true;
143
* Checks the characters of the given String to determine if they
144
* syntactically match the production of an NMToken
145
* @param str the String to check
146
* @return true if the given String follows the Syntax of an NMToken
148
public static boolean isNMToken(String str) {
150
if (str == null) return false;
151
char[] chars = str.toCharArray();
153
for (int i = 0; i < chars.length; i++) {
155
if (isLetter(ch) || isDigit(ch)) continue;
156
if (isExtender(ch) || isCombiningChar(ch)) continue;
171
* Checks the characters of the given String to determine if they
172
* syntactically match the production of a CDATA
173
* @param str the String to check
174
* @return true if the given String follows the Syntax of an NMToken
176
public static boolean isCDATA(String str) {
178
if (str == null) return false;
179
char[] chars = str.toCharArray();
181
for (int i = 0; i < chars.length; i++) {
196
* Returns true if the given character is a valid XML Extender
197
* character, according to the XML 1.0 specification
198
* @param ch the character to check
199
* @return true if the character is a valid XML Extender character
201
public static boolean isExtender(char ch) {
203
if ((ch >= 0x3031) && (ch <= 0x3035)) return true;
204
if ((ch >= 0x30FC) && (ch <= 0x30FE)) return true;
225
* Checks the characters of the given String to determine if they
226
* syntactically match the production of an QName as defined
227
* by the W3C XML Namespaces recommendation
228
* @param str the String to check
229
* @return true if the given String follows the Syntax of an QName
231
public static boolean isQName(String str) {
233
if ((str == null) || (str.length() == 0)) return false;
236
char[] chars = str.toCharArray();
240
//-- make sure String starts with a letter or '_'
241
if ((!isLetter(ch)) && (ch != '_'))
244
for (int i = 1; i < chars.length; i++) {
245
if (chars[i] == ':') continue;
246
if (!isNCNameChar(chars[i])) return false;
254
public static void main(String[] args) {
255
System.out.println("0x00B7: " + (char)0x00B7);
258
/** Test if two strings are equal in XML.
259
* @param s1 First string.
260
* @param s2 Second string.
261
* @return <code>true</code> if strings are equal.
263
public static boolean areEqualXMLValues(String s1, String s2) {
265
return ((s1 == null && s2 == null)
266
|| (s1 == null && isEmpty(s2))
267
|| (isEmpty(s1) && s2 == null)
268
|| (s1 != null && s1.equals(s2)));
271
/** Test if a string is empty.
272
* @param s String to test
273
* @return <code>true</code> if string is empty
275
public static boolean isEmpty(String s) {
276
return ((null == s) || (s.trim().length() == 0));