You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by dl...@locus.apache.org on 2000/12/04 19:08:45 UTC

cvs commit: xml-xalan/java/samples/DomToDom DomToDom.java foo.xml foo.xsl readme.html

dleslie     00/12/04 10:08:45

  Added:       java/samples/DomToDom DomToDom.java foo.xml foo.xsl
                        readme.html
  Log:
  Renamed and moved from TransformToDOM (also uses a DOMSource).
  
  Revision  Changes    Path
  1.1                  xml-xalan/java/samples/DomToDom/DomToDom.java
  
  Index: DomToDom.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xalan" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, Lotus
   * Development Corporation., http://www.lotus.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  // Imported TraX classes
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerConfigurationException;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.dom.DOMResult;
  
  // Imported java.io classes
  import java.io.IOException;
  import java.io.FileNotFoundException;
  
  // Imported SAX classes
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  // Imported DOM classes
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  
  // Imported Serializer classes
  import org.apache.xalan.serialize.OutputFormat;
  import org.apache.xalan.serialize.Serializer;
  import org.apache.xalan.serialize.SerializerFactory;
  
  // Imported JAVA API for XML Parsing classes
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException; 
  
    /**
     * Show how to transform a DOM tree into another DOM tree.  
     * This uses the javax.xml.parsers to parse an XML file into a 
     * DOM, and create an output DOM.
     */
  public class DomToDom
  {
  	public static void main(String[] args)
      throws TransformerException, TransformerConfigurationException, FileNotFoundException,
             ParserConfigurationException, SAXException, IOException
    {    
  	  TransformerFactory tFactory = TransformerFactory.newInstance();
  
      if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE))
      {
        // Process the stylesheet StreamSource and generate a Transformer.
        Transformer transformer = tFactory.newTransformer(new StreamSource("foo.xsl"));
        
        //Instantiate a DocumentBuilderFactory.
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        
        //Use the DocumentBuilderFactory to create a DocumentBuilder.
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
        
        //Use the DocumentBuilder to parse the XML input.
        Document doc = dBuilder.parse("foo.xml");
        
        // Create an empty DOMResult for the Result.
        DOMResult domResult = new DOMResult();
    
    	  // Perform the transformation, placing the output in the DOMResult.
        transformer.transform(new DOMSource(doc), domResult);
  	  
  	    //Instantiate an XML serializer and use it to serialize the output DOM to System.out
  	    // using a default output format.
        Serializer serializer = SerializerFactory.getSerializer("xml");
        serializer.setOutputStream(System.out);
        serializer.asDOMSerializer().serialize(domResult.getNode());
  	}
      else
      {
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
      }
    }
  }
  
  
  
  1.1                  xml-xalan/java/samples/DomToDom/foo.xml
  
  Index: foo.xml
  ===================================================================
  <upload>
    <day>
      <dow>tue</dow>
      <order>
        <line>
           <product>Belt</product>
           <price>5.54</price>
        </line>
      </order>
    </day>
    <day>
      <dow>wed</dow>
    </day>
    <day>
      <dow>thu</dow>
      <order>
        <line>
           <product>Boots</product>
           <price>9.23</price>
        </line>
      </order>
    </day>
    <day>
      <dow>fri</dow>
      <order>
      </order>
      <order>
      </order>
    </day>
    <day>
      <dow>sat</dow>
      <order>
        <line>
           <product>Jacket</product>
           <price>9.84</price>
        </line>
      </order>
    </day>
    <day>
      <dow>mon</dow>
      <order>
      </order>
      <order>
        <line>
           <product>Tie</product>
           <price>5.37</price>
        </line>
      </order>
    </day>
    <day>
      <dow>tue</dow>
    </day>
    <day>
      <dow>wed</dow>
      <order>
      </order>
    </day>
    <day>
      <dow>thu</dow>
      <order>
        <line>
           <product>Overalls</product>
           <price>3.16</price>
        </line>
      </order>
    </day>
  </upload>
  
  
  
  1.1                  xml-xalan/java/samples/DomToDom/foo.xsl
  
  Index: foo.xsl
  ===================================================================
  <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns="http://www.w3.org/TR/REC-html40">
  
    <!-- FileName: misc-evans -->
    <!-- Document: http://www.w3.org/TR/xslt -->
    <!-- DocVersion: 19991116 -->
    <!-- Purpose: COPY of perf test;"A Practical Suggestion for XSLT Performance Improvement" by Clark Evans. -->
  
     <xsl:variable name="day-list" 
       select="//dow[not(.=following::dow)]" />
  
     <xsl:variable name="product-list" 
              select="//product[not(.=following::product)]" />
       
  <xsl:template match="/">
    <xsl:variable name="my-test"><a><b/></a></xsl:variable>
    <html>
      <xsl:copy-of select="$my-test"/>
      <body>
      <table>
        <tr>
          <td><xsl:text> </xsl:text></td>
          <xsl:for-each select="$day-list">
            <xsl:sort order="ascending" select="." />
            <th><xsl:value-of select="."/></th>
          </xsl:for-each>
        </tr>
        <xsl:for-each select="$product-list">
          <xsl:sort    order="ascending" select="." />
          <xsl:variable name="product" select="." />
          <tr>
            <td>
              <xsl:value-of select="$product" />
            </td>
            <xsl:for-each select="$day-list">
              <xsl:sort order="ascending" select="." />
              <xsl:variable name="day" select="." />
              <td>
                <xsl:value-of 
               select="sum(//price[../product=$product][../../../dow=$day])"
  /> .
              </td>
            </xsl:for-each>
            <td>
              <xsl:value-of 
                select="sum(//price[../product=$product])" /> .
             </td>
          </tr>
        </xsl:for-each>
        <tr>
          <td><xsl:text> </xsl:text></td>
          <xsl:for-each select="$day-list">
            <xsl:sort    order="ascending" select="." />
            <xsl:variable name="day" select="." />
            <td>
              <xsl:value-of 
                select="sum(//price[../../../dow=$day])" />
            </td>
          </xsl:for-each>
          <td>
            <xsl:value-of select="sum(//price)" />
          </td>
        </tr>
      </table>
      </body>
    </html>
  </xsl:template>
  </xsl:stylesheet>
  
  
  
  1.1                  xml-xalan/java/samples/DomToDom/readme.html
  
  Index: readme.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  
  <html>
  <head>
  	<title>Xalan Samples</title>
  </head>
  <body>
  <h2>Xalan Samples</h2>
  <p>For information about the samples (what they illustrate and how to run them), see <a href="../../docs/samples.html">Samples</a>.</p>
  
  
  </body>
  </html>