1 package net.sf.saxon.functions; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.expr.CastExpression; 5 import net.sf.saxon.expr.Expression; 6 import net.sf.saxon.om.NamespaceConstant; 7 import net.sf.saxon.style.StandardNames; 8 import net.sf.saxon.trans.StaticError; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.type.AtomicType; 11 import net.sf.saxon.type.SchemaType; 12 import net.sf.saxon.type.Type; 13 14 18 19 public class ConstructorFunctionLibrary implements FunctionLibrary { 20 21 private Configuration config; 22 23 27 28 public ConstructorFunctionLibrary(Configuration config) { 29 this.config = config; 30 } 31 32 42 43 public boolean isAvailable(int fingerprint, String uri, String local, int arity) { 44 if (arity != 1 && arity != -1) { 45 return false; 46 } 47 if (uri.equals(NamespaceConstant.SCHEMA)) { 48 AtomicType type = (AtomicType)Type.getBuiltInItemType(uri, local); 49 return type != null && type.getFingerprint() != StandardNames.XS_NOTATION; 50 } else if (NamespaceConstant.isXDTNamespace(uri)) { 51 AtomicType type = (AtomicType)Type.getBuiltInItemType(NamespaceConstant.XDT, local); 52 return type != null && type.getFingerprint() != StandardNames.XDT_ANY_ATOMIC_TYPE; 53 } 54 55 SchemaType st = config.getSchemaType(fingerprint); 56 return (st != null && st instanceof AtomicType); 57 } 58 59 75 76 public Expression bind(int nameCode, String uri, String localName, Expression[] arguments) 77 throws XPathException { 78 String targetURI = uri; 79 boolean builtInNamespace = uri.equals(NamespaceConstant.SCHEMA); 80 if (!builtInNamespace && NamespaceConstant.isXDTNamespace(uri)) { 81 targetURI = NamespaceConstant.XDT; 82 builtInNamespace = true; 83 } 84 if (builtInNamespace) { 85 if (arguments.length != 1) { 87 throw new StaticError("A constructor function must have exactly one argument"); 88 } 89 AtomicType type = (AtomicType)Type.getBuiltInItemType(targetURI, localName); 90 if (type==null || type.getFingerprint() == StandardNames.XDT_ANY_ATOMIC_TYPE || 91 type.getFingerprint() == StandardNames.XS_NOTATION) { 92 StaticError err = new StaticError("Unknown constructor function: {" + uri + '}' + localName); 93 err.setErrorCode("XPST0017"); 94 throw err; 95 } 96 97 return new CastExpression(arguments[0], type, true); 98 } 99 100 102 if (arguments.length == 1) { 103 SchemaType st = config.getSchemaType(nameCode & 0xfffff); 104 if (st != null && st instanceof AtomicType) { 105 return new CastExpression(arguments[0], (AtomicType)st, true); 106 } 107 } 108 109 return null; 110 } 111 112 119 120 public FunctionLibrary copy() { 121 return this; 122 } 123 124 } 125 | Popular Tags |