KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.value;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.trans.DynamicError;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.*;
6 import net.sf.saxon.ConversionContext;
7 import net.sf.saxon.functions.EscapeURI;
8
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URI JavaDoc;
11 import java.net.URISyntaxException JavaDoc;
12 import java.net.URL JavaDoc;
13
14
15
16 /**
17 * An XPath value of type xs:anyURI.
18  *
19  * <p>This is implemented as a subtype of StringValue even though xs:anyURI is not a subtype of
20  * xs:string in the XPath type hierarchy. This enables type promotion from URI to String to happen
21  * automatically in most cases where it is appropriate.</p>
22  *
23  * <p>This implementation of xs:anyURI allows any string to be contained in the value space. It is possible
24  * to validate that the string is a "valid URI" in the sense of XML Schema Part 2 (which refers to the XLink
25  * specification and to RFC 2396); however, this validation is optional, and is not carried out by default.
26  * In particular, there is no constraint that namespace URIs, collation URIs, and the like should be valid
27  * URIs. However, casting from strings to xs:anyURI does invoke validation.</p>
28 */

29
30 public final class AnyURIValue extends StringValue {
31
32     public static final AnyURIValue EMPTY_URI = new AnyURIValue("");
33
34     /**
35     * Constructor
36     * @param value the String value. Null is taken as equivalent to "".
37     */

38
39     public AnyURIValue(CharSequence JavaDoc value) {
40         this.value = (value==null ? "" : trimWhitespace(value).toString());
41     }
42
43     /**
44      * Check whether a string consititutes a valid URI
45      */

46
47     public static boolean isValidURI(CharSequence JavaDoc value) {
48
49         String JavaDoc sv = value.toString().trim();
50
51         // Allow zero-length strings (RFC2396 is ambivalent on this point)
52
if (sv.length() == 0) {
53             return true;
54         }
55
56         // Allow a string if the java.net.URI class accepts it
57
try {
58             new URI(sv);
59             return true;
60         } catch (URISyntaxException JavaDoc e) {
61             // keep trying
62
}
63
64         // Allow a string if it can be escaped into a form that java.net.URI accepts
65
sv = EscapeURI.escape(sv, false).toString();
66         try {
67             new URI(sv);
68             return true;
69         } catch (URISyntaxException JavaDoc e) {
70             return false;
71         }
72     }
73
74     /**
75     * Convert to target data type
76     * @param requiredType integer code representing the item type required
77     * @param conversion
78      * @return the result of the conversion, or an ErrorValue
79     */

80
81     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
82         int req = requiredType.getPrimitiveType();
83         switch(req) {
84         case Type.ATOMIC:
85         case Type.ITEM:
86         case Type.ANY_URI:
87             return this;
88         case Type.UNTYPED_ATOMIC:
89             return new UntypedAtomicValue(value);
90         case Type.STRING:
91             return new StringValue(value);
92         case Type.NORMALIZED_STRING:
93         case Type.TOKEN:
94         case Type.LANGUAGE:
95         case Type.NAME:
96         case Type.NCNAME:
97         case Type.ID:
98         case Type.IDREF:
99         case Type.ENTITY:
100         case Type.NMTOKEN:
101             return RestrictedStringValue.makeRestrictedString(value, req, validate);
102
103         default:
104             ValidationException err = new ValidationException("Cannot convert anyURI to " +
105                                      requiredType.getDisplayName());
106             err.setErrorCode("FORG0001");
107             return new ValidationErrorValue(err);
108         }
109     }
110
111     /**
112     * Return the type of the expression
113     * @return Type.ANY_URI_TYPE (always)
114     */

115
116     public ItemType getItemType() {
117         return Type.ANY_URI_TYPE;
118     }
119
120     /**
121      * Determine if two AnyURIValues are equal, according to XML Schema rules. (This method
122      * is not used for XPath comparisons, which are always under the control of a collation.)
123      * @throws ClassCastException if the values are not comparable
124      */

125
126     public boolean equals(Object JavaDoc other) {
127         // Force a ClassCastException if the other value isn't an anyURI or derived from anyURI
128
AnyURIValue otherVal = (AnyURIValue) ((AtomicValue) other).getPrimitiveValue();
129         // cannot use equals() directly on two unlike CharSequences
130
return getStringValue().equals(otherVal.getStringValue());
131     }
132
133     /**
134      * Get the effective boolean value of the value
135      *
136      * @param context the evaluation context (not used in this implementation)
137      * @return true, unless the value is boolean false, numeric zero, or
138      * zero-length string
139      */

140     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
141         DynamicError err = new DynamicError(
142                 "Effective boolean value is not defined for a value of type xs:anyURI");
143         err.setIsTypeError(true);
144         err.setXPathContext(context);
145         throw err;
146     }
147
148     /**
149     * Convert to Java object (for passing to external functions)
150     * @param target the Java class to which conversion is required
151     * @return the result of the conversion
152     * @throws XPathException if conversion to this target type is not possible
153     */

154
155     public Object JavaDoc convertToJava(Class JavaDoc target, XPathContext context) throws XPathException {
156         if (target==Object JavaDoc.class) {
157             return value;
158         } else if (target.isAssignableFrom(StringValue.class)) {
159             return this;
160         } else if (target==URI.class) {
161             try {
162                 return new URI(value.toString());
163             } catch (URISyntaxException JavaDoc err) {
164                 throw new DynamicError("The anyURI value '" + value + "' is not an acceptable Java URI");
165             }
166         } else if (target==URL JavaDoc.class) {
167             try {
168                 return new URL JavaDoc(value.toString());
169             } catch (MalformedURLException JavaDoc err) {
170                 throw new DynamicError("The anyURI value '" + value + "' is not an acceptable Java URL");
171             }
172         } else if (target==String JavaDoc.class) {
173             return value;
174         } else if (target==CharSequence JavaDoc.class) {
175             return value;
176         } else {
177              Object JavaDoc o = super.convertToJava(target, context);
178             if (o == null) {
179                 throw new DynamicError("Conversion of anyURI to " + target.getName() +
180                         " is not supported");
181             }
182             return o;
183         }
184     }
185
186
187 }
188
189 //
190
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
191
// you may not use this file except in compliance with the License. You may obtain a copy of the
192
// License at http://www.mozilla.org/MPL/
193
//
194
// Software distributed under the License is distributed on an "AS IS" basis,
195
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
196
// See the License for the specific language governing rights and limitations under the License.
197
//
198
// The Original Code is: all this file.
199
//
200
// The Initial Developer of the Original Code is Michael H. Kay
201
//
202
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
203
//
204
// Contributor(s): none.
205
//
206

207
Popular Tags