/*
 * Created on 27. jan.. 2007
 *
 * Copyright (c) 2005, Karl Trygve Kalleberg <karltk@ii.uib.no>
 * 
 * Licensed under the GNU General Public License, v2
 */
package org.spoofax.ecjadapter;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.spoofax.DebugUtil;
import org.spoofax.interpreter.adapter.ecj.ECJFactory;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.InlinePrinter;

public class ParseJava {

    public static void main(String[] args) throws IOException, InterpreterException {
        
    	String inputFile = null;
    	String outputFile = null;
    	boolean statmentsRecovery=false;
    	
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("--debug")) {
                DebugUtil.setDebug(true);
            } else if (args[i].equals("-i")) {
                inputFile = args[i + 1];
            } else if (args[i].equals("-o")) {
                outputFile = args[i + 1];
            }else if (args[i].equals("-sr")) {
                statmentsRecovery=true;
            }
        }

        if(inputFile == null) {
            System.err.println("Usage: ecj-parse-java [--debug] -i file.java");
            System.exit(2);
        }

        IStrategoTerm r = parseJava(inputFile, statmentsRecovery);
        if(r == null) {
        	System.err.println("Failed to open and read file " + inputFile);
        	System.exit(3);
        }

        InlinePrinter ip = new InlinePrinter();
        r.prettyPrint(ip);
        String output= ip.getString();
        System.out.println(output);
        
        if(outputFile!=null){
        	FileOutputStream ous = new FileOutputStream(outputFile);
        	ous.write(output.getBytes());
        }
    }
 
    private static IStrategoTerm parseJava(String fileName, boolean useStatementsRecovery) throws FileNotFoundException, IOException {
    
    	ASTParser parser = ASTParser.newParser(AST.JLS3);
    	parser.setStatementsRecovery(useStatementsRecovery);   	
    	
    	parser.setSource(getBytes(fileName));
    
    	ASTNode ast = parser.createAST(null);
    	ECJFactory f = new ECJFactory();
    	return f.parseFromTree(ast);
    }

    private static char[] getBytes(String fileName) throws FileNotFoundException, IOException {

        BufferedReader r = new BufferedReader(new FileReader(fileName));
        StringBuilder sb = new StringBuilder();
        String s = r.readLine();
        while(s != null) {
            sb.append(s);
            s = r.readLine();
        }
            
        return sb.toString().toCharArray();
    }

}

@@ -347,106 +347,91 @@
 on;%0A
+%0A
 import 
-java.util.HashMap;%0Aimport java.util.Hashtable;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A
+org.eclipse.jdt.core.ICompilationUnit;%0Aimport org.eclipse.jdt.core.dom.AST;
 %0Aimp
@@ -459,15 +459,18 @@
 ore.
-JavaCor
+dom.ASTNod
 e;%0Ai
@@ -495,32 +495,38 @@
 jdt.core.dom.AST
+Parser
 ;%0Aimport org.ecl
@@ -534,32 +534,64 @@
 pse.jdt.
-core.dom.ASTNode
+internal.compiler.ast.CompilationUnitDeclaration
 ;%0Aimport
@@ -607,34 +607,45 @@
 pse.jdt.
+internal.
 core.
-dom.ASTParser
+CompilationUnit
 ;%0Aimport
@@ -1081,24 +1081,25 @@
 boolean stat
+e
 mentsRecover
@@ -1104,12 +1104,11 @@
 ery=
-fals
+tru
 e;%0A 
@@ -1463,16 +1463,17 @@
 quals(%22-
+n
 sr%22)) %7B%0A
@@ -1488,24 +1488,25 @@
         stat
+e
 mentsRecover
@@ -1503,27 +1503,28 @@
 ntsRecovery=
-tru
+fals
 e;%0A         
@@ -1741,16 +1741,17 @@
 le, stat
+e
 mentsRec
@@ -2456,26 +2456,108 @@
 covery);
+%0A 
    %09
-%0A    %09
+parser.setKind(ASTParser.K_COMPILATION_UNIT);%0A        parser.setResolveBindings(true);
 %0A    %09pa
@@ -2588,24 +2588,63 @@
 fileName));%0A
+    %09parser.setBindingsRecovery(true);%0A
     %0A    %09AS
@@ -2679,16 +2679,22 @@
 (null);%0A
+    %09%0A
     %09ECJ

Enable statements recovery by default.