Skip to content

Commit 18d9e5a

Browse files
committed
added javadoc pom entry, updates to FilesAndStreams and preps for SDK
1 parent d8efb5b commit 18d9e5a

File tree

4 files changed

+124
-20
lines changed

4 files changed

+124
-20
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@
6161
<finalName>sepia-core-tools-v${project.version}</finalName>
6262
</configuration>
6363
</plugin>
64+
65+
<!-- Java docs, use with: mvn javadoc:jar -->
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-javadoc-plugin</artifactId>
69+
<version>3.0.1</version>
70+
<configuration>
71+
<doclint>none</doclint> <!-- TODO: fix errors found by linter -->
72+
<finalName>sepia-core-tools-v${project.version}</finalName>
73+
</configuration>
74+
</plugin>
6475
</plugins>
6576
</build>
6677

src/main/java/net/b07z/sepia/server/core/tools/ClassBuilder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package net.b07z.sepia.server.core.tools;
22

33
import java.lang.reflect.Constructor;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.tools.DiagnosticCollector;
8+
import javax.tools.JavaCompiler;
9+
import javax.tools.JavaFileObject;
10+
import javax.tools.ToolProvider;
411

512
/**
613
* Build classes from strings. Very handy for configuration files and plug-ins.
@@ -81,5 +88,23 @@ public static Object construct(String module_name){
8188
throw new RuntimeException(DateTime.getLogDate() + " ERROR - Class not found: " + module_name, e);
8289
}
8390
}
91+
92+
/**
93+
* Experimental string source-code compiler.
94+
* @param className
95+
* @param classCode
96+
* @param fileName
97+
*/
98+
public static void compile(String className, String classCode, String fileName){
99+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
100+
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
101+
102+
List<JavaFileObject> compilationUnits = new ArrayList<JavaFileObject>();
103+
JavaFileObject file = new SourceCodeFromString(className, classCode);
104+
compilationUnits.add(file);
105+
106+
JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);
107+
System.out.println(task.call() + diagnostics.getDiagnostics().toString()); //passes every time
108+
}
84109

85110
}

src/main/java/net/b07z/sepia/server/core/tools/FilesAndStreams.java

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package net.b07z.sepia.server.core.tools;
22

3-
import java.io.BufferedInputStream;
43
import java.io.BufferedReader;
54
import java.io.File;
65
import java.io.FileInputStream;
76
import java.io.FileOutputStream;
87
import java.io.IOException;
98
import java.io.InputStream;
109
import java.io.InputStreamReader;
11-
import java.io.OutputStream;
10+
import java.io.OutputStreamWriter;
11+
import java.nio.charset.Charset;
1212
import java.nio.charset.StandardCharsets;
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
1616
import java.util.ArrayList;
1717
import java.util.List;
1818
import java.util.Properties;
19+
import java.util.function.Consumer;
1920

2021
/**
2122
* Handles file read/write/edit etc.
@@ -36,21 +37,62 @@ public static interface LineOperation {
3637
*/
3738
public String run(String lineInput);
3839
}
40+
41+
/**
42+
* Consume streams with custom consumer. Usage example with println:<br>
43+
* <br>
44+
* BufferedInputStreamConsumer streamGobbler = new StreamGobbler(myStream, System.out::println);<br>
45+
* Executors.newSingleThreadExecutor().submit(streamGobbler);<br>
46+
* <br>
47+
* As seen on <a href="https://www.baeldung.com/run-shell-command-in-java">baeldung.com</a>
48+
*/
49+
public static class BufferedInputStreamConsumer implements Runnable {
50+
private InputStream inputStream;
51+
private Consumer<String> consumer;
52+
private Charset charset;
53+
54+
public BufferedInputStreamConsumer(InputStream inputStream, Charset charset, Consumer<String> consumer) {
55+
this.inputStream = inputStream;
56+
this.consumer = consumer;
57+
this.charset = charset;
58+
}
59+
@Override
60+
public void run() {
61+
new BufferedReader(new InputStreamReader(inputStream, charset)).lines()
62+
.forEach(consumer);
63+
}
64+
}
3965

4066
/**
41-
* Collect all data of an InputStream to a string.<br>
42-
* NOTE: Please define encoding of stream!
67+
* Collect all data of an InputStream to a string via BufferedReader and InputStreamReader.<br>
68+
* NOTE: Please check correct encoding of stream!
4369
*/
44-
public static String getStringFromStream(InputStream stream) {
45-
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {
70+
public static String getStringFromStream(InputStream stream, Charset charset) throws IOException{
71+
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, charset))) {
4672
String inputLine;
4773
StringBuilder response = new StringBuilder();
4874
while ((inputLine = in.readLine()) != null) {
49-
response.append(inputLine);
75+
response.append(inputLine + System.lineSeparator());
5076
}
5177
return response.toString();
52-
} catch (IOException e) {
53-
throw new RuntimeException(e);
78+
}catch (IOException e){
79+
throw e;
80+
}
81+
}
82+
/**
83+
* Collect all data of an InputStream to a list, line-by-line via BufferedReader and InputStreamReader.<br>
84+
* NOTE: Please check correct encoding of stream!
85+
*/
86+
public static List<String> getLinesFromStream(InputStream stream, Charset charset) throws IOException{
87+
try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, charset))) {
88+
String inputLine;
89+
List<String> response = new ArrayList<>();
90+
while ((inputLine = in.readLine()) != null) {
91+
response.add(inputLine);
92+
}
93+
return response;
94+
}catch (IOException e){
95+
throw e;
5496
}
5597
}
5698

@@ -141,7 +183,7 @@ public static boolean replaceLineInFile(String pathWithName, String lineMatchReg
141183
Path path = Paths.get(pathWithName);
142184
List<String> fileContent = new ArrayList<>(Files.readAllLines(path, StandardCharsets.UTF_8));
143185
boolean foundLine = false;
144-
for (int i = 0; i < fileContent.size(); i++) {
186+
for (int i=0; i<fileContent.size(); i++) {
145187
String line = fileContent.get(i);
146188
if (line.matches(lineMatchRegExp)) {
147189
foundLine = true;
@@ -164,27 +206,29 @@ public static boolean replaceLineInFile(String pathWithName, String lineMatchReg
164206
}
165207

166208
/**
167-
* Save settings to properties file.
168-
* @param config_file - path and file
209+
* Save settings to properties file (UTF-8).
210+
* @param configFile - path and file
169211
* @param config - Properties with settings to store
170212
*/
171-
public static void saveSettings(String config_file, Properties config) throws Exception{
172-
OutputStream out =null;
173-
File f = new File(config_file);
174-
out = new FileOutputStream( f );
213+
public static void saveSettings(String configFile, Properties config) throws Exception{
214+
File f = new File(configFile);
215+
OutputStreamWriter out = new OutputStreamWriter(
216+
new FileOutputStream(f), StandardCharsets.UTF_8
217+
);
175218
config.store(out, null);
176219
out.flush();
177220
out.close();
178221
}
179222

180223
/**
181-
* Load settings from properties file and return Properties.
224+
* Load settings from properties file (UTF-8) and return Properties.
182225
* @param config_file - path and file
183226
*/
184-
public static Properties loadSettings(String config_file) throws Exception{
185-
BufferedInputStream stream=null;
227+
public static Properties loadSettings(String configFile) throws Exception{
186228
Properties config = new Properties();
187-
stream = new BufferedInputStream(new FileInputStream(config_file));
229+
InputStreamReader stream = new InputStreamReader(
230+
new FileInputStream(configFile), StandardCharsets.UTF_8
231+
);
188232
config.load(stream);
189233
stream.close();
190234
return config;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.b07z.sepia.server.core.tools;
2+
3+
import java.net.URI;
4+
5+
import javax.tools.SimpleJavaFileObject;
6+
7+
/**
8+
* Class used during on-the-fly compiling of a source-code string.
9+
*
10+
* @author Florian Quirin
11+
*/
12+
public class SourceCodeFromString extends SimpleJavaFileObject {
13+
final String code;
14+
15+
public SourceCodeFromString(String name, String code){
16+
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
17+
this.code = code;
18+
}
19+
20+
@Override
21+
public CharSequence getCharContent(boolean ignoreEncodingErrors){
22+
return code;
23+
}
24+
}

0 commit comments

Comments
 (0)