KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > RestrictedStringValue


1 package net.sf.saxon.value;
2
3 import net.sf.saxon.ConversionContext;
4 import net.sf.saxon.om.FastStringBuffer;
5 import net.sf.saxon.om.XMLChar;
6 import net.sf.saxon.type.*;
7
8
9 /**
10  * A value conforming to one of the built-in subtypes of String, specifically
11  * normalizedString, token, language, Name, NCName, ID, IDREF, ENTITY, NMTOKEN.
12  * This class doesnt' handle the types derived by list: IDREFS, NMTOKENS, ENTITIES.
13  */

14
15 public final class RestrictedStringValue extends StringValue {
16
17     private int type;
18
19     /**
20      * Factory method to create a restricted string value from a string
21      * @param value the String value. Null is taken as equivalent to "".
22      * @param validate true if validation is required, false if the caller already knows that the value is valid
23      * @return either the required RestrictedStringValue if the value is valid, or an ErrorValue encapsulating
24      * the error message if not.
25      */

26
27     public static AtomicValue makeRestrictedString(CharSequence JavaDoc value, int type, boolean validate) {
28         RestrictedStringValue rsv = new RestrictedStringValue();
29         rsv.type = type;
30         if (value == null) {
31             rsv.value = "";
32         } else if (type == Type.NORMALIZED_STRING) {
33             rsv.value = normalizeWhitespace(value);
34         } else if (type == Type.TOKEN) {
35             rsv.value = collapseWhitespace(value);
36         } else {
37             rsv.value = trimWhitespace(value);
38             if (validate) {
39                 ValidationException err = rsv.validate();
40                 if (err == null) {
41                     return rsv;
42                 } else {
43                     return new ValidationErrorValue(err);
44                 }
45             } else {
46                 return rsv;
47             }
48         }
49         return rsv;
50     }
51
52     /**
53      * Validate that the string conforms to the rules for its type
54      * @return null if the value is OK, otherwise a DynamicError containing details of the failure
55      */

56
57     private ValidationException validate() {
58         switch (type) {
59
60             case Type.TOKEN:
61                 return null;
62             case Type.NORMALIZED_STRING:
63                 return null;
64             case Type.LANGUAGE:
65                 String JavaDoc regex =
66                         "(([a-z]|[A-Z])([a-z]|[A-Z])|" // ISO639Code
67
+ "([iI]-([a-z]|[A-Z])+)|" // IanaCode
68
+ "([xX]-([a-z]|[A-Z])+))" // UserCode
69
+ "(-([a-z]|[A-Z])+)*"; // Subcode
70
if (!java.util.regex.Pattern.matches(regex, value.toString())) {
71                     ValidationException err = new ValidationException("The value '" + value + "' is not a valid xs:language");
72                     err.setErrorCode("FORG0001");
73                     return err;
74                 }
75                 return null;
76             case Type.NAME:
77                 // replace any colons by underscores and then test if it's a valid NCName
78
FastStringBuffer buff = new FastStringBuffer(value.length());
79                 buff.append(value.toString());
80                 for (int i = 0; i < buff.length(); i++) {
81                     if (buff.charAt(i) == ':') {
82                         buff.setCharAt(i, '_');
83                     }
84                 }
85                 if (!XMLChar.isValidNCName(buff.toString())) {
86                     ValidationException err = new ValidationException("The value '" + value + "' is not a valid Name");
87                     err.setErrorCode("FORG0001");
88                     return err;
89                 }
90                 return null;
91             case Type.NCNAME:
92             case Type.ID:
93             case Type.IDREF:
94             case Type.ENTITY:
95                 if (!XMLChar.isValidNCName(value.toString())) {
96                     ValidationException err = new ValidationException("The value '" + value + "' is not a valid NCName");
97                     err.setErrorCode("FORG0001");
98                     return err;
99                 }
100                 return null;
101             case Type.NMTOKEN:
102                 if (!XMLChar.isValidNmtoken(value.toString())) {
103                     ValidationException err = new ValidationException("The value '" + value + "' is not a valid NMTOKEN");
104                     err.setErrorCode("FORG0001");
105                     return err;
106                 }
107                 return null;
108             default:
109                 throw new IllegalArgumentException JavaDoc("Unknown string value type " + type);
110         }
111     }
112
113
114     /**
115      * Return the type of the expression
116      */

117
118     public ItemType getItemType() {
119         return (AtomicType) BuiltInSchemaFactory.getSchemaType(type);
120     }
121
122     /**
123      * Convert to target data type
124      * @param requiredType an integer identifying the required atomic type
125      * @param conversion
126      * @return an AtomicValue, a value of the required type; or an ErrorValue
127      */

128
129     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
130         int req = requiredType.getPrimitiveType();
131         if (req == Type.STRING) {
132             return StringValue.makeStringValue(value);
133         } else if (req == Type.UNTYPED_ATOMIC) {
134             return new UntypedAtomicValue(value);
135         } else {
136             return super.convertPrimitive(requiredType, validate, conversion);
137         }
138     }
139
140     public String JavaDoc toString() {
141         return getItemType().toString() + '(' + super.toString() + ')';
142     }
143
144
145 }
146
147 //
148
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
149
// you may not use this file except in compliance with the License. You may obtain a copy of the
150
// License at http://www.mozilla.org/MPL/
151
//
152
// Software distributed under the License is distributed on an "AS IS" basis,
153
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
154
// See the License for the specific language governing rights and limitations under the License.
155
//
156
// The Original Code is: all this file.
157
//
158
// The Initial Developer of the Original Code is Michael H. Kay.
159
//
160
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
161
//
162
// Contributor(s): none.
163
//
164

165
Popular Tags