jvanzyl     2002/07/14 19:45:00

  Added:       src/plugins-build/transformer/src/java/org/apache/maven/transformer
                        Instructions.java SourceTransformer.java
                        Transformation.java
  Log:
  o move to plugin
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-maven/src/plugins-build/transformer/src/java/org/apache/maven/transformer/Instructions.java
  
  Index: Instructions.java
  ===================================================================
  package org.apache.maven.transformer;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" 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 Maven", 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.util.ArrayList;
  import java.util.List;
  
  /**
  */
  public class Instructions
  {
      private ArrayList transformations = new ArrayList();
      
      public void addTransformation(Transformation t)
      {
          transformations.add(t);
      }
      
      public List getTransformations()
      {
          return transformations;
      }        
  }
  
  
  
  1.1                  
jakarta-turbine-maven/src/plugins-build/transformer/src/java/org/apache/maven/transformer/SourceTransformer.java
  
  Index: SourceTransformer.java
  ===================================================================
  package org.apache.maven.transformer;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" 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 Maven", 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.Iterator;
  
  import org.apache.maven.build.BeanReader;
  
  import org.apache.maven.MavenUtils;
  
  import org.apache.oro.text.perl.Perl5Util;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.DirectoryScanner;
  import org.apache.tools.ant.Task;
  
  import org.apache.velocity.util.StringUtils;
  
  /**
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: SourceTransformer.java,v 1.1 2002/07/15 02:45:00 jvanzyl Exp $
   * @task Get fileContentsToString() in the commons and break vel dep.
   */
  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 Instructions instructions;
  
      /**
       * Directory of source files that need to be
       * transformed.
       */
      private File srcDir;
      
      /**
       * Directory to place the resultant transformed
       * sources.
       */
      private File targetDir;
      
      /**
       * Transformation descriptor. A series of targets
       * and results.
       */
      private File descriptor;
  
      /**
       * Set the source directory.
       *
       * @param srcDir source directory
       */
      public void setSrcDir(File srcDir)
      {
          this.srcDir = srcDir;
      }
      
      /**
       * Get source directory.
       *
       * @return File source directory
       */
      public File getSrcDir()
      {
          return srcDir;
      }        
  
      /**
       * Set the target directory.
       *
       * @param targetDir target directory
       */
      public void setTargetDir(File targetDir)
      {
          this.targetDir = targetDir;
      }
      
      /**
       * Get source directory.
       *
       * @return File target directory
       */
      public File getTargetDir()
      {
          return targetDir;
      }        
  
      /**
       * Set the transformation descriptor.
       *
       * @param descriptor transformation descriptor
       */
      public void setDescriptor(File descriptor)
      {
          this.descriptor = descriptor;
      }        
  
      /**
       * Iterator through a set of transformations, applying
       * the transformation to each source file in the
       * source directory.
       *
       * @throws BuildException
       */
      public void execute()
          throws BuildException
      {
          if (!srcDir.exists())
          {
              throw new BuildException(
                  "The specified srcDir does not exist: " + srcDir);
          }
          
          try
          {
              BeanReader reader = new BeanReader(Instructions.class);
              instructions =  (Instructions) reader.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 sourceFile source file to migrate.
       * @return migrated source file.
       */
      private String convertSourceFile(String sourceFile)
      {
          originalSourceFile = StringUtils.fileContentsToString(sourceFile);
          perl = new Perl5Util();
          Iterator i = instructions.getTransformations().iterator();
          
          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-maven/src/plugins-build/transformer/src/java/org/apache/maven/transformer/Transformation.java
  
  Index: Transformation.java
  ===================================================================
  package org.apache.maven.transformer;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" 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 Maven", 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/>.
   */
  
  /**
   * 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 target targetRegex
       */
      public void setTarget(String target)
      {
          this.target = target;
      }
      
      /**
       * Get the target .
       *
       * @return String target
       */
      public String getTarget()
      {
          return target;
      }        
  
      /**
       * Set the result .
       *
       * @param result
       */
      public void setResult(String result)
      {
          this.result = result;
      }
      
      /**
       * Get the result .
       *
       * @return String result .
       */
      public String getResult()
      {
          return result;
      }        
  }
  
  
  

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

Reply via email to