You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ni...@apache.org on 2002/08/28 19:46:56 UTC

cvs commit: xml-cocoon2/src/blocks/fop/java/org/apache/cocoon/serialization FOPSerializer.java

nicolaken    2002/08/28 10:46:56

  Added:       tools/src blocks-build.xsl
               src/blocks/fop/java/org/apache/cocoon/components/renderer
                        ExtendableRendererFactory.java
               src/blocks/fop/conf fop.xmap
               src/blocks/fop/java/org/apache/cocoon/serialization
                        FOPSerializer.java
  Log:
  Initial support for the move of block code to the blocks dir, and
  relative build process.
  Moved Fop there for analysis of the system prior to general commitment.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/tools/src/blocks-build.xsl
  
  Index: blocks-build.xsl
  ===================================================================
  <?xml version="1.0"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" version="1.0" indent="yes" />
  
     <xsl:template match="/">
        <project default="all" basedir="." name="blocks-build">
           <description>Autogenerated Ant build file that creates the build dependencies.</description>
  
           <xsl:apply-templates select="module" />
        </project>
     </xsl:template>
  
     <xsl:template match="module">
  
       <target name="cocoon-core">
          <ant antfile="build.xml" target="package"/>
       </target>
        
       <target name="all">
         <xsl:attribute name="depends">cocoon-core<xsl:for-each select = "project">,<xsl:value-of select="@name" /></xsl:for-each></xsl:attribute>   
       </target>
       
       <xsl:apply-templates select="project" />
  
     </xsl:template>
  
     <xsl:template match="project">
     
        <xsl:variable name="current-project-target" select="@name" />
        <xsl:variable name="block-name" select="substring-before(@name,'-block')"/>
         
        <target name="{$current-project-target}" if="cocoon.blocks.{$block-name}.include" >
        
          <!-- if there is a dependency... -->
           <xsl:if test="depend">
              <xsl:attribute name="depends">cocoon-core<xsl:for-each select="depend[not(@version)]">,<xsl:value-of select="@project" /></xsl:for-each></xsl:attribute>
           </xsl:if>
  
      <path id="currentblock.classpath">
        <fileset><xsl:attribute name="dir">${lib.dir}</xsl:attribute>
          <include name="**/*.jar"/>
        </fileset>
        <fileset><xsl:attribute name="dir">${build.dir}</xsl:attribute>      
          <include><xsl:attribute name="name">${name}*.jar</xsl:attribute></include>
        </fileset>
      </path>
  
      <property name="currentblock.dir"><xsl:attribute name="value">${blocks.dir}/<xsl:value-of select="$block-name" /></xsl:attribute></property>  
      <property name="currentblock.java.dir"><xsl:attribute name="value">${currentblock.dir}/java</xsl:attribute></property>
      <property name="currentblock.conf.dir"><xsl:attribute name="value">${currentblock.dir}/conf</xsl:attribute></property>
          
      <property name="build.currentblock.dir"><xsl:attribute name="value">${build.blocks.root}/<xsl:value-of select="$block-name" /></xsl:attribute></property>
      <property name="build.currentblock.src"><xsl:attribute name="value">${build.currentblock.dir}/src</xsl:attribute></property>
      <property name="build.currentblock.dest"><xsl:attribute name="value">${build.currentblock.dir}/dest</xsl:attribute></property>
     
      <mkdir><xsl:attribute name="dir">${build.currentblock.src}</xsl:attribute></mkdir>
      <mkdir><xsl:attribute name="dir">${build.currentblock.dest}</xsl:attribute></mkdir>
          
      <copy filtering="on"><xsl:attribute name="todir">${build.currentblock.src}</xsl:attribute>
        <fileset><xsl:attribute name="dir">${currentblock.java.dir}</xsl:attribute>
          <include name="**/*.java"/>      
        </fileset>
      </copy>
  
     <copy filtering="off"><xsl:attribute name="todir">${build.currentblock.dest}</xsl:attribute>
        <fileset><xsl:attribute name="dir">${currentblock.java.dir}</xsl:attribute>
          <include name="**/Manifest.mf"/>
          <include name="META-INF/**"/>
        </fileset>
      </copy>
  
     <copy filtering="off"><xsl:attribute name="todir">${build.currentblock.dest}</xsl:attribute>
        <fileset><xsl:attribute name="dir">${currentblock.conf.dir}</xsl:attribute>
          <include name="**/*.x*"/>
        </fileset>
      </copy>
  
     <copy filtering="off"><xsl:attribute name="todir">${build.blocks.root}</xsl:attribute>
        <fileset><xsl:attribute name="dir">${currentblock.conf.dir}</xsl:attribute>
          <include name="**/*.x*"/>
        </fileset>
      </copy>    
      
      <!-- A task to change the roles file. It is used to add optional components -->
      <taskdef name="roles-tool" classname="XConfToolTask"><xsl:attribute name="classpath">${tools.dir}/anttasks</xsl:attribute></taskdef>
      <!-- Invoke the XConfTool to add optional roles for components -->
      
      <roles-tool extension="xroles">
          <xsl:attribute name="directory">${currentblock.conf.dir}</xsl:attribute>
          <xsl:attribute name="configuration">${build.dest}/org/apache/cocoon/cocoon.roles</xsl:attribute>               
      </roles-tool>            
                  
      
      
      <echo message="Compiling block..."/>
  
      <javac>
         <xsl:attribute name="srcdir">${build.currentblock.src}</xsl:attribute>
         <xsl:attribute name="destdir">${build.currentblock.dest}</xsl:attribute>
        <classpath refid="currentblock.classpath"/>
      </javac>
  
  
      <jar>
        <xsl:attribute name="jarfile">${build.dir}/<xsl:value-of select="$block-name" />-block.jar</xsl:attribute>
        <fileset>
          <xsl:attribute name="dir">${build.currentblock.dest}</xsl:attribute>
          <include name="org/**"/>
          <include name="META-INF/**"/>
        </fileset>
      </jar>
           
      
                 
        </target>
     </xsl:template>
  </xsl:stylesheet>
  
  
  
  
  
  1.1                  xml-cocoon2/src/blocks/fop/java/org/apache/cocoon/components/renderer/ExtendableRendererFactory.java
  
  Index: ExtendableRendererFactory.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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 "Apache Cocoon" 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 (INCLU-
   DING, 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 created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.renderer;
  
  import org.apache.fop.render.Renderer;
  import org.apache.fop.render.pcl.PCLRenderer;
  import org.apache.fop.render.pdf.PDFRenderer;
  import org.apache.fop.render.ps.PSRenderer;
  
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * An extendable FOP Renderer factory.
   * When given a MIME type, find a Renderer which supports that MIME
   * type. This factory is extendable as new <code>Renderer</code>s can
   * be added at runtime.
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @version CVS $Id: ExtendableRendererFactory.java,v 1.1 2002/08/28 17:46:56 nicolaken Exp $
   */
  public class ExtendableRendererFactory implements RendererFactory {
  
    protected static Map renderers = new HashMap();
  
    protected final static RendererFactory singleton = new ExtendableRendererFactory();
  
    private ExtendableRendererFactory() {
      // Add the default renderers which come with Apache FOP.
      addRenderer("application/pdf", PDFRenderer.class);
      addRenderer("application/postscript", PSRenderer.class);
      addRenderer("application/vnd.hp-PCL", PCLRenderer.class);
    }
  
    /**
     * Get a reference to this Renderer Factory.
     */
    public final static RendererFactory getRendererFactoryImplementation() {
      return singleton;
    }
  
    /**
     * Create a renderer for a specified MIME type.
     * @param mimeType The MIME type of the destination format
     * @return A suitable renderer, or <code>null</code> if one cannot be found
     */
    public Renderer createRenderer(String mimeType) {
      Class rendererClass = (Class)renderers.get(mimeType);
      if (rendererClass == null) {
        return null;
      } else {
        try {
          return (Renderer)rendererClass.newInstance();
        } catch (Exception ex) {
          return null;
        }
      }
    }
  
    /**
     * Add a mapping from the specified MIME type to a renderer.
     * Note: The renderer must have a no-argument constructor.
     * @param mimeType The MIME type of the Renderer
     * @param rendererClass The <code>Class</code> object for the Renderer.
     */
    public void addRenderer(String mimeType, Class rendererClass) {
      renderers.put(mimeType, rendererClass);
    }
  
    /**
     * Remove the mapping from a specified MIME type.
     * @param mimeType The MIME type to remove from the mapping.
     */
    public void removeRenderer(String mimeType) {
      renderers.remove(mimeType);
    }
  }
  
  
  
  1.1                  xml-cocoon2/src/blocks/fop/conf/fop.xmap
  
  Index: fop.xmap
  ===================================================================
  <?xml version="1.0"?>
  <xmap xpath="/sitemap/components/serializers" unless="serializer[@name='fo2pdf']">
      <map:serializer logger="sitemap.serializer.fo2pdf" name="fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf"/>
  
      <map:serializer logger="sitemap.serializer.fo2ps" name="fo2ps" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/postscript"/>
  
      <map:serializer logger="sitemap.serializer.fo2pcl" name="fo2pcl" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="vnd.hp-PCL"/>
  </xmap>
  
  
  
  1.1                  xml-cocoon2/src/blocks/fop/java/org/apache/cocoon/serialization/FOPSerializer.java
  
  Index: FOPSerializer.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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 "Apache Cocoon" 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 (INCLU-
   DING, 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 created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.serialization;
  
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.cocoon.caching.CacheableProcessingComponent;
  import org.apache.cocoon.components.renderer.ExtendableRendererFactory;
  import org.apache.cocoon.components.renderer.RendererFactory;
  import org.apache.cocoon.util.ClassUtils;
  import org.apache.excalibur.source.SourceValidity;
  import org.apache.excalibur.source.impl.validity.NOPValidity;
  import org.apache.fop.apps.Driver;
  import org.apache.fop.apps.Options;
  import org.apache.fop.messaging.MessageHandler;
  import org.apache.fop.render.Renderer;
  
  import java.io.OutputStream;
  
  /**
   * @author ?
   * @version CVS $Id: FOPSerializer.java,v 1.1 2002/08/28 17:46:56 nicolaken Exp $
   */
  public class FOPSerializer
  extends AbstractSerializer
  implements Configurable, CacheableProcessingComponent {
  
      /**
        * The <code>Options</code> used by FOP.
        */
      protected Options options;
  
      /**
        * The <code>Driver</code> which is FOP.
        */
      protected Driver driver;
  
      /**
        * The current <code>Renderer</code>.
        */
      protected Renderer renderer;
  
      /**
        * The Renderer Factory to use
        */
      protected static RendererFactory factory = ExtendableRendererFactory.getRendererFactoryImplementation();
  
      /**
        * The current <code>mime-type</code>.
        */
      protected String mimetype;
  
      /**
       * The renderer name if configured
       */
      protected String rendererName;
  
      /**
       * Should we set the content length ?
       */
      protected boolean setContentLength = true;
  
      /** This logger is used for FOP */
      protected org.apache.avalon.framework.logger.Logger logger;
  
      /**
        * Set the configurations for this serializer.
        */
      public void configure(Configuration conf)
      throws ConfigurationException {
          this.logger = getLogger().getChildLogger("fop");
          MessageHandler.setScreenLogger(this.logger);
          String userConfig = null;
          java.io.File userConfigFile = null;
  
          if (conf != null) {
              Configuration child = conf.getChild("user-config");
              if (child != null) {
                  try {
                      userConfig = child.getAttribute("src");
                  } catch(Exception ex) {
                      // No config file specified
                  }
              }
  
              this.setContentLength = conf.getChild("set-content-length").getValueAsBoolean(true);
          }
  
          // Check for null, use external or inbuilt config.
          if(userConfig != null) {
              try {
                  userConfigFile = new java.io.File(userConfig);
                  options = new Options(userConfigFile);
                  getLogger().debug("Using config file " + userConfig);
              } catch (Exception ex) {
                  getLogger().error("Cannot load  config " + userConfig, ex);
                  throw new ConfigurationException("Cannot load config " + userConfig, ex);
              }
          } else {
              try {
                  options = new Options();
                  getLogger().debug("Using default config file");
              } catch (Exception e) {
                  getLogger().error("Cannot load default config ", e);
              }
          }
  
          // Get the mime type.
          this.mimetype = conf.getAttribute("mime-type");
          getLogger().debug("FOPSerializer mime-type:" + mimetype);
  
          // Using the Renderer Factory, get the default renderer
          // for this MIME type.
          this.renderer = factory.createRenderer(mimetype);
          // Iterate through the parameters, looking for a renderer reference
          Configuration[] parameters = conf.getChildren("parameter");
          for (int i = 0; i < parameters.length; i++) {
              String name = parameters[i].getAttribute("name");
              if ("renderer".equals(name)) {
                  this.rendererName = parameters[i].getAttribute("value");
                  try {
                      this.renderer = (Renderer)ClassUtils.newInstance(rendererName);
                  } catch (Exception ex) {
                      getLogger().error("Cannot load  class " + rendererName, ex);
                      throw new ConfigurationException("Cannot load class " + rendererName, ex);
                  }
              }
          }
  
          // Do we have a renderer yet?
          if (this.renderer == null ) {
              throw new ConfigurationException(
                  "Could not autodetect renderer for FOPSerializer and "
                  + "no renderer was specified in the sitemap configuration."
              );
          }
      }
  
      /**
       * Return the MIME type.
       */
      public String getMimeType() {
          return mimetype;
      }
  
      /**
       * Create the FOP driver
       * Set the <code>OutputStream</code> where the XML should be serialized.
       */
      public void setOutputStream(OutputStream out) {
          // load the fop driver
          this.driver = new Driver();
  
          this.driver.setLogger(this.logger);
          if (this.rendererName == null) {
              this.renderer = factory.createRenderer(mimetype);
          } else {
              try {
                  this.renderer = (Renderer)ClassUtils.newInstance(this.rendererName);
              } catch (Exception ex) {
                  this.getLogger().error("Cannot load  class " + this.rendererName, ex);
                  throw new RuntimeException("Cannot load class " + this.rendererName);
              }
          }
          this.driver.setRenderer(this.renderer);
          this.driver.setOutputStream(out);
          this.setContentHandler( this.driver.getContentHandler() );
      }
  
      /**
       * Generate the unique key.
       * This key must be unique inside the space of this component.
       * This method must be invoked before the generateValidity() method.
       *
       * @return The generated key or <code>0</code> if the component
       *              is currently not cacheable.
       */
      public java.io.Serializable generateKey() {
          return "1";
      }
  
      /**
       * Generate the validity object.
       * Before this method can be invoked the generateKey() method
       * must be invoked.
       *
       * @return The generated validity object or <code>null</code> if the
       *         component is currently not cacheable.
       */
      public SourceValidity generateValidity() {
          return NOPValidity.SHARED_INSTANCE;
      }
  
      /**
        * Recycle serializer by removing references
        */
      public void recycle() {
          super.recycle();
          this.options = null;
          this.driver = null;
          this.renderer = null;
      }
  
      /**
      * Test if the component wants to set the content length
      */
      public boolean shouldSetContentLength() {
          return this.setContentLength;
      }
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org