jvanzyl     01/09/17 07:24:47

  Added:       src/tdk/tool/org/apache/tdk/migrator Migrator.java
                        Transformation.java TransformationDigester.java
                        transformations.xml
  Log:
  - playing around with a simple first version of the migrator.
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-tdk/src/tdk/tool/org/apache/tdk/migrator/Migrator.java
  
  Index: Migrator.java
  ===================================================================
  package org.apache.tdk.migrator;
  
  /* ====================================================================
   * 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.DirectoryScanner;
  
  /**
   * This utility class attempts to help Turbine developers migrate
   * their wepapp sources across versions of Turbine by applying
   * a series of regexes and rules to a source tree and creates
   * a new source tree that is compatible with subsequent
   * releases of Turbine.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   *
   * @version $Id: Migrator.java,v 1.1 2001/09/17 14:24:47 jvanzyl Exp $
   */
  public class Migrator
  {
      /**
       * 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 ArrayList transformations;
  
      /**
       * Default ctor.
       */
      public Migrator()
      {
          transformations = new ArrayList();
      }        
  
      /**
       * Iterate through the set of find/replace regexes
       * that will convert a given source tree from one
       */
      public void migrate(String args[])
      {
          if (args.length < 1)
          {
              usage();
          }
  
          File file = new File(args[0]);
  
          if (!file.exists())
          {
              System.err.println(
                  "The specified template or directory does not exist");
  
              System.exit(1);
          }
  
          if (file.isDirectory())
          {
              String basedir = args[0];
              String newBasedir = basedir + ".new";
  
              DirectoryScanner ds = new DirectoryScanner();
              ds.setBasedir(basedir);
              ds.addDefaultExcludes();
              ds.scan();
              String[] files = ds.getIncludedFiles();
  
              for (int i = 0; i < files.length; i++)
              {
                  writeSource(files[i], basedir, newBasedir);
              }
          }
          else
          {
              writeSource(args[0], "", "");
          }
      }
  
      /**
       * Write out the converted template to the given named file
       * and base directory.
       */
      private boolean writeSource(String file, String basedir, String newBasedir)
      {
          System.out.println("Converting " + file + "...");
  
          String sourceFile;
          String sourceDir;
          String newSourceFile;
          File outputDirectory;
  
          if (basedir.length() == 0)
          {
              sourceFile = file;
              sourceDir = "";
              newSourceFile = file;
          }
          else
          {
              sourceFile = basedir + pathSeparator + file;
              sourceDir = newBasedir + pathSeparator +
                  file.substring(0, file.lastIndexOf(pathSeparator));
  
              outputDirectory = new File(sourceDir);
  
              if (! outputDirectory.exists())
              {
                  outputDirectory.mkdirs();
              }
  
              newSourceFile = newBasedir + pathSeparator + file;
          }
  
          String convertedSourceFile = convertSourceFile(sourceFile);
  
          try
          {
              FileWriter fw = new FileWriter(newSourceFile);
              fw.write(convertedSourceFile);
              fw.close();
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
  
          return true;
      }
  
      /**
       * How to use this little puppy :-)
       */
      public void usage()
      {
          System.err.println("Usage: directory");
          System.exit(1);
      }
  
      /**
       * 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);
  
          /*
           * overcome current velocity 0.71 limitation.
           * Hmm. Not sure if this applies to source files.
           * I will try this.
           */
          if ( !originalSourceFile.endsWith("\n") )
          {
            originalSourceFile += "\n";
          }
  
          perl = new Perl5Util();
          Iterator i = transformations.iterator();
          
          while (i.hasNext())
          {
              Transformation t = (Transformation) i.next();
              String targetRegex = t.getTargetRegex();
              String resultRegex = t.getResultRegex();
              
              while (perl.match("/" + targetRegex + "/", originalSourceFile))
              {
                  originalSourceFile = perl.substitute(
                      "s/" + res[i] + "/" + resultRegex + "/", originalSourceFile);
              }
          }
  
          return originalSourceFile;
      }
  
      /**
       * Main hook for the conversion process.
       */
      public static void main(String[] args)
      {
          Migrator migrator = new Migrator();
          migrator.migrate(args);
      }
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/tool/org/apache/tdk/migrator/Transformation.java
  
  Index: Transformation.java
  ===================================================================
  package org.apache.tdk.migrator;
  
  /**
   * 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 targetRegex;
      
      /**
       * Regex to used to result the target.
       */
      private String resultRegex;
  
      /**
       * Set the target regex.
       *
       * @param String targetRegex
       */
      public void setTargetRegex(String targetRegex)
      {
          this.targetRegex = targetRegex;
      }
      
      /**
       * Get the target regex.
       *
       * @return String targetRegex
       */
      public String getTargetRegex()
      {
          return targetRegex;
      }        
  
      /**
       * Set the result regex.
       *
       * @param String resultRegex
       */
      public void setResultRegex(String resultRegex)
      {
          this.resultRegex = resultRegex;
      }
      
      /**
       * Get the result regex.
       *
       * @return String result regex.
       */
      public String getResultRegex()
      {
          return resultRegex;
      }        
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/tool/org/apache/tdk/migrator/TransformationDigester.java
  
  Index: TransformationDigester.java
  ===================================================================
  package org.apache.tdk.migrator;
  
  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: TransformationDigester.java,v 1.1 2001/09/17 14:24:47 jvanzyl Exp $
   */
  public class TransformationDigester
      extends Digester
  {
      /**
       * Have we been configured yet?
       */
      protected boolean configured = false;
  
      protected String repositoryClass = "org.apache.tdk.migrator.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;
          }            
  
          addObjectCreate("", repositoryClass);
          addSetProperties("repository");
          addCallMethod("repository/root/method", "setMethod", 0);
          addCallMethod("repository/root/user", "setUser", 0);
          addCallMethod("repository/root/password", "setPassword", 0);
          addCallMethod("repository/root/hostname", "setHostname", 0);
          addCallMethod("repository/root/path", "setPath", 0);
          
          // Mark this digester as having been configured
          configured = true;
      }
  }
  
  
  
  1.1                  
jakarta-turbine-tdk/src/tdk/tool/org/apache/tdk/migrator/transformations.xml
  
  Index: transformations.xml
  ===================================================================
  <transformations>
    <!-- RunData is now a top level API interface -->
    <transformation>
      <target>import org.apache.turbine.util.RunData</target>
      <result>import org.apache.turbine.RunData</result>
    </transformation>
    
    <!-- Criteria is now in Torque -->
    <transformation>
      <target>import org.apache.turbine.util.db.Criteria</target>
      <result>import org.apache.torque.util.Criteria</result>
    </transformation>
  </transformations>
  
  
  

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

Reply via email to