1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.SourceLocator ; 22 import javax.xml.transform.TransformerException ; 23 24 import org.apache.xalan.res.XSLMessages; 25 import org.apache.xalan.res.XSLTErrorResources; 26 import org.apache.xalan.transformer.TransformerImpl; 27 import org.apache.xml.utils.QName; 28 import org.apache.xpath.VariableStack; 29 import org.apache.xpath.XPathContext; 30 import org.apache.xpath.objects.XObject; 31 32 43 public class ElemCallTemplate extends ElemForEach 44 { 45 46 51 public QName m_templateName = null; 52 53 60 public void setName(QName name) 61 { 62 m_templateName = name; 63 } 64 65 72 public QName getName() 73 { 74 return m_templateName; 75 } 76 77 81 private ElemTemplate m_template = null; 82 83 89 public int getXSLToken() 90 { 91 return Constants.ELEMNAME_CALLTEMPLATE; 92 } 93 94 99 public String getNodeName() 100 { 101 return Constants.ELEMNAME_CALLTEMPLATE_STRING; 102 } 103 104 110 public void compose(StylesheetRoot sroot) throws TransformerException 111 { 112 super.compose(sroot); 113 114 int length = getParamElemCount(); 117 for (int i = 0; i < length; i++) 118 { 119 ElemWithParam ewp = getParamElem(i); 120 ewp.compose(sroot); 121 } 122 123 if ((null != m_templateName) && (null == m_template)) { 124 m_template = 125 this.getStylesheetRoot().getTemplateComposed(m_templateName); 126 127 if (null == m_template) { 128 String themsg = 129 XSLMessages.createMessage( 130 XSLTErrorResources.ER_ELEMTEMPLATEELEM_ERR, 131 new Object [] { m_templateName }); 132 133 throw new TransformerException (themsg, this); 134 } 136 137 length = getParamElemCount(); 138 for (int i = 0; i < length; i++) 139 { 140 ElemWithParam ewp = getParamElem(i); 141 ewp.m_index = -1; 142 int etePos = 0; 145 for (ElemTemplateElement ete = m_template.getFirstChildElem(); 146 null != ete; ete = ete.getNextSiblingElem()) 147 { 148 if(ete.getXSLToken() == Constants.ELEMNAME_PARAMVARIABLE) 149 { 150 ElemParam ep = (ElemParam)ete; 151 if(ep.getName().equals(ewp.getName())) 152 { 153 ewp.m_index = etePos; 154 } 155 } 156 else 157 break; 158 etePos++; 159 } 160 161 } 162 } 163 } 164 165 168 public void endCompose(StylesheetRoot sroot) throws TransformerException 169 { 170 int length = getParamElemCount(); 171 for (int i = 0; i < length; i++) 172 { 173 ElemWithParam ewp = getParamElem(i); 174 ewp.endCompose(sroot); 175 } 176 177 super.endCompose(sroot); 178 } 179 180 190 public void execute( 191 TransformerImpl transformer) 192 throws TransformerException 193 { 194 195 if (TransformerImpl.S_DEBUG) 196 transformer.getTraceManager().fireTraceEvent(this); 197 198 if (null != m_template) 199 { 200 XPathContext xctxt = transformer.getXPathContext(); 201 VariableStack vars = xctxt.getVarStack(); 202 203 int thisframe = vars.getStackFrame(); 204 int nextFrame = vars.link(m_template.m_frameSize); 205 206 if(m_template.m_inArgsSize > 0) 209 { 210 vars.clearLocalSlots(0, m_template.m_inArgsSize); 211 212 if(null != m_paramElems) 213 { 214 int currentNode = xctxt.getCurrentNode(); 215 vars.setStackFrame(thisframe); 216 int size = m_paramElems.length; 217 218 for (int i = 0; i < size; i++) 219 { 220 ElemWithParam ewp = m_paramElems[i]; 221 if(ewp.m_index >= 0) 222 { 223 if (TransformerImpl.S_DEBUG) 224 transformer.getTraceManager().fireTraceEvent(ewp); 225 XObject obj = ewp.getValue(transformer, currentNode); 226 if (TransformerImpl.S_DEBUG) 227 transformer.getTraceManager().fireTraceEndEvent(ewp); 228 229 vars.setLocalVariable(ewp.m_index, obj, nextFrame); 233 } 234 } 235 vars.setStackFrame(nextFrame); 236 } 237 } 238 239 SourceLocator savedLocator = xctxt.getSAXLocator(); 240 241 try 242 { 243 xctxt.setSAXLocator(m_template); 244 245 transformer.pushElemTemplateElement(m_template); 247 m_template.execute(transformer); 248 } 249 finally 250 { 251 transformer.popElemTemplateElement(); 252 xctxt.setSAXLocator(savedLocator); 253 vars.unlink(thisframe); 263 } 264 } 265 else 266 { 267 transformer.getMsgMgr().error(this, XSLTErrorResources.ER_TEMPLATE_NOT_FOUND, 268 new Object []{ m_templateName }); } 270 271 if (TransformerImpl.S_DEBUG) 272 transformer.getTraceManager().fireTraceEndEvent(this); 273 274 } 275 276 278 protected ElemWithParam[] m_paramElems = null; 279 280 284 public int getParamElemCount() 285 { 286 return (m_paramElems == null) ? 0 : m_paramElems.length; 287 } 288 289 296 public ElemWithParam getParamElem(int i) 297 { 298 return m_paramElems[i]; 299 } 300 301 306 public void setParamElem(ElemWithParam ParamElem) 307 { 308 if (null == m_paramElems) 309 { 310 m_paramElems = new ElemWithParam[1]; 311 m_paramElems[0] = ParamElem; 312 } 313 else 314 { 315 int length = m_paramElems.length; 318 ElemWithParam[] ewp = new ElemWithParam[length + 1]; 319 System.arraycopy(m_paramElems, 0, ewp, 0, length); 320 m_paramElems = ewp; 321 ewp[length] = ParamElem; 322 } 323 } 324 325 339 public ElemTemplateElement appendChild(ElemTemplateElement newChild) 340 { 341 342 int type = ((ElemTemplateElement) newChild).getXSLToken(); 343 344 if (Constants.ELEMNAME_WITHPARAM == type) 345 { 346 setParamElem((ElemWithParam) newChild); 347 } 348 349 return super.appendChild(newChild); 352 } 353 354 358 public void callChildVisitors(XSLTVisitor visitor, boolean callAttrs) 359 { 360 371 super.callChildVisitors(visitor, callAttrs); 372 } 373 } 374 | Popular Tags |