cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cross...@apache.org
Subject cvs commit: xml-cocoon2/tools/src ClassAvailableTask.java SitemapToolTask.java UserInputTask.java XConfToolTask.java ClassAvailable.java SitemapTool.java UserInput.java XConfTool.java
Date Fri, 25 Jan 2002 02:31:53 GMT
crossley    02/01/24 18:31:53

  Modified:    .        build-i.xml build-s.xml build.xml
  Added:       tools/src ClassAvailableTask.java SitemapToolTask.java
                        UserInputTask.java XConfToolTask.java
  Removed:     tools/src ClassAvailable.java SitemapTool.java
                        UserInput.java XConfTool.java
  Log:
  Changed class names for Ant tasks, to be consistent (end with Task).
  Changed names for Ant task declarations in build.xml to use
  use the "xml-type" convention instead of the "javaType" convention.
  Submitted by:	"Nicola Ken Barozzi" <barozzi@nicolaken.com>
  
  Revision  Changes    Path
  1.3       +1 -1      xml-cocoon2/build-i.xml
  
  Index: build-i.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/build-i.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- build-i.xml	24 Jan 2002 07:46:20 -0000	1.2
  +++ build-i.xml	25 Jan 2002 02:31:53 -0000	1.3
  @@ -25,7 +25,7 @@
       <echo message=" clean -------- cleans the build directory"/>
       <echo message=" "/>
       <echo message=" "/>
  -    <taskdef name="user-input" classname="UserInput"
  +    <taskdef name="user-input" classname="UserInputTask"
           classpath="./tools/anttasks"/>
   
       <property name="input.selection" value="compile"/>
  
  
  
  1.3       +1 -1      xml-cocoon2/build-s.xml
  
  Index: build-s.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/build-s.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- build-s.xml	24 Jan 2002 07:46:20 -0000	1.2
  +++ build-s.xml	25 Jan 2002 02:31:53 -0000	1.3
  @@ -26,7 +26,7 @@
       <echo message=" --------------"/>
       <echo message=" --------------"/>
       <echo message=" "/>
  -    <taskdef name="user-input" classname="UserInput"
  +    <taskdef name="user-input" classname="UserInputTask"
           classpath="./tools/anttasks"/>
   
       <property name="input.selection" value=""/>
  
  
  
  1.147     +27 -27    xml-cocoon2/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/build.xml,v
  retrieving revision 1.146
  retrieving revision 1.147
  diff -u -r1.146 -r1.147
  --- build.xml	24 Jan 2002 14:17:36 -0000	1.146
  +++ build.xml	25 Jan 2002 02:31:53 -0000	1.147
  @@ -277,7 +277,7 @@
             description="Tests for optional classes missing to the environment">
     
       <!-- A simple task to test for a class -->
  -    <taskdef name="ClassAvailable" classname="ClassAvailable" 
  +    <taskdef name="class-available" classname="ClassAvailableTask" 
           classpath="./tools/anttasks"/>
   
       <available property="jaxen.present" classname="org.jaxen.dom.XPath">
  @@ -288,47 +288,47 @@
       </available>
   
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="xmldb.present" 
                       classname="org.xmldb.api.DatabaseManager"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="jfor.present" 
                       classname="org.jfor.jfor.converter.Converter"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="servlet.present" 
                       classname="javax.servlet.http.HttpServlet"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="servlet23.present" 
                       classname="javax.servlet.http.HttpServletRequestWrapper"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="bsf.present" 
                       classname="com.ibm.bsf.BSFException"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="rhino.present" 
                       classname="org.mozilla.javascript.tools.jsc.Main"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="jstyle.present" 
                       classname="jstyle.JSFormatter"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="xpath.present" 
                       classname="org.apache.xpath.XPathAPI"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="deli.present" 
                       classname="com.hp.hpl.deli.Profile"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="velocity.present" 
                       classname="org.apache.velocity.app.Velocity"/>
   
  -    <ClassAvailable classpathref="classpath"
  +    <class-available classpathref="classpath"
                       property="resolver.present" 
                       classname="com.sun.resolver.Resolver"/>
   
  @@ -351,39 +351,39 @@
         <classpath refid="classpath"/>
       </available>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="php.present" 
                       classname="net.php.servlet"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="xt.present" 
                       classname="com.jclark.xsl.sax.XMLProcessorImpl"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="naming.present" 
                       classname="javax.naming.Context"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="svg.present" 
                       classname="org.apache.batik.transcoder.Transcoder"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="fop.present" 
                       classname="org.apache.fop.apps.Driver"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="tidy.present" 
                       classname="org.w3c.tidy.Tidy"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="maybeupload.present" 
                       classname="uk.co.weft.maybeupload.MaybeUploadRequestWrapper"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="lucene.present" 
                       classname="org.apache.lucene.search.Searcher"/>
   
  -    <ClassAvailable classpathref="classpath" 
  +    <class-available classpathref="classpath" 
                       property="hsqldb.present" 
                       classname="org.hsqldb.Server"/>
     </target>
  @@ -895,7 +895,7 @@
     </target>
   
     <!-- =================================================================== -->
  -  <!-- The documentation system (nearly beta...)                           -->
  +  <!-- The documentation system                                            -->
     <!-- =================================================================== -->
     <target name="docs"
             depends="package, prepare-docs, docs_check, docs_done"
  @@ -969,19 +969,19 @@
     <!-- =================================================================== -->
     <target name="prepare-webapp" depends="copy-webapp">
         <!-- A task to change the sitemap. It is used to add optional components -->
  -      <taskdef name="SitemapTool" classname="SitemapTool" 
  +      <taskdef name="sitemap-tool" classname="SitemapToolTask" 
             classpath="${tools.dir}/anttasks"/>
         <!-- A task to change the xconf. It is used to add optional components -->
  -      <taskdef name="XConfTool" classname="XConfTool" 
  +      <taskdef name="xconf-tool" classname="XConfToolTask" 
             classpath="${tools.dir}/anttasks"/>
   
  -      <!-- Invoke the SitemapTool to add optional entries -->
  -      <SitemapTool directory="${build.src}"
  +      <!-- Invoke the SitemapToolTask to add optional entries -->
  +      <sitemap-tool directory="${build.src}"
                      extension="sitemap"
                      sitemap="${build.war}/sitemap.xmap"/>
   
         <!-- Invoke the XConfTool to add optional entries -->
  -      <XConfTool directory="${build.src}"
  +      <xconf-tool directory="${build.src}"
                    extension="xconf"
                    configuration="${build.war}/cocoon.xconf"/>
     </target>
  
  
  
  1.1                  xml-cocoon2/tools/src/ClassAvailableTask.java
  
  Index: ClassAvailableTask.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) 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 which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  import java.io.*;
  import java.util.*;
  import java.util.zip.*;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.taskdefs.*;
  import org.apache.tools.ant.types.*;
  
  /**
   * Will set the given property if the requested class is available in the
   * specified classpath. The found class is not loaded!
   * This class is heavily based on the available task in the ant package:
   * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
   *
   * This task searches only in the defined path but not in the parents path
   * unless explicitly overridden by the value of ${build.sysclasspath}
   * like the original available task does.
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/01/25 02:31:53 $
   */
  
  public class ClassAvailableTask extends Task {
  
      /**
       * A hashtable of zip files opened by the classloader
       */
      private Hashtable zipFiles = new Hashtable();
  
      private String property;
      private String classname;
      private Path classpath;
      private String value = "true";
  
      public void setClasspath(Path classpath) {
          createClasspath().append(classpath);
      }
  
      public Path createClasspath() {
          if (this.classpath == null) {
              this.classpath = new Path(this.project);
          }
          return this.classpath.createPath();
      }
  
      public void setClasspathRef(Reference r) {
          createClasspath().setRefid(r);
      }
  
      public void setProperty(String property) {
          this.property = property;
      }
  
      public void setValue(String value) {
          this.value = value;
      }
  
      public void setClassname(String classname) {
          if (!"".equals(classname)) {
              this.classname = classname;
          }
      }
  
      public void execute() throws BuildException {
          if (property == null) {
              throw new BuildException("property attribute is required", location);
          }
  
          if (eval()) {
              this.project.setProperty(property, value);
          }
      }
  
      public boolean  eval() throws BuildException {
          if (classname == null) {
              throw new BuildException("At least one of (classname|file|resource) is required",
location);
          }
  
          if (classpath != null) {
              classpath.setProject(project);
              classpath = classpath.concatSystemClasspath("ignore");
          }
  
          if (!findClassInComponents(classname)) {
              log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE);
              return false;
          }
  
          return true;
      }
  
      /**
       * Get an inputstream to a given resource in the given file which may
       * either be a directory or a zip file.
       *
       * @param file the file (directory or jar) in which to search for the resource.
       * @param resourceName the name of the resource for which a stream is required.
       *
       * @return a stream to the required resource or null if the resource cannot be
       * found in the given file object
       */
      private boolean contains(File file, String resourceName) {
          try {
              if (!file.exists()) {
                  return false;
              }
  
              if (file.isDirectory()) {
                  File resource = new File(file, resourceName);
  
                  if (resource.exists()) {
                      return true;
                  }
              }
              else {
                  // is the zip file in the cache
                  ZipFile zipFile = (ZipFile)zipFiles.get(file);
                  if (zipFile == null) {
                      zipFile = new ZipFile(file);
                      zipFiles.put(file, zipFile);
                  }
                  ZipEntry entry = zipFile.getEntry(resourceName);
                  if (entry != null) {
                      return true;
                  }
              }
          }
          catch (Exception e) {
              log("Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() +
                  " reading resource " + resourceName + " from " + file, Project.MSG_VERBOSE);
          }
  
          return false;
      }
  
      /**
       * Find a class on the given classpath.
       */
      private boolean findClassInComponents(String name) {
          // we need to search the components of the path to see if we can find the
          // class we want.
          final String classname = name.replace('.', '/') + ".class";
          final String[] list = classpath.list();
          boolean found = false;
          int i = 0;
          while (i < list.length && found == false) {
              final File pathComponent = (File)project.resolveFile(list[i]);
              found = this.contains(pathComponent, classname);
              i++;
          }
          return found;
      }
  }
  
  
  
  1.1                  xml-cocoon2/tools/src/SitemapToolTask.java
  
  Index: SitemapToolTask.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) 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 which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  import java.io.*;
  import java.util.*;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.taskdefs.*;
  import org.apache.tools.ant.types.*;
  
  /**
   * Add components to the sitemap
   *
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/01/25 02:31:53 $
   */
  
  public final class SitemapToolTask extends Task {
  
      private String sitemap;
      private String directory;
      private String extension;
  
      public void setSitemap(String sitemap) {
          this.sitemap = sitemap;
      }
  
      public void setDirectory(String directory) {
          this.directory = directory;
      }
  
      public void setExtension(String extension) {
          this.extension = extension;
      }
  
      public void execute() throws BuildException {
          if (this.sitemap == null) {
              throw new BuildException("sitemap attribute is required", location);
          }
  
          if (this.extension == null) {
              throw new BuildException("extension attribute is required", location);
          }
          if (this.directory == null) {
              throw new BuildException("directory attribute is required", location);
          }
  
          try {
              // process recursive
              this.process(new File(this.directory), this.extension, this.sitemap);
          } catch (IOException ioe) {
              throw new BuildException("IOException: " + ioe);
          }
      }
  
      /**
       * Scan recursive
       */
      private void process(final File   directoryFile,
                           final String ext,
                           final String sitemapLocation)
      throws IOException, BuildException {
  
          final File[] files = directoryFile.listFiles();
  
          for(int i = 0; i < files.length; i++) {
  
              if (files[i].isDirectory() == true) {
                  this.process(files[i], ext, sitemapLocation);
              } else {
  
                  if (files[i].getName().endsWith("."+ext) == true) {
                      System.out.println("Reading: " + files[i].getAbsolutePath());
                      final String data = this.load(files[i].getAbsolutePath());
  
                      // separate the data by lines
                      final StringTokenizer st = new StringTokenizer(data);
                      while (st.hasMoreElements() == true) {
  
                          // now get the properties of a line. These are separated by a "|"
                          final String line = (String)st.nextElement();
                          final StringTokenizer prop = new StringTokenizer(line, "|");
                          String category = null;
                          String componentName = null;
                          String className = null;
                          String configuration = null;
                          String label = null;
                          String mimeType = null;
                          while (prop.hasMoreElements() == true) {
                              final String property = (String)prop.nextElement();
                              final int pos = property.indexOf(":");
                              final String propName = property.substring(0, pos);
                              final String propVal  = property.substring(pos+1);
                              if     (propName.equals("category")) category = propVal;
                              else if (propName.equals("componentName")) componentName = propVal;
                              else if (propName.equals("componentClass")) className = propVal;
                              else if (propName.equals("configuration")) configuration = propVal;
                              else if (propName.equals("label")) label = propVal;
                              else if (propName.equals("mimeType")) mimeType = propVal;
                              else throw new BuildException("Unknown property " + propName
+ " in file " + files[i].getAbsolutePath());
                          }
  
                          // Test for required values
                          if (category == null) {
                              throw new BuildException("category property is required in file
" + files[i].getAbsolutePath(), location);
                          }
                          if (componentName == null) {
                              throw new BuildException("componentName property is required
in file " + files[i].getAbsolutePath(), location);
                          }
                          if (className == null) {
                              throw new BuildException("componentClass property is required
in file " + files[i].getAbsolutePath(), location);
                          }
  
                          this.add(sitemapLocation,
                                   category,
                                   componentName,
                                   className,
                                   configuration,
                                   label,
                                   mimeType);
                      }
                  }
              }
          }
      }
  
      /**
       * Add entry to sitemap
       */
      private void add(final String sitemapLocation,
                       final String category,
                       final String componentName,
                       final String className,
                       final String configuration,
                       final String label,
                       final String mimeType)
      throws IOException {
          final String data = load( sitemapLocation );
  
          final String searchString = new StringBuffer( "</map:" )
                                          .append( category )
                                          .append( ">" ).toString();
          final int pos = data.indexOf( searchString );
          int categoryStartPos = data.indexOf(new StringBuffer( "<map:" )
                                              .append( category )
                                              .append( ">" ).toString() );
  
          if ( categoryStartPos == -1 )
              categoryStartPos = data.indexOf(new StringBuffer( "<map:" )
                                              .append( category )
                                              .append( " " ).toString() );
  
          if ( categoryStartPos != -1 && categoryStartPos < pos && pos
!= -1 ) {
              // the category exists, now search if a component
              // with the name already exists
              int componentPos = data.substring( categoryStartPos, pos ).indexOf(
                      new StringBuffer( "name=\"" )
                                .append( componentName )
                                .append( "\"" ).toString() );
              if ( componentPos == -1 ) {
                  StringBuffer buffer = new StringBuffer( data.substring( 0, pos ) )
                              .append( "<map:" )
                              .append( category.substring( 0, category.length() - 1 ) )
                              .append( " name=\"" )
                              .append( componentName )
                              .append( "\" src=\"" ).append( className ).append( "\"" )
                              .append( " logger=\"sitemap.")
                              .append( category.substring( 0, category.length() - 1 ) )
                              .append( '.' )
                              .append( componentName )
                              .append( '\"');
  
                  if ( null != mimeType && mimeType.length() > 0) {
                       buffer.append( " mime-type=\"" ).append( mimeType ).append( "\"" );
                  }
                  if ( null != label && label.length() > 0) {
                       buffer.append( " label=\"" ).append( label ).append( "\"" );
                  }
                  if ( null != configuration ) {
                      buffer.append( ">\n" ).append( configuration ).append( "\n" )
                          .append( "</map:" ).append( category.substring( 0, category.length()
- 1 ) ).append( ">\n" );
                  } else {
                      buffer.append( "/>\n" );
                  }
                  buffer.append( data.substring( pos ) ).toString();
                  this.save( sitemapLocation, buffer.toString() );
              }
          }
  
      }
  
      /**
       * Load a file and return the content as a string.
       */
      public String load( String filename )
      throws IOException {
          FileInputStream fis;
  
          fis = new FileInputStream( filename );
          int available;
          byte[] data = null;
          byte[] tempData;
          byte[] copyData;
          do
          {
              available = 1024;
              tempData = new byte[available];
              available = fis.read( tempData, 0, available );
              if ( available > 0 )
              {
                  copyData = new byte[( data == null ? 0 : data.length ) + available];
                  if ( data != null )
                  {
                      System.arraycopy( data, 0, copyData, 0, data.length );
                  }
                  System.arraycopy( tempData, 0, copyData, ( data == null ? 0 : data.length
), available );
                  data = copyData;
              }
          } while ( available > 0 );
          fis.close();
  
          return ( data != null ? new String( data ) : "" );
      }
  
      /**
       * Save the string to a file
       */
      public void save( String filename, String data )
      throws IOException
      {
          FileWriter fw = new FileWriter( filename );
          fw.write( data );
          fw.close();
      }
  
  }
  
  
  
  1.1                  xml-cocoon2/tools/src/UserInputTask.java
  
  Index: UserInputTask.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) 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 which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  import java.io.InputStreamReader;
  import java.io.BufferedReader;
  import java.io.IOException;
  import org.apache.tools.ant.taskdefs.Property;
  
  /**
   * Task to ask property values to the user. Uses current value as default.
   *
   * @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
   * @created 14 January 2002
   * @version CVS $Revision: 1.1 $ $Date: 2002/01/25 02:31:53 $
   */
  public class UserInputTask extends org.apache.tools.ant.Task {
  
    private String question;
    private String name;
    private String value;
  
    /**
     * Constructor.
     */
    public UserInputTask() {
      super();
    }
  
    /**
     * Initializes the task.
     */
    public void init() {
      super.init();
      question = "?";
    }
  
    /**
     * Run the task.
     * @exception org.apache.tools.ant.BuildException The exception raised during task execution.
     */
    public void execute() throws org.apache.tools.ant.BuildException {
      value = project.getProperty(name);
      String defaultvalue = value;
  
      //if the property exists
      if (value != null) {
  
        System.out.println("\n"+question + " ["+value + "] ");
  
        BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
  
        try
        {
          value = reader.readLine();
        }
        catch (IOException e)
        {
          value = defaultvalue;
        }
  
        if (!value.equals("")) {
          project.setProperty(name, value);
        } else {
          project.setProperty(name, defaultvalue);
        }
      }
    }
  
    /**
     * Sets the prompt text that will be presented to the user.
     * @param prompt String
     */
    public void addText(String question) {
      this.question=question;
    }
  
    public void setQuestion(String question) {
      this.question = question;
    }
  
    public void setName(String name) {
      this.name = name;
    }
  
  }
  
  
  
  
  1.1                  xml-cocoon2/tools/src/XConfToolTask.java
  
  Index: XConfToolTask.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) 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 which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  import java.io.*;
  import java.util.*;
  import org.apache.tools.ant.*;
  import org.apache.tools.ant.taskdefs.*;
  import org.apache.tools.ant.types.*;
  
  /**
   * Add components to the cocoon.xconf
   * This is only a ugly first shot
   *
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/01/25 02:31:53 $
   */
  
  public final class XConfToolTask extends Task {
  
      private String configuration;
      private String directory;
      private String extension;
  
      public void setConfiguration(String configuration) {
          this.configuration = configuration;
      }
  
      public void setDirectory(String directory) {
          this.directory = directory;
      }
  
      public void setExtension(String extension) {
          this.extension = extension;
      }
  
      public void execute() throws BuildException {
          if (this.configuration == null) {
              throw new BuildException("configuration attribute is required", location);
          }
  
          if (this.extension == null) {
              throw new BuildException("extension attribute is required", location);
          }
          if (this.directory == null) {
              throw new BuildException("directory attribute is required", location);
          }
  
          try {
              // process recursive
              this.process(new File(this.directory), this.extension, this.configuration);
          } catch (IOException ioe) {
              throw new BuildException("IOException: " + ioe);
          }
      }
  
      /**
       * Scan recursive
       */
      private void process(final File   directoryFile,
                           final String ext,
                           final String configurationLocation)
      throws IOException, BuildException {
  
          final File[] files = directoryFile.listFiles();
  
          for(int i = 0; i < files.length; i++) {
  
              if (files[i].isDirectory() == true) {
                  this.process(files[i], ext, configurationLocation);
              } else {
  
                  if (files[i].getName().endsWith("."+ext) == true) {
                      System.out.println("Reading: " + files[i].getAbsolutePath());
                      final String newComponent = this.load(files[i].getAbsolutePath());
  
                      this.add(configurationLocation,
                               newComponent);
                  }
              }
          }
      }
  
      /**
       * Add entry to sitemap
       */
      private void add(final String configurationLocation,
                       final String newComponent)
      throws IOException {
          final String data = load( configurationLocation );
  
          // first search if component already present:
          if ( data.indexOf( newComponent ) == -1 ) {
              int pos = data.indexOf( "<cocoon" );
              if (pos != -1) {
                  pos = data.indexOf( ">", pos);
  
                  if (pos != -1) {
                      StringBuffer buffer = new StringBuffer( data.substring( 0, pos+1 ) )
                                  .append( "\n\n" )
                                  .append( newComponent )
                                  .append( data.substring( pos+1 ) );
                      this.save( configurationLocation, buffer.toString() );
                  }
              }
          }
  
      }
  
      /**
       * Load a file and return the content as a string.
       */
      public String load( String filename )
      throws IOException {
          FileInputStream fis;
  
          fis = new FileInputStream( filename );
          int available;
          byte[] data = null;
          byte[] tempData;
          byte[] copyData;
          do
          {
              available = 1024;
              tempData = new byte[available];
              available = fis.read( tempData, 0, available );
              if ( available > 0 )
              {
                  copyData = new byte[( data == null ? 0 : data.length ) + available];
                  if ( data != null )
                  {
                      System.arraycopy( data, 0, copyData, 0, data.length );
                  }
                  System.arraycopy( tempData, 0, copyData, ( data == null ? 0 : data.length
), available );
                  data = copyData;
              }
          } while ( available > 0 );
          fis.close();
  
          return ( data != null ? new String( data ) : "" );
      }
  
      /**
       * Save the string to a file
       */
      public void save( String filename, String data )
      throws IOException
      {
          FileWriter fw = new FileWriter( filename );
          fw.write( data );
          fw.close();
      }
  
  }
  
  
  

----------------------------------------------------------------------
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


Mime
View raw message