1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.tree.NodeImpl; 4 import com.icl.saxon.Context; 5 import com.icl.saxon.Controller; 6 import com.icl.saxon.Bindery; 7 import com.icl.saxon.ParameterSet; 8 import com.icl.saxon.om.NamespaceException; 9 import com.icl.saxon.om.NodeInfo; 10 import com.icl.saxon.om.NamePool; 11 import com.icl.saxon.output.Outputter; 12 import com.icl.saxon.output.ErrorEmitter; 13 import com.icl.saxon.expr.Value; 14 import com.icl.saxon.expr.StringValue; 15 import com.icl.saxon.trace.TraceListener; import javax.xml.transform.TransformerException ; 17 import javax.xml.transform.TransformerConfigurationException ; 18 19 24 25 public class SAXONFunction extends StyleElement { 26 27 int functionFingerprint = -1; 28 Procedure procedure = new Procedure(); 29 30 36 37 protected void processExtensionElementAttribute(int nc) 38 throws TransformerConfigurationException { 39 extensionNamespaces = new short[1]; 40 NamePool pool = getNamePool(); 41 short uriCode = pool.getURICode(getNameCode()); 42 extensionNamespaces[0] = uriCode; 43 } 44 45 public void prepareAttributes() throws TransformerConfigurationException { 46 47 StandardNames sn = getStandardNames(); 48 AttributeCollection atts = getAttributeList(); 49 50 String nameAtt = null; 51 52 for (int a=0; a<atts.getLength(); a++) { 53 int nc = atts.getNameCode(a); 54 int f = nc & 0xfffff; 55 if (f==sn.NAME) { 56 nameAtt = atts.getValue(a); 57 if (nameAtt.indexOf(':')<0) { 58 compileError("Function name must have a namespace prefix"); 59 } 60 try { 61 int functionCode = makeNameCode(nameAtt, false); 62 functionFingerprint = functionCode & 0xfffff; 63 } catch (NamespaceException err) { 64 compileError(err.getMessage()); 65 } 66 } else { 67 checkUnknownAttribute(nc); 68 } 69 } 70 71 if (nameAtt==null) { 72 reportAbsence("name"); 73 } 74 } 75 76 80 81 public boolean mayContainTemplateBody() { 82 return true; 83 } 84 85 public void validate() throws TransformerConfigurationException { 86 checkTopLevel(); 87 } 88 89 public void preprocess() throws TransformerConfigurationException { 90 getPrincipalStyleSheet().allocateLocalSlots(procedure.getNumberOfVariables()); 91 } 92 93 public void process(Context context) {} 94 95 98 99 public Procedure getProcedure() { 100 return procedure; 101 } 102 103 public int getFunctionFingerprint() { 104 if (functionFingerprint==-1) { 105 try { 107 prepareAttributes(); 108 } catch (TransformerConfigurationException err) { 109 return -1; } 111 } 112 return functionFingerprint; 113 } 114 115 119 120 public int getNthParameter(int n) { 121 NodeImpl node = (NodeImpl)getFirstChild(); 122 int pos = 0; 123 while (node!=null) { 124 if (node instanceof XSLParam) { 125 if (pos==n) { 126 return ((XSLParam)node).getVariableFingerprint(); 127 } else { 128 pos++; 129 } 130 } 131 node = (NodeImpl)node.getNextSibling(); 132 } 133 return -1; 134 } 135 136 139 140 public Value call(ParameterSet params, Context context) throws TransformerException { 141 Bindery bindery = context.getBindery(); 142 bindery.openStackFrame(params); 143 Controller controller = context.getController(); 144 Outputter old = controller.getOutputter(); 145 controller.changeOutputDestination(null, new ErrorEmitter()); 146 147 if (controller.isTracing()) { TraceListener listener = controller.getTraceListener(); 149 listener.enter(this, context); 150 processChildren(context); 151 listener.leave(this, context); 152 } else { 153 processChildren(context); 154 } 155 156 controller.resetOutputDestination(old); 157 bindery.closeStackFrame(); 158 Value result = context.getReturnValue(); 159 if (result==null) { 160 result = new StringValue(""); 161 } 162 context.setReturnValue(null); 163 return result; 164 } 165 166 } 167 168 | Popular Tags |