Commits

Tim Vernum  committed 62d4624

Add args4j support to conversion tool

  • Participants
  • Parent commits 441f396

Comments (0)

Files changed (3)

File bin/convert.sh

 cd ..
 
 CP="parser/output/ant/classes/parser:convert/output/ant/classes/main"
+for JAR in lib/core/*.jar
+do
+    CP="${CP}:${JAR}"
+done
+
 MAIN="org.adjective.syntactic.convert.j8to7.Main"
 
+OUTPUT="output/convert"
+mkdir -p ${OUTPUT}
+
 for FILE in "$@"
 do
-    java -cp "${CP}" "${MAIN}" "${FILE}"
+    java -cp "${CP}" "${MAIN}" --source "${FILE}" --output "${OUTPUT}"
+    if [ -f ${FILE} ]
+    then
+        show "${OUTPUT}/$( basename ${FILE} )"
+    else
+        show ${OUTPUT}
+    fi
 done
 

File convert/build.xml

 
   <path id="path.compile.main" >
      <pathelement location="../parser/output/ant/classes/parser" />
+     <fileset dir="../lib/core/" >
+        <include name="args4j-*.jar" />
+     </fileset>
   </path>
 
   <path id="path.compile.test" >

File convert/source/java/main/org/adjective/syntactic/convert/j8to7/Main.java

 
 package org.adjective.syntactic.convert.j8to7;
 
+import org.adjective.syntactic.parser.jj.JavaParser;
 import org.adjective.syntactic.parser.node.BaseNode;
 import org.adjective.syntactic.parser.util.PrintSourceVisitor;
-import org.adjective.syntactic.parser.jj.JavaParser;
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.Option;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
 
 public class Main
 {
-    public static void main(String[] args)
+    @Option(name = "--source", aliases = {"-s"}, usage = "Source directory or file", required = true)
+    private File _source;
+
+    @Option(name = "--output", aliases = {"-d", "-o"}, usage = "Output directory or file", required = true)
+    private File _output;
+
+    @Option(name = "--classpath", aliases = {"-cp"}, usage = "Classpath for references classes", required = false)
+    private String _classpath;
+
+    @SuppressWarnings("FieldCanBeLocal")
+    @Option(name = "--flatten", aliases = {"-f"}, usage = "Flatten directory structure", required = false)
+    private boolean _flatten = false;
+
+    public static void main(String[] args) throws CmdLineException, FileNotFoundException
     {
-        for (String arg : args)
+        final Main main = new Main();
+        final CmdLineParser parser = new CmdLineParser(main);
+        parser.setUsageWidth(120);
+        parser.parseArgument(args);
+        main.execute();
+    }
+
+    public void execute() throws FileNotFoundException
+    {
+        if (!_source.exists())
         {
-            process(arg);
+            throw new FileNotFoundException("No such file or directory: " + _source.getAbsolutePath());
+        }
+        if (_source.isFile())
+        {
+            processFile(_source, _output);
+        }
+        else if (_source.isDirectory())
+        {
+            processDirectory(_source, _output);
         }
     }
 
-    private static void process(final String filename)
+    private void processDirectory(final File source, final File output) throws FileNotFoundException
     {
-        BaseNode ast = parse(filename);
-        if (ast != null)
+        final File[] files = source.listFiles();
+        if (files == null)
         {
-            ast.jjtAccept(new Convert8To7Visitor(), null);
-            ast.jjtAccept(new PrintSourceVisitor(), System.out);
+            return;
+        }
+        for (File file : files)
+        {
+            File out = new File(output, file.getName());
+            if (file.isDirectory())
+            {
+                if (_flatten)
+                {
+                    out = output;
+                }
+                if (!out.isDirectory() && !out.mkdir())
+                {
+                    throw new FileNotFoundException("Cannot create directory " + out);
+                }
+                processDirectory(file, out);
+            }
+            else if (file.getName().endsWith(".java"))
+            {
+                processFile(file, out);
+            }
         }
     }
 
-    private static BaseNode parse(final String arg)
+    private void processFile(final File source, File output) throws FileNotFoundException
+    {
+        if (output.isDirectory())
+        {
+            output = new File(output, source.getName());
+        }
+        BaseNode ast = parse(source);
+        if (ast == null)
+        {
+            return;
+        }
+        PrintStream out = new PrintStream(new FileOutputStream(output));
+        try
+        {
+            ast.jjtAccept(new Convert8To7Visitor(), null);
+            ast.jjtAccept(new PrintSourceVisitor(), out);
+        }
+        finally
+        {
+            out.close();
+        }
+    }
+
+    private static BaseNode parse(final File source)
     {
         try
         {
-            return (BaseNode) JavaParser.process(arg);
+            return JavaParser.process(source);
         }
         catch (RuntimeException e)
         {
-            System.err.println("Cannot parse: " + arg);
+            System.err.println("Cannot parse: " + source.getPath());
             throw e;
         }
     }