cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dles...@locus.apache.org
Subject cvs commit: xml-stylebook/src/org/apache/stylebook/processors Xalan2Processor.java
Date Mon, 11 Sep 2000 20:59:01 GMT
dleslie     00/09/11 13:58:59

  Added:       src/org/apache/stylebook/processors Xalan2Processor.java
  Log:
  Added targets (compile2 and package2) for building a version
  of Stylebook that works with Xalan-J2.
  IMPORTANT: must exclude org.apache.stylebook.processors.XalanProcessor
  when building new version. Must exclude Xalan2Processor in same
  package when building old version (that uses Xalan-J 1).
  
  Revision  Changes    Path
  1.1                  xml-stylebook/src/org/apache/stylebook/processors/Xalan2Processor.java
  
  Index: Xalan2Processor.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) 2000 The Apache Software Foundation.   All rights reserved. *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1,  a copy of wich has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.stylebook.processors;
  
  import org.apache.stylebook.*;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.PrintStream;
  import java.io.Reader;
  import java.net.URL;
  import java.util.Enumeration;
  import java.util.StringTokenizer;
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.w3c.dom.ProcessingInstruction;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  // Imported TraX classes
  import trax.Templates;
  import trax.Transformer; 
  import trax.Result;
  import trax.ProcessorException; 
  import trax.ProcessorFactoryException;
  import trax.TransformException; 
  
  // Imported JAVA API for XML Parsing 1.0 classes
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  /*
   * Build Stylebook with Xalan2Processor rather than XalanProcessor, and you can
   * use Stylebook to process documents with Xalan-J 2.
   *
   * @author (of revision to XalanProcessor) Donald Leslie (donald_leslie@lotus.com)
   */
  
  public class Xalan2Processor extends AbstractComponent implements Processor {
  
      public Document process(Document doc, CreationContext c, Parameters p)
      throws CreationException, IOException {
          try {
              // Retrieve the style
              String styf=p.getParameter("stylesheet");
              if (null==styf) styf=this.getStyleSheet(doc);
              if (null==styf) return(doc);
  			// Create processor, templates (StylesheetRoot) and transformer.
  			trax.Processor processor = trax.Processor.newInstance("xslt");
              Templates templates = processor.process(new InputSource(styf));
              Transformer transformer = templates.newTransformer();
              // Set stylesheet parameters
              Enumeration enum=p.getParameterNames();
              while (enum.hasMoreElements()) {
                  String name=(String)enum.nextElement();
                  transformer.setParameter(name, null, p.getParameter(name));
              }			
  			// Set up DOM container for result.
              DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
              Document res = docBuilder.newDocument();
  			// Perform the transformation.
              log("Applying XSL sheet \""+styf+"\"");	
   	        transformer.transformNode(doc, new Result(res));
              return(res);
          } catch (Exception e) {
              e.printStackTrace(System.out);
              throw new CreationException(e.getMessage(),e,doc);
          }
      }
  
      /** Search for the <?xml:stylesheet ... ?> processing instruction. */
      private String getStyleSheet(Document sourceTree) {
          String uri=null;
          NodeList children=sourceTree.getChildNodes();
          int nNodes=children.getLength();
          for(int i=0; i<nNodes; i++) {
              Node child=children.item(i);
              if (Node.PROCESSING_INSTRUCTION_NODE==child.getNodeType()) {
                  ProcessingInstruction pi=(ProcessingInstruction)child;
                  if(pi.getNodeName().equals("xml-stylesheet")) {
                      boolean isOK=true;
                      StringTokenizer tok=new StringTokenizer(pi.getNodeValue(),
                                                                    " \t=");
                      while(tok.hasMoreTokens()) {
                          if(tok.nextToken().equals("type")) {
                              String typeVal=tok.nextToken();
                              typeVal=typeVal.substring(1, typeVal.length()-1);
                              if(!typeVal.equals("text/xsl")) isOK=false;
                          }
                      }
                      if(isOK) {
                          tok=new StringTokenizer(pi.getNodeValue()," \t=");
                          while(tok.hasMoreTokens()) {
                              if(tok.nextToken().equals("href")) {
                                  uri=tok.nextToken();
                                  uri=uri.substring(1,uri.length()-1);
                              }
                          }
                          break;
                      }
                  }
              }
          }
          return(uri);
      }
  }
  
  
  

Mime
View raw message