package net.sf.jasperreports.engine.design;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.util.JRClassLoader;
import net.sf.jasperreports.engine.util.JRProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
import org.eclipse.jdt.internal.compiler.IProblemFactory;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;

public class JRJdtCompiler extends JRAbstractJavaCompiler {
  private static final String JDT_PROPERTIES_PREFIX = "org.eclipse.jdt.core.";
  
  static final Log log = LogFactory.getLog(JRJdtCompiler.class);
  
  private final ClassLoader classLoader;
  
  Constructor constrNameEnvAnsBin;
  
  Constructor constrNameEnvAnsCompUnit;
  
  boolean is2ArgsConstr;
  
  Constructor constrNameEnvAnsBin2Args;
  
  Constructor constrNameEnvAnsCompUnit2Args;
  
  public JRJdtCompiler() {
    super(false);
    boolean bool;
    this.classLoader = getClassLoader();
    try {
      Class classAccessRestriction = loadClass("org.eclipse.jdt.internal.compiler.env.AccessRestriction");
      this.constrNameEnvAnsBin2Args = NameEnvironmentAnswer.class.getConstructor(new Class[] { IBinaryType.class, classAccessRestriction });
      this.constrNameEnvAnsCompUnit2Args = NameEnvironmentAnswer.class.getConstructor(new Class[] { ICompilationUnit.class, classAccessRestriction });
      this.is2ArgsConstr = true;
      bool = true;
    } catch (NoSuchMethodException e) {
      bool = false;
    } catch (ClassNotFoundException ex) {
      bool = false;
    } 
    if (!bool)
      try {
        this.constrNameEnvAnsBin = NameEnvironmentAnswer.class.getConstructor(new Class[] { IBinaryType.class });
        this.constrNameEnvAnsCompUnit = NameEnvironmentAnswer.class.getConstructor(new Class[] { ICompilationUnit.class });
        this.is2ArgsConstr = false;
      } catch (NoSuchMethodException ex) {
        throw new JRRuntimeException("Not able to load JDT classes", ex);
      }  
  }
  
  class CompilationUnit implements ICompilationUnit {
    protected String srcCode;
    
    protected String className;
    
    private final JRJdtCompiler this$0;
    
    public CompilationUnit(String srcCode, String className) {
      this.srcCode = srcCode;
      this.className = className;
    }
    
    public char[] getFileName() {
      return this.className.toCharArray();
    }
    
    public char[] getContents() {
      return this.srcCode.toCharArray();
    }
    
    public char[] getMainTypeName() {
      return this.className.toCharArray();
    }
    
    public char[][] getPackageName() {
      return new char[0][0];
    }
  }
  
  protected String compileUnits(JRCompilationUnit[] units, String classpath, File tempDirFile) {
    StringBuffer problemBuffer = new StringBuffer();
    INameEnvironment env = getNameEnvironment(units);
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    Map settings = getJdtSettings();
    DefaultProblemFactory defaultProblemFactory = new DefaultProblemFactory(Locale.getDefault());
    ICompilerRequestor requestor = getCompilerRequestor(units, problemBuffer);
    ICompilationUnit[] compilationUnits = new ICompilationUnit[units.length];
    for (int i = 0; i < compilationUnits.length; i++)
      compilationUnits[i] = new CompilationUnit(units[i].getSourceCode(), units[i].getName()); 
    Compiler compiler = new Compiler(env, policy, settings, requestor, (IProblemFactory)defaultProblemFactory);
    compiler.compile(compilationUnits);
    if (problemBuffer.length() > 0)
      return problemBuffer.toString(); 
    return null;
  }
  
  protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
    INameEnvironment env = new INameEnvironment() {
        private final JRCompilationUnit[] val$units;
        
        private final JRJdtCompiler this$0;
        
        public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
          StringBuffer result = new StringBuffer();
          String sep = "";
          for (int i = 0; i < compoundTypeName.length; i++) {
            result.append(sep);
            result.append(compoundTypeName[i]);
            sep = ".";
          } 
          return findType(result.toString());
        }
        
        public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
          StringBuffer result = new StringBuffer();
          String sep = "";
          for (int i = 0; i < packageName.length; i++) {
            result.append(sep);
            result.append(packageName[i]);
            sep = ".";
          } 
          result.append(sep);
          result.append(typeName);
          return findType(result.toString());
        }
        
        private int getClassIndex(String className) {
          int classIdx;
          for (classIdx = 0; classIdx < units.length; classIdx++) {
            if (className.equals(units[classIdx].getName()))
              break; 
          } 
          if (classIdx >= units.length)
            classIdx = -1; 
          return classIdx;
        }
        
        private NameEnvironmentAnswer findType(String className) {
          try {
            int classIdx = getClassIndex(className);
            if (classIdx >= 0) {
              ICompilationUnit compilationUnit = new JRJdtCompiler.CompilationUnit(units[classIdx].getSourceCode(), className);
              if (JRJdtCompiler.this.is2ArgsConstr)
                return JRJdtCompiler.this.constrNameEnvAnsCompUnit2Args.newInstance(new Object[] { compilationUnit, null }); 
              return JRJdtCompiler.this.constrNameEnvAnsCompUnit.newInstance(new Object[] { compilationUnit });
            } 
            String resourceName = className.replace('.', '/') + ".class";
            InputStream is = JRJdtCompiler.this.getResource(resourceName);
            if (is != null) {
              byte[] buf = new byte[8192];
              ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
              int count;
              while ((count = is.read(buf, 0, buf.length)) > 0)
                baos.write(buf, 0, count); 
              baos.flush();
              byte[] classBytes = baos.toByteArray();
              char[] fileName = className.toCharArray();
              ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
              if (JRJdtCompiler.this.is2ArgsConstr)
                return JRJdtCompiler.this.constrNameEnvAnsBin2Args.newInstance(new Object[] { classFileReader, null }); 
              return JRJdtCompiler.this.constrNameEnvAnsBin.newInstance(new Object[] { classFileReader });
            } 
          } catch (IOException exc) {
            JRJdtCompiler.log.error("Compilation error", exc);
          } catch (ClassFormatException exc) {
            JRJdtCompiler.log.error("Compilation error", (Throwable)exc);
          } catch (InvocationTargetException e) {
            throw new JRRuntimeException("Not able to create NameEnvironmentAnswer", e);
          } catch (IllegalArgumentException e) {
            throw new JRRuntimeException("Not able to create NameEnvironmentAnswer", e);
          } catch (InstantiationException e) {
            throw new JRRuntimeException("Not able to create NameEnvironmentAnswer", e);
          } catch (IllegalAccessException e) {
            throw new JRRuntimeException("Not able to create NameEnvironmentAnswer", e);
          } 
          return null;
        }
        
        private boolean isPackage(String result) {
          int classIdx = getClassIndex(result);
          if (classIdx >= 0)
            return false; 
          String resourceName = result.replace('.', '/') + ".class";
          boolean isPackage = true;
          InputStream is = JRJdtCompiler.this.getResource(resourceName);
          if (is != null)
            try {
              isPackage = (is.read() > 0);
            } catch (IOException e) {
              try {
                is.close();
              } catch (IOException iOException) {}
            } finally {
              try {
                is.close();
              } catch (IOException e) {}
            }  
          return isPackage;
        }
        
        public boolean isPackage(char[][] parentPackageName, char[] packageName) {
          StringBuffer result = new StringBuffer();
          String sep = "";
          if (parentPackageName != null)
            for (int i = 0; i < parentPackageName.length; i++) {
              result.append(sep);
              result.append(parentPackageName[i]);
              sep = ".";
            }  
          if (Character.isUpperCase(packageName[0]))
            if (!isPackage(result.toString()))
              return false;  
          result.append(sep);
          result.append(packageName);
          return isPackage(result.toString());
        }
        
        public void cleanup() {}
      };
    return env;
  }
  
  protected ICompilerRequestor getCompilerRequestor(final JRCompilationUnit[] units, final StringBuffer problemBuffer) {
    ICompilerRequestor requestor = new ICompilerRequestor() {
        private final JRCompilationUnit[] val$units;
        
        private final StringBuffer val$problemBuffer;
        
        private final JRJdtCompiler this$0;
        
        public void acceptResult(CompilationResult result) {
          String className = ((JRJdtCompiler.CompilationUnit)result.getCompilationUnit()).className;
          int classIdx;
          for (classIdx = 0; classIdx < units.length; classIdx++) {
            if (className.equals(units[classIdx].getName()))
              break; 
          } 
          if (result.hasErrors()) {
            String sourceCode = units[classIdx].getSourceCode();
            IProblem[] problems = JRJdtCompiler.this.getJavaCompilationErrors(result);
            for (int i = 0; i < problems.length; i++) {
              IProblem problem = problems[i];
              problemBuffer.append(i + 1);
              problemBuffer.append(". ");
              problemBuffer.append(problem.getMessage());
              if (problem.getSourceStart() >= 0 && problem.getSourceEnd() >= 0) {
                int problemStartIndex = sourceCode.lastIndexOf("\n", problem.getSourceStart()) + 1;
                int problemEndIndex = sourceCode.indexOf("\n", problem.getSourceEnd());
                if (problemEndIndex < 0)
                  problemEndIndex = sourceCode.length(); 
                problemBuffer.append("\n");
                problemBuffer.append(sourceCode.substring(problemStartIndex, problemEndIndex));
                problemBuffer.append("\n");
                int j;
                for (j = problemStartIndex; j < problem.getSourceStart(); j++)
                  problemBuffer.append(" "); 
                if (problem.getSourceStart() == problem.getSourceEnd()) {
                  problemBuffer.append("^");
                } else {
                  problemBuffer.append("<");
                  for (j = problem.getSourceStart() + 1; j < problem.getSourceEnd(); j++)
                    problemBuffer.append("-"); 
                  problemBuffer.append(">");
                } 
              } 
              problemBuffer.append("\n");
            } 
            problemBuffer.append(problems.length);
            problemBuffer.append(" errors\n");
          } 
          if (problemBuffer.length() == 0) {
            ClassFile[] resultClassFiles = result.getClassFiles();
            for (int i = 0; i < resultClassFiles.length; i++)
              units[classIdx].setCompileData((Serializable)resultClassFiles[i].getBytes()); 
          } 
        }
      };
    return requestor;
  }
  
  protected Map getJdtSettings() {
    Map settings = new HashMap();
    settings.put("org.eclipse.jdt.core.compiler.debug.lineNumber", "generate");
    settings.put("org.eclipse.jdt.core.compiler.debug.sourceFile", "generate");
    settings.put("org.eclipse.jdt.core.compiler.problem.deprecation", "ignore");
    List properties = JRProperties.getProperties("org.eclipse.jdt.core.");
    for (Iterator it = properties.iterator(); it.hasNext(); ) {
      JRProperties.PropertySuffix property = it.next();
      String propVal = property.getValue();
      if (propVal != null && propVal.length() > 0)
        settings.put(property.getKey(), propVal); 
    } 
    Properties systemProps = System.getProperties();
    for (Enumeration enumeration = systemProps.propertyNames(); enumeration.hasMoreElements(); ) {
      String propName = (String)enumeration.nextElement();
      if (propName.startsWith("org.eclipse.jdt.core.")) {
        String propVal = systemProps.getProperty(propName);
        if (propVal != null && propVal.length() > 0)
          settings.put(propName, propVal); 
      } 
    } 
    return settings;
  }
  
  private ClassLoader getClassLoader() {
    ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
    if (clsLoader != null)
      try {
        Class.forName(JRJdtCompiler.class.getName(), true, clsLoader);
      } catch (ClassNotFoundException e) {
        clsLoader = null;
      }  
    if (clsLoader == null)
      clsLoader = JRClassLoader.class.getClassLoader(); 
    return clsLoader;
  }
  
  protected InputStream getResource(String resourceName) {
    if (this.classLoader == null)
      return JRJdtCompiler.class.getResourceAsStream("/" + resourceName); 
    return this.classLoader.getResourceAsStream(resourceName);
  }
  
  protected Class loadClass(String className) throws ClassNotFoundException {
    if (this.classLoader == null)
      return Class.forName(className); 
    return this.classLoader.loadClass(className);
  }
  
  protected void checkLanguage(String language) throws JRException {
    if (!"java".equals(language))
      throw new JRException("Language \"" + language + "\" not supported by this report compiler.\n" + "Expecting \"java\" instead."); 
  }
  
  protected JRCompilationSourceCode generateSourceCode(JRSourceCompileTask sourceTask) throws JRException {
    return JRClassGenerator.generateClass(sourceTask);
  }
  
  protected String getSourceFileName(String unitName) {
    return unitName + ".java";
  }
  
  protected String getCompilerClass() {
    return JRJavacCompiler.class.getName();
  }
  
  protected IProblem[] getJavaCompilationErrors(CompilationResult result) {
    try {
      Method getErrorsMethod = result.getClass().getMethod("getErrors", null);
      return (IProblem[])getErrorsMethod.invoke(result, null);
    } catch (SecurityException e) {
      throw new JRRuntimeException("Error resolving JDT methods", e);
    } catch (NoSuchMethodException e) {
      throw new JRRuntimeException("Error resolving JDT methods", e);
    } catch (IllegalArgumentException e) {
      throw new JRRuntimeException("Error invoking JDT methods", e);
    } catch (IllegalAccessException e) {
      throw new JRRuntimeException("Error invoking JDT methods", e);
    } catch (InvocationTargetException e) {
      throw new JRRuntimeException("Error invoking JDT methods", e);
    } 
  }
}
