Skip to content

Commit 7adc087

Browse files
committed
added improved file-line-replace tool
1 parent 59788ec commit 7adc087

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
*
2525
*/
2626
public class FilesAndStreams {
27+
28+
//----helpers:
29+
30+
/**
31+
* Generic class to give line operations as lambda expressions.
32+
*/
33+
public static interface LineOperation {
34+
/**
35+
* Take line as input and return modified line.
36+
*/
37+
public String run(String lineInput);
38+
}
2739

2840
/**
2941
* Collect all data of an InputStream to a string.
@@ -103,14 +115,27 @@ public static boolean writeFileFromList(String pathWithName, List<String> fileCo
103115
}
104116

105117
/**
106-
* Open a file, search first line by regular expression, then replace with new line.
118+
* Open a file, search line by regular expression then replace with new line.
107119
* Stops after first match.
108120
* @param pathWithName - path to file including file-name
109121
* @param lineMatchRegExp - regular expression to find line
110122
* @param replacement - complete line is replaced by this
111123
* @return true (all good), false (error during read/write or line not found)
112124
*/
113125
public static boolean replaceLineInFile(String pathWithName, String lineMatchRegExp, String replacement){
126+
return replaceLineInFile(pathWithName, lineMatchRegExp, (oldLine) -> {
127+
return replacement;
128+
});
129+
}
130+
/**
131+
* Open a file, search line by regular expression then modify line by custom operation and store modifications.
132+
* Stops after first match.
133+
* @param pathWithName - path to file including file-name
134+
* @param lineMatchRegExp - regular expression to find line
135+
* @param lineOperation - modify line by using this with {@link LineOperation} (use lambda expression)
136+
* @return true (all good), false (error during read/write or line not found)
137+
*/
138+
public static boolean replaceLineInFile(String pathWithName, String lineMatchRegExp, LineOperation lineOperation){
114139
try {
115140
Path path = Paths.get(pathWithName);
116141
List<String> fileContent = new ArrayList<>(Files.readAllLines(path, StandardCharsets.UTF_8));
@@ -119,7 +144,8 @@ public static boolean replaceLineInFile(String pathWithName, String lineMatchReg
119144
String line = fileContent.get(i);
120145
if (line.matches(lineMatchRegExp)) {
121146
foundLine = true;
122-
fileContent.set(i, replacement);
147+
String newLine = lineOperation.run(line);
148+
fileContent.set(i, newLine);
123149
break;
124150
}
125151
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.net.MalformedURLException;
55
import java.net.URL;
66
import java.net.URLClassLoader;
7-
import java.util.ArrayList;
7+
import java.util.List;
88

99
/**
1010
* Class loader to be used for plugins to make them run in a sandbox.
@@ -14,7 +14,7 @@
1414
*/
1515
public class SandboxClassLoader extends URLClassLoader {
1616
//black-list classes
17-
private ArrayList<String> blackList;
17+
private List<String> blackList;
1818

1919
/*
2020
public SandboxClassLoader(URL fileUrl) {
@@ -24,7 +24,7 @@ public SandboxClassLoader(File fileOrDir) throws MalformedURLException {
2424
super(new URL[]{fileOrDir.toURI().toURL()});
2525
}
2626
*/
27-
public SandboxClassLoader(File fileOrDir, ArrayList<String> blackList) throws MalformedURLException {
27+
public SandboxClassLoader(File fileOrDir, List<String> blackList) throws MalformedURLException {
2828
super(new URL[]{fileOrDir.toURI().toURL()});
2929
this.blackList = blackList;
3030
}

src/test/java/tools/ToolsPlayground.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ public static void main(String[] args) {
3333
System.out.println("password client hash: " + hash);
3434
hash = hashPassword("!§$%&/()=?`");
3535
System.out.println("password client hash: " + hash);
36+
hash = hashPassword("test12345!_");
37+
System.out.println("password client hash: " + hash);
3638

3739
/* -- Elasticsearch queries -- */
38-
40+
/*
3941
//double-match for simple field
4042
String query = getBoolMustMatch();
4143
System.out.println("query must-must: " + query);
@@ -58,7 +60,7 @@ public static void main(String[] args) {
5860
//getAnswersByType query with JsonGenerator
5961
query = getAnswersQueryWithBuilder();
6062
System.out.println("getAnswersByType query with builder: " + query);
61-
63+
*/
6264
}
6365

6466
/* -- Password client hash -- */

0 commit comments

Comments
 (0)