Skip to content

Commit 0545720

Browse files
committed
added StringTools, made Debugger settings public, smaller improvements
1 parent 4491278 commit 0545720

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public static Object construct(String canonicalClassName){
8484
}
8585

8686
/**
87-
* Experimental string source-code compiler.
87+
* Source code string compiler. Compiles code to class and stores result in given folder.
8888
* @param className - full class name including package, e.g. com.example.my_package.MyNewClass
8989
* @param classCode - source-code as seen in Java files
90-
* @param targetFolder - parent directory of compiled class file (without package-path) or null
90+
* @param targetFolder - parent directory of compiled class file (without package-path)
9191
* @return Compile errors as readable String or empty
9292
*/
9393
public static String compile(String className, String classCode, File targetFolder){
@@ -123,7 +123,7 @@ public static String compile(String className, String classCode, File targetFold
123123
if (task.call()){
124124
//Done
125125
Debugger.println("ClassBuilder - compiled '" + className + "' to '" + folderOrMemory, 3);
126-
return "";
126+
return ""; //TODO: if this is MEMORY ONLY how do we get the clas later?
127127
}else{
128128
//Error(s)
129129
StringBuilder errors = new StringBuilder("Compile errors: \n");
@@ -135,5 +135,12 @@ public static String compile(String className, String classCode, File targetFold
135135
return errors.toString();
136136
}
137137
}
138+
139+
/**
140+
* Simply use file name to get simple class name.
141+
*/
142+
public static String getSimpleClassNameFromFileName(String fileName){
143+
return fileName.replaceFirst("\\$.*\\.", ".").replaceFirst("\\.(\\w+)$", "").trim();
144+
}
138145

139146
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.Random;
1212

13+
import org.json.simple.JSONArray;
1314
import org.json.simple.JSONObject;
1415
import org.json.simple.parser.JSONParser;
1516
import org.json.simple.parser.ParseException;
@@ -389,6 +390,18 @@ public static void addJsonToMapAsStrings(JSONObject jsonSource, Map<String, Stri
389390
}
390391
}
391392

393+
/**
394+
* Convert a JSONArray to an ArrayList of strings.
395+
* @param jArray - array to convert, entries are cast to String via '.toString()'
396+
*/
397+
public static List<String> jsonArrayToStringList(JSONArray jArray){
398+
List<String> list = new ArrayList<>();
399+
for (Object o : jArray){
400+
list.add(o.toString());
401+
}
402+
return list;
403+
}
404+
392405
/**
393406
* Makes an unchecked (cause you can't check it, can you?) cast from Object to HashMap&#60String, Object&#62.
394407
* @param input - object that is supposed to be the expected HashMap

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
public class Debugger {
1919

20-
static boolean error = true; //1
21-
static boolean info = false; //2
22-
static boolean log = true; //3
20+
public static boolean error = true; //1
21+
public static boolean info = false; //2
22+
public static boolean log = true; //3
2323

2424
/**
2525
* Print a debug message with debug-type.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.nio.charset.Charset;
44
import java.nio.charset.StandardCharsets;
5+
import java.util.Collection;
56
import java.util.List;
67
import java.util.concurrent.TimeUnit;
78
import java.util.stream.Stream;
@@ -113,6 +114,21 @@ public static RuntimeResult getWindowsShellCodepage(){
113114
public static RuntimeResult runCommand(String[] command){
114115
return runCommand(command, 5000);
115116
}
117+
/**
118+
* Execute runtime command. Chooses between "cmd.exe" and "sh" shell by OS.
119+
* @param command - e.g.: 'Arrays.asList("ping", "-c", "3", "sepia-framework.github.io")'
120+
* @param customTimeout - custom value between 0 and 15000 ms
121+
* @return
122+
*/
123+
public static RuntimeResult runCommand(Collection<String> command, long customTimeout){
124+
return runCommand(command.toArray(new String[command.size()]), 5000);
125+
}
126+
/**
127+
* Execute runtime command. Chooses between "cmd.exe" and "sh" shell by OS.
128+
* @param command - e.g.: 'new String[]{"ping", "-c", "3", "sepia-framework.github.io"}'
129+
* @param customTimeout - custom value between 0 and 15000 ms
130+
* @return
131+
*/
116132
public static RuntimeResult runCommand(String[] command, long customTimeout){
117133
Charset encoding = StandardCharsets.UTF_8;
118134
if (isWindows() && windowsShellCodepage == null && !command[0].equals("chcp")){
@@ -127,6 +143,8 @@ public static RuntimeResult runCommand(String[] command, long customTimeout){
127143
}
128144
if (customTimeout > 15000){
129145
customTimeout = 15000;
146+
}else if (customTimeout <= 0){
147+
customTimeout = 5000;
130148
}
131149
Process process;
132150
try{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.b07z.sepia.server.core.tools;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
7+
/**
8+
* Some convenience methods to work with strings.
9+
*
10+
* @author Florian Quirin
11+
*
12+
*/
13+
public class StringTools {
14+
15+
/**
16+
* Find first matching regular expression or return empty string.
17+
* @param input - input to search in
18+
* @param regEx - regular expression to search for
19+
*/
20+
public static String findFirstRexEx(String input, String regEx){
21+
Pattern pattern = Pattern.compile(regEx);
22+
Matcher matcher = pattern.matcher(input);
23+
if (matcher.find()){
24+
return matcher.group(0);
25+
}else{
26+
return "";
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)