Dateiverwaltung für die WebBox
ulrich
2017-03-20 a450f233393a9e3297fea633373635d060a3c709
commit | author | age
0ac262 1 /*
U 2     Dateiverwaltung - File management in your browser
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 package de.uhilger.filecms.api;
19
e3043f 20 import de.uhilger.filecms.data.CompilerIssue;
0ac262 21 import java.io.File;
72e43d 22 import java.io.FileFilter;
0ac262 23 import java.io.IOException;
U 24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Locale;
72e43d 29 import java.util.logging.Level;
0ac262 30 import java.util.logging.Logger;
U 31 import javax.tools.Diagnostic;
32 import javax.tools.DiagnosticCollector;
33 import javax.tools.JavaCompiler;
34 import javax.tools.JavaFileObject;
35 import javax.tools.StandardJavaFileManager;
36 import javax.tools.ToolProvider;
a450f2 37 import org.apache.commons.io.FileUtils;
0ac262 38
U 39 /**
40  *
41  */
72e43d 42 public class CompileService extends Api {
0ac262 43   
U 44   private static final Logger logger = Logger.getLogger(CompileService.class.getName());
72e43d 45     
U 46   public List<CompilerIssue> compileAll(String relPath) {
47     logger.fine(relPath);
48     List<CompilerIssue> compilerIssues = new ArrayList();
49     try {
50       File targetDir = getTargetDir(relPath);
51       ArrayList<File> files = new ArrayList();
52       collectFiles(files, targetDir, new JavaFileFilter());
53       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
54       DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
55       StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
56       Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
a450f2 57       
U 58       File buildDir = new File(targetDir.getParentFile(), "build");
59       final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
60                     /*"-cp", project.getClassPath(),*/
61                     /*
62                       ausgehend von src eins hoeher, dann nach build
63                     */
64                     "-d", buildDir.getAbsolutePath()
65                     });      
66       FileUtils.deleteDirectory(buildDir);
67       buildDir.mkdir();
68       compiler.getTask(null, null, diagnostics, options, null, compilationUnits).call();
72e43d 69       fileManager.close();
a450f2 70       collectResults(diagnostics, compilerIssues, relPath);
72e43d 71     } catch(Exception ex) {
U 72       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
73     }
74     return compilerIssues;
f23f2b 75   }
U 76   
a450f2 77   private void collectResults(DiagnosticCollector<JavaFileObject> diagnostics, List<CompilerIssue> compilerIssues, String relPath) {
f23f2b 78     List compileResults = diagnostics.getDiagnostics();
U 79     Iterator i = compileResults.iterator();
80     while (i.hasNext()) {
81       Object o = i.next();
82       Diagnostic<? extends JavaFileObject> err;
83       if (o instanceof Diagnostic) {
84         err = (Diagnostic) o;
85         CompilerIssue issue = new CompilerIssue();
86         issue.setKind(err.getKind().name());
87         issue.setLineNumber(err.getLineNumber());
88         issue.setMessage(err.getMessage(Locale.GERMANY));
a450f2 89         
U 90         String srcName = err.getSource().getName().replace("\\", "/");
91         String cleanRelPath = relPath.replace(HOME_DIR_NAME + "/", "").replace(PUB_DIR_NAME + "/", "");
92         int pos = srcName.indexOf(cleanRelPath);
93         String className = srcName.substring(pos + cleanRelPath.length());
94         issue.setSourceName(className.replace("/", ".").substring(1));
95         //issue.setSourceName(srcName + "\r\n" + relPath);
f23f2b 96         compilerIssues.add(issue);
U 97       }
98     }
72e43d 99   }
0ac262 100   
72e43d 101   private void collectFiles(ArrayList<File> files, File dir, FileFilter filter) {
U 102     File[] dirFiles = dir.listFiles(filter);
103     for(int i = 0; i < dirFiles.length; i++) {
104       if(dirFiles[i].isDirectory()) {
105         logger.fine("drill down to " + dirFiles[i].getAbsolutePath());
106         collectFiles(files, dirFiles[i], filter);
107       } else {
108         logger.fine("add " + dirFiles[i].getAbsolutePath());
109         files.add(dirFiles[i]);
110       }
111     }
112   }
113   
114   public class JavaFileFilter implements FileFilter {
115
116     @Override
117     public boolean accept(File pathname) {
118       boolean doAccept = false;
119       if(pathname.getName().endsWith(".java") || pathname.isDirectory()) {
120         doAccept = true;
121       }
122       return doAccept;
123     }
124   
125   }
0ac262 126   
fd0b4c 127   /**
U 128    * 
129    * @param relPath
130    * @param fileNames
131    * @param mode 0 = test, 1 = build
132    * @return
133    * @throws IOException 
134    */
135   public List<CompilerIssue> compile(String relPath, List fileNames, String mode) throws IOException {
0ac262 136     File targetDir = getTargetDir(relPath);
U 137     ArrayList<File> files = new ArrayList();
138     for(int i=0; i < fileNames.size(); i++) {
139       Object o = fileNames.get(i);
140       if(o instanceof ArrayList) {
141         ArrayList al = (ArrayList) o;
142         logger.fine(al.get(0).toString());
143         File targetFile = new File(targetDir, al.get(0).toString());
144         logger.fine(targetFile.getAbsolutePath());
145         files.add(targetFile);
146       }
147     }
148     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
149     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
150     StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
f23f2b 151     Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);    
fd0b4c 152     if( mode.equals("1")) {
U 153       final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
154               /*"-cp", project.getClassPath(),*/
0ac262 155               "-d", targetDir.getAbsolutePath()
U 156               });
fd0b4c 157       compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
U 158     } else {
159       compiler.getTask(null, null, diagnostics, null, null, compilationUnits1).call();
160     }     
0ac262 161     fileManager.close();
e3043f 162     List<CompilerIssue> compilerIssues = new ArrayList();
a450f2 163     collectResults(diagnostics, compilerIssues, relPath);
e3043f 164     return compilerIssues;
0ac262 165   }
e3043f 166   
0ac262 167 }
U 168
169
170 /*
171
172  Beispeil fuer einen dynamischen Compiler-Aufruf
173  'in memory'
174
175  String className = "mypackage.MyClass";
176  String javaCode = "package mypackage;\n" +
177                   "public class MyClass implements Runnable {\n" +
178                   "    public void run() {\n" +
179                   "        System.out.println("\"Hello World\");\n" +
180                   "    }\n" +
181                   "}\n";
182  Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
183  Runnable runner = (Runnable) aClass.newInstance();
184  runner.run();
185
d920b7 186 */
U 187
188 /*
189   CodeMirror Breakpoint bzw. Gutter Marker
190
191   https://codemirror.net/demo/marker.html
0ac262 192 */