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 ; 10 import java.net.URI ; 11 import java.net.URISyntaxException ; 12 import java.net.URL ; 13 14 15 16 29 30 public final class AnyURIValue extends StringValue { 31 32 public static final AnyURIValue EMPTY_URI = new AnyURIValue(""); 33 34 38 39 public AnyURIValue(CharSequence value) { 40 this.value = (value==null ? "" : trimWhitespace(value).toString()); 41 } 42 43 46 47 public static boolean isValidURI(CharSequence value) { 48 49 String sv = value.toString().trim(); 50 51 if (sv.length() == 0) { 53 return true; 54 } 55 56 try { 58 new URI(sv); 59 return true; 60 } catch (URISyntaxException e) { 61 } 63 64 sv = EscapeURI.escape(sv, false).toString(); 66 try { 67 new URI(sv); 68 return true; 69 } catch (URISyntaxException e) { 70 return false; 71 } 72 } 73 74 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 115 116 public ItemType getItemType() { 117 return Type.ANY_URI_TYPE; 118 } 119 120 125 126 public boolean equals(Object other) { 127 AnyURIValue otherVal = (AnyURIValue) ((AtomicValue) other).getPrimitiveValue(); 129 return getStringValue().equals(otherVal.getStringValue()); 131 } 132 133 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 154 155 public Object convertToJava(Class target, XPathContext context) throws XPathException { 156 if (target==Object .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 err) { 164 throw new DynamicError("The anyURI value '" + value + "' is not an acceptable Java URI"); 165 } 166 } else if (target==URL .class) { 167 try { 168 return new URL (value.toString()); 169 } catch (MalformedURLException err) { 170 throw new DynamicError("The anyURI value '" + value + "' is not an acceptable Java URL"); 171 } 172 } else if (target==String .class) { 173 return value; 174 } else if (target==CharSequence .class) { 175 return value; 176 } else { 177 Object 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 207 | Popular Tags |