1 package com.icl.saxon.functions; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.om.NodeInfo; 4 import com.icl.saxon.om.NodeEnumeration; 5 import com.icl.saxon.expr.*; 6 7 8 public class GenerateId extends Function { 9 10 13 14 public String getName() { 15 return "generate-id"; 16 }; 17 18 22 23 public int getDataType() { 24 return Value.STRING; 25 } 26 27 30 31 public Expression simplify() throws XPathException { 32 checkArgumentCount(0, 1); 33 return this; 34 } 35 36 39 40 public String evaluateAsString(Context c) throws XPathException { 41 int numArgs = getNumberOfArguments(); 42 43 if (numArgs==0) { 44 NodeInfo node = c.getContextNodeInfo(); 45 String localId = node.generateId(); 46 return "d" + 47 c.getController().getDocumentPool(). 48 getDocumentNumber(node.getDocumentRoot()) + 49 localId; 50 } 51 52 NodeEnumeration enum = argument[0].enumerate(c, true); 53 if (enum.hasMoreElements()) { 54 NodeInfo node = enum.nextElement(); 55 String localId = node.generateId(); 56 return "d" + 57 c.getController().getDocumentPool(). 58 getDocumentNumber(node.getDocumentRoot()) + 59 localId; 60 } else { 61 return ""; 62 } 63 } 64 65 68 69 public Value evaluate(Context c) throws XPathException { 70 return new StringValue(evaluateAsString(c)); 71 } 72 73 76 77 public int getDependencies() { 78 if (getNumberOfArguments()==0) { 79 return Context.CONTEXT_NODE; 80 } else { 81 return argument[0].getDependencies(); 82 } 83 } 84 85 88 89 public Expression reduce(int dep, Context c) throws XPathException { 90 GenerateId f = new GenerateId(); 91 if (getNumberOfArguments()==1) { 92 f.addArgument(argument[0].reduce(dep, c)); 93 f.setStaticContext(getStaticContext()); 94 return f; 95 } else { 96 if ((dep & Context.CONTEXT_NODE)!=0) { 97 return evaluate(c); 98 } else { 99 return this; 100 } 101 } 102 103 } 104 105 } 106 107 108 109 | Popular Tags |