KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > validator > visitor > ValidationUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.validator.visitor;
21
22 /**
23  * Copied from castor validation utils.
24  *
25  *
26  */

27 public class ValidationUtils {
28     
29     
30     //----------------/
31
//- Constructors -/
32
//----------------/
33

34     private ValidationUtils() {
35         super();
36     }
37     
38     //------------------/
39
//- Public Methods -/
40
//------------------/
41

42     
43     /**
44      * Checks the given character to determine if it is a valid
45      * CombiningChar as defined by the W3C XML 1.0 Recommendation
46      * @return true if the given character is a CombiningChar
47      **/

48     public static boolean isCombiningChar(char ch) {
49         
50         //-- NOTE: THIS METHOD IS NOT COMPLETE
51

52         return false;
53         
54     } //-- isCombiningChar
55

56     /**
57      * @param ch the character to check
58      * @return true if the given character is a digit
59      **/

60     public static boolean isDigit(char ch) {
61         return Character.isDigit(ch);
62     } //-- isDigit
63

64     
65     /**
66      * @param ch the character to check
67      * @return true if the given character is a letter
68      **/

69     public static boolean isLetter(char ch) {
70         return Character.isLetter(ch);
71     } //-- isLetter
72

73     /**
74      * Checks the characters of the given String to determine if they
75      * syntactically match the production of an NCName as defined
76      * by the W3C XML Namespaces recommendation
77      * @param str the String to check
78      * @return true if the given String follows the Syntax of an NCName
79      **/

80     public static boolean isNCName(String JavaDoc str) {
81         
82         if ((str == null) || (str.length() == 0)) return false;
83         
84         
85         char[] chars = str.toCharArray();
86         
87         char ch = chars[0];
88         
89         //-- make sure String starts with a letter or '_'
90
if ((!isLetter(ch)) && (ch != '_'))
91             return false;
92         
93         for (int i = 1; i < chars.length; i++) {
94             if (!isNCNameChar(chars[i])) return false;
95         }
96         return true;
97     } //-- isNCName
98

99     /**
100      * Checks the the given character to determine if it is
101      * a valid NCNameChar as defined by the W3C XML
102      * Namespaces recommendation
103      * @param ch the char to check
104      * @return true if the given char is an NCNameChar
105      **/

106     public static boolean isNCNameChar(char ch) {
107         if (isLetter(ch) || isDigit(ch)) return true;
108         if (isExtender(ch) || isCombiningChar(ch)) return true;
109         switch(ch) {
110         case '.':
111         case '-':
112         case '_':
113             return true;
114         default:
115             return false;
116         }
117     } //-- isNCNameChar
118

119     
120     /**
121      * Checks the characters of the given String to determine if they
122      * syntactically match the production of an NMToken
123      * @param str the String to check
124      * @return true if the given String follows the Syntax of an NMToken
125      **/

126     public static boolean isNMToken(String JavaDoc str) {
127         
128         if (str == null) return false;
129         char[] chars = str.toCharArray();
130         
131         for (int i = 0; i < chars.length; i++) {
132             char ch = chars[i];
133             if (isLetter(ch) || isDigit(ch)) continue;
134             if (isExtender(ch) || isCombiningChar(ch)) continue;
135             switch(ch) {
136             case '.':
137             case '-':
138             case '_':
139             case ':':
140                 break;
141             default:
142                 return false;
143             }
144         }
145         return true;
146     } //-- isNMToken
147

148     /**
149      * Checks the characters of the given String to determine if they
150      * syntactically match the production of a CDATA
151      * @param str the String to check
152      * @return true if the given String follows the Syntax of an NMToken
153      **/

154     public static boolean isCDATA(String JavaDoc str) {
155         
156         if (str == null) return false;
157         char[] chars = str.toCharArray();
158         
159         for (int i = 0; i < chars.length; i++) {
160             char ch = chars[i];
161             switch(ch) {
162             case '\r':
163             case '\n':
164             case '\t':
165                 return false;
166             default:
167                 continue;
168             }
169         }
170         return true;
171     } //-- isCDATA
172

173     /**
174      * Returns true if the given character is a valid XML Extender
175      * character, according to the XML 1.0 specification
176      * @param ch the character to check
177      * @return true if the character is a valid XML Extender character
178      **/

179     public static boolean isExtender(char ch) {
180         
181         if ((ch >= 0x3031) && (ch <= 0x3035)) return true;
182         if ((ch >= 0x30FC) && (ch <= 0x30FE)) return true;
183         
184         switch(ch) {
185         case 0x00B7:
186         case 0x02D0:
187         case 0x02D1:
188         case 0x0387:
189         case 0x0640:
190         case 0x0E46:
191         case 0x0EC6:
192         case 0x3005:
193         case 0x309D:
194         case 0x309E:
195             return true;
196         default:
197             break;
198         }
199         return false;
200     } //-- isExtender
201

202     /**
203      * Checks the characters of the given String to determine if they
204      * syntactically match the production of an QName as defined
205      * by the W3C XML Namespaces recommendation
206      * @param str the String to check
207      * @return true if the given String follows the Syntax of an QName
208      **/

209     public static boolean isQName(String JavaDoc str) {
210         
211         if ((str == null) || (str.length() == 0)) return false;
212         
213         
214         char[] chars = str.toCharArray();
215         
216         char ch = chars[0];
217         
218         //-- make sure String starts with a letter or '_'
219
if ((!isLetter(ch)) && (ch != '_'))
220             return false;
221         
222         for (int i = 1; i < chars.length; i++) {
223             if (chars[i] == ':') continue;
224             if (!isNCNameChar(chars[i])) return false;
225         }
226         return true;
227     } //-- isQName
228

229     /**
230      * Test
231      **
232      public static void main(String[] args) {
233      System.out.println("0x00B7: " + (char)0x00B7);
234      }
235      /* */

236     /** Test if two strings are equal in XML.
237      * @param s1 First string.
238      * @param s2 Second string.
239      * @return <code>true</code> if strings are equal.
240      */

241     public static boolean areEqualXMLValues(String JavaDoc s1, String JavaDoc s2) {
242
243         return ((s1 == null && s2 == null)
244             || (s1 == null && isEmpty(s2))
245             || (isEmpty(s1) && s2 == null)
246             || (s1 != null && s1.equals(s2)));
247     }
248     
249     /** Test if a string is empty.
250      * @param s String to test
251      * @return <code>true</code> if string is empty
252      */

253     public static boolean isEmpty(String JavaDoc s) {
254         return ((null == s) || (s.trim().length() == 0));
255     }
256 }
257
258
Popular Tags