示例#1
0
  /**
   * Receive notification of the start of an xsl:output element.
   *
   * @param handler The calling StylesheetHandler/TemplatesBuilder.
   * @param uri The Namespace URI, or the empty string if the element has no Namespace URI or if
   *     Namespace processing is not being performed.
   * @param localName The local name (without prefix), or the empty string if Namespace processing
   *     is not being performed.
   * @param rawName The raw XML 1.0 name (with prefix), or the empty string if raw names are not
   *     available.
   * @param attributes The attributes attached to the element. If there are no attributes, it shall
   *     be an empty Attributes object.
   * @throws org.xml.sax.SAXException
   */
  public void startElement(
      StylesheetHandler handler,
      String uri,
      String localName,
      String rawName,
      Attributes attributes)
      throws org.xml.sax.SAXException {
    // Hmmm... for the moment I don't think I'll have default properties set for this. -sb
    m_outputProperties = new OutputProperties();

    m_outputProperties.setDOMBackPointer(handler.getOriginatingNode());
    m_outputProperties.setLocaterInfo(handler.getLocator());
    m_outputProperties.setUid(handler.nextUid());
    setPropertiesFromAttributes(handler, rawName, attributes, this);

    // Access this only from the Hashtable level... we don't want to
    // get default properties.
    String entitiesFileName =
        (String) m_outputProperties.getProperties().get(OutputPropertiesFactory.S_KEY_ENTITIES);

    if (null != entitiesFileName) {
      try {
        String absURL =
            SystemIDResolver.getAbsoluteURI(entitiesFileName, handler.getBaseIdentifier());
        m_outputProperties.getProperties().put(OutputPropertiesFactory.S_KEY_ENTITIES, absURL);
      } catch (TransformerException te) {
        handler.error(te.getMessage(), te);
      }
    }

    handler.getStylesheet().setOutput(m_outputProperties);

    ElemTemplateElement parent = handler.getElemTemplateElement();
    parent.appendChild(m_outputProperties);

    m_outputProperties = null;
  }
示例#2
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);
    }
  }