1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.NamespaceConstant; 5 import net.sf.saxon.trans.StaticError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.AnyItemType; 8 import net.sf.saxon.type.AtomicType; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.value.SequenceType; 11 12 import java.io.PrintStream ; 13 import java.util.ArrayList ; 14 15 16 19 20 public abstract class SystemFunction extends FunctionCall { 21 22 29 30 public static FunctionCall makeSystemFunction(String name, int arity, NamePool pool) { 31 StandardFunction.Entry entry = StandardFunction.getFunction(name, arity); 32 if (entry==null) { 33 return null; 34 } 35 Class functionClass = entry.implementationClass; 36 try { 37 SystemFunction f = (SystemFunction)functionClass.newInstance(); 38 f.setDetails(entry); 39 f.setFunctionNameCode(pool.allocate("", NamespaceConstant.FN, name)); 43 return f; 45 } catch (IllegalAccessException err) { 46 return null; 47 } catch (InstantiationException err) { 48 return null; 49 } 50 } 51 52 55 56 private StandardFunction.Entry details; 57 protected int operation; 58 59 62 63 public void setDetails(StandardFunction.Entry entry) { 64 details = entry; 65 operation = details.opcode; 66 } 67 68 71 72 protected StandardFunction.Entry getDetails() { 73 return details; 74 } 75 76 81 82 86 89 90 public void checkArguments(StaticContext env) throws XPathException { 91 checkArgumentCount(details.minArguments, details.maxArguments, env); 92 for (int i=0; i<argument.length; i++) { 93 checkArgument(i, env); 94 } 95 } 96 97 101 102 private void checkArgument(int arg, StaticContext env) throws XPathException { 103 RoleLocator role = new RoleLocator(RoleLocator.FUNCTION, new Integer (getFunctionNameCode()), arg, env.getNamePool()); 104 role.setSourceLocator(this); 105 argument[arg] = TypeChecker.staticTypeCheck( 106 argument[arg], 107 getRequiredType(arg), 108 env.isInBackwardsCompatibleMode(), 109 role, env); 110 argument[arg] = argument[arg].simplify(env); 111 } 112 113 116 117 protected SequenceType getRequiredType(int arg) { 118 return details.argumentTypes[arg]; 119 } 121 122 125 126 public ItemType getItemType() { 127 if (details == null) { 128 return AnyItemType.getInstance(); 130 } 131 ItemType type = details.itemType; 132 if (type == StandardFunction.SAME_AS_FIRST_ARGUMENT) { 133 if (argument.length > 0) { 134 return argument[0].getItemType(); 135 } else { 136 return AnyItemType.getInstance(); 137 } 139 } else { 140 return type; 141 } 142 } 143 144 147 148 public int computeCardinality() { 149 if (details==null) { 150 return StaticProperty.ALLOWS_ZERO_OR_MORE; 152 } 153 return details.cardinality; 154 } 155 156 164 165 public int computeSpecialProperties() { 166 int p = super.computeSpecialProperties(); 167 if (getItemType() instanceof AtomicType) { 168 return p | StaticProperty.NON_CREATIVE; 169 } 170 for (int i=0; i<argument.length; i++) { 171 if ((argument[i].getSpecialProperties() & StaticProperty.NON_CREATIVE) != 0) { 172 return p; 174 } 175 } 176 return p | StaticProperty.NON_CREATIVE; 177 } 178 179 182 183 protected final void useContextItemAsDefault() { 184 if (argument.length==0) { 185 argument = new Expression[1]; 186 argument[0] = new ContextItemExpression(); 187 ExpressionTool.copyLocationInfo(this, argument[0]); 188 ((ContextItemExpression)argument[0]).setParentExpression(this); 189 } 190 } 194 195 199 200 protected final void addContextDocumentArgument(int pos, String augmentedName) 201 throws StaticError { 202 if (argument.length > pos) { 203 return; 204 } 206 if (argument.length != pos) { 207 throw new StaticError("Too few arguments in call to " + augmentedName + "() function"); 208 } 209 Expression[] newArgs = new Expression[pos+1]; 210 System.arraycopy(argument, 0, newArgs, 0, argument.length); 211 final RootExpression rootExpression = new RootExpression(); 212 ExpressionTool.copyLocationInfo(this, newArgs[pos]); 213 rootExpression.setParentExpression(this); 214 newArgs[pos] = rootExpression; 215 argument = newArgs; 216 setDetails(StandardFunction.getFunction(augmentedName, newArgs.length)); 217 } 218 219 222 223 public void display(int level, NamePool pool, PrintStream out) { 224 out.println(ExpressionTool.indent(level) + "function " + getDisplayName(pool)); 225 for (int a=0; a<argument.length; a++) { 226 argument[a].display(level+1, pool, out); 227 } 228 } 229 230 235 236 public static void main(String [] args) throws Exception { 237 ArrayList a = new ArrayList (20); 238 a.add(new Adjust()); 239 a.add(new Aggregate()); 240 a.add(new Available()); 241 a.add(new BaseURI()); 242 a.add(new BooleanFn()); 243 a.add(new Collection()); 244 a.add(new Compare()); 245 a.add(new Component()); 246 a.add(new Concat()); 247 a.add(new Contains()); 248 a.add(new Current()); 249 a.add(new CurrentDateTime()); 250 a.add(new CurrentGroup()); 251 a.add(new Data()); 252 a.add(new DeepEqual()); 253 a.add(new DefaultCollation()); 254 a.add(new DistinctValues()); 255 a.add(new Doc()); 256 a.add(new Document()); 257 a.add(new Error ()); 258 a.add(new EscapeURI()); 259 a.add(new Evaluate()); 260 a.add(new Existence()); 261 a.add(new ForceCase()); 262 a.add(new FormatDate()); 263 a.add(new FormatNumber2()); 264 a.add(new Id()); 265 a.add(new Idref()); 266 a.add(new IndexOf()); 267 a.add(new InScopePrefixes()); 268 a.add(new Insert()); 269 a.add(new KeyFn()); 270 a.add(new Lang()); 271 a.add(new Last()); 272 a.add(new Matches()); 273 a.add(new Minimax()); 274 a.add(new NamePart()); 275 a.add(new NamespaceForPrefix()); 276 a.add(new NormalizeSpace()); 277 a.add(new NumberFn()); 278 a.add(new Parse()); 279 a.add(new Position()); 280 a.add(new QNameFn()); 281 a.add(new RegexGroup()); 282 a.add(new Remove()); 283 a.add(new Replace()); 284 a.add(new ResolveQName()); 285 a.add(new ResolveURI()); 286 a.add(new Reverse()); 287 a.add(new Root()); 288 a.add(new Rounding()); 289 a.add(new Serialize()); 290 a.add(new StaticBaseURI()); 291 a.add(new StringFn()); 292 a.add(new StringJoin()); 293 a.add(new StringLength()); 294 a.add(new Subsequence()); 295 a.add(new Substring()); 296 a.add(new SystemProperty()); 297 a.add(new Tokenize()); 298 a.add(new Trace()); 299 a.add(new Translate()); 300 a.add(new TreatFn()); 301 a.add(new Unicode()); 302 a.add(new Unordered()); 303 a.add(new UnparsedEntity()); 304 a.add(new UnparsedText()); 305 } 306 307 } 308 309 | Popular Tags |