|
|
|
package com.fr.plugin.file.submit.oss.fun;
|
|
|
|
|
|
|
|
import com.fr.base.BaseFormula;
|
|
|
|
import com.fr.general.GeneralUtils;
|
|
|
|
import com.fr.log.FineLoggerFactory;
|
|
|
|
import com.fr.stable.CommonUtils;
|
|
|
|
import com.fr.stable.StringUtils;
|
|
|
|
import com.fr.stable.UtilEvalError;
|
|
|
|
import com.fr.stable.script.CalculatorProvider;
|
|
|
|
import com.fr.stable.xml.XMLPrintWriter;
|
|
|
|
import com.fr.stable.xml.XMLable;
|
|
|
|
import com.fr.stable.xml.XMLableReader;
|
|
|
|
|
|
|
|
public class OssVariableValue implements XMLable {
|
|
|
|
|
|
|
|
public static final String XML_TAG = "OssVariableValue";
|
|
|
|
|
|
|
|
private Type type;
|
|
|
|
|
|
|
|
private Object value;
|
|
|
|
|
|
|
|
public OssVariableValue() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public OssVariableValue(Object value) {
|
|
|
|
if (value instanceof BaseFormula) {
|
|
|
|
type = Type.FORMULA;
|
|
|
|
this.value = value;
|
|
|
|
} else {
|
|
|
|
String content = GeneralUtils.objectToString(value);
|
|
|
|
if (CommonUtils.canBeFormula(content)) {
|
|
|
|
type = Type.FORMULA;
|
|
|
|
this.value = BaseFormula.createFormulaBuilder().build(content);
|
|
|
|
} else {
|
|
|
|
type = Type.PLAIN;
|
|
|
|
this.value = content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPlainText() {
|
|
|
|
if (type == Type.FORMULA) {
|
|
|
|
return ((BaseFormula)value).getPureContent();
|
|
|
|
} else {
|
|
|
|
return GeneralUtils.objectToString(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getResult(CalculatorProvider cal) {
|
|
|
|
Object result = value;
|
|
|
|
if (type == Type.FORMULA) {
|
|
|
|
try {
|
|
|
|
result = ((BaseFormula) value).evalValue(cal);
|
|
|
|
} catch (UtilEvalError u) {
|
|
|
|
FineLoggerFactory.getLogger().error(u.getMessage(), u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GeneralUtils.objectToString(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readXML(XMLableReader reader) {
|
|
|
|
if (reader.isChildNode()) {
|
|
|
|
String tagName = reader.getTagName();
|
|
|
|
if ("Attr".equals(tagName)) {
|
|
|
|
type = Type.parse(reader.getAttrAsInt("type", 0));
|
|
|
|
if (type == Type.FORMULA) {
|
|
|
|
value = BaseFormula.createFormulaBuilder().build(reader.getAttrAsString("value", StringUtils.EMPTY));
|
|
|
|
} else {
|
|
|
|
value = reader.getAttrAsString("value", StringUtils.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeXML(XMLPrintWriter writer) {
|
|
|
|
writer.startTAG("Attr");
|
|
|
|
if (value instanceof BaseFormula) {
|
|
|
|
writer.attr("type", Type.FORMULA.getIndex()).attr("value", ((BaseFormula) value).getPureContent());
|
|
|
|
} else {
|
|
|
|
writer.attr("type", Type.PLAIN.getIndex()).attr("value", GeneralUtils.objectToString(value));
|
|
|
|
}
|
|
|
|
writer.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object clone() throws CloneNotSupportedException {
|
|
|
|
return super.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return value == null ? StringUtils.EMPTY : GeneralUtils.objectToString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum Type {
|
|
|
|
|
|
|
|
PLAIN(0), FORMULA(1);
|
|
|
|
|
|
|
|
private int index;
|
|
|
|
|
|
|
|
Type(int index) {
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getIndex() {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Type parse(int index) {
|
|
|
|
for (Type type : values()) {
|
|
|
|
if (type.index == index) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Type.PLAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|