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.
111 lines
3.4 KiB
111 lines
3.4 KiB
package com.fr.plugin.third.party.jsdjjed.schedule.utils; |
|
|
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.script.Calculator; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.UtilEvalError; |
|
|
|
import java.text.SimpleDateFormat; |
|
import java.util.Date; |
|
|
|
/** |
|
* @Author: xx |
|
* @Date: 2020/9/13 11:44 |
|
*/ |
|
public class PlaceholderResolver { |
|
|
|
public static final String TEXT = "这是一个包含日期:${today()}和当前时间:${now()} 的两个公式文本"; |
|
|
|
/** |
|
* 默认前缀占位符 |
|
*/ |
|
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${"; |
|
|
|
/** |
|
* 默认后缀占位符 |
|
*/ |
|
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}"; |
|
|
|
/** |
|
* 默认单例解析器 |
|
*/ |
|
private static PlaceholderResolver defaultResolver = new PlaceholderResolver(); |
|
|
|
/** |
|
* 占位符前缀 |
|
*/ |
|
private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX; |
|
|
|
/** |
|
* 占位符后缀 |
|
*/ |
|
private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX; |
|
|
|
|
|
private PlaceholderResolver() { |
|
} |
|
|
|
private PlaceholderResolver(String placeholderPrefix, String placeholderSuffix) { |
|
this.placeholderPrefix = placeholderPrefix; |
|
this.placeholderSuffix = placeholderSuffix; |
|
} |
|
|
|
/** |
|
* 获取默认的占位符解析器,即占位符前缀为"${", 后缀为"}" |
|
* |
|
* @return |
|
*/ |
|
public static PlaceholderResolver getDefaultResolver() { |
|
return defaultResolver; |
|
} |
|
|
|
public static PlaceholderResolver getResolver(String placeholderPrefix, String placeholderSuffix) { |
|
return new PlaceholderResolver(placeholderPrefix, placeholderSuffix); |
|
} |
|
|
|
/** |
|
* 解析带有指定占位符的模板字符串,默认占位符为前缀:${ 后缀:} |
|
* 如:"这是一个包含日期:${today()}和当前时间:${sum(1,2)} 的两个公式文本" |
|
* |
|
* @param content 要解析的带有占位符的模板字符串 |
|
* @return |
|
*/ |
|
public String resolve(String content) { |
|
int start = content.indexOf(this.placeholderPrefix); |
|
if (start == -1) { |
|
return content; |
|
} |
|
|
|
int valueIndex = 0; |
|
StringBuilder resultContent = new StringBuilder(content); |
|
while (start != -1) { |
|
int end = resultContent.indexOf(this.placeholderSuffix); |
|
String place = content.substring(start, end + this.placeholderSuffix.length()); |
|
String formula = place.replace(this.placeholderPrefix, "").replace(this.placeholderSuffix, ""); |
|
Calculator calculator = Calculator.createCalculator(); |
|
String value = StringUtils.EMPTY; |
|
try { |
|
Object o = calculator.evalValue(formula); |
|
|
|
if (o!=null){ |
|
if (o instanceof Date){ |
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
value = format.format(o); |
|
}else { |
|
value =o.toString(); |
|
} |
|
} |
|
|
|
} catch (UtilEvalError utilEvalError) { |
|
FineLoggerFactory.getLogger().error(utilEvalError,"JSD:>>calculator formula in text error!",utilEvalError.getMessage()); |
|
} |
|
resultContent.replace(start, end + this.placeholderSuffix.length(), value); |
|
start = resultContent.indexOf(this.placeholderPrefix, start + value.length()); |
|
} |
|
return resultContent.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
}
|
|
|