You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@forrest.apache.org by st...@apache.org on 2002/07/11 10:54:54 UTC

cvs commit: xml-forrest/src/scratchpad/src/java/org/apache/forrest/components/generator XNIConfigurableFileGenerator.java

stevenn     2002/07/11 01:54:54

  Modified:    .        build.xml
               src/documentation/content/xdocs book.xml
               src/resources/conf sitemap.xmap
  Added:       src/documentation/content/xdocs dtd-docs.xml
               src/resources/library/xslt dtdx2flat.xsl flat2document.xsl
               src/scratchpad/src/java/org/apache/forrest/components/generator
                        XNIConfigurableFileGenerator.java
  Log:
  added NekoDTD support as a Cocoon generator
  
  Revision  Changes    Path
  1.11      +0 -25     xml-forrest/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-forrest/build.xml,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- build.xml	9 Jul 2002 16:05:02 -0000	1.10
  +++ build.xml	11 Jul 2002 08:54:53 -0000	1.11
  @@ -346,31 +346,6 @@
        </xmlvalidate>
     </target>
   
  -
  -  <!-- =================================================================== -->
  -  <!-- Generate DTD documentation                                          -->
  -  <!-- =================================================================== -->
  -  
  -  <!-- caution: unfinished work! -->
  -  
  -  <!-- part one: NekoDTD + resolver.jar support -->
  -  <target name="nekodtd" description="Transforms DTDs in the DTDx format." depends="init, compile">
  -    <java classname="org.apache.forrest.XNIWriter" fork="true" failonerror="true">
  -      <classpath>
  -        <path refid="classpath"/>
  -        <pathelement location="build/scratchpad/classes"/>
  -        <pathelement location="src/resources/schema"/>
  -      </classpath>
  -      <arg line="-p org.cyberneko.dtd.DTDConfiguration"/>
  -      <arg line="-o ${build.context}/document-v11.dtdx.xml"/>
  -      <arg line="-cat src/resources/schema/catalog"/>
  -      <arg value="src/resources/schema/dtd/document-v11.dtd"/>
  -    </java>
  -  </target>
  -  
  -  <!-- part two (TBD): producing HTML output -->
  -
  -
     <!-- =================================================================== -->
     <!-- The documentation system                                            -->
     <!-- =================================================================== -->
  
  
  
  1.15      +1 -0      xml-forrest/src/documentation/content/xdocs/book.xml
  
  Index: book.xml
  ===================================================================
  RCS file: /home/cvs/xml-forrest/src/documentation/content/xdocs/book.xml,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- book.xml	12 Jun 2002 04:45:53 -0000	1.14
  +++ book.xml	11 Jul 2002 08:54:54 -0000	1.15
  @@ -33,6 +33,7 @@
     </menu>
   
     <menu label="Document Samples">
  +    <menu-item label="DTD documentation" href="dtd-docs.html"/>
       <menu-item label="document-v11" href="document-v11.html"/>
       <menu-item label="How-Tos" href="community/howto/index.html"/>
       <menu-item label="xml.apache.org" href="xml-site/index.html"/>
  
  
  
  1.1                  xml-forrest/src/documentation/content/xdocs/dtd-docs.xml
  
  Index: dtd-docs.xml
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.1//EN" "document-v11.dtd">
  <document>
    <header>
      <title>Forrest DTD documentation</title><authors><person
        name="Steven Noels" email="stevenn@apache.org"/></authors>
    </header>
    <body>
      <p>We use the
        <link
     href="http://www.apache.org/~andyc/neko/doc/dtd/index.html">NekoDTD</link> tool
    packaged as a Cocoon Generator to generate automagically some hyperlinked DTD
    references. Note that these references do not reflect the modularity of our
    DTDs, they are primarily user-oriented.</p>
    <p>There exists generated documentation for:</p>
    <ul>
      <li><link href="document-v11.dtdx.html">document-v11</link></li>
    <li><link href="howto-v10.dtdx.html">howto-v10</link></li>
    <li><link href="faq-v11.dtdx.html">faq-v11</link></li>
    </ul>
    <p>Hope this helps ;-)</p>
    </body>
  </document>
  
  
  
  1.10      +15 -0     xml-forrest/src/resources/conf/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  RCS file: /home/cvs/xml-forrest/src/resources/conf/sitemap.xmap,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- sitemap.xmap	7 Jul 2002 19:12:27 -0000	1.9
  +++ sitemap.xmap	11 Jul 2002 08:54:54 -0000	1.10
  @@ -11,6 +11,7 @@
      <map:generator  name="directory"   src="org.apache.cocoon.generation.DirectoryGenerator" label="content"/>
      <map:generator  name="html"        src="org.apache.cocoon.generation.HTMLGenerator" label="content"/>
      <map:generator  name="libre"       src="org.outerj.yer.use.cocoon.HierarchyGenerator" label="content"/>
  +   <map:generator  name="nekodtd"     src="org.apache.forrest.components.generator.XNIConfigurableFileGenerator" label="content"/>
      <!-- FIXME: Change this once better view handling is implemented -->
      <map:generator  name="file-nolabel" src="org.apache.cocoon.generation.FileGenerator"/>
     </map:generators>
  @@ -222,6 +223,20 @@
        <map:transform src="library/xslt/doclist2document.xsl"/>
        <map:call resource="skinit">
         <map:parameter name="type" value="document2html"/>
  +     </map:call>
  +   </map:match>
  +
  +   <!-- HTML DTD Documentation generation pipeline, makes use of NekoDTD tool -->
  +
  +   <map:match pattern="body-**.dtdx.xml">
  +     <map:generate type="nekodtd" src="resources/schema/dtd/{1}.dtd">
  +       <map:parameter name="config-class" value="org.cyberneko.dtd.DTDConfiguration" />
  +     </map:generate>
  +     <map:transform src="library/xslt/dtdx2flat.xsl"/>
  +     <map:transform src="library/xslt/flat2document.xsl"/>
  +     <map:call resource="skinit">
  +      <map:parameter name="type" value="document2html"/>
  +      <map:parameter name="isfaq" value="true"/>
        </map:call>
      </map:match>
   
  
  
  
  1.1                  xml-forrest/src/resources/library/xslt/dtdx2flat.xsl
  
  Index: dtdx2flat.xsl
  ===================================================================
  <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
  
   <xsl:output indent="yes"/>
  
   <xsl:template match='/'>
    <xsl:apply-templates/>
   </xsl:template>
  
   <xsl:template match='@*|dtd|attlist|attributeDecl|enumeration|notationDecl'>
    <xsl:copy>
     <xsl:apply-templates select='@*|*'/>
    </xsl:copy>
   </xsl:template>
  
   <xsl:template match='group[count(*)=1][group]'>
    <xsl:apply-templates/>
   </xsl:template>
  
   <xsl:template match='contentModel|any|empty|group|pcdata|element|separator|occurrence'>
    <xsl:copy>
     <xsl:apply-templates select='@*|*'/>
    </xsl:copy>
   </xsl:template>
  
   <xsl:template match="comment"/>
  
   <xsl:template match='*'>
    <xsl:apply-templates/>
   </xsl:template>
  
  </xsl:stylesheet>
  
  
  
  1.1                  xml-forrest/src/resources/library/xslt/flat2document.xsl
  
  Index: flat2document.xsl
  ===================================================================
  <?xml version='1.0'?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:key name="attlistbyname" match="attlist" use="@ename"/>
  <xsl:key name="contentmodelbychildren" match="contentModel" use="descendant::element/@name"/>
  
  <xsl:template match="dtd">
    <document>
      <header>
        <title>DTD documentation</title>
        <subtitle><xsl:value-of select="@sysid"/></subtitle>
      </header>
      <body>
        <section><title>Top-level element(s)</title>
          <ul>
            <xsl:for-each select="/dtd/contentModel[not(key('contentmodelbychildren', @ename))]">
              <li>
                <link href="#{@ename}"><xsl:value-of select="@ename"/></link>
              </li>
            </xsl:for-each>
          </ul>
        </section>
        <section><title>List of elements</title>
          <ul>
            <xsl:for-each select="contentModel">
              <xsl:sort select="@ename"/>
              <li>
                <link href="#{@ename}"><xsl:value-of select="@ename"/></link>
              </li>
            </xsl:for-each>
          </ul>
        </section>
        <section>
          <title>Element declarations</title>
          <xsl:apply-templates select="contentModel"/>
        </section>
      </body>
    </document>
  </xsl:template>
  
  <xsl:template match="contentModel">
    <section id="{@ename}">
      <title><xsl:value-of select="@ename"/></title>
      <section><title>Content Model</title>
        <p>
          <xsl:apply-templates/>
        </p>
      </section>
      <xsl:if test="key('attlistbyname',@ename)">
        <section><title>Attributes</title>
          <xsl:apply-templates select="key('attlistbyname',@ename)"/>
        </section>
      </xsl:if>
      <xsl:if test="key('contentmodelbychildren',@ename)">
        <section><title>Used inside</title>
          <p>
            <xsl:for-each select="key('contentmodelbychildren',@ename)">
              <link href="#{@ename}"><xsl:value-of select="@ename"/></link>
              <xsl:if test="not(position() = last())">, </xsl:if>
            </xsl:for-each>
          </p>
        </section>
      </xsl:if>
    </section>
  </xsl:template>
  
  <xsl:template match="empty">
    EMPTY
  </xsl:template>
  
  <xsl:template match="pcdata">
    #PCDATA
  </xsl:template>
  
  <xsl:template match="element">
    <link href="#{@name}"><xsl:value-of select="@name"/></link>
  </xsl:template>
  
  <xsl:template match="group">
    <xsl:text>( </xsl:text><xsl:apply-templates/><xsl:text> )</xsl:text>
  </xsl:template>
  
  <xsl:template match="separator">
    <xsl:text> </xsl:text><xsl:value-of select="@type"/><xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template match="occurrence">
    <xsl:text> </xsl:text><xsl:value-of select="@type"/>
  </xsl:template>
  
  <xsl:template match="attlist">
    <dl>
      <xsl:apply-templates/>
    </dl>
  </xsl:template>
  
  <xsl:template match="attributeDecl">
    <dt>
      <strong><xsl:value-of select="@aname"/></strong>
    </dt>
    <dd>
      <xsl:if test="not(enumeration)">
        <em>type: </em><xsl:value-of select="@atype"/><br/>
      </xsl:if>
      <xsl:if test="@required">
        required attribute<br/>
      </xsl:if>
      <xsl:if test="@default">
        <em>default value: </em><xsl:value-of select="@default"/><br/>
      </xsl:if>
      <xsl:if test="@fixed">
        <em>fixed value: </em><xsl:value-of select="@default"/><br/>
      </xsl:if>
      <xsl:if test="enumeration">
        <em>possible values: </em><xsl:for-each select="enumeration">
          <xsl:value-of select="@value"/><xsl:text> </xsl:text>
        </xsl:for-each>
        <br/>
      </xsl:if>
    </dd>
  </xsl:template>
  
  </xsl:stylesheet>
  
  
  
  1.1                  xml-forrest/src/scratchpad/src/java/org/apache/forrest/components/generator/XNIConfigurableFileGenerator.java
  
  Index: XNIConfigurableFileGenerator.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001, 2002 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 "Xerces" 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, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.forrest.components.generator;
  
  import org.apache.cocoon.generation.ComposerGenerator;
  import org.apache.cocoon.caching.Cacheable;
  import org.apache.cocoon.caching.CacheValidity;
  import org.apache.cocoon.caching.TimeStampCacheValidity;
  import org.apache.cocoon.environment.Source;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.ResourceNotFoundException;
  //import org.apache.cocoon.components.resolver.Resolver;
  import org.apache.avalon.excalibur.pool.Recyclable;
  import org.apache.avalon.excalibur.xml.EntityResolver;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.parameters.ParameterException;
  import org.apache.xerces.xni.parser.XMLParserConfiguration;
  import org.apache.xerces.xni.parser.XMLConfigurationException;
  import org.apache.xerces.util.EntityResolverWrapper;
  import org.apache.xerces.parsers.AbstractSAXParser;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  
  import java.util.Map;
  import java.io.IOException;
  
  
  /** Class <code>org.apache.forrest.components.generator.XNIConfigurableFileGenerator</code>
   *  This class implements a Cocoon Generator that is configurable through
   *  the XNI mechanism that is built into xerces 2.x
   *  The generator is used with:
   *
      <map:generators default="">
        <map:generator name="nekodtd" src="org.apache.forrest.components.generator.XNIConfigurableFileGenerator" label="content" />
      </map:generators>
   *
   * and:
   *
     <map:match pattern="foobar">
       <map:generate type="nekodtd" src="resources/schema/dtd/{1}.dtd">
         <map:parameter name="config-class" value="org.cyberneko.dtd.DTDConfiguration" />
       </map:generate>
       <map:serialize type="xml"/>
     </map:match>
   *
   * TODO: check how some XNIConfigurableXMLReader component (Excalibur style
   *   like the JaxpParser) can be built to do this.  Then the complete
   *   parser can be recycled.
   *
   * @author $Author: stevenn $
   * @version CVS $Id: XNIConfigurableFileGenerator.java,v 1.1 2002/07/11 08:54:54 stevenn Exp $
   */
  public class XNIConfigurableFileGenerator
  extends ComposerGenerator implements Cacheable, Recyclable
  {
  
    /** Default constructor
     * 
     */
    public XNIConfigurableFileGenerator()
    {
    }
  
    public static final String CONFIGCLASS_PARAMETER = "config-class";
    public static final String FULL_ENTITY_RESOLVER_PROPERTY_URI =
        org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX +
        org.apache.xerces.impl.Constants.ENTITY_RESOLVER_PROPERTY;
    /** The  source */
    private Source inputSource;
  
    /** The XNIConfiguredParser */
    XMLParserConfiguration parserConfig;
  
    /**
     * Recycle this component.
     * All instance variables are set to <code>null</code>.
     */
    public void recycle() {
      super.recycle();
      if (this.inputSource != null) {
        this.inputSource.recycle();
        this.inputSource = null;
      }
    }
  
    /**
     * Copy paste en serious cut from cocoon HTML Generator
     */
    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
    throws ProcessingException, SAXException, IOException {
      super.setup(resolver, objectModel, src, par);
      this.inputSource = resolver.resolve(super.source);
      String parserName = null;
  
      try {
        parserName = par.getParameter(CONFIGCLASS_PARAMETER);
        parserConfig = (XMLParserConfiguration)Class.forName(parserName).newInstance();
      } catch(ParameterException e) {
        getLogger().error("Missing parameter " + CONFIGCLASS_PARAMETER, e);
        throw new ProcessingException("XNIConfigurable.setup()",e);
      } catch(InstantiationException e) {
        getLogger().error("Can not make instance of " + parserName, e);
        throw new ProcessingException("XNIConfigurable.setup()",e);
      } catch(IllegalAccessException e) {
        getLogger().error("Can not access constructor of " + parserName, e);
        throw new ProcessingException("XNIConfigurable.setup()",e);
      } catch(ClassNotFoundException e) {
        getLogger().error("Can not find " + parserName, e);
        throw new ProcessingException("XNIConfigurable.setup()",e);
      }
    }
  
    /**
     * 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 long generateKey() {
  /* TODO: investigate how we can handle dependend files
     this work was originally for DTD stuff, and external entities to this
     will not be known to have changed, so currently no cache.
  
     if (this.inputSource.getLastModified() != 0) {
        return HashUtil.hash(this.inputSource.getSystemId());
      }
   */
      return 0;
    }
  
    /**
     * 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 CacheValidity generateValidity() {
      if (this.inputSource.getLastModified() != 0) {
        return new TimeStampCacheValidity(this.inputSource.getLastModified());
      }
      return null;
    }
  
    /**
     * Generate XML data.
     */
    public void generate()
    throws IOException, SAXException, ProcessingException {
      try {
        getLogger().debug("XNIConfigurable generator start generate()");
  
        //TODO: The Configurable Parser should become a full blown avalon component
        //      And the resolver should be configured and composed on that level.
  
        final EntityResolver catalogResolver = (EntityResolver)this.manager.lookup(EntityResolver.ROLE);
        final String[] extendRecognizedProperties = {FULL_ENTITY_RESOLVER_PROPERTY_URI};
        parserConfig.addRecognizedProperties(extendRecognizedProperties);
        parserConfig.setProperty(FULL_ENTITY_RESOLVER_PROPERTY_URI, new EntityResolverWrapper(catalogResolver));
        final XMLReader parser = new AbstractSAXParser(parserConfig){};
        parser.setFeature("http://xml.org/sax/features/namespaces", true);
        parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
  
        //parser.setEntityResolver(catalogResolver);
  
  
        parser.setContentHandler(this.contentHandler);
        parser.parse(this.inputSource.getInputSource());
  
        // TODO: and afterwards we could release that Parser.
        this.manager.release(catalogResolver);
      } catch (IOException e){
        getLogger().warn("XNIConfigurable.generate()", e);
        throw new ResourceNotFoundException("Could not get resource to process:\n["
                + "src = " + this.inputSource.getSystemId() + "]\n", e);
      } catch (SAXException e){
        getLogger().error("XNIConfigurable.generate()", e);
        throw e;
      } catch (ProcessingException e){
        throw e;
      } catch (XMLConfigurationException e) {
        getLogger().error( "Misconfig " + e.getType(), e);
        throw new ProcessingException("XNIConfigurable.generate()",e);
      } catch (Exception e){
        getLogger().error("Some strange thing just happened!!", e);
        throw new ProcessingException("XNIConfigurable.generate()",e);
      }
    }
  }