elemvariable.java
来自「java jdk 1.4的源码」· Java 代码 · 共 561 行 · 第 1/2 页
JAVA
561 行
*/ public void execute(TransformerImpl transformer) throws TransformerException { if (TransformerImpl.S_DEBUG) transformer.getTraceManager().fireTraceEvent(this); int sourceNode = transformer.getXPathContext().getCurrentNode(); XObject var = getValue(transformer, sourceNode); // transformer.getXPathContext().getVarStack().pushVariable(m_qname, var); transformer.getXPathContext().getVarStack().setLocalVariable(m_index, var); if (TransformerImpl.S_DEBUG) transformer.getTraceManager().fireTraceEndEvent(this); } /** * Get the XObject representation of the variable. * * @param transformer non-null reference to the the current transform-time state. * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>. * * @return the XObject representation of the variable. * * @throws TransformerException */ public XObject getValue(TransformerImpl transformer, int sourceNode) throws TransformerException { XObject var; XPathContext xctxt = transformer.getXPathContext(); xctxt.pushCurrentNode(sourceNode); try { if (null != m_selectPattern) { var = m_selectPattern.execute(xctxt, sourceNode, this); var.allowDetachToRelease(false); if (TransformerImpl.S_DEBUG) transformer.getTraceManager().fireSelectedEvent(sourceNode, this, "select", m_selectPattern, var); } else if (null == getFirstChildElem()) { var = XString.EMPTYSTRING; } else { // Use result tree fragment. // Global variables may be deferred (see XUnresolvedVariable) and hence // need to be assigned to a different set of DTMs than local variables // so they aren't popped off the stack on return from a template. int df; // Bugzilla 7118: A variable set via an RTF may create local // variables during that computation. To keep them from overwriting // variables at this level, push a new variable stack. ////// PROBLEM: This is provoking a variable-used-before-set ////// problem in parameters. Needs more study. try { //////////xctxt.getVarStack().link(0); if(m_parentNode instanceof Stylesheet) // Global variable df = transformer.transformToGlobalRTF(this); else df = transformer.transformToRTF(this); } finally{ //////////////xctxt.getVarStack().unlink(); } var = new XRTreeFrag(df, xctxt, this); } } finally { xctxt.popCurrentNode(); } return var; } /** * This function is called after everything else has been * recomposed, and allows the template to set remaining * values that may be based on some other property that * depends on recomposition. */ public void compose(StylesheetRoot sroot) throws TransformerException { // See if we can reduce an RTF to a select with a string expression. if(null == m_selectPattern && org.apache.xalan.processor.TransformerFactoryImpl.m_optimize) { XPath newSelect = rewriteChildToExpression(this); if(null != newSelect) m_selectPattern = newSelect; } StylesheetRoot.ComposeState cstate = sroot.getComposeState(); // This should be done before addVariableName, so we don't have visibility // to the variable now being defined. java.util.Vector vnames = cstate.getVariableNames(); if(null != m_selectPattern) m_selectPattern.fixupVariables(vnames, cstate.getGlobalsSize()); // Only add the variable if this is not a global. If it is a global, // it was already added by stylesheet root. if(!(m_parentNode instanceof Stylesheet)) { m_index = cstate.addVariableName(m_qname) - cstate.getGlobalsSize(); } else { // If this is a global, then we need to treat it as if it's a xsl:template, // and count the number of variables it contains. So we set the count to // zero here. cstate.resetStackFrameSize(); } // This has to be done after the addVariableName, so that the variable // pushed won't be immediately popped again in endCompose. super.compose(sroot); } /** * This after the template's children have been composed. We have to get * the count of how many variables have been declared, so we can do a link * and unlink. */ public void endCompose(StylesheetRoot sroot) throws TransformerException { super.endCompose(sroot); if(m_parentNode instanceof Stylesheet) { StylesheetRoot.ComposeState cstate = sroot.getComposeState(); m_frameSize = cstate.getFrameSize(); cstate.resetStackFrameSize(); } } // /**// * This after the template's children have been composed.// */// public void endCompose() throws TransformerException// {// super.endCompose();// } /** * If the children of a variable is a single xsl:value-of or text literal, * it is cheaper to evaluate this as an expression, so try and adapt the * child an an expression. * * @param varElem Should be a ElemParam, ElemVariable, or ElemWithParam. * * @return An XPath if rewrite is possible, else null. * * @throws TransformerException */ static XPath rewriteChildToExpression(ElemTemplateElement varElem) throws TransformerException { ElemTemplateElement t = varElem.getFirstChildElem(); // Down the line this can be done with multiple string objects using // the concat function. if (null != t && null == t.getNextSiblingElem()) { int etype = t.getXSLToken(); if (Constants.ELEMNAME_VALUEOF == etype) { ElemValueOf valueof = (ElemValueOf) t; // %TBD% I'm worried about extended attributes here. if (valueof.getDisableOutputEscaping() == false && valueof.getDOMBackPointer() == null) { varElem.m_firstChild = null; return new XPath(new XRTreeFragSelectWrapper(valueof.getSelect().getExpression())); } } else if (Constants.ELEMNAME_TEXTLITERALRESULT == etype) { ElemTextLiteral lit = (ElemTextLiteral) t; if (lit.getDisableOutputEscaping() == false && lit.getDOMBackPointer() == null) { String str = lit.getNodeValue(); XString xstr = new XString(str); varElem.m_firstChild = null; return new XPath(new XRTreeFragSelectWrapper(xstr)); } } } return null; } /** * This function is called during recomposition to * control how this element is composed. * @param root The root stylesheet for this transformation. */ public void recompose(StylesheetRoot root) { root.recomposeVariables(this); } /** * Set the parent as an ElemTemplateElement. * * @param parent This node's parent as an ElemTemplateElement */ public void setParentElem(ElemTemplateElement p) { super.setParentElem(p); p.m_hasVariableDecl = true; } /** * Accept a visitor and call the appropriate method * for this class. * * @param visitor The visitor whose appropriate method will be called. * @return true if the children of the object should be visited. */ protected boolean accept(XSLTVisitor visitor) { return visitor.visitVariableOrParamDecl(this); } /** * Call the children visitors. * @param visitor The visitor whose appropriate method will be called. */ protected void callChildVisitors(XSLTVisitor visitor, boolean callAttrs) { if(null != m_selectPattern) m_selectPattern.getExpression().callVisitors(m_selectPattern, visitor); super.callChildVisitors(visitor, callAttrs); } /** * Tell if this is a psuedo variable reference, declared by Xalan instead * of by the user. */ public boolean isPsuedoVar() { java.lang.String ns = m_qname.getNamespaceURI(); if((null != ns) && ns.equals(RedundentExprEliminator.PSUEDOVARNAMESPACE)) { if(m_qname.getLocalName().startsWith("#")) return true; } return false; }
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?