/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.jca.codegenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Code generator main class
 * 
 * @author Jeff Zhang
 * @version $Revision: $
 */
public class Main
{

   /**
    * Code generator standalone tool
    * 
    * @param args command line arguments
    */
   public static void main(String[] args)
   {
      Locale locale = Locale.getDefault();
      
      try 
      {
         ResourceBundle dbconf = ResourceBundle.getBundle("codegenerator", locale);

         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         System.out.print(dbconf.getString("package.name"));
         String packageName = in.readLine();
         System.out.print(dbconf.getString("class.name"));
         String className = in.readLine();
         

         Template template = new SimpleTemplate();
         Definition def = new Definition();
         def.setRaPackage(packageName);
         def.setRaClass(className);
         
         List<ConfigPropType> props = new ArrayList<ConfigPropType>();
         while (true)
         {
            System.out.println(dbconf.getString("config.properties"));
            System.out.print("    " + dbconf.getString("config.properties.name"));
            String name = in.readLine();
            if (name == null || name.equals(""))
               break;
            System.out.print("    " + dbconf.getString("config.properties.type"));
            String type = in.readLine();
            System.out.print("    " + dbconf.getString("config.properties.value"));
            String value = in.readLine();
            System.out.println();
            
            ConfigPropType config = new ConfigPropType(name, type, value);
            props.add(config);
         }
         def.setRaConfigProps(props);
         
         System.out.print(dbconf.getString("output.dir"));
         String output = in.readLine();
         if (output == null || output.equals(""))
            output = ".";
         
         File outDir = new File(output);
         outDir.mkdirs();
         
         File report = new File(outDir, className + ".java");
         FileWriter fw = new FileWriter(report);
         template.process(def, fw);
         fw.close();
         System.out.println(dbconf.getString("java.wrote"));
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

}

@@ -1394,16 +1394,58 @@
  Main%0A%7B%0A
+   private static final int OTHER = 2;%0A   
 %0A   /**%0A
@@ -1593,51 +1593,647 @@
     
-Locale locale = Locale.getDefault();%0A      
+String outputDir = %22.%22;%0A      int arg = 0;%0A %0A      if (args.length %3E 0)%0A      %7B%0A         while (args.length %3E arg + 1)%0A         %7B%0A            if (args%5Barg%5D.startsWith(%22-%22))%0A            %7B%0A               if (args%5Barg%5D.equals(%22-o%22))%0A               %7B%0A                  arg++;%0A                  if (arg %3E= args.length)%0A                  %7B%0A                     usage();%0A                     System.exit(OTHER);%0A                  %7D%0A                  outputDir = args%5Barg%5D;%0A               %7D%0A            %7D %0A            else%0A            %7B%0A               usage();%0A               System.exit(OTHER);%0A            %7D%0A            arg++;%0A         %7D%0A      %7D
 %0A   
@@ -2327,14 +2327,27 @@
 r%22, 
-l
+L
 ocale
+.getDefault()
 );%0A%0A
@@ -3655,192 +3655,8 @@
 s);%0A
-         %0A         System.out.print(dbconf.getString(%22output.dir%22));%0A         String output = in.readLine();%0A         if (output == null %7C%7C output.equals(%22%22))%0A            output = %22.%22;
 %0A   
@@ -3700,16 +3700,19 @@
 e(output
+Dir
 );%0A     
@@ -4057,10 +4057,148 @@
 %7D%0A   %7D%0A%0A
+   /**%0A    * Tool usage%0A    */%0A   private static void usage()%0A   %7B%0A      System.out.println(%22Usage: codegenerator %5B-o directory%5D%22);%0A   %7D%0A%0A
 %7D%0A

support -o to redirect output directory