1 16 19 package org.apache.xalan.templates; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.transformer.TransformerImpl; 24 import org.apache.xml.utils.QName; 25 import org.apache.xpath.XPath; 26 import org.apache.xpath.XPathContext; 27 import org.apache.xpath.objects.XObject; 28 import org.apache.xpath.objects.XRTreeFrag; 29 import org.apache.xpath.objects.XRTreeFragSelectWrapper; 30 import org.apache.xpath.objects.XString; 31 import org.apache.xalan.res.XSLTErrorResources; 32 33 45 public class ElemVariable extends ElemTemplateElement 46 { 47 48 52 public ElemVariable(){} 53 54 57 protected int m_index; 58 59 64 int m_frameSize = -1; 65 66 67 72 public void setIndex(int index) 73 { 74 m_index = index; 75 } 76 77 82 public int getIndex() 83 { 84 return m_index; 85 } 86 87 91 private XPath m_selectPattern; 92 93 103 public void setSelect(XPath v) 104 { 105 m_selectPattern = v; 106 } 107 108 118 public XPath getSelect() 119 { 120 return m_selectPattern; 121 } 122 123 127 protected QName m_qname; 128 129 139 public void setName(QName v) 140 { 141 m_qname = v; 142 } 143 144 154 public QName getName() 155 { 156 return m_qname; 157 } 158 159 163 private boolean m_isTopLevel = false; 164 165 172 public void setIsTopLevel(boolean v) 173 { 174 m_isTopLevel = v; 175 } 176 177 184 public boolean getIsTopLevel() 185 { 186 return m_isTopLevel; 187 } 188 189 196 public int getXSLToken() 197 { 198 return Constants.ELEMNAME_VARIABLE; 199 } 200 201 206 public String getNodeName() 207 { 208 return Constants.ELEMNAME_VARIABLE_STRING; 209 } 210 211 218 public ElemVariable(ElemVariable param) throws TransformerException 219 { 220 221 m_selectPattern = param.m_selectPattern; 222 m_qname = param.m_qname; 223 m_isTopLevel = param.m_isTopLevel; 224 225 } 228 229 239 public void execute(TransformerImpl transformer) throws TransformerException 240 { 241 242 if (TransformerImpl.S_DEBUG) 243 transformer.getTraceManager().fireTraceEvent(this); 244 245 int sourceNode = transformer.getXPathContext().getCurrentNode(); 246 247 XObject var = getValue(transformer, sourceNode); 248 249 transformer.getXPathContext().getVarStack().setLocalVariable(m_index, var); 251 252 if (TransformerImpl.S_DEBUG) 253 transformer.getTraceManager().fireTraceEndEvent(this); 254 } 255 256 266 public XObject getValue(TransformerImpl transformer, int sourceNode) 267 throws TransformerException 268 { 269 270 XObject var; 271 XPathContext xctxt = transformer.getXPathContext(); 272 273 xctxt.pushCurrentNode(sourceNode); 274 275 try 276 { 277 if (null != m_selectPattern) 278 { 279 var = m_selectPattern.execute(xctxt, sourceNode, this); 280 281 var.allowDetachToRelease(false); 282 283 if (TransformerImpl.S_DEBUG) 284 transformer.getTraceManager().fireSelectedEvent(sourceNode, this, 285 "select", m_selectPattern, var); 286 } 287 else if (null == getFirstChildElem()) 288 { 289 var = XString.EMPTYSTRING; 290 } 291 else 292 { 293 294 int df; 299 300 try 306 { 307 if(m_parentNode instanceof Stylesheet) df = transformer.transformToGlobalRTF(this); 310 else 311 df = transformer.transformToRTF(this); 312 } 313 finally{ 314 } 316 317 var = new XRTreeFrag(df, xctxt, this); 318 } 319 } 320 finally 321 { 322 xctxt.popCurrentNode(); 323 } 324 325 return var; 326 } 327 328 329 335 public void compose(StylesheetRoot sroot) throws TransformerException 336 { 337 if(null == m_selectPattern 339 && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize) 340 { 341 XPath newSelect = rewriteChildToExpression(this); 342 if(null != newSelect) 343 m_selectPattern = newSelect; 344 } 345 346 StylesheetRoot.ComposeState cstate = sroot.getComposeState(); 347 348 java.util.Vector vnames = cstate.getVariableNames(); 351 if(null != m_selectPattern) 352 m_selectPattern.fixupVariables(vnames, cstate.getGlobalsSize()); 353 354 if(!(m_parentNode instanceof Stylesheet) && m_qname != null) 357 { 358 m_index = cstate.addVariableName(m_qname) - cstate.getGlobalsSize(); 359 } 360 else if (m_parentNode instanceof Stylesheet) 361 { 362 cstate.resetStackFrameSize(); 366 } 367 368 super.compose(sroot); 371 } 372 373 378 public void endCompose(StylesheetRoot sroot) throws TransformerException 379 { 380 super.endCompose(sroot); 381 if(m_parentNode instanceof Stylesheet) 382 { 383 StylesheetRoot.ComposeState cstate = sroot.getComposeState(); 384 m_frameSize = cstate.getFrameSize(); 385 cstate.resetStackFrameSize(); 386 } 387 } 388 389 390 391 399 400 411 static XPath rewriteChildToExpression(ElemTemplateElement varElem) 412 throws TransformerException 413 { 414 415 ElemTemplateElement t = varElem.getFirstChildElem(); 416 417 if (null != t && null == t.getNextSiblingElem()) 420 { 421 int etype = t.getXSLToken(); 422 423 if (Constants.ELEMNAME_VALUEOF == etype) 424 { 425 ElemValueOf valueof = (ElemValueOf) t; 426 427 if (valueof.getDisableOutputEscaping() == false 429 && valueof.getDOMBackPointer() == null) 430 { 431 varElem.m_firstChild = null; 432 433 return new XPath(new XRTreeFragSelectWrapper(valueof.getSelect().getExpression())); 434 } 435 } 436 else if (Constants.ELEMNAME_TEXTLITERALRESULT == etype) 437 { 438 ElemTextLiteral lit = (ElemTextLiteral) t; 439 440 if (lit.getDisableOutputEscaping() == false 441 && lit.getDOMBackPointer() == null) 442 { 443 String str = lit.getNodeValue(); 444 XString xstr = new XString(str); 445 446 varElem.m_firstChild = null; 447 448 return new XPath(new XRTreeFragSelectWrapper(xstr)); 449 } 450 } 451 } 452 453 return null; 454 } 455 456 461 public void recompose(StylesheetRoot root) 462 { 463 root.recomposeVariables(this); 464 } 465 466 471 public void setParentElem(ElemTemplateElement p) 472 { 473 super.setParentElem(p); 474 p.m_hasVariableDecl = true; 475 } 476 477 484 protected boolean accept(XSLTVisitor visitor) 485 { 486 return visitor.visitVariableOrParamDecl(this); 487 } 488 489 490 494 protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs) 495 { 496 if(null != m_selectPattern) 497 m_selectPattern.getExpression().callVisitors(m_selectPattern, visitor); 498 super.callChildVisitors(visitor, callAttrs); 499 } 500 501 505 public boolean isPsuedoVar() 506 { 507 java.lang.String ns = m_qname.getNamespaceURI(); 508 if((null != ns) && ns.equals(RedundentExprEliminator.PSUEDOVARNAMESPACE)) 509 { 510 if(m_qname.getLocalName().startsWith("#")) 511 return true; 512 } 513 return false; 514 } 515 516 525 public ElemTemplateElement appendChild(ElemTemplateElement elem) 526 { 527 if (m_selectPattern != null) 529 { 530 error(XSLTErrorResources.ER_CANT_HAVE_CONTENT_AND_SELECT, 531 new Object []{"xsl:" + this.getNodeName()}); 532 return null; 533 } 534 return super.appendChild(elem); 535 } 536 537 } 538 | Popular Tags |