001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.compapp.casaeditor.model.casa.validator.visitor;
043:
044: /**
045: * Copied from castor validation utils.
046: *
047: *
048: */
049: public class ValidationUtils {
050:
051: //----------------/
052: //- Constructors -/
053: //----------------/
054:
055: private ValidationUtils() {
056: super ();
057: }
058:
059: //------------------/
060: //- Public Methods -/
061: //------------------/
062:
063: /**
064: * Checks the given character to determine if it is a valid
065: * CombiningChar as defined by the W3C XML 1.0 Recommendation
066: * @return true if the given character is a CombiningChar
067: **/
068: public static boolean isCombiningChar(char ch) {
069:
070: //-- NOTE: THIS METHOD IS NOT COMPLETE
071:
072: return false;
073:
074: } //-- isCombiningChar
075:
076: /**
077: * @param ch the character to check
078: * @return true if the given character is a digit
079: **/
080: public static boolean isDigit(char ch) {
081: return Character.isDigit(ch);
082: } //-- isDigit
083:
084: /**
085: * @param ch the character to check
086: * @return true if the given character is a letter
087: **/
088: public static boolean isLetter(char ch) {
089: return Character.isLetter(ch);
090: } //-- isLetter
091:
092: /**
093: * Checks the characters of the given String to determine if they
094: * syntactically match the production of an NCName as defined
095: * by the W3C XML Namespaces recommendation
096: * @param str the String to check
097: * @return true if the given String follows the Syntax of an NCName
098: **/
099: public static boolean isNCName(String str) {
100:
101: if ((str == null) || (str.length() == 0))
102: return false;
103:
104: char[] chars = str.toCharArray();
105:
106: char ch = chars[0];
107:
108: //-- make sure String starts with a letter or '_'
109: if ((!isLetter(ch)) && (ch != '_'))
110: return false;
111:
112: for (int i = 1; i < chars.length; i++) {
113: if (!isNCNameChar(chars[i]))
114: return false;
115: }
116: return true;
117: } //-- isNCName
118:
119: /**
120: * Checks the the given character to determine if it is
121: * a valid NCNameChar as defined by the W3C XML
122: * Namespaces recommendation
123: * @param ch the char to check
124: * @return true if the given char is an NCNameChar
125: **/
126: public static boolean isNCNameChar(char ch) {
127: if (isLetter(ch) || isDigit(ch))
128: return true;
129: if (isExtender(ch) || isCombiningChar(ch))
130: 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: * Checks the characters of the given String to determine if they
143: * syntactically match the production of an NMToken
144: * @param str the String to check
145: * @return true if the given String follows the Syntax of an NMToken
146: **/
147: public static boolean isNMToken(String str) {
148:
149: if (str == null)
150: 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))
156: continue;
157: if (isExtender(ch) || isCombiningChar(ch))
158: continue;
159: switch (ch) {
160: case '.':
161: case '-':
162: case '_':
163: case ':':
164: break;
165: default:
166: return false;
167: }
168: }
169: return true;
170: } //-- isNMToken
171:
172: /**
173: * Checks the characters of the given String to determine if they
174: * syntactically match the production of a CDATA
175: * @param str the String to check
176: * @return true if the given String follows the Syntax of an NMToken
177: **/
178: public static boolean isCDATA(String str) {
179:
180: if (str == null)
181: return false;
182: char[] chars = str.toCharArray();
183:
184: for (int i = 0; i < chars.length; i++) {
185: char ch = chars[i];
186: switch (ch) {
187: case '\r':
188: case '\n':
189: case '\t':
190: return false;
191: default:
192: continue;
193: }
194: }
195: return true;
196: } //-- isCDATA
197:
198: /**
199: * Returns true if the given character is a valid XML Extender
200: * character, according to the XML 1.0 specification
201: * @param ch the character to check
202: * @return true if the character is a valid XML Extender character
203: **/
204: public static boolean isExtender(char ch) {
205:
206: if ((ch >= 0x3031) && (ch <= 0x3035))
207: return true;
208: if ((ch >= 0x30FC) && (ch <= 0x30FE))
209: return true;
210:
211: switch (ch) {
212: case 0x00B7:
213: case 0x02D0:
214: case 0x02D1:
215: case 0x0387:
216: case 0x0640:
217: case 0x0E46:
218: case 0x0EC6:
219: case 0x3005:
220: case 0x309D:
221: case 0x309E:
222: return true;
223: default:
224: break;
225: }
226: return false;
227: } //-- isExtender
228:
229: /**
230: * Checks the characters of the given String to determine if they
231: * syntactically match the production of an QName as defined
232: * by the W3C XML Namespaces recommendation
233: * @param str the String to check
234: * @return true if the given String follows the Syntax of an QName
235: **/
236: public static boolean isQName(String str) {
237:
238: if ((str == null) || (str.length() == 0))
239: return false;
240:
241: char[] chars = str.toCharArray();
242:
243: char ch = chars[0];
244:
245: //-- make sure String starts with a letter or '_'
246: if ((!isLetter(ch)) && (ch != '_'))
247: return false;
248:
249: for (int i = 1; i < chars.length; i++) {
250: if (chars[i] == ':')
251: continue;
252: if (!isNCNameChar(chars[i]))
253: return false;
254: }
255: return true;
256: } //-- isQName
257:
258: /**
259: * Test
260: **
261: public static void main(String[] args) {
262: System.out.println("0x00B7: " + (char)0x00B7);
263: }
264: /* */
265: /** Test if two strings are equal in XML.
266: * @param s1 First string.
267: * @param s2 Second string.
268: * @return <code>true</code> if strings are equal.
269: */
270: public static boolean areEqualXMLValues(String s1, String s2) {
271:
272: return ((s1 == null && s2 == null)
273: || (s1 == null && isEmpty(s2))
274: || (isEmpty(s1) && s2 == null) || (s1 != null && s1
275: .equals(s2)));
276: }
277:
278: /** Test if a string is empty.
279: * @param s String to test
280: * @return <code>true</code> if string is empty
281: */
282: public static boolean isEmpty(String s) {
283: return ((null == s) || (s.trim().length() == 0));
284: }
285: }
|