1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.style.SAXONFunction; 4 import com.icl.saxon.om.NodeInfo; 5 import javax.xml.transform.TransformerException ; 6 7 8 import java.util.*; 9 10 11 14 15 public class StyleSheetFunctionCall extends Function { 16 17 private SAXONFunction function; 18 private Controller boundController = null; 19 private NodeInfo boundContextNode = null; 20 private int boundContextPosition = -1; 21 private int boundContextSize = -1; 22 23 26 27 public void setFunction(SAXONFunction f) { 28 function = f; 29 } 30 31 36 37 public String getName() { 38 return function.getAttribute("name"); } 40 41 45 46 public int getDataType() { 47 return Value.ANY; 48 } 49 50 51 54 55 public Expression simplify() throws XPathException { 56 for (int i=0; i<getNumberOfArguments(); i++) { 57 argument[i] = argument[i].simplify(); 58 } 59 return this; 60 } 61 62 63 68 69 public int getDependencies() { 70 71 73 int dep = Context.NO_DEPENDENCIES; 74 if (boundController==null) dep |= Context.CONTROLLER; 75 if (boundContextNode==null) dep |= Context.CONTEXT_NODE; 76 if (boundContextPosition==-1) dep |= Context.POSITION; 77 if (boundContextSize==-1) dep |= Context.LAST; 78 79 for (int i=0; i<getNumberOfArguments(); i++) { 80 dep |= argument[i].getDependencies(); 81 } 82 return dep; 83 84 } 85 86 89 90 public Expression reduce(int dependencies, Context context) 91 throws XPathException { 92 93 StyleSheetFunctionCall nf = new StyleSheetFunctionCall(); 94 nf.setFunction(function); 95 nf.setStaticContext(getStaticContext()); 96 nf.boundController = boundController; 97 nf.boundContextNode = boundContextNode; 98 nf.boundContextPosition = boundContextPosition; 99 nf.boundContextSize = boundContextSize; 100 101 for (int a=0; a<getNumberOfArguments(); a++) { 102 nf.addArgument(argument[a].reduce(dependencies, context)); 103 } 104 105 if (boundController==null && (dependencies & Context.CONTROLLER) != 0) { 106 nf.boundController = context.getController(); 107 } 108 if (boundContextNode==null && (dependencies & Context.CONTEXT_NODE) != 0) { 109 nf.boundContextNode = context.getContextNodeInfo(); 110 } 111 if (boundContextPosition==-1 && (dependencies & Context.POSITION) != 0) { 112 nf.boundContextPosition = context.getContextPosition(); 113 } 114 if (boundContextSize==-1 && (dependencies & Context.LAST) != 0) { 115 nf.boundContextSize = context.getLast(); 116 } 117 118 return nf.simplify(); 119 } 120 121 128 129 public Value evaluate(Context c) throws XPathException { 130 Context context = c.newContext(); 131 if (boundController!=null) { 132 context.setController(boundController); 133 } 134 if (boundContextNode!=null) { 135 context.setCurrentNode(boundContextNode); 136 context.setContextNode(boundContextNode); 137 } 138 if (boundContextPosition!=-1) { 139 context.setPosition(boundContextPosition); 140 } 141 if (boundContextSize!=-1) { 142 context.setLast(boundContextSize); 143 } 144 145 ParameterSet ps = new ParameterSet(); 146 for (int i=0; i<getNumberOfArguments(); i++) { 147 int param = function.getNthParameter(i); 148 if (param==-1) { 149 throw new XPathException("Too many arguments"); 150 } 151 ps.put(param, argument[i].evaluate(c)); 152 } 153 try { 154 return function.call(ps, context); 155 } catch (TransformerException err) { 156 throw new XPathException(err); 157 } 158 } 159 160 } 161 162 | Popular Tags |