Dateiverwaltung für die WebBox
ulrich
2017-03-19 f23f2b611c8670bb9da8e6ddd32d3755289166c2
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;
37
38 /**
39  *
40  */
72e43d 41 public class CompileService extends Api {
0ac262 42   
U 43   private static final Logger logger = Logger.getLogger(CompileService.class.getName());
72e43d 44     
U 45   public List<CompilerIssue> compileAll(String relPath) {
46     logger.fine(relPath);
47     List<CompilerIssue> compilerIssues = new ArrayList();
48     try {
49       File targetDir = getTargetDir(relPath);
50       ArrayList<File> files = new ArrayList();
51       collectFiles(files, targetDir, new JavaFileFilter());
52       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
53       DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
54       StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
55       Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
56       compiler.getTask(null, null, diagnostics, null, null, compilationUnits).call();
57       fileManager.close();
f23f2b 58       collectResults(diagnostics, compilerIssues);
72e43d 59     } catch(Exception ex) {
U 60       logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
61     }
62     return compilerIssues;
f23f2b 63   }
U 64   
65   private void collectResults(DiagnosticCollector<JavaFileObject> diagnostics, List<CompilerIssue> compilerIssues) {
66     List compileResults = diagnostics.getDiagnostics();
67     Iterator i = compileResults.iterator();
68     while (i.hasNext()) {
69       Object o = i.next();
70       Diagnostic<? extends JavaFileObject> err;
71       if (o instanceof Diagnostic) {
72         err = (Diagnostic) o;
73         CompilerIssue issue = new CompilerIssue();
74         issue.setKind(err.getKind().name());
75         issue.setLineNumber(err.getLineNumber());
76         issue.setMessage(err.getMessage(Locale.GERMANY));
77         issue.setSoureName(err.getSource().getName());
78         compilerIssues.add(issue);
79       }
80     }
72e43d 81   }
0ac262 82   
72e43d 83   private void collectFiles(ArrayList<File> files, File dir, FileFilter filter) {
U 84     File[] dirFiles = dir.listFiles(filter);
85     for(int i = 0; i < dirFiles.length; i++) {
86       if(dirFiles[i].isDirectory()) {
87         logger.fine("drill down to " + dirFiles[i].getAbsolutePath());
88         collectFiles(files, dirFiles[i], filter);
89       } else {
90         logger.fine("add " + dirFiles[i].getAbsolutePath());
91         files.add(dirFiles[i]);
92       }
93     }
94   }
95   
96   public class JavaFileFilter implements FileFilter {
97
98     @Override
99     public boolean accept(File pathname) {
100       boolean doAccept = false;
101       if(pathname.getName().endsWith(".java") || pathname.isDirectory()) {
102         doAccept = true;
103       }
104       return doAccept;
105     }
106   
107   }
0ac262 108   
fd0b4c 109   /**
U 110    * 
111    * @param relPath
112    * @param fileNames
113    * @param mode 0 = test, 1 = build
114    * @return
115    * @throws IOException 
116    */
117   public List<CompilerIssue> compile(String relPath, List fileNames, String mode) throws IOException {
0ac262 118     File targetDir = getTargetDir(relPath);
U 119     ArrayList<File> files = new ArrayList();
120     for(int i=0; i < fileNames.size(); i++) {
121       Object o = fileNames.get(i);
122       if(o instanceof ArrayList) {
123         ArrayList al = (ArrayList) o;
124         logger.fine(al.get(0).toString());
125         File targetFile = new File(targetDir, al.get(0).toString());
126         logger.fine(targetFile.getAbsolutePath());
127         files.add(targetFile);
128       }
129     }
130     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
131     DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
132     StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
f23f2b 133     Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);    
fd0b4c 134     if( mode.equals("1")) {
U 135       final Iterable<String> options = Arrays.asList(new String[]{"-Xlint",
136               /*"-cp", project.getClassPath(),*/
0ac262 137               "-d", targetDir.getAbsolutePath()
U 138               });
fd0b4c 139       compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits1).call();
U 140     } else {
141       compiler.getTask(null, null, diagnostics, null, null, compilationUnits1).call();
142     }     
0ac262 143     fileManager.close();
e3043f 144     List<CompilerIssue> compilerIssues = new ArrayList();
f23f2b 145     collectResults(diagnostics, compilerIssues);
e3043f 146     return compilerIssues;
0ac262 147   }
e3043f 148   
0ac262 149 }
U 150
151
152 /*
153
154  Beispeil fuer einen dynamischen Compiler-Aufruf
155  'in memory'
156
157  String className = "mypackage.MyClass";
158  String javaCode = "package mypackage;\n" +
159                   "public class MyClass implements Runnable {\n" +
160                   "    public void run() {\n" +
161                   "        System.out.println("\"Hello World\");\n" +
162                   "    }\n" +
163                   "}\n";
164  Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);
165  Runnable runner = (Runnable) aClass.newInstance();
166  runner.run();
167
d920b7 168 */
U 169
170 /*
171   CodeMirror Breakpoint bzw. Gutter Marker
172
173   https://codemirror.net/demo/marker.html
0ac262 174 */