com.alogic.xscript.plugins.Validate Maven / Gradle / Ivy
package com.alogic.xscript.plugins;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import com.alogic.validator.Validator;
import com.alogic.validator.ValidatorFactory;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
/**
* 验证是否取值是否合法
* @author yyduan
* @since 1.6.12.11 [20181206 duanyy]
*/
public class Validate extends AbstractLogiclet{
protected String $id;
protected String validatorId = null;
protected String $value;
public Validate(String tag, Logiclet p) {
super(tag, p);
}
@Override
public void configure(Properties p) {
super.configure(p);
$id = PropertiesConstants.getRaw(p,"id","$" + this.getXmlTag());
$value = PropertiesConstants.getRaw(p,"value","");
validatorId = getValidatorId(p);
}
protected String getValidatorId(Properties p){
return PropertiesConstants.getString(p,"type",validatorId,true);
}
@Override
protected void onExecute(XsObject root,XsObject current, LogicletContext ctx,
ExecuteWatcher watcher) {
String id = PropertiesConstants.transform(ctx, $id, "$" + this.getXmlTag());
if (StringUtils.isNotEmpty(id)){
String value = PropertiesConstants.transform(ctx, $value, "");
boolean sucessful = true;
if (StringUtils.isNotEmpty(value)){
Validator validator = ValidatorFactory.getDefault().load(validatorId, true);
if (validator != null){
sucessful = validator.check(value, false);
if (!sucessful){
ctx.SetValue(id + ".code", validator.getCode());
ctx.SetValue(id + ".reason", validator.getMessage());
}
}
}
ctx.SetValue(id, BooleanUtils.toStringTrueFalse(sucessful));
}
}
}