扩展我的评论,这是我设想的那种解决方案:
class HaxlEngine
{
static boolean isANSI(final char c)
{
return c <= 0x7F;
}
public static Set<List<String>> doSum(String formula)
{
JexlEngine engine = new JexlEngine();
final Map<Character, Character> mappedChars = new HashMap<>();
char ansiCounter = 'A';
for(final char c : formula.toCharArray())
{
if(!isANSI(c))
{
char newValue;
do
{
newValue = ansiCounter++;
}
while(formula.contains(String.valueOf(newValue)));
mappedChars.put(c, newValue);
formula = formula.replace(c, newValue);
}
}
final Set<List<String>> exactlyWhatINeed = engine.createScript(formula).getVariables();
for(final List<String> list : exactlyWhatINeed)
{
for(String str : list)
{
for(final Map.Entry<Character, Character> entry : mappedChars.entrySet())
{
str = str.replace(entry.getValue(), entry.getKey());
}
}
}
return exactlyWhatINeed;
}
}
这将检查您给它的公式中的每个字符,并将任何不是有效 ANSI 字符的字符替换为标准A
、B
、C
等。我无法测试实际的引擎部件,但是替换似乎在我的测试中有效(见下文),所以我相信当您插入库时它会起作用。
static boolean isANSI(final char c)
{
return c <= 0x7F;
}
static String testReplaceNonANSI(String maybeANSI)
{
final Map<Character, Character> mappedChars = new HashMap<>();
char ansiCounter = 'A';
for(final char c : maybeANSI.toCharArray())
{
if(!isANSI(c))
{
char newValue;
do
{
newValue = ansiCounter++;
}
while(maybeANSI.contains(String.valueOf(newValue)));
mappedChars.put(c, newValue);
maybeANSI = maybeANSI.replace(c, newValue);
}
}
return maybeANSI;
}
public static void main(String[] args)
{
String a = "(A+B/C) * X";
String b = "(О+А/C) * П";
System.out.println(testReplaceNonANSI(a)); //(A+B/C) * X
System.out.println(testReplaceNonANSI(b)); //(A+B/C) * C
}