1 package net.sf.saxon.value; 2 3 import net.sf.saxon.Err; 4 import net.sf.saxon.ConversionContext; 5 import net.sf.saxon.expr.ExpressionTool; 6 import net.sf.saxon.expr.StaticContext; 7 import net.sf.saxon.expr.StaticProperty; 8 import net.sf.saxon.expr.XPathContext; 9 import net.sf.saxon.om.*; 10 import net.sf.saxon.trans.StaticError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.trans.DynamicError; 13 import net.sf.saxon.type.*; 14 15 import java.io.PrintStream ; 16 17 18 31 32 public abstract class AtomicValue extends Value implements Item { 33 34 38 39 public boolean hasBuiltInType() { 40 return true; 41 } 42 43 47 48 public int getImplementationMethod() { 49 return EVALUATE_METHOD; 50 } 51 52 58 59 public void process(XPathContext context) throws XPathException { 60 Item item = evaluateItem(context); 61 if (item != null) { 62 context.getReceiver().append(item, 0, NodeInfo.ALL_NAMESPACES); 63 } 64 } 65 66 72 73 public final int getCardinality() { 74 return StaticProperty.EXACTLY_ONE; 75 } 76 77 90 91 public final AtomicValue convert(int requiredType, ConversionContext conversion) throws XPathException { 92 SchemaType schemaType = BuiltInSchemaFactory.getSchemaType(requiredType); 93 if (schemaType instanceof BuiltInAtomicType) { 94 AtomicValue val = convertPrimitive((BuiltInAtomicType)schemaType, true, conversion); 95 if (val instanceof ValidationErrorValue) { 96 throw ((ValidationErrorValue)val).getException(); 97 } 98 return val; 99 } else { 100 throw new IllegalArgumentException ( 101 "This method can only be used for conversion to a built-in atomic type"); 102 } 103 }; 104 105 119 public abstract AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion); 120 121 134 135 public AtomicValue convert(AtomicType targetType, ConversionContext context, boolean validate) { 136 if (targetType instanceof BuiltInAtomicType) { 137 return convertPrimitive((BuiltInAtomicType)targetType, validate, context); 138 } else { 139 CharSequence lexicalValue = getStringValueCS(); 140 AtomicValue v = convertPrimitive( 141 (BuiltInAtomicType)targetType.getPrimitiveItemType(), 142 validate, 143 context); 144 if (v instanceof ValidationErrorValue) { 145 return v; 147 } 148 return targetType.makeDerivedValue(v, lexicalValue, validate); 149 } 150 } 151 152 157 158 public final int getLength() { 159 return 1; 160 } 161 162 169 170 public Item evaluateItem(XPathContext context) throws XPathException { 171 return this; 172 } 173 174 181 182 public final SequenceIterator iterate(XPathContext context) { 183 return SingletonIterator.makeIterator(this); 184 } 185 186 189 190 public final String evaluateAsString(XPathContext context) { 191 return getStringValue(); 192 } 193 194 200 201 public abstract String getStringValue(); 202 203 207 208 public CharSequence getStringValueCS() { 209 return getStringValue(); 210 } 211 212 217 218 public final SequenceIterator getTypedValue() { 219 return SingletonIterator.makeIterator(this); 220 } 221 222 232 233 public AtomicValue getPrimitiveValue() { 234 return this; 236 } 237 238 245 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 246 DynamicError err = new DynamicError("Effective boolean value is not defined for an atomic value of type " + 247 (context == null ? 248 "other than boolean, number, or string" : 249 getItemType().toString(context.getController().getNamePool()))); 250 err.setIsTypeError(true); 251 err.setXPathContext(context); 252 throw err; 253 } 255 256 260 261 public AtomicValue getComponent(int component) throws XPathException { 262 throw new UnsupportedOperationException ("Data type does not support component extraction"); 263 } 264 265 275 276 public void checkPermittedContents(SchemaType parentType, StaticContext env, boolean whole) throws XPathException { 277 if (whole) { 278 SimpleType stype = null; 279 if (parentType instanceof SimpleType) { 280 stype = (SimpleType)parentType; 281 } else if (parentType instanceof ComplexType && ((ComplexType)parentType).isSimpleContent()) { 282 stype = ((ComplexType)parentType).getSimpleContentType(); 283 } 284 if (stype != null && !stype.isNamespaceSensitive()) { 285 XPathException err = stype.validateContent(getStringValueCS(), null, env.getConfiguration()); 287 if (err != null) { 288 throw err; 289 } 290 return; 291 } 292 } 293 if (parentType instanceof ComplexType && 294 !((ComplexType)parentType).isSimpleContent() && 295 !((ComplexType)parentType).isMixedContent() && 296 !Whitespace.isWhite(getStringValueCS())) { 297 StaticError err = new StaticError("Complex type " + parentType.getDescription() + 298 " does not allow text content " + 299 Err.wrap(getStringValueCS())); 300 err.setIsTypeError(true); 301 throw err; 302 } 303 } 304 305 310 311 public String toString() { 312 return getItemType().toString() + " (\"" + getStringValueCS() + "\")"; 313 } 314 315 322 323 public boolean schemaEquals(Value obj) { 324 return equals(obj); 325 } 326 327 333 334 public final void display(int level, NamePool pool, PrintStream out) { 335 out.println(ExpressionTool.indent(level) + toString()); 336 } 337 338 } 339 340 358 | Popular Tags |