Example #1
0
  /**
   * Extension element for piping an XML document through a series of 1 or more transformations.
   *
   * <pre>Common usage pattern: A stylesheet transforms a listing of documents to be
   * transformed into a TOC. For each document in the listing calls the pipeDocument
   * extension element to pipe that document through a series of 1 or more stylesheets
   * to the desired output document.
   *
   * Syntax:
   * &lt;xsl:stylesheet version="1.0"
   *                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   *                xmlns:pipe="http://xml.apache.org/xalan/PipeDocument"
   *                extension-element-prefixes="pipe"&gt;
   * ...
   * &lt;pipe:pipeDocument   source="source.xml" target="target.xml"&gt;
   *   &lt;stylesheet href="ss1.xsl"&gt;
   *     &lt;param name="param1" value="value1"/&gt;
   *   &lt;/stylesheet&gt;
   *   &lt;stylesheet href="ss2.xsl"&gt;
   *     &lt;param name="param1" value="value1"/&gt;
   *     &lt;param name="param2" value="value2"/&gt;
   *   &lt;/stylesheet&gt;
   *   &lt;stylesheet href="ss1.xsl"/&gt;
   * &lt;/pipe:pipeDocument&gt;
   *
   * Notes:</pre>
   *
   * <ul>
   *   <li>The base URI for the source attribute is the XML "listing" document.
   *   <li/>
   *   <li>The target attribute is taken as is (base is the current user directory).
   *   <li/>
   *   <li>The stylsheet containg the extension element is the base URI for the stylesheet hrefs.
   *   <li/>
   * </ul>
   */
  public void pipeDocument(XSLProcessorContext context, ElemExtensionCall elem)
      throws TransformerException, TransformerConfigurationException, SAXException, IOException,
          FileNotFoundException {

    SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance();

    // XML doc to transform.
    String source = elem.getAttribute("source", context.getContextNode(), context.getTransformer());
    TransformerImpl transImpl = context.getTransformer();

    // Base URI for input doc, so base for relative URI to XML doc to transform.
    String baseURLOfSource = transImpl.getBaseURLOfSource();
    // Absolute URI for XML doc to transform.
    String absSourceURL = SystemIDResolver.getAbsoluteURI(source, baseURLOfSource);

    // Transformation target
    String target = elem.getAttribute("target", context.getContextNode(), context.getTransformer());

    XPathContext xctxt = context.getTransformer().getXPathContext();
    int xt = xctxt.getDTMHandleFromNode(context.getContextNode());

    // Get System Id for stylesheet; to be used to resolve URIs to other stylesheets.
    String sysId = elem.getSystemId();

    NodeList ssNodes = null;
    NodeList paramNodes = null;
    Node ssNode = null;
    Node paramNode = null;
    if (elem.hasChildNodes()) {
      ssNodes = elem.getChildNodes();
      // Vector to contain TransformerHandler for each stylesheet.
      Vector vTHandler = new Vector(ssNodes.getLength());

      // The child nodes of an extension element node are instances of
      // ElemLiteralResult, which requires does not fully support the standard
      // Node interface. Accordingly, some special handling is required (see below)
      // to get attribute values.
      for (int i = 0; i < ssNodes.getLength(); i++) {
        ssNode = ssNodes.item(i);
        if (ssNode.getNodeType() == Node.ELEMENT_NODE
            && ((Element) ssNode).getTagName().equals("stylesheet")
            && ssNode instanceof ElemLiteralResult) {
          AVT avt = ((ElemLiteralResult) ssNode).getLiteralResultAttribute("href");
          String href = avt.evaluate(xctxt, xt, elem);
          String absURI = SystemIDResolver.getAbsoluteURI(href, sysId);
          Templates tmpl = saxTFactory.newTemplates(new StreamSource(absURI));
          TransformerHandler tHandler = saxTFactory.newTransformerHandler(tmpl);
          Transformer trans = tHandler.getTransformer();

          // AddTransformerHandler to vector
          vTHandler.addElement(tHandler);

          paramNodes = ssNode.getChildNodes();
          for (int j = 0; j < paramNodes.getLength(); j++) {
            paramNode = paramNodes.item(j);
            if (paramNode.getNodeType() == Node.ELEMENT_NODE
                && ((Element) paramNode).getTagName().equals("param")
                && paramNode instanceof ElemLiteralResult) {
              avt = ((ElemLiteralResult) paramNode).getLiteralResultAttribute("name");
              String pName = avt.evaluate(xctxt, xt, elem);
              avt = ((ElemLiteralResult) paramNode).getLiteralResultAttribute("value");
              String pValue = avt.evaluate(xctxt, xt, elem);
              trans.setParameter(pName, pValue);
            }
          }
        }
      }
      usePipe(vTHandler, absSourceURL, target);
    }
  }