From d9d37bcb9b8550280c5a5737527bb81dad58256b Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Mon, 20 Mar 2017 15:06:35 +0000
Subject: [PATCH] Classpath fuer WebBox/CatalinaBase/lib und webapp/WEB-INF/lib in Compiler-Optionen aufgenommen

---
 src/java/de/uhilger/filecms/api/CompileService.java |   99 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 84 insertions(+), 15 deletions(-)

diff --git a/src/java/de/uhilger/filecms/api/CompileService.java b/src/java/de/uhilger/filecms/api/CompileService.java
index ceb507e..2aaf5aa 100644
--- a/src/java/de/uhilger/filecms/api/CompileService.java
+++ b/src/java/de/uhilger/filecms/api/CompileService.java
@@ -34,6 +34,7 @@
 import javax.tools.JavaFileObject;
 import javax.tools.StandardJavaFileManager;
 import javax.tools.ToolProvider;
+import org.apache.commons.io.FileUtils;
 
 /**
  *
@@ -53,16 +54,20 @@
       DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
       StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
       Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
-      compiler.getTask(null, null, diagnostics, null, null, compilationUnits).call();
+      
+      
+      final Iterable<String> options = buildOptions(targetDir);
+      
+      compiler.getTask(null, null, diagnostics, options, null, compilationUnits).call();
       fileManager.close();
-      collectResults(diagnostics, compilerIssues);
+      collectResults(diagnostics, compilerIssues, relPath);
     } catch(Exception ex) {
       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
     }
     return compilerIssues;
   }
   
-  private void collectResults(DiagnosticCollector<JavaFileObject> diagnostics, List<CompilerIssue> compilerIssues) {
+  private void collectResults(DiagnosticCollector<JavaFileObject> diagnostics, List<CompilerIssue> compilerIssues, String relPath) {
     List compileResults = diagnostics.getDiagnostics();
     Iterator i = compileResults.iterator();
     while (i.hasNext()) {
@@ -74,7 +79,13 @@
         issue.setKind(err.getKind().name());
         issue.setLineNumber(err.getLineNumber());
         issue.setMessage(err.getMessage(Locale.GERMANY));
-        issue.setSoureName(err.getSource().getName());
+        
+        String srcName = err.getSource().getName().replace("\\", "/");
+        String cleanRelPath = relPath.replace(HOME_DIR_NAME + "/", "").replace(PUB_DIR_NAME + "/", "");
+        int pos = srcName.indexOf(cleanRelPath);
+        String className = srcName.substring(pos + cleanRelPath.length());
+        issue.setSourceName(className.replace("/", ".").substring(1));
+        //issue.setSourceName(srcName + "\r\n" + relPath);
         compilerIssues.add(issue);
       }
     }
@@ -99,6 +110,19 @@
     public boolean accept(File pathname) {
       boolean doAccept = false;
       if(pathname.getName().endsWith(".java") || pathname.isDirectory()) {
+        doAccept = true;
+      }
+      return doAccept;
+    }
+  
+  }
+  
+  public class JarFileFilter implements FileFilter {
+
+    @Override
+    public boolean accept(File pathname) {
+      boolean doAccept = false;
+      if(pathname.getName().endsWith(".jar")) {
         doAccept = true;
       }
       return doAccept;
@@ -131,24 +155,69 @@
     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
     StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
     Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);    
-    if( mode.equals("1")) {
-      final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
-              /*"-cp", project.getClassPath(),*/
-              "-d", targetDir.getAbsolutePath()
-              });
-      compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
-    } else {
-      compiler.getTask(null, null, diagnostics, null, null, compilationUnits1).call();
-    }     
+    final Iterable<String> options = buildOptions(targetDir);
+    compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
     fileManager.close();
     List<CompilerIssue> compilerIssues = new ArrayList();
-    collectResults(diagnostics, compilerIssues);
+    collectResults(diagnostics, compilerIssues, relPath);
     return compilerIssues;
   }
-  
+
+  private final Iterable<String> buildOptions(File targetDir) {
+      String cbase = getCatalinaBase();
+      File lib = new File(cbase, "lib");
+      String cp = "";
+      cp = buildCPFromDir(cp, lib);
+      logger.fine(lib.getAbsolutePath());
+      logger.fine(cp);
+      /*
+        wegen dieser Funktion MUSS alles in 'src' liegen
+      */
+      File srcDir = targetDir;
+      while(!srcDir.getName().endsWith("src")) {        
+        srcDir = srcDir.getParentFile();
+      }
+      File appDir = srcDir.getParentFile();
+      File appLibDir = new File(appDir, "web/WEB-INF/lib");
+      cp = buildCPFromDir(cp, appLibDir);
+      logger.fine(cp);
+      /*
+        ausgehend von src eins hoeher, dann nach build
+      */
+      File buildDir = new File(appDir, "build");
+      final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
+                    "-cp", cp,
+                    "-d", buildDir.getAbsolutePath()
+                    });      
+      try {
+        FileUtils.deleteDirectory(buildDir);
+        buildDir.mkdir();
+      } catch (IOException ex) {
+        logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
+      }
+      return options;
+  }
+  /*
+  -classpath $JLIB/jettison-1.3.3.jar:$JLIB/xstream-1.4.7.jar
+  */
+  private String buildCPFromDir(String cp, File dir) {
+    StringBuffer buf = new StringBuffer(cp);
+    File[] files = dir.listFiles(new JarFileFilter());
+    for(int i = 0; i < files.length; i++) {
+      if(buf.length() > 0) {
+        buf.append(File.pathSeparatorChar);
+      }
+      //buf.append("\"");
+      buf.append(files[i].getAbsolutePath());
+      //buf.append("\"");
+    }
+    
+    return buf.toString();
+  }
 }
 
 
+
 /*
 
  Beispeil fuer einen dynamischen Compiler-Aufruf

--
Gitblit v1.9.3