You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.5 KiB
129 lines
3.5 KiB
package com.fr.plugin.file.submit.oss.fun; |
|
|
|
import com.fr.base.BaseFormula; |
|
import com.fanruan.api.util.GeneralKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.cal.FormulaKit; |
|
import com.fanruan.api.util.StringKit; |
|
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 = GeneralKit.objectToString(value); |
|
if (FormulaKit.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 GeneralKit.objectToString(value); |
|
} |
|
} |
|
|
|
public String getResult(CalculatorProvider cal){ |
|
Object result = value; |
|
if (type == Type.FORMULA) { |
|
try { |
|
result = ((BaseFormula) value).evalValue(cal); |
|
} catch (UtilEvalError u) { |
|
LogKit.error(u.getMessage(), u); |
|
} |
|
} |
|
return GeneralKit.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", StringKit.EMPTY)); |
|
} else { |
|
value = reader.getAttrAsString("value", StringKit.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", GeneralKit.objectToString(value)); |
|
} |
|
writer.end(); |
|
} |
|
|
|
@Override |
|
public Object clone() throws CloneNotSupportedException { |
|
return super.clone(); |
|
} |
|
|
|
public String toString() { |
|
return value == null ? StringKit.EMPTY : GeneralKit.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; |
|
} |
|
} |
|
}
|
|
|