Update CleanerPool, no more interface and constants everywhere
This commit is contained in:
parent
4befbd7147
commit
d2fd5aeff1
2 changed files with 61 additions and 91 deletions
|
@ -2,88 +2,78 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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 List<Cleaner> cleaners;
|
||||||
private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_";
|
|
||||||
private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_";
|
|
||||||
|
|
||||||
private List<String> constants;
|
public CleanerPool(List<Cleaner> cleaners){
|
||||||
|
this.cleaners = new ArrayList<>(cleaners);
|
||||||
private CleanerPool(){
|
|
||||||
this.constants = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String clean(String statement){
|
public String clean(String value){
|
||||||
char[] chars = statement.toCharArray();
|
for(Cleaner cleaner : this.cleaners) value = cleaner.clean(value);
|
||||||
StringBuilder builder = new StringBuilder();
|
return value;
|
||||||
for(int i = 0; i < chars.length; i++){
|
}
|
||||||
char current = chars[i];
|
|
||||||
if(current== '"'){
|
public boolean isConstant(String value){
|
||||||
int constantPos = this.constants.size();
|
for(Cleaner cleaner : this.cleaners) if(value.startsWith(cleaner.getPattern())) return true;
|
||||||
String constant = cutConstant(chars, i);
|
return false;
|
||||||
i+=constant.length()+1;
|
}
|
||||||
builder.append(CONSTANT_REPLACER_STRING+constantPos);
|
|
||||||
this.constants.add(constant);
|
public String getConstant(String value){
|
||||||
}else{
|
for(Cleaner cleaner : this.cleaners){
|
||||||
builder.append(current);
|
if(value.startsWith(cleaner.getPattern())){
|
||||||
|
return cleaner.getConstant(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Cleaner{
|
||||||
|
|
||||||
for(String s : constants){
|
private String pattern;
|
||||||
System.out.println("CONSTANT="+s);
|
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();
|
StringBuilder builder = new StringBuilder();
|
||||||
System.out.println("cutOpenable "+value);
|
|
||||||
|
|
||||||
for(int i = 0; i < value.length(); i++){
|
for(int i = 0; i < value.length(); i++){
|
||||||
char c = value.charAt(i);
|
char c = value.charAt(i);
|
||||||
if(c == open){
|
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));
|
builder.append(pattern+(constants.size()-1));
|
||||||
}else if(c == close){
|
}else if(c == close){
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -2,39 +2,19 @@ package be.jeffcheasey88.peeratcode.parser.java;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
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 {
|
public class CleanerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cutter(){
|
void cutter(){
|
||||||
Cutter generic = new Cutter(){
|
CleanerPool cleaner = new CleanerPool(Arrays.asList(new Cleaner("$TEST",'<','>')));
|
||||||
@Override
|
String result = cleaner.clean("test<Test<Lol>>");
|
||||||
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");
|
|
||||||
assertEquals("test$TEST1", result);
|
assertEquals("test$TEST1", result);
|
||||||
|
assertEquals("Lol", cleaner.getConstant("$TEST0"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue