com.dexcoder.dal.batis.xml.TextSqlNode Maven / Gradle / Ivy
package com.dexcoder.dal.batis.xml;
import java.util.regex.Pattern;
import com.dexcoder.dal.batis.build.DynamicContext;
import com.dexcoder.dal.exceptions.JdbcAssistantException;
import com.dexcoder.dal.handler.GenericTokenParser;
import com.dexcoder.dal.handler.TokenHandler;
/**
* Created by liyd on 2015-11-26.
*/
public class TextSqlNode implements SqlNode {
private String text;
private Pattern injectionFilter;
public TextSqlNode(String text) {
this(text, null);
}
public TextSqlNode(String text, Pattern injectionFilter) {
this.text = text;
this.injectionFilter = injectionFilter;
}
public boolean isDynamic() {
DynamicCheckerTokenParser checker = new DynamicCheckerTokenParser();
GenericTokenParser parser = createParser(checker);
parser.parse(text);
return checker.isDynamic();
}
public boolean apply(DynamicContext context) {
GenericTokenParser parser = createParser(new BindingTokenParser(context, injectionFilter));
context.appendSql(parser.parse(text));
return true;
}
private GenericTokenParser createParser(TokenHandler handler) {
return new GenericTokenParser("${", "}", handler);
}
private static class BindingTokenParser implements TokenHandler {
private DynamicContext context;
private Pattern injectionFilter;
public BindingTokenParser(DynamicContext context, Pattern injectionFilter) {
this.context = context;
this.injectionFilter = injectionFilter;
}
public String handleToken(String content) {
Object parameter = context.getBindings().get("_parameter");
if (parameter == null) {
context.getBindings().put("value", null);
}
// else if (SimpleTypeRegistry.isSimpleType(parameter.getClass())) {
// context.getBindings().put("value", parameter);
// }
// Object value = OgnlCache.getValue(content, context.getBindings());
Object value = null;
String srtValue = (value == null ? "" : String.valueOf(value)); // issue #274 return "" instead of "null"
checkInjection(srtValue);
return srtValue;
}
private void checkInjection(String value) {
if (injectionFilter != null && !injectionFilter.matcher(value).matches()) {
throw new JdbcAssistantException("Invalid input. Please conform to regex" + injectionFilter.pattern());
}
}
}
private static class DynamicCheckerTokenParser implements TokenHandler {
private boolean isDynamic;
public DynamicCheckerTokenParser() {
// Prevent Synthetic Access
}
public boolean isDynamic() {
return isDynamic;
}
public String handleToken(String content) {
this.isDynamic = true;
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy