jvanzyl     01/10/20 18:15:08

  Added:       src/tdk/task/org/apache/tdk/transformer
                        SourceTransformer.java Transformation.java
                        Transformations.java TransformationsDigester.java
  Log:
  - working version of the source migration task.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/transformer/SourceTransformer.java
  
  Index: SourceTransformer.java
  ===================================================================
  package org.apache.tdk.task.transformer;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 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 "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.File;
  import java.io.FileWriter;
  import java.util.ArrayList;
  import java.util.Iterator;
  import org.apache.oro.text.perl.Perl5Util;
  import org.apache.velocity.util.StringUtils;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.DirectoryScanner;
  import org.apache.tools.ant.Task;
  
  /**
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   *
   * @version $Id: SourceTransformer.java,v 1.1 2001/10/21 01:15:07 jvanzyl Exp $
   */
  public class SourceTransformer
      extends Task
  {
      /**
       * Source file currently being migrated.
       */
      protected String originalSourceFile;
  
      /**
       * Regular expression tool
       */
      protected Perl5Util perl;
  
      /**
       * Path separator property
       */
      protected String pathSeparator = File.separator;
  
      /**
       * Transformations that the source files
       * must undergoe.
       */
      protected Transformations transformations;
  
      /**
       * File to operate on, may be a directory.
       */
      private File srcDir;
      private File targetDir;
      private File descriptor;
      
      public void setSrcDir(File srcDir)
      {
          this.srcDir = srcDir;
      }
      
      public File getSrcDir()
      {
          return srcDir;
      }        
  
      public void setTargetDir(File targetDir)
      {
          this.targetDir = targetDir;
      }
      
      public File getTargetDir()
      {
          return targetDir;
      }        
  
      public void setDescriptor(File descriptor)
      {
          this.descriptor = descriptor;
      }        
  
      /**
       * Iterate through the set of find/replace regexes
       * that will convert a given source tree from one
       */
      public void execute()
          throws BuildException
      {
          if (!srcDir.exists())
          {
              throw new BuildException(
                  "The specified srcDir does not exist: " + srcDir);
          }
          
          // Retrieve the transformations to perform on source files.
          TransformationsDigester td = new TransformationsDigester();
          
          try
          {
              transformations = (Transformations) td.parse(descriptor);
          }
          catch (Exception e)
          {
              throw new BuildException(e);
          }
          
          DirectoryScanner ds = new DirectoryScanner();
          ds.setBasedir(srcDir);
          ds.addDefaultExcludes();
          ds.scan();
          String[] files = ds.getIncludedFiles();
  
          for (int i = 0; i < files.length; i++)
          {
              writeSource(files[i], srcDir, targetDir);
          }
      }
  
      /**
       * Write out the converted template to the given named file
       * and base directory.
       */
      private void writeSource(String file, File srcDir, File targetDir)
      {
          log("Converting " + file + "...");
  
          String sourceFile;
          String sourceDir;
          String newSourceFile;
          File outputDirectory;
  
          sourceFile = srcDir + pathSeparator + file;
          sourceDir = targetDir + pathSeparator +
              file.substring(0, file.lastIndexOf(pathSeparator));
  
          outputDirectory = new File(sourceDir);
  
          if (outputDirectory.exists() == false)
          {
              outputDirectory.mkdirs();
          }
  
          newSourceFile = targetDir + pathSeparator + file;
          String convertedSourceFile = convertSourceFile(sourceFile);
  
          try
          {
              FileWriter fw = new FileWriter(newSourceFile);
              fw.write(convertedSourceFile);
              fw.close();
          }
          catch (Exception e)
          {
              throw new BuildException(
                  "Problem transforming source file: " + sourceFile);
          }
      }
  
      /**
       * Apply find/replace regexes to our Turbine source file.
       *
       * @param String source file to migrate.
       * @return String migrated source file.
       */
      public String convertSourceFile(String sourceFile)
      {
          originalSourceFile = StringUtils.fileContentsToString(sourceFile);
          perl = new Perl5Util();
          Iterator i = transformations.getTransformations().iterator();
          
          System.out.println(transformations.getTransformations().size());
          
          while (i.hasNext())
          {
              Transformation t = (Transformation) i.next();
              String target = t.getTarget();
              String result = t.getResult();
              
              log("Looking for " + target + " and replacing with " + result);
              
              while (perl.match("/" + target + "/", originalSourceFile))
              {
                  originalSourceFile = perl.substitute(
                      "s/" + target + "/" + result + "/", originalSourceFile);
              }
          }
  
          return originalSourceFile;
      }
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/transformer/Transformation.java
  
  Index: Transformation.java
  ===================================================================
  package org.apache.tdk.task.transformer;
  
  /**
   * RegexTransform represents a transformation to
   * be performed based on a target regex to find in
   * a source file and a regex which represents the
   * transformation to make.
   */
  public class Transformation
  {
      /**
       * Target regex to look for.
       */
      private String target;
      
      /**
       * Regex to used to result the target.
       */
      private String result;
  
      /**
       * Set the target regex.
       *
       * @param String targetRegex
       */
      public void setTarget(String target)
      {
          this.target = target;
      }
      
      /**
       * Get the target .
       *
       * @return String target
       */
      public String getTarget()
      {
          return target;
      }        
  
      /**
       * Set the result .
       *
       * @param String result
       */
      public void setResult(String result)
      {
          this.result = result;
      }
      
      /**
       * Get the result .
       *
       * @return String result .
       */
      public String getResult()
      {
          return result;
      }        
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/transformer/Transformations.java
  
  Index: Transformations.java
  ===================================================================
  package org.apache.tdk.task.transformer;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Iterator;
  
  /**
  */
  public class Transformations
  {
      private ArrayList transformations = new ArrayList();
      
      public void addTransformation(Transformation t)
      {
          transformations.add(t);
      }
      
      public List getTransformations()
      {
          return transformations;
      }        
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/transformer/TransformationsDigester.java
  
  Index: TransformationsDigester.java
  ===================================================================
  package org.apache.tdk.task.transformer;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  import org.apache.commons.digester.Digester;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: TransformationsDigester.java,v 1.1 2001/10/21 01:15:07 jvanzyl Exp $
   */
  public class TransformationsDigester
      extends Digester
  {
      /**
       * Have we been configured yet?
       */
      protected boolean configured = false;
  
      protected String transformationsClass =
          "org.apache.tdk.task.transformer.Transformations";
  
      protected String transformationClass = 
          "org.apache.tdk.task.transformer.Transformation";
  
      /**
       * Parse the content of the specified file using this Digester.  Returns
       * the root element from the object stack.
       *
       * @param file File containing the XML data to be parsed
       *
       * @exception IOException if an input/output error occurs
       * @exception SAXException if a parsing exception occurs
       */
      public Object parse(File file) 
          throws IOException, SAXException
      {
          configure();
          return (super.parse(file));
      }
  
      /**
       * Parse the content of the specified input source using this Digester.
       * Returns the root element from the object stack.
       *
       * @param input Input source containing the XML data to be parsed
       *
       * @exception IOException if an input/output error occurs
       * @exception SAXException if a parsing exception occurs
       */
      public Object parse(InputSource input) 
          throws IOException, SAXException
      {
          configure();
          return (super.parse(input));
      }
  
      /**
       * Parse the content of the specified input stream using this Digester.
       * Returns the root element from the object stack.
       *
       * @param input Input stream containing the XML data to be parsed
       *
       * @exception IOException if an input/output error occurs
       * @exception SAXException if a parsing exception occurs
       */
      public Object parse(InputStream input) 
          throws IOException, SAXException
      {
          configure();
          return (super.parse(input));
      }
  
      /**
       * Parse the content of the specified URI using this Digester.
       * Returns the root element from the object stack.
       *
       * @param uri URI containing the XML data to be parsed
       *
       * @exception IOException if an input/output error occurs
       * @exception SAXException if a parsing exception occurs
       */
      public Object parse(String uri) throws IOException, SAXException
      {
          configure();
          return (super.parse(uri));
      }
  
      /**
       * Configure the parsing rules that will be used to process RSS input.
       */
      protected void configure()
      {
          if (configured)
          {
              return;
          }            
          
          // Deal with the parent transformations class.
          addObjectCreate("transformations", transformationsClass);
          addSetProperties("transformations");
  
          // Deal with each child transformation.
          addObjectCreate("transformations/transformation", transformationClass);
          addSetNext("transformations/transformation", "addTransformation", 
transformationClass);
          addSetProperties("transformations/transformation");
  
          // Mark this digester as having been configured
          configured = true;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to