package io.ebeaninternal.server.deploy;
import java.util.Set;
public abstract class DeployParser {
private static final char SINGLE_QUOTE = '\'';
private static final char COLON = ':';
private static final char UNDERSCORE = '_';
private static final char OPEN_SQUARE_BRACKET = '[';
private static final char CLOSE_SQUARE_BRACKET = ']';
private static final char DOUBLE_QUOTE = '\"';
private static final char BACK_QUOTE = '`';
private static final char PERIOD = '.';
private static final char OPEN_BRACKET = '(';
boolean encrypted;
private String source;
private StringBuilder sb;
private int sourceLength;
private int pos;
String priorWord;
String word;
private char wordTerminator;
private StringBuilder wordBuffer;
protected abstract String convertWord();
public abstract String getDeployWord(String expression);
public abstract Set<String> getIncludes();
public void setEncrypted(boolean encrypted) {
this.encrypted = encrypted;
}
public String parse(String source) {
if (source == null) {
return null;
}
pos = -1;
this.source = source;
this.sourceLength = source.length();
this.sb = new StringBuilder(source.length() + 20);
while (nextWord()) {
if (skipWordConvert()) {
sb.append(word);
priorWord = word;
} else {
String deployWord = convertWord();
sb.append(deployWord);
priorWord = deployWord;
}
if (pos < sourceLength) {
if (wordTerminator != OPEN_BRACKET) {
sb.append(wordTerminator);
}
if (wordTerminator == SINGLE_QUOTE) {
readLiteral();
}
}
}
return sb.toString();
}
boolean skipWordConvert() {
return false;
}
private boolean nextWord() {
if (!findWordStart()) {
return false;
}
wordBuffer = new StringBuilder();
wordBuffer.append(source.charAt(pos));
while (++pos < sourceLength) {
char ch = source.charAt(pos);
if (isWordPart(ch)) {
wordBuffer.append(ch);
} else {
wordTerminator = ch;
break;
}
}
word = wordBuffer.toString();
return true;
}
private boolean findWordStart() {
while (++pos < sourceLength) {
char ch = source.charAt(pos);
if (ch == SINGLE_QUOTE) {
sb.append(ch);
readLiteral();
} else if (ch == COLON) {
sb.append(ch);
readNamedParameter();
} else if (isWordStart(ch)) {
return true;
} else {
sb.append(ch);
}
}
return false;
}
private void readLiteral() {
while (++pos < sourceLength) {
char ch = source.charAt(pos);
sb.append(ch);
if (ch == SINGLE_QUOTE) {
break;
}
}
}
private void readNamedParameter() {
while (++pos < sourceLength) {
char ch = source.charAt(pos);
sb.append(ch);
if (Character.isWhitespace(ch)) {
break;
} else if (ch == ',') {
break;
}
}
}
private boolean isWordPart(char ch) {
if (ch == OPEN_BRACKET) {
wordBuffer.append(ch);
return false;
}
return Character.isLetterOrDigit(ch) || ch == UNDERSCORE || ch == PERIOD || ch == DOUBLE_QUOTE || ch == CLOSE_SQUARE_BRACKET || ch == BACK_QUOTE;
}
private boolean isWordStart(char ch) {
return Character.isLetter(ch) || ch == UNDERSCORE || ch == DOUBLE_QUOTE || ch == OPEN_SQUARE_BRACKET || ch == BACK_QUOTE;
}
}