cziegeler    2004/05/01 11:06:12

  Modified:    src/java/org/apache/cocoon/generation FileGenerator.java
               tools/src/anttasks SitemapTask.java DocumentCache.java
  Added:       src/documentation/templates sitemap-component.xml
  Log:
  Generate XML documentation from a template
  
  Revision  Changes    Path
  1.8       +4 -1      
cocoon-2.1/src/java/org/apache/cocoon/generation/FileGenerator.java
  
  Index: FileGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/generation/FileGenerator.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FileGenerator.java        30 Apr 2004 07:20:36 -0000      1.7
  +++ FileGenerator.java        1 May 2004 18:06:12 -0000       1.8
  @@ -37,6 +37,9 @@
    * @cocoon.sitemap.component.name   file
    * @cocoon.sitemap.component.label  content
    * @cocoon.sitemap.component.logger sitemap.generator.file
  + * @cocoon.sitemap.component.documentation.caching
  + *               Uses the last modification date of the xml document for 
validation
  + * 
    * @cocoon.sitemap.component.pooling.min   8
    * @cocoon.sitemap.component.pooling.max  32
    * @cocoon.sitemap.component.pooling.grow  4
  
  
  
  1.5       +123 -18   cocoon-2.1/tools/src/anttasks/SitemapTask.java
  
  Index: SitemapTask.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/tools/src/anttasks/SitemapTask.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SitemapTask.java  1 May 2004 16:31:15 -0000       1.4
  +++ SitemapTask.java  1 May 2004 18:06:12 -0000       1.5
  @@ -16,7 +16,6 @@
    */
   
   import java.io.File;
  -import java.io.FileWriter;
   import java.io.IOException;
   import java.util.ArrayList;
   import java.util.HashMap;
  @@ -24,6 +23,8 @@
   import java.util.List;
   import java.util.Map;
   
  +import javax.xml.transform.TransformerException;
  +
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.FileSet;
  @@ -56,7 +57,11 @@
       public static final String NO_DOC_TAG = 
"cocoon.sitemap.component.documentation.disabled";
       /** The documentation (optional) */
       public static final String DOC_TAG    = 
"cocoon.sitemap.component.documentation";
  -    
  +    /** Configuration (optional) */
  +    public static final String CONF_TAG   = 
"cocoon.sitemap.component.configuration";
  +    /** Caching info (optional) */
  +    public static final String CACHING_INFO_TAG = 
"cocoon.sitemap.component.documentation.caching";
  +
       /** Pooling min (optional) */
       public static final String POOL_MIN_TAG = 
"cocoon.sitemap.component.pooling.min";
       /** Pooling max (optional) */
  @@ -233,7 +238,11 @@
           while ( iter.hasNext() ) {
               final SitemapComponent component = (SitemapComponent)iter.next();
               
  -            component.generateDocs(this.docDir);
  +            // Read template
  +            final File templateFile = 
this.getProject().resolveFile("src/documentation/templates/sitemap-component.xml");
  +            Document template = DocumentCache.getDocument(templateFile, 
this);
  +            
  +            component.generateDocs(template, this.docDir, this.getProject());
           }
           
       }
  @@ -309,7 +318,14 @@
               }
               parent.appendChild(node);
               newLine(parent);
  -            // TODO Add configuration
  +            
  +            // add configuration
  +            String configuration = this.getTagValue(CONF_TAG, null);
  +            if ( configuration != null ) {
  +                configuration = "<root>" + configuration + "</root>";
  +                final Document confDoc = 
DocumentCache.getDocument(configuration);
  +                setValue(node, null, 
confDoc.getDocumentElement().getChildNodes());
  +            }
   
               return true;
           }
  @@ -335,24 +351,79 @@
               node.appendChild(n);
           }
           
  -        public void generateDocs(File parentDir) {
  +        public void generateDocs(Document template, File parentDir, Project 
project) 
  +        throws TransformerException {
  +            final File componentsDir = new File(parentDir, this.type+'s');
  +            componentsDir.mkdir();
  +            
  +            final File docFile = new File(componentsDir, this.name + 
"-generated.xml");
  +
               final String doc = this.getDocumentation();
               if ( doc == null ) {
  +                if ( docFile.exists() ) {
  +                    docFile.delete();
  +                }
                   return;
               }
  -            try {
  -                final File componentsDir = new File(parentDir, 
this.type+'s');
  -                componentsDir.mkdir();
  -                
  -                final File docFile = new File(componentsDir, this.name + 
".txt");
  -                docFile.createNewFile();
  -                
  -                final FileWriter writer = new FileWriter(docFile);
  -                writer.write(doc);
  -                writer.close();
  -            } catch (IOException ioe) {
  -                throw new BuildException("Error writing doc.", ioe);
  +            // get body from template
  +            final Node body = XPathAPI.selectSingleNode(template, 
"/document/body");
  +            
  +            // append root element and surrounding paragraph
  +            final String description = "<root><p>" + doc + "</p></root>";
  +            final Document descriptionDoc = 
DocumentCache.getDocument(description);
  +            
  +            // Title
  +            setValue(template, "/document/header/title", 
  +                     "Description of the " + this.name + " " + this.type);
  +            
  +            // Version
  +            setValue(template, "/document/header/version",
  +                     project.getProperty("version"));
  +            
  +            // Description
  +            setValue(body, "[EMAIL PROTECTED]'Description']", 
  +                     descriptionDoc.getDocumentElement().getChildNodes());
  +            
  +            // check: deprecated?
  +            if ( this.getTagValue("deprecated", null) != null ) {
  +                Node node = XPathAPI.selectSingleNode(body, "[EMAIL 
PROTECTED]'Description']");
  +                // node is never null - this is ensured by the test above
  +                Element e = node.getOwnerDocument().createElement("note");
  +                node.appendChild(e);
  +                e.appendChild(node.getOwnerDocument().createTextNode("This 
component is deprecated."));
  +                final String info = this.getTagValue("deprecated", null);
  +                if ( info != null ) {
  +                    
e.appendChild(node.getOwnerDocument().createTextNode(info));
  +                }
  +            }
  +            
  +            // Info - Name
  +            setValue(body, "[EMAIL PROTECTED]'Info']/table/tr[1]/td[2]", 
this.name);
  +            // Info - Class
  +            setValue(body, "[EMAIL PROTECTED]'Info']/table/tr[2]/td[2]", 
this.javaClass.getFullyQualifiedName());
  +            // Info - Cacheable
  +            String cacheInfo;
  +            if ( 
this.javaClass.isA("org.apache.cocoon.caching.CacheableProcessingComponent") ) {
  +                cacheInfo = this.getTagValue(CACHING_INFO_TAG, null);
  +                if ( cacheInfo != null ) {
  +                    cacheInfo = "Yes - " + cacheInfo;
  +                } else {
  +                    cacheInfo = "Yes";
  +                }
  +            } else if ( 
this.javaClass.isA("org.apache.cocoon.caching.Cacheable") ) {
  +                cacheInfo = this.getTagValue(CACHING_INFO_TAG, null);
  +                if ( cacheInfo != null ) {
  +                    cacheInfo = "Yes (2.0 Caching) - " + cacheInfo;
  +                } else {
  +                    cacheInfo = "Yes (2.0 Caching)";
  +                }
  +            } else {
  +                cacheInfo = "No";
               }
  +            setValue(body, "[EMAIL PROTECTED]'Info']/table/tr[3]/td[2]", 
cacheInfo);
  +            
  +            // finally write the doc
  +            DocumentCache.writeDocument(docFile, template, null);            
           }
           
           /**
  @@ -394,6 +465,40 @@
               } else {
                   throw new BuildException("Sitemap component " + 
clazz.getName() + " does not implement a sitemap component interface.");
               }            
  +        }
  +        
  +        private static void setValue(Node node, String xpath, String value) {
  +            try {
  +                final Node insertNode = (xpath == null ? node : 
XPathAPI.selectSingleNode(node, xpath));
  +                if ( insertNode == null ) {
  +                    throw new BuildException("Node (" + xpath + ") not 
found.");
  +                }
  +                Node text = 
insertNode.getOwnerDocument().createTextNode(value);
  +                while (insertNode.hasChildNodes() ) {
  +                    insertNode.removeChild(insertNode.getFirstChild());
  +                }
  +                insertNode.appendChild(text);
  +            } catch (TransformerException e) {
  +                throw new BuildException(e);
  +            }
  +        }
  +
  +        private static void setValue(Node node, String xpath, NodeList 
nodes) {
  +            try {
  +                final Node insertNode = (xpath == null ? node : 
XPathAPI.selectSingleNode(node, xpath));
  +                if ( insertNode == null ) {
  +                    throw new BuildException("Node (" + xpath + ") not 
found.");
  +                }
  +                while (insertNode.hasChildNodes() ) {
  +                    insertNode.removeChild(insertNode.getFirstChild());
  +                }
  +                for(int i=0; i<nodes.getLength(); i++) {
  +                    final Node current = nodes.item(i);
  +                    
insertNode.appendChild(insertNode.getOwnerDocument().importNode(current, true));
  +                }
  +            } catch (TransformerException e) {
  +                throw new BuildException(e);
  +            }
           }
       }
   }
  
  
  
  1.4       +16 -3     cocoon-2.1/tools/src/anttasks/DocumentCache.java
  
  Index: DocumentCache.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/tools/src/anttasks/DocumentCache.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DocumentCache.java        1 May 2004 16:12:05 -0000       1.3
  +++ DocumentCache.java        1 May 2004 18:06:12 -0000       1.4
  @@ -16,6 +16,7 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.io.StringReader;
   import java.util.HashMap;
   import java.util.Map;
   import java.util.Properties;
  @@ -35,6 +36,7 @@
   import org.apache.tools.ant.Task;
   import org.w3c.dom.Document;
   import org.w3c.dom.DocumentType;
  +import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
   
   /**
  @@ -91,15 +93,26 @@
           return document;
       }
       
  +    public static Document getDocument(String string) {
  +        try {
  +            final InputSource is = new InputSource(new StringReader(string));
  +            return builder.parse(is);
  +        } catch (Exception e) {
  +            throw new BuildException("Unable to parse string.");
  +        }
  +    }
  +    
       public static void storeDocument(File file, Document document, Task 
task) 
  -    throws IOException {  
  +    throws IOException {
           task.log("Storing file in cache: " + file, Project.MSG_DEBUG);
           final String fileName = file.toURL().toExternalForm();
           fileCache.put(fileName, document);
       }
   
       public static void writeDocument(File file, Document document, Task 
task) {
  -        task.log("Writing: " + file);
  +        if ( task != null ) {
  +            task.log("Writing: " + file);
  +        }
           // Set the DOCTYPE output option on the transformer 
           // if we have any DOCTYPE declaration in the input xml document
           final DocumentType doctype = document.getDoctype();
  
  
  
  1.1                  
cocoon-2.1/src/documentation/templates/sitemap-component.xml
  
  Index: sitemap-component.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!--
    Copyright 1999-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.0//EN" 
"dtd/document-v10.dtd">
  <document>
    <header>
      <title></title>
      <version></version>
      <type>Reference</type>
    </header>
    <body>
      <s1 title="Description">
      </s1>
      <s1 title="Info">
        <table>
          <tr>
            <td>Name</td>
            <td/>
          </tr>
          <tr>
            <td>Class</td>
            <td/>
          </tr>
          <tr>
            <td>Cacheable</td>
            <td/>
          </tr>
        </table>
      </s1>
    </body>
  </document>
  
  
  

Reply via email to