~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdl/api/src/org/netbeans/modules/xml/wsdl/validator/visitor/ValidationUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.wsdl.validator.visitor;
 
43
 
 
44
/**
 
45
 * Copied from castor validation utils.
 
46
 *
 
47
 *
 
48
 */
 
49
public class ValidationUtils {
 
50
    
 
51
    
 
52
    //----------------/
 
53
    //- Constructors -/
 
54
    //----------------/
 
55
    
 
56
    private ValidationUtils() {
 
57
        super();
 
58
    }
 
59
    
 
60
    //------------------/
 
61
    //- Public Methods -/
 
62
    //------------------/
 
63
    
 
64
    
 
65
    /**
 
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
 
69
     **/
 
70
    public static boolean isCombiningChar(char ch) {
 
71
        
 
72
        //-- NOTE: THIS METHOD IS NOT COMPLETE
 
73
        
 
74
        return false;
 
75
        
 
76
    } //-- isCombiningChar
 
77
    
 
78
    /**
 
79
     * @param ch the character to check
 
80
     * @return true if the given character is a digit
 
81
     **/
 
82
    public static boolean isDigit(char ch) {
 
83
        return Character.isDigit(ch);
 
84
    } //-- isDigit
 
85
    
 
86
    
 
87
    /**
 
88
     * @param ch the character to check
 
89
     * @return true if the given character is a letter
 
90
     **/
 
91
    public static boolean isLetter(char ch) {
 
92
        return Character.isLetter(ch);
 
93
    } //-- isLetter
 
94
    
 
95
    /**
 
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
 
101
     **/
 
102
    public static boolean isNCName(String str) {
 
103
        
 
104
        if ((str == null) || (str.length() == 0)) return false;
 
105
        
 
106
        
 
107
        char[] chars = str.toCharArray();
 
108
        
 
109
        char ch = chars[0];
 
110
        
 
111
        //-- make sure String starts with a letter or '_'
 
112
        if ((!isLetter(ch)) && (ch != '_'))
 
113
            return false;
 
114
        
 
115
        for (int i = 1; i < chars.length; i++) {
 
116
            if (!isNCNameChar(chars[i])) return false;
 
117
        }
 
118
        return true;
 
119
    } //-- isNCName
 
120
    
 
121
    /**
 
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
 
127
     **/
 
128
    public static boolean isNCNameChar(char ch) {
 
129
        if (isLetter(ch) || isDigit(ch)) return true;
 
130
        if (isExtender(ch) || isCombiningChar(ch)) return true;
 
131
        switch(ch) {
 
132
        case '.':
 
133
        case '-':
 
134
        case '_':
 
135
            return true;
 
136
        default:
 
137
            return false;
 
138
        }
 
139
    } //-- isNCNameChar
 
140
    
 
141
    
 
142
    /**
 
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
 
147
     **/
 
148
    public static boolean isNMToken(String str) {
 
149
        
 
150
        if (str == null) return false;
 
151
        char[] chars = str.toCharArray();
 
152
        
 
153
        for (int i = 0; i < chars.length; i++) {
 
154
            char ch = chars[i];
 
155
            if (isLetter(ch) || isDigit(ch)) continue;
 
156
            if (isExtender(ch) || isCombiningChar(ch)) continue;
 
157
            switch(ch) {
 
158
            case '.':
 
159
            case '-':
 
160
            case '_':
 
161
            case ':':
 
162
                break;
 
163
            default:
 
164
                return false;
 
165
            }
 
166
        }
 
167
        return true;
 
168
    } //-- isNMToken
 
169
    
 
170
    /**
 
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
 
175
     **/
 
176
    public static boolean isCDATA(String str) {
 
177
        
 
178
        if (str == null) return false;
 
179
        char[] chars = str.toCharArray();
 
180
        
 
181
        for (int i = 0; i < chars.length; i++) {
 
182
            char ch = chars[i];
 
183
            switch(ch) {
 
184
            case '\r':
 
185
            case '\n':
 
186
            case '\t':
 
187
                return false;
 
188
            default:
 
189
                continue;
 
190
            }
 
191
        }
 
192
        return true;
 
193
    } //-- isCDATA
 
194
    
 
195
    /**
 
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
 
200
     **/
 
201
    public static boolean isExtender(char ch) {
 
202
        
 
203
        if ((ch >= 0x3031) && (ch <= 0x3035)) return true;
 
204
        if ((ch >= 0x30FC) && (ch <= 0x30FE)) return true;
 
205
        
 
206
        switch(ch) {
 
207
        case 0x00B7:
 
208
        case 0x02D0:
 
209
        case 0x02D1:
 
210
        case 0x0387:
 
211
        case 0x0640:
 
212
        case 0x0E46:
 
213
        case 0x0EC6:
 
214
        case 0x3005:
 
215
        case 0x309D:
 
216
        case 0x309E:
 
217
            return true;
 
218
        default:
 
219
            break;
 
220
        }
 
221
        return false;
 
222
    } //-- isExtender
 
223
    
 
224
    /**
 
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
 
230
     **/
 
231
    public static boolean isQName(String str) {
 
232
        
 
233
        if ((str == null) || (str.length() == 0)) return false;
 
234
        
 
235
        
 
236
        char[] chars = str.toCharArray();
 
237
        
 
238
        char ch = chars[0];
 
239
        
 
240
        //-- make sure String starts with a letter or '_'
 
241
        if ((!isLetter(ch)) && (ch != '_'))
 
242
            return false;
 
243
        
 
244
        for (int i = 1; i < chars.length; i++) {
 
245
            if (chars[i] == ':') continue;
 
246
            if (!isNCNameChar(chars[i])) return false;
 
247
        }
 
248
        return true;
 
249
    } //-- isQName
 
250
    
 
251
    /**
 
252
     * Test
 
253
     **
 
254
     public static void main(String[] args) {
 
255
     System.out.println("0x00B7: " + (char)0x00B7);
 
256
     }
 
257
     /* */
 
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.
 
262
     */
 
263
    public static boolean areEqualXMLValues(String s1, String s2) {
 
264
 
 
265
        return ((s1 == null && s2 == null)
 
266
            || (s1 == null && isEmpty(s2))
 
267
            || (isEmpty(s1) && s2 == null)
 
268
            || (s1 != null && s1.equals(s2)));
 
269
    }
 
270
    
 
271
    /** Test if a string is empty.
 
272
     * @param   s   String to test
 
273
     * @return  <code>true</code> if string is empty
 
274
     */
 
275
    public static boolean isEmpty(String s) {
 
276
        return ((null == s) || (s.trim().length() == 0));
 
277
    }
 
278
}
 
279