1 package net.sf.saxon.instruct; 2 import net.sf.saxon.Controller; 3 import net.sf.saxon.expr.*; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.om.NodeInfo; 7 import net.sf.saxon.style.StandardNames; 8 import net.sf.saxon.trans.DynamicError; 9 import net.sf.saxon.trans.Mode; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.type.ItemType; 12 13 import java.io.PrintStream ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 20 21 public class ApplyImports extends Instruction { 22 23 WithParam[] actualParams = null; 24 WithParam[] tunnelParams = null; 25 private boolean backwardsCompatible; 26 27 public ApplyImports(boolean backwardsCompatible) { 28 this.backwardsCompatible = backwardsCompatible; 29 } 30 31 34 35 public void setActualParameters( 36 WithParam[] actualParams, 37 WithParam[] tunnelParams ) { 38 this.actualParams = actualParams; 39 this.tunnelParams = tunnelParams; 40 } 41 42 45 public int getInstructionNameCode() { 46 return StandardNames.XSL_APPLY_IMPORTS; 47 } 48 49 57 58 public Expression simplify(StaticContext env) throws XPathException { 59 WithParam.simplify(actualParams, env); 60 WithParam.simplify(tunnelParams, env); 61 return this; 62 } 63 64 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 65 WithParam.typeCheck(actualParams, env, contextItemType); 66 WithParam.typeCheck(tunnelParams, env, contextItemType); 67 return this; 68 } 69 70 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 71 WithParam.optimize(opt, actualParams, env, contextItemType); 72 WithParam.optimize(opt, tunnelParams, env, contextItemType); 73 return this; 74 } 75 76 81 82 public final boolean createsNewNodes() { 83 return true; 84 } 85 86 91 92 protected void promoteInst(PromotionOffer offer) throws XPathException { 93 WithParam.promoteParams(actualParams, offer); 94 WithParam.promoteParams(tunnelParams, offer); 95 } 96 97 102 103 public Iterator iterateSubExpressions() { 104 ArrayList list = new ArrayList (10); 105 WithParam.getXPathExpressions(actualParams, list); 106 WithParam.getXPathExpressions(tunnelParams, list); 107 return list.iterator(); 108 } 109 110 public TailCall processLeavingTail(XPathContext context) throws XPathException { 111 112 Controller controller = context.getController(); 113 115 ParameterSet params = assembleParams(context, actualParams); 116 ParameterSet tunnels = assembleTunnelParams(context, tunnelParams); 117 118 Template currentTemplate = context.getCurrentTemplate(); 119 if (currentTemplate==null) { 120 DynamicError e = new DynamicError("There is no current template rule"); 121 e.setXPathContext(context); 122 e.setErrorCode("XTDE0560"); 123 e.setLocator(this); 124 throw e; 125 } 126 127 int min = currentTemplate.getMinImportPrecedence(); 128 int max = currentTemplate.getPrecedence()-1; 129 Mode mode = context.getCurrentMode(); 130 if (mode == null) { 131 mode = controller.getRuleManager().getMode(Mode.DEFAULT_MODE); 132 } 133 if (context.getCurrentIterator()==null) { 134 DynamicError e = new DynamicError("Cannot call xsl:apply-imports when there is no context item"); 135 e.setXPathContext(context); 136 e.setErrorCode("XTDE0565"); 137 e.setLocator(this); 138 throw e; 139 } 140 Item currentItem = context.getCurrentIterator().current(); 141 if (!(currentItem instanceof NodeInfo)) { 142 DynamicError e = new DynamicError("Cannot call xsl:apply-imports when context item is not a node"); 143 e.setXPathContext(context); 144 e.setErrorCode("XTDE0565"); 145 e.setLocator(this); 146 throw e; 147 } 148 NodeInfo node = (NodeInfo)currentItem; 149 Template nh = controller.getRuleManager().getTemplateRule(node, mode, min, max, context); 150 151 if (nh==null) { ApplyTemplates.defaultAction(node, params, tunnels, context, backwardsCompatible, getLocationId()); 153 } else { 154 XPathContextMajor c2 = context.newContext(); 155 c2.setOrigin(this); 156 c2.setLocalParameters(params); 157 c2.setTunnelParameters(tunnels); 158 c2.openStackFrame(nh.getStackFrameMap()); 159 nh.process(c2); 160 } 161 return null; 162 } 164 165 166 167 174 175 public void display(int level, NamePool pool, PrintStream out) { 176 out.println(ExpressionTool.indent(level) + "apply-imports"); 177 } 178 179 } 180 181 | Popular Tags |