/**
   * 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 && sroot.getOptimizer()) {
      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_qname != null) {
      m_index = cstate.addVariableName(m_qname) - cstate.getGlobalsSize();
    } else if (m_parentNode instanceof Stylesheet) {
      // 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);
  }
  /**
   * Recalculate the precedence of this stylesheet in the global import list. The lowest precedence
   * stylesheet is 0. A higher number has a higher precedence.
   */
  void recomposeImports() {

    m_importNumber = getStylesheetRoot().getImportNumber(this);

    StylesheetRoot root = getStylesheetRoot();
    int globalImportCount = root.getGlobalImportCount();

    m_importCountComposed = (globalImportCount - m_importNumber) - 1;

    // Now get the count of composed imports from this stylesheet's imports
    int count = getImportCount();
    if (count > 0) {
      m_endImportCountComposed += count;
      while (count > 0)
        m_endImportCountComposed += this.getImport(--count).getEndImportCountComposed();
    }

    // Now get the count of composed imports from this stylesheet's
    // composed includes.
    count = getIncludeCountComposed();
    while (count > 0) {
      int imports = getIncludeComposed(--count).getImportCount();
      m_endImportCountComposed += imports;
      while (imports > 0)
        m_endImportCountComposed +=
            getIncludeComposed(count).getImport(--imports).getEndImportCountComposed();
    }
  }
  /**
   * Get a stylesheet from the "import" list.
   *
   * @see <a href="http://www.w3.org/TR/xslt#import">import in XSLT Specification</a>
   * @param i Index of stylesheet in import list
   * @return The stylesheet at the given index
   * @throws ArrayIndexOutOfBoundsException
   */
  public StylesheetComposed getImportComposed(int i) throws ArrayIndexOutOfBoundsException {

    StylesheetRoot root = getStylesheetRoot();

    // Get the stylesheet that is offset past this stylesheet.
    // Thus, if the index of this stylesheet is 3, an argument
    // to getImportComposed of 0 will return the 4th stylesheet
    // in the global import list.
    return root.getGlobalImport(1 + m_importNumber + i);
  }
Esempio n. 4
0
  /**
   * Augment resolvePrefixTables, resolving the namespace aliases once the superclass has resolved
   * the tables.
   *
   * @throws TransformerException
   */
  public void resolvePrefixTables() throws TransformerException {

    super.resolvePrefixTables();

    StylesheetRoot stylesheet = getStylesheetRoot();

    if ((null != m_namespace) && (m_namespace.length() > 0)) {
      NamespaceAlias nsa = stylesheet.getNamespaceAliasComposed(m_namespace);

      if (null != nsa) {
        m_namespace = nsa.getResultNamespace();

        // String resultPrefix = nsa.getResultPrefix();
        String resultPrefix = nsa.getStylesheetPrefix(); // As per xsl WG, Mike Kay

        if ((null != resultPrefix) && (resultPrefix.length() > 0))
          m_rawName = resultPrefix + ":" + m_localName;
        else m_rawName = m_localName;
      }
    }

    if (null != m_avts) {
      int n = m_avts.size();

      for (int i = 0; i < n; i++) {
        AVT avt = (AVT) m_avts.get(i);

        // Should this stuff be a method on AVT?
        String ns = avt.getURI();

        if ((null != ns) && (ns.length() > 0)) {
          NamespaceAlias nsa = stylesheet.getNamespaceAliasComposed(m_namespace); // %REVIEW% ns?

          if (null != nsa) {
            String namespace = nsa.getResultNamespace();

            // String resultPrefix = nsa.getResultPrefix();
            String resultPrefix = nsa.getStylesheetPrefix(); // As per XSL WG
            String rawName = avt.getName();

            if ((null != resultPrefix) && (resultPrefix.length() > 0))
              rawName = resultPrefix + ":" + rawName;

            avt.setURI(namespace);
            avt.setRawName(rawName);
          }
        }
      }
    }
  }
 /**
  * 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();
   }
 }
Esempio n. 6
0
  public void compose(StylesheetRoot sroot) throws TransformerException {
    super.compose(sroot);
    String prefix = getPrefix();
    String declNamespace = getNamespaceForPrefix(prefix);
    String lang = null;
    String srcURL = null;
    String scriptSrc = null;
    if (null == declNamespace)
      throw new TransformerException(
          XSLMessages.createMessage(
              XSLTErrorResources.ER_NO_NAMESPACE_DECL, new Object[] {prefix}));
    // "Prefix " + prefix does not have a corresponding namespace declaration");
    for (ElemTemplateElement child = getFirstChildElem();
        child != null;
        child = child.getNextSiblingElem()) {
      if (Constants.ELEMNAME_EXTENSIONSCRIPT == child.getXSLToken()) {
        ElemExtensionScript sdecl = (ElemExtensionScript) child;
        lang = sdecl.getLang();
        srcURL = sdecl.getSrc();
        ElemTemplateElement childOfSDecl = sdecl.getFirstChildElem();
        if (null != childOfSDecl) {
          if (Constants.ELEMNAME_TEXTLITERALRESULT == childOfSDecl.getXSLToken()) {
            ElemTextLiteral tl = (ElemTextLiteral) childOfSDecl;
            char[] chars = tl.getChars();
            scriptSrc = new String(chars);
            if (scriptSrc.trim().length() == 0) scriptSrc = null;
          }
        }
      }
    }
    if (null == lang) lang = "javaclass";
    if (lang.equals("javaclass") && (scriptSrc != null))
      throw new TransformerException(
          XSLMessages.createMessage(
              XSLTErrorResources.ER_ELEM_CONTENT_NOT_ALLOWED, new Object[] {scriptSrc}));
    // "Element content not allowed for lang=javaclass " + scriptSrc);

    // Register the extension namespace if it has not already been registered.
    ExtensionNamespaceSupport extNsSpt = null;
    ExtensionNamespacesManager extNsMgr = sroot.getExtensionNamespacesManager();
    if (extNsMgr.namespaceIndex(declNamespace, extNsMgr.getExtensions()) == -1) {
      if (lang.equals("javaclass")) {
        if (null == srcURL) {
          extNsSpt = extNsMgr.defineJavaNamespace(declNamespace);
        } else if (extNsMgr.namespaceIndex(srcURL, extNsMgr.getExtensions()) == -1) {
          extNsSpt = extNsMgr.defineJavaNamespace(declNamespace, srcURL);
        }
      } else // not java
      {
        String handler = "org.apache.xalan.extensions.ExtensionHandlerGeneral";
        Object[] args = {
          declNamespace, this.m_elements, this.m_functions, lang, srcURL, scriptSrc, getSystemId()
        };
        extNsSpt = new ExtensionNamespaceSupport(declNamespace, handler, args);
      }
    }
    if (extNsSpt != null) extNsMgr.registerExtension(extNsSpt);
  }
Esempio n. 7
0
  /**
   * 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.
   *
   * <p>NEEDSDOC @param sroot
   *
   * @throws TransformerException
   */
  public void compose(StylesheetRoot sroot) throws TransformerException {

    super.compose(sroot);

    int length = getSortElemCount();

    for (int i = 0; i < length; i++) {
      getSortElem(i).compose(sroot);
    }

    java.util.Vector vnames = sroot.getComposeState().getVariableNames();

    if (null != m_selectExpression)
      m_selectExpression.fixupVariables(vnames, sroot.getComposeState().getGlobalsSize());
    else {
      m_selectExpression = getStylesheetRoot().m_selectDefault.getExpression();
    }
  }
Esempio n. 8
0
  /**
   * 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 {
    resolvePrefixTables();
    ElemTemplateElement t = getFirstChildElem();
    m_hasTextLitOnly =
        ((t != null)
            && (t.getXSLToken() == Constants.ELEMNAME_TEXTLITERALRESULT)
            && (t.getNextSiblingElem() == null));

    StylesheetRoot.ComposeState cstate = sroot.getComposeState();
    cstate.pushStackMark();
  }
Esempio n. 9
0
  /**
   * 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 {
    super.compose(sroot);
    StylesheetRoot.ComposeState cstate = sroot.getComposeState();
    java.util.Vector vnames = cstate.getVariableNames();
    if (null != m_avts) {
      int nAttrs = m_avts.size();

      for (int i = (nAttrs - 1); i >= 0; i--) {
        AVT avt = (AVT) m_avts.get(i);
        avt.fixupVariables(vnames, cstate.getGlobalsSize());
      }
    }
  }
Esempio n. 10
0
 /**
  * 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.recomposeAttributeSets(this);
 }
  /**
   * <meta name="usage" content="advanced"/> Perform a query if needed, and call transformNode for
   * each child.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param template The owning template context.
   * @throws TransformerException Thrown in a variety of circumstances.
   */
  public void transformSelectedNodes(TransformerImpl transformer) throws TransformerException {

    final XPathContext xctxt = transformer.getXPathContext();
    final int sourceNode = xctxt.getCurrentNode();
    DTMIterator sourceNodes = m_selectExpression.asIterator(xctxt, sourceNode);
    VariableStack vars = xctxt.getVarStack();
    int nParams = getParamElemCount();
    int thisframe = vars.getStackFrame();
    StackGuard guard = transformer.getStackGuard();
    boolean check = (guard.getRecursionLimit() > -1) ? true : false;

    try {

      final Vector keys =
          (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);

      // Sort if we need to.
      if (null != keys) sourceNodes = sortNodes(xctxt, keys, sourceNodes);

      if (TransformerImpl.S_DEBUG) {
        transformer
            .getTraceManager()
            .fireSelectedEvent(
                sourceNode,
                this,
                "select",
                new XPath(m_selectExpression),
                new org.apache.xpath.objects.XNodeSet(sourceNodes));
      }

      final ResultTreeHandler rth = transformer.getResultTreeHandler();
      ContentHandler chandler = rth.getContentHandler();
      final StylesheetRoot sroot = transformer.getStylesheet();
      final TemplateList tl = sroot.getTemplateListComposed();
      final boolean quiet = transformer.getQuietConflictWarnings();

      // Should be able to get this from the iterator but there must be a bug.
      DTM dtm = xctxt.getDTM(sourceNode);

      int argsFrame = -1;
      if (nParams > 0) {
        // This code will create a section on the stack that is all the
        // evaluated arguments.  These will be copied into the real params
        // section of each called template.
        argsFrame = vars.link(nParams);
        vars.setStackFrame(thisframe);

        for (int i = 0; i < nParams; i++) {
          ElemWithParam ewp = m_paramElems[i];
          XObject obj = ewp.getValue(transformer, sourceNode);

          vars.setLocalVariable(i, obj, argsFrame);
        }
        vars.setStackFrame(argsFrame);
      }

      xctxt.pushCurrentNode(DTM.NULL);
      IntStack currentNodes = xctxt.getCurrentNodeStack();

      xctxt.pushCurrentExpressionNode(DTM.NULL);
      IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();

      xctxt.pushSAXLocatorNull();
      xctxt.pushContextNodeList(sourceNodes);
      transformer.pushElemTemplateElement(null);
      // pushParams(transformer, xctxt);

      int child;
      while (DTM.NULL != (child = sourceNodes.nextNode())) {
        currentNodes.setTop(child);
        currentExpressionNodes.setTop(child);

        if (xctxt.getDTM(child) != dtm) {
          dtm = xctxt.getDTM(child);
        }

        final int exNodeType = dtm.getExpandedTypeID(child);
        final int nodeType = dtm.getNodeType(child);

        final QName mode = transformer.getMode();

        ElemTemplate template = tl.getTemplateFast(xctxt, child, exNodeType, mode, -1, quiet, dtm);

        // If that didn't locate a node, fall back to a default template rule.
        // See http://www.w3.org/TR/xslt#built-in-rule.
        if (null == template) {
          switch (nodeType) {
            case DTM.DOCUMENT_FRAGMENT_NODE:
            case DTM.ELEMENT_NODE:
              template = sroot.getDefaultRule();
              // %OPT% direct faster?
              break;
            case DTM.ATTRIBUTE_NODE:
            case DTM.CDATA_SECTION_NODE:
            case DTM.TEXT_NODE:
              // if(rth.m_elemIsPending || rth.m_docPending)
              //  rth.flushPending(true);
              transformer.pushPairCurrentMatched(sroot.getDefaultTextRule(), child);
              transformer.setCurrentElement(sroot.getDefaultTextRule());
              // dtm.dispatchCharactersEvents(child, chandler, false);
              dtm.dispatchCharactersEvents(child, rth, false);
              transformer.popCurrentMatched();
              continue;
            case DTM.DOCUMENT_NODE:
              template = sroot.getDefaultRootRule();
              break;
            default:

              // No default rules for processing instructions and the like.
              continue;
          }
        } else {
          transformer.setCurrentElement(template);
        }

        transformer.pushPairCurrentMatched(template, child);
        if (check) guard.checkForInfinateLoop();

        int currentFrameBottom; // See comment with unlink, below
        if (template.m_frameSize > 0) {
          xctxt.pushRTFContext();
          currentFrameBottom = vars.getStackFrame(); // See comment with unlink, below
          vars.link(template.m_frameSize);
          // You can't do the check for nParams here, otherwise the
          // xsl:params might not be nulled.
          if (
          /* nParams > 0 && */ template.m_inArgsSize > 0) {
            int paramIndex = 0;
            for (ElemTemplateElement elem = template.getFirstChildElem();
                null != elem;
                elem = elem.getNextSiblingElem()) {
              if (Constants.ELEMNAME_PARAMVARIABLE == elem.getXSLToken()) {
                ElemParam ep = (ElemParam) elem;

                int i;
                for (i = 0; i < nParams; i++) {
                  ElemWithParam ewp = m_paramElems[i];
                  if (ewp.m_qnameID == ep.m_qnameID) {
                    XObject obj = vars.getLocalVariable(i, argsFrame);
                    vars.setLocalVariable(paramIndex, obj);
                    break;
                  }
                }
                if (i == nParams) vars.setLocalVariable(paramIndex, null);
              } else break;
              paramIndex++;
            }
          }
        } else currentFrameBottom = 0;

        // Fire a trace event for the template.
        if (TransformerImpl.S_DEBUG) transformer.getTraceManager().fireTraceEvent(template);

        // And execute the child templates.
        // Loop through the children of the template, calling execute on
        // each of them.
        for (ElemTemplateElement t = template.m_firstChild; t != null; t = t.m_nextSibling) {
          xctxt.setSAXLocator(t);
          try {
            transformer.pushElemTemplateElement(t);
            t.execute(transformer);
          } finally {
            transformer.popElemTemplateElement();
          }
        }

        if (TransformerImpl.S_DEBUG) transformer.getTraceManager().fireTraceEndEvent(template);

        if (template.m_frameSize > 0) {
          // See Frank Weiss bug around 03/19/2002 (no Bugzilla report yet).
          // While unlink will restore to the proper place, the real position
          // may have been changed for xsl:with-param, so that variables
          // can be accessed.
          // of right now.
          // More:
          // When we entered this function, the current
          // frame buffer (cfb) index in the variable stack may
          // have been manually set.  If we just call
          // unlink(), however, it will restore the cfb to the
          // previous link index from the link stack, rather than
          // the manually set cfb.  So,
          // the only safe solution is to restore it back
          // to the same position it was on entry, since we're
          // really not working in a stack context here. (Bug4218)
          vars.unlink(currentFrameBottom);
          xctxt.popRTFContext();
        }

        transformer.popCurrentMatched();
      } // end while (DTM.NULL != (child = sourceNodes.nextNode()))
    } catch (SAXException se) {
      transformer.getErrorListener().fatalError(new TransformerException(se));
    } finally {
      if (TransformerImpl.S_DEBUG)
        transformer
            .getTraceManager()
            .fireSelectedEndEvent(
                sourceNode,
                this,
                "select",
                new XPath(m_selectExpression),
                new org.apache.xpath.objects.XNodeSet(sourceNodes));

      // Unlink to the original stack frame
      if (nParams > 0) vars.unlink(thisframe);
      xctxt.popSAXLocator();
      xctxt.popContextNodeList();
      transformer.popElemTemplateElement();
      xctxt.popCurrentExpressionNode();
      xctxt.popCurrentNode();
      sourceNodes.detach();
    }
  }
 /**
  * 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);
 }
Esempio n. 13
0
 /**
  * Get information about whether or not whitespace can be stripped.
  *
  * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
  * @return true if the whitespace can be stripped.
  */
 public boolean canStripWhiteSpace() {
   StylesheetRoot sroot = this.getStylesheetRoot();
   return (null != sroot) ? sroot.canStripWhiteSpace() : false;
 }
Esempio n. 14
0
  /**
   * Combine the parent's namespaces with this namespace for fast processing, taking care to
   * reference the parent's namespace if this namespace adds nothing new. (Recursive method, walking
   * the elements depth-first, processing parents before children). Note that this method builds
   * m_prefixTable with aliased namespaces, *not* the original namespaces.
   *
   * @throws TransformerException
   */
  public void resolvePrefixTables() throws TransformerException {
    // Always start with a fresh prefix table!
    m_prefixTable = null;

    // If we have declared declarations, then we look for
    // a parent that has namespace decls, and add them
    // to this element's decls.  Otherwise we just point
    // to the parent that has decls.
    if (null != this.m_declaredPrefixes) {
      StylesheetRoot stylesheet = this.getStylesheetRoot();

      // Add this element's declared prefixes to the
      // prefix table.
      int n = m_declaredPrefixes.size();

      for (int i = 0; i < n; i++) {
        XMLNSDecl decl = (XMLNSDecl) m_declaredPrefixes.elementAt(i);
        String prefix = decl.getPrefix();
        String uri = decl.getURI();
        if (null == uri) uri = "";
        boolean shouldExclude = excludeResultNSDecl(prefix, uri);

        // Create a new prefix table if one has not already been created.
        if (null == m_prefixTable) m_prefixTable = new Vector();

        NamespaceAlias nsAlias = stylesheet.getNamespaceAliasComposed(uri);
        if (null != nsAlias) {
          // Should I leave the non-aliased element in the table as
          // an excluded element?

          // The exclusion should apply to the non-aliased prefix, so
          // we don't calculate it here.  -sb
          // Use stylesheet prefix, as per xsl WG
          decl =
              new XMLNSDecl(
                  nsAlias.getStylesheetPrefix(), nsAlias.getResultNamespace(), shouldExclude);
        } else decl = new XMLNSDecl(prefix, uri, shouldExclude);

        m_prefixTable.addElement(decl);
      }
    }

    ElemTemplateElement parent = this.getParentNodeElem();

    if (null != parent) {

      // The prefix table of the parent should never be null!
      Vector prefixes = parent.m_prefixTable;

      if (null == m_prefixTable && !needToCheckExclude()) {

        // Nothing to combine, so just use parent's table!
        this.m_prefixTable = parent.m_prefixTable;
      } else {

        // Add the prefixes from the parent's prefix table.
        int n = prefixes.size();

        for (int i = 0; i < n; i++) {
          XMLNSDecl decl = (XMLNSDecl) prefixes.elementAt(i);
          boolean shouldExclude = excludeResultNSDecl(decl.getPrefix(), decl.getURI());

          if (shouldExclude != decl.getIsExcluded()) {
            decl = new XMLNSDecl(decl.getPrefix(), decl.getURI(), shouldExclude);
          }

          // m_prefixTable.addElement(decl);
          addOrReplaceDecls(decl);
        }
      }
    } else if (null == m_prefixTable) {

      // Must be stylesheet element without any result prefixes!
      m_prefixTable = new Vector();
    }
  }
Esempio n. 15
0
 /** This after the template's children have been composed. */
 public void endCompose(StylesheetRoot sroot) throws TransformerException {
   StylesheetRoot.ComposeState cstate = sroot.getComposeState();
   cstate.popStackMark();
 }
Esempio n. 16
0
 /**
  * This function is called to recompose() all of the namespace alias properties elements.
  *
  * @param root The owning root stylesheet
  */
 public void recompose(StylesheetRoot root) {
   root.recomposeNamespaceAliases(this);
 }
Esempio n. 17
0
 /**
  * Get information about whether or not an element should strip whitespace.
  *
  * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
  * @param support The XPath runtime state.
  * @param targetElement Element to check
  * @return true if the whitespace should be stripped.
  * @throws TransformerException
  */
 public boolean shouldStripWhiteSpace(
     org.apache.xpath.XPathContext support, org.w3c.dom.Element targetElement)
     throws TransformerException {
   StylesheetRoot sroot = this.getStylesheetRoot();
   return (null != sroot) ? sroot.shouldStripWhiteSpace(support, targetElement) : false;
 }