java处理过的文本直接返回到报表上会提示空指针异常
报表支持自定义函数
自定义regularFun 格式化函数
public class RegularFun extends AbstractFunction {
@Override
public String getName() {
return "regularFun";
}
@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
// 参数用此方法获取 数字也这么获取 然后自己转
String str = ExpressUtil.getArgString(arg1, env);
//自定义正则表达式处理工具类
str = ExpressionConversionUtils.getPlainText(str);
return AviatorRuntimeJavaType.valueOf(str);
}
}
注册自定义函数regularFun
@Component
public class JmExpressCustomImpl implements IJmExpressCustom {
@Override
public void addFunction(AviatorEvaluatorInstance instance) {
RegularFun fun = new RegularFun();
instance.addFunction(fun.getName(), fun);
//OtherFun fun1 = new OtherFun ();
//instance.addFunction(fun1.getName(), fun1);
}
}
自定义表达式处理类
public class ExpressionConversionUtils {
public static String getPlainText(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
//htmlStr = htmlStr.replaceAll("</p>","\n");
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签
//htmlStr=htmlStr.replaceAll(" ","");
htmlStr = StringEscapeUtils.unescapeHtml(htmlStr);//html标签转义为字符
return htmlStr; //返回文本字符串
}