Update CleanerPool, no more interface and constants everywhere

This commit is contained in:
jeffcheasey88 2023-04-30 22:09:18 +02:00
parent 4befbd7147
commit d2fd5aeff1
2 changed files with 61 additions and 91 deletions

View file

@ -2,88 +2,78 @@ package be.jeffcheasey88.peeratcode.parser.java;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class CleanerPool {
public class CleanerPool{
private static String CONSTANT_REPLACER_STRING = "$STRING_STATEMENT_CONSTANT_";
private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_";
private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_";
private List<Cleaner> cleaners;
private List<String> constants;
private CleanerPool(){
this.constants = new ArrayList<>();
public CleanerPool(List<Cleaner> cleaners){
this.cleaners = new ArrayList<>(cleaners);
}
public String clean(String statement){
char[] chars = statement.toCharArray();
StringBuilder builder = new StringBuilder();
for(int i = 0; i < chars.length; i++){
char current = chars[i];
if(current== '"'){
int constantPos = this.constants.size();
String constant = cutConstant(chars, i);
i+=constant.length()+1;
builder.append(CONSTANT_REPLACER_STRING+constantPos);
this.constants.add(constant);
}else{
builder.append(current);
public String clean(String value){
for(Cleaner cleaner : this.cleaners) value = cleaner.clean(value);
return value;
}
public boolean isConstant(String value){
for(Cleaner cleaner : this.cleaners) if(value.startsWith(cleaner.getPattern())) return true;
return false;
}
public String getConstant(String value){
for(Cleaner cleaner : this.cleaners){
if(value.startsWith(cleaner.getPattern())){
return cleaner.getConstant(value);
}
}
return null;
}
public static class Cleaner{
for(String s : constants){
System.out.println("CONSTANT="+s);
private String pattern;
private char open;
private char close;
public List<String> constants;
public Cleaner(String pattern, char open, char close){
this.pattern = pattern;
this.open = open;
this.close = close;
this.constants = new ArrayList<>();
}
return builder.toString();
}
public boolean isConstant(String region){
return region.startsWith(CONSTANT_REPLACER_STRING);
}
public String getConstant(String replacer){
if(!replacer.startsWith(CONSTANT_REPLACER_STRING)) return null;
return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER_STRING,"")));
}
public List<String> getConstants(){
return this.constants;
}
private static Pattern parenthesisPattern = Pattern.compile("^\\$SQL_STATEMENT_PARENTHESIS_([0-9]*$)");
public boolean isParenthesis(String region){
return parenthesisPattern.matcher(region).matches();
}
private String cutConstant(char[] chars, int pos){
StringBuilder builder = new StringBuilder();
for(int i = pos+1; i < chars.length; i++){
char current = chars[i];
if(current == '"'){
if(current == '\\'){ //toChange
builder.append(current);
}else break;
}else{
builder.append(current);
}
}
return builder.toString();
}
public static interface Cutter{
String execute(String value, List<String> constants, String pattern);
public String getPattern(){
return this.pattern;
}
default int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
public String getConstant(String value){
return this.constants.get(Integer.parseInt(value.replace(this.pattern, "")));
}
public String clean(String value){
StringBuilder builder = new StringBuilder();
for(int i = 0; i < value.length(); i++){
char c = value.charAt(i);
if(c == '<'){
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
builder.append(pattern+(constants.size()-1));
}else{
builder.append(c);
}
}
return builder.toString();
}
private int cutOpenable(String value, List<String> constants, String pattern, char open, char close){
StringBuilder builder = new StringBuilder();
System.out.println("cutOpenable "+value);
for(int i = 0; i < value.length(); i++){
char c = value.charAt(i);
if(c == open){
i+=cutOpenable(value.substring(i+1), constants, pattern, '<', '>');
i+=cutOpenable(value.substring(i+1), constants, pattern, open, close);
builder.append(pattern+(constants.size()-1));
}else if(c == close){
break;

View file

@ -2,39 +2,19 @@ package be.jeffcheasey88.peeratcode.parser.java;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cutter;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class CleanerTest {
@Test
void cutter(){
Cutter generic = new Cutter(){
@Override
public String execute(String value, List<String> constants, String pattern){
String result = "";
for(int i = 0; i < value.length(); i++){
char c = value.charAt(i);
if(c == '<'){
i+=cutOpenable(value.substring(i+1), constants, pattern, '<', '>');
result+=pattern+(constants.size()-1);
}else{
result+=c;
}
}
return result;
}
};
List<String> list = new ArrayList<>();
String result = generic.execute("test<Test<Lol>>", list, "$TEST");
assertEquals(list.size(), 2);
assertEquals(list.get(0), "Lol");
assertEquals(list.get(1), "Test$TEST0");
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
String result = cleaner.clean("test<Test<Lol>>");
assertEquals("test$TEST1", result);
assertEquals("Lol", cleaner.getConstant("$TEST0"));
}
}