001: /*
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: ValidationUtils.java 6587 2006-12-26 03:20:00Z ekuns $
044: */
045: package org.exolab.castor.xml.validators;
046:
047: /**
048: * A static class for performing simple validation.
049: *
050: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
051: * @version $Revision: 6587 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
052: */
053: public final class ValidationUtils {
054:
055: // ----------------/
056: // - Constructors -/
057: // ----------------/
058:
059: /**
060: * No-arg constructor.
061: */
062: private ValidationUtils() {
063: super ();
064: }
065:
066: // ------------------/
067: // - Public Methods -/
068: // ------------------/
069:
070: /**
071: * Checks the given character to determine if it is a valid CombiningChar as
072: * defined by the W3C XML 1.0 Recommendation.
073: * <p>
074: * FIXME: This method needs to be properly implemented.
075: * @param ch THe character to check.
076: *
077: * @return true if the given character is a CombiningChar
078: */
079: public static boolean isCombiningChar(final char ch) {
080: // -- NOTE: THIS METHOD IS NOT COMPLETE
081: return false;
082: } // -- isCombiningChar
083:
084: /**
085: * Checks a character to see if it is a digit or not.
086: * @param ch
087: * the character to check
088: * @return true if the given character is a digit
089: */
090: public static boolean isDigit(final char ch) {
091: return Character.isDigit(ch);
092: } // -- isDigit
093:
094: /**
095: * Checks a character to see if it is a letter or not.
096: * @param ch
097: * the character to check
098: * @return true if the given character is a letter
099: */
100: public static boolean isLetter(final char ch) {
101: return Character.isLetter(ch);
102: } // -- isLetter
103:
104: /**
105: * Checks all characters of the given String to determine if they
106: * syntactically match the production of an NCName as defined by the W3C XML
107: * Namespaces recommendation.
108: *
109: * @param str
110: * the String to check
111: * @return true if the given String follows the Syntax of an NCName
112: */
113: public static boolean isNCName(final String str) {
114: if (str == null || str.length() == 0) {
115: return false;
116: }
117:
118: char[] chars = str.toCharArray();
119:
120: // -- make sure String starts with a letter or '_'
121: char ch = chars[0];
122: if (!isLetter(ch) && ch != '_') {
123: return false;
124: }
125:
126: for (int i = 1; i < chars.length; i++) {
127: if (!isNCNameChar(chars[i])) {
128: return false;
129: }
130: }
131: return true;
132: } // -- isNCName
133:
134: /**
135: * Checks the the given character to determine if it is a valid NCNameChar
136: * as defined by the W3C XML Namespaces recommendation.
137: *
138: * @param ch
139: * the char to check
140: * @return true if the given char is an NCNameChar
141: */
142: public static boolean isNCNameChar(final char ch) {
143: if (isLetter(ch) || isDigit(ch)) {
144: return true;
145: }
146:
147: if (isExtender(ch) || isCombiningChar(ch)) {
148: return true;
149: }
150:
151: switch (ch) {
152: case '.':
153: case '-':
154: case '_':
155: return true;
156: default:
157: return false;
158: }
159: } // -- isNCNameChar
160:
161: /**
162: * Checks all characters of the given String to determine if they
163: * syntactically match the production of an NMToken.
164: *
165: * @param str
166: * the String to check
167: * @return true if the given String follows the Syntax of an NMToken
168: */
169: public static boolean isNMToken(final String str) {
170: if (str == null) {
171: return false;
172: }
173:
174: char[] chars = str.toCharArray();
175:
176: for (int i = 0; i < chars.length; i++) {
177: char ch = chars[i];
178: if (isLetter(ch) || isDigit(ch) || isExtender(ch)
179: || isCombiningChar(ch)) {
180: continue;
181: }
182:
183: switch (ch) {
184: case '.':
185: case '-':
186: case '_':
187: case ':':
188: break;
189: default:
190: return false;
191: }
192: }
193: return true;
194: } // -- isNMToken
195:
196: /**
197: * Checks all characters of the given String to determine if they
198: * syntactically match the production of a CDATA.
199: *
200: * @param str
201: * the String to check
202: * @return true if the given String follows the Syntax of an NMToken
203: */
204: public static boolean isCDATA(final String str) {
205: if (str == null) {
206: return false;
207: }
208:
209: char[] chars = str.toCharArray();
210:
211: for (int i = 0; i < chars.length; i++) {
212: char ch = chars[i];
213: switch (ch) {
214: case '\r':
215: case '\n':
216: case '\t':
217: return false;
218: default:
219: continue;
220: }
221: }
222: return true;
223: } // -- isCDATA
224:
225: /**
226: * Returns true if the given character is a valid XML Extender character,
227: * according to the XML 1.0 specification.
228: *
229: * @param ch
230: * the character to check
231: * @return true if the character is a valid XML Extender character
232: */
233: public static boolean isExtender(final char ch) {
234: if ((ch >= 0x3031) && (ch <= 0x3035)) {
235: return true;
236: }
237:
238: if ((ch >= 0x30FC) && (ch <= 0x30FE)) {
239: return true;
240: }
241:
242: switch (ch) {
243: case 0x00B7:
244: case 0x02D0:
245: case 0x02D1:
246: case 0x0387:
247: case 0x0640:
248: case 0x0E46:
249: case 0x0EC6:
250: case 0x3005:
251: case 0x309D:
252: case 0x309E:
253: return true;
254: default:
255: break;
256: }
257: return false;
258: } // -- isExtender
259:
260: /**
261: * Checks all characters of the given String to determine if they
262: * syntactically match the production of an QName as defined by the W3C XML
263: * Namespaces recommendation.
264: *
265: * @param str
266: * the String to check
267: * @return true if the given String follows the Syntax of an QName
268: */
269: public static boolean isQName(final String str) {
270: if (str == null || str.length() == 0) {
271: return false;
272: }
273:
274: char[] chars = str.toCharArray();
275:
276: // -- make sure String starts with a letter or '_'
277: char ch = chars[0];
278: if (!isLetter(ch) && ch != '_') {
279: return false;
280: }
281:
282: for (int i = 1; i < chars.length; i++) {
283: if (chars[i] == ':') {
284: continue;
285: }
286:
287: if (!isNCNameChar(chars[i])) {
288: return false;
289: }
290: }
291: return true;
292: } //-- isQName
293:
294: } //-- Validator
|