diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractIntersectionFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractIntersectionFunction.java index f0e1f7d30b0309f401b806e8835f6994be804e8d..ce2d0f503a3a27699a65e446d8208d0af578e275 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractIntersectionFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractIntersectionFunction.java @@ -4,6 +4,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 交集抽象函数 @@ -22,18 +23,18 @@ public abstract class AbstractIntersectionFunction extends AbstractScriptFunc Object... parameters) throws ScriptException { try{ if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(checkParameters(parameters, 2)){ T t1 = (T) parameters[0]; T t2 = (T) parameters[1]; return intersect(t1,t2); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch(ScriptException e){ throw e; }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractSubtractFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractSubtractFunction.java index 8882e3c53ce3b178d1e429a1d4c1d211766d04af..fe9b79d4441a00a3e7c704d07fc1c01c51166758 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractSubtractFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractSubtractFunction.java @@ -4,6 +4,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 差集抽象函数 @@ -22,18 +23,18 @@ public abstract class AbstractSubtractFunction extends AbstractScriptFunction Object... parameters) throws ScriptException { try{ if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(checkParameters(parameters, 2)){ T t1 = (T) parameters[0]; T t2 = (T) parameters[1]; return subtract(t1,t2); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch(ScriptException e){ throw e; }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractUnionFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractUnionFunction.java index b70cb648d9a8b41481516547f2ee82c1fec722aa..e373ce0f54285a0fe62431f087559be74c50b245 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractUnionFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractUnionFunction.java @@ -4,6 +4,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 并集抽象函数 @@ -21,18 +22,18 @@ public abstract class AbstractUnionFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try{ if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(checkParameters(parameters, 2)){ T t1 = (T) parameters[0]; T t2 = (T) parameters[1]; return unite(t1,t2); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch(ScriptException e){ throw e; }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractXorFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractXorFunction.java index 1099f6e4617c5bb379719e1787d0b62f76a5f822..11a80260bf5dcc05b218189c806eff2065ce3f89 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractXorFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/AbstractXorFunction.java @@ -4,6 +4,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 相对差(异或)抽象函数 @@ -22,18 +23,18 @@ public abstract class AbstractXorFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try{ if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(checkParameters(parameters, 2)){ T t1 = (T) parameters[0]; T t2 = (T) parameters[1]; return xor(t1,t2); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch(ScriptException e){ throw e; }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/CopyFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/CopyFunction.java index 999ae52ffc8b6a6481435e92d56ae9ab2d1de235..eaea3749b1502a5f300d90cc46e0d22caf43e593 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/CopyFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/CopyFunction.java @@ -7,6 +7,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class CopyFunction extends AbstractScriptFunction { @@ -23,19 +24,19 @@ public class CopyFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("copy函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (checkParameters(parameters, 1)) { List list = (List) parameters[0]; List newList = new ArrayList(); newList.addAll(list); return newList; } else { - throw new ScriptException("copy函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("copy函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/DpKnapsackFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/DpKnapsackFunction.java index 401913f7be5d6c73db31b98d7274210984f6517a..1070e938221595fb8ea013e43c2b0262752f3329 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/DpKnapsackFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/DpKnapsackFunction.java @@ -12,6 +12,7 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractDpKnapsackFunction; import org.tinygroup.tinyscript.interpret.LambdaFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class DpKnapsackFunction extends AbstractDpKnapsackFunction { @@ -23,7 +24,7 @@ public class DpKnapsackFunction extends AbstractDpKnapsackFunction { @Override public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { if (parameters == null || parameters.length <= 3) { - throw new ScriptException("dpKnapsack参数 错误!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", "dpKnapsack")); } List result = null; @@ -44,7 +45,7 @@ public class DpKnapsackFunction extends AbstractDpKnapsackFunction { result = dpKnapsackResult(weight, (double[]) convertToArray(value, double.class), bagSize, maxCount); } catch (Exception e) { - throw new ScriptException("dpKnapsack函数执行发生异常:", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", "dpKnapsack"), e); } } else if (checkParameters(parameters, 5)) {// 混合背包和多重背包 try { @@ -57,7 +58,7 @@ public class DpKnapsackFunction extends AbstractDpKnapsackFunction { result = dpKnapsackResult(weight, (double[]) convertToArray(value, double.class), bagSize, maxCount); } catch (Exception e) { - throw new ScriptException("dpKnapsack函数执行发生异常:", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", "dpKnapsack"), e); } } else if (checkParameters(parameters, 6)) {// 用户定义规则 try { @@ -73,7 +74,7 @@ public class DpKnapsackFunction extends AbstractDpKnapsackFunction { result = dpKnapsackResult(weight, (double[]) convertToArray(value, double.class), bagSize, maxCount, list, context, lambdaFunction); } catch (Exception e) { - throw new ScriptException("dpKnapsack函数执行发生异常:", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", "dpKnapsack"), e); } } return getLastResult(result, list); @@ -131,7 +132,8 @@ public class DpKnapsackFunction extends AbstractDpKnapsackFunction { obj = Array.newInstance(clazz, Array.getLength(array) + 1); System.arraycopy(array, 0, obj, 1, Array.getLength(array)); } else { - throw new ScriptException("转换数组发生异常"); + throw new ScriptException( + ResourceBundleUtil.getDefaultMessage("function.parameter.error", "convertToArray")); } return obj; } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/FillFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/FillFunction.java index 5a533af54ee8d4208aa3e5da2a39deb10fab7e51..3e7f20db768b326571bd9c0ae7b8740ac5909678 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/FillFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/list/FillFunction.java @@ -7,6 +7,7 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.expression.ExpressionUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class FillFunction extends AbstractScriptFunction { @@ -23,7 +24,7 @@ public class FillFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("fill函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (parameters.length == 2 && parameters[0] != null) { List list = (List) parameters[0]; for(int i=0;i list = (List) obj; Comparator c = getComparator(rule, segment, list); - if(c==null){ - throw new ScriptException(String.format("解析排序规则[%s]失败", rule)); + if (c == null) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.sort.rule.error", rule)); } Collections.sort(list, c); return list; - }catch(ScriptException e){ + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("序列按规则[%s]排序发生异常:", rule),e); + } catch (Exception e) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.sort.error", rule), e); } } @SuppressWarnings("unchecked") - protected Comparator createComparator(String rule, ScriptSegment segment, - Object source) throws Exception { + protected Comparator createComparator(String rule, ScriptSegment segment, Object source) throws Exception { List rules = new ArrayList(); - try{ - if(matchFieldRule(rule)){ + try { + if (matchFieldRule(rule)) { String[] ss = rule.trim().split(","); - for(String s:ss){ + for (String s : ss) { FieldSortRule fieldSortRule = parse(s); - if(fieldSortRule!=null){ - rules.add(fieldSortRule); + if (fieldSortRule != null) { + rules.add(fieldSortRule); } } FieldSortComparator comparator = new FieldSortComparator(); comparator.setFieldSortRuleList(rules); return comparator; } - }catch(ScriptException e){ + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("序列按规则[%s]排序发生异常:", rule),e); + } catch (Exception e) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.sort.error", rule), e); } return null; } - + /** * 解析字段信息 + * * @param s * @return - * @throws Exception + * @throws Exception */ - private FieldSortRule parse(String s) throws Exception{ - FieldSortRule rule=null; - if(!StringUtil.isEmpty(s)){ - String [] ss = s.trim().split("\\s+"); - rule = new FieldSortRule(); - if(ss.length>=2 && ss[1].equalsIgnoreCase("desc")){ - rule.setAsc(false); - }else{ - rule.setAsc(true); - } - rule.setContextName(DEFAULT_CONTEXT_NAME); - rule.setSegment(createSegment(rule,ss[0])); + private FieldSortRule parse(String s) throws Exception { + FieldSortRule rule = null; + if (!StringUtil.isEmpty(s)) { + String[] ss = s.trim().split("\\s+"); + rule = new FieldSortRule(); + if (ss.length >= 2 && ss[1].equalsIgnoreCase("desc")) { + rule.setAsc(false); + } else { + rule.setAsc(true); + } + rule.setContextName(DEFAULT_CONTEXT_NAME); + rule.setSegment(createSegment(rule, ss[0])); } - + return rule; } - + /** * return object.id; + * * @param rule * @param text * @return */ - private String createSegment(FieldSortRule rule,String text){ + private String createSegment(FieldSortRule rule, String text) { StringBuilder sb = new StringBuilder(); sb.append("return ").append(DEFAULT_CONTEXT_NAME); sb.append(".").append(text); @@ -104,12 +110,10 @@ public class SortFunction extends AbstractSortFunction{ } @SuppressWarnings("unchecked") - protected Object sortByLambda(Object sortObject, Comparator c) - throws Exception { + protected Object sortByLambda(Object sortObject, Comparator c) throws Exception { List list = (List) sortObject; Collections.sort(list, c); return list; } - } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/AllPermutationFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/AllPermutationFunction.java index 3261c4cb981995595eaacb212b92c3be4420470d..aa737185da2666c17fd609feffe146a544c9f999 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/AllPermutationFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/AllPermutationFunction.java @@ -10,10 +10,12 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.interpret.LambdaFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptResult; /** * 全排列函数 + * * @author yancheng11334 * */ @@ -22,83 +24,86 @@ public class AllPermutationFunction extends AbstractScriptFunction { public String getNames() { return "permuteAll"; } - + public String getBindingTypes() { return "java.util.Collection"; } @SuppressWarnings({ "unchecked", "rawtypes" }) - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ - if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); - }else if(checkParameters(parameters, 2)){ + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { + if (parameters == null || parameters.length == 0) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { Collection c = (Collection) parameters[0]; LambdaFunction function = (LambdaFunction) parameters[1]; Object[] source = new ArrayList(c).toArray(); - return getAllPermutations(source,function,context,source.length); - }else if(checkParameters(parameters, 3)){ + return getAllPermutations(source, function, context, source.length); + } else if (checkParameters(parameters, 3)) { Collection c = (Collection) parameters[0]; Integer length = (Integer) parameters[1]; - if(length<1 || length>c.size()){ - throw new ScriptException(String.format("%s函数的参数格式不正确:length超出正常范围", getNames(),length)); + if (length < 1 || length > c.size()) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.length.error", getNames())); } LambdaFunction function = (LambdaFunction) parameters[2]; Object[] source = new ArrayList(c).toArray(); - return getAllPermutations(source,function,context,length); + return getAllPermutations(source, function, context, length); } - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); - }catch(ScriptException e){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected ScriptResult getAllPermutations(Object[] source,LambdaFunction function,ScriptContext context,int length) throws Exception { - getCombinations(source,function,context,length); + + protected ScriptResult getAllPermutations(Object[] source, LambdaFunction function, ScriptContext context, + int length) throws Exception { + getCombinations(source, function, context, length); return ScriptResult.VOID_RESULT; } - protected void getPermutations(LambdaFunction function,ScriptContext context, - Object[] source, int start, int len) throws Exception { + protected void getPermutations(LambdaFunction function, ScriptContext context, Object[] source, int start, int len) + throws Exception { if (start == len) { // 未来扩展表达式或lambda函数 function.execute(context, Arrays.asList(source)); } for (int i = start; i != len; i++) { swap(source, i, start); - getPermutations(function,context, source, start + 1, len); + getPermutations(function, context, source, start + 1, len); swap(source, i, start); } } - protected void swap(Object[] source, int i, int j) { + protected void swap(Object[] source, int i, int j) { Object t; t = source[i]; source[i] = source[j]; source[j] = t; } - - protected ScriptResult getCombinations(Object[] source,LambdaFunction function,ScriptContext context,int length) throws Exception{ + + protected ScriptResult getCombinations(Object[] source, LambdaFunction function, ScriptContext context, int length) + throws Exception { List temp = new ArrayList(); - getCombinations(function,context,source,temp,0,length); + getCombinations(function, context, source, temp, 0, length); return ScriptResult.VOID_RESULT; } - - protected void getCombinations(LambdaFunction function,ScriptContext context,Object[] source,List temp,int start,int len) throws Exception{ - if(len==0){ - //执行代码块 - getPermutations(function,context,temp.toArray(),0,temp.size()); - return ; + + protected void getCombinations(LambdaFunction function, ScriptContext context, Object[] source, List temp, + int start, int len) throws Exception { + if (len == 0) { + // 执行代码块 + getPermutations(function, context, temp.toArray(), 0, temp.size()); + return; } - if(start==source.length){ - return ; + if (start == source.length) { + return; } temp.add(source[start]); - getCombinations(function,context,source,temp,start+1,len-1); - temp.remove(temp.size()-1); - getCombinations(function,context,source,temp,start+1,len); + getCombinations(function, context, source, temp, start + 1, len - 1); + temp.remove(temp.size() - 1); + getCombinations(function, context, source, temp, start + 1, len); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/CombinationFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/CombinationFunction.java index ad1f4fae8ec69c5cb4b497a133584f66d499e729..914523dfdb6b4379e571da902dcfcf03d081efce 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/CombinationFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/CombinationFunction.java @@ -9,10 +9,12 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.interpret.LambdaFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptResult; /** * 组合函数 + * * @author yancheng11334 * */ @@ -21,70 +23,73 @@ public class CombinationFunction extends AbstractScriptFunction { public String getNames() { return "combine"; } - + public String getBindingTypes() { return "java.util.Collection"; } @SuppressWarnings({ "unchecked", "rawtypes" }) - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ - if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); - }else if(checkParameters(parameters, 2)){ - //默认实现Cmm(m=n) + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { + if (parameters == null || parameters.length == 0) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { + // 默认实现Cmm(m=n) Collection c = (Collection) parameters[0]; LambdaFunction function = (LambdaFunction) parameters[1]; Object[] source = new ArrayList(c).toArray(); - return getCombinations(source,function,context); - }else if(checkParameters(parameters, 3)){ - //实现Cmn(用户需要指定n) + return getCombinations(source, function, context); + } else if (checkParameters(parameters, 3)) { + // 实现Cmn(用户需要指定n) Collection c = (Collection) parameters[0]; Integer length = (Integer) parameters[1]; - if(length<1 || length>c.size()){ - throw new ScriptException(String.format("%s函数的参数格式不正确:length超出正常范围", getNames(),length)); + if (length < 1 || length > c.size()) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.length.error", getNames())); } LambdaFunction function = (LambdaFunction) parameters[2]; Object[] source = new ArrayList(c).toArray(); - return getCombinations(source,function,context,length); + return getCombinations(source, function, context, length); } - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); - }catch(ScriptException e){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected ScriptResult getCombinations(Object[] source,LambdaFunction function,ScriptContext context,int len) throws Exception{ + + protected ScriptResult getCombinations(Object[] source, LambdaFunction function, ScriptContext context, int len) + throws Exception { List temp = new ArrayList(); - getCombinations(function,context,source,temp,0,len); + getCombinations(function, context, source, temp, 0, len); return ScriptResult.VOID_RESULT; } - - protected ScriptResult getCombinations(Object[] source,LambdaFunction function,ScriptContext context) throws Exception{ + + protected ScriptResult getCombinations(Object[] source, LambdaFunction function, ScriptContext context) + throws Exception { List temp = new ArrayList(); - int len = source.length+1; - for(int i = 1 ; i != len ; i ++){ - getCombinations(function,context,source,temp,0,i); + int len = source.length + 1; + for (int i = 1; i != len; i++) { + getCombinations(function, context, source, temp, 0, i); } return ScriptResult.VOID_RESULT; } - - protected void getCombinations(LambdaFunction function,ScriptContext context,Object[] source,List temp,int start,int len) throws Exception{ - if(len==0){ - //执行代码块 - function.execute(context, temp); - return ; + + protected void getCombinations(LambdaFunction function, ScriptContext context, Object[] source, List temp, + int start, int len) throws Exception { + if (len == 0) { + // 执行代码块 + function.execute(context, temp); + return; } - if(start==source.length){ - return ; + if (start == source.length) { + return; } temp.add(source[start]); - getCombinations(function,context,source,temp,start+1,len-1); - temp.remove(temp.size()-1); - getCombinations(function,context,source,temp,start+1,len); + getCombinations(function, context, source, temp, start + 1, len - 1); + temp.remove(temp.size() - 1); + getCombinations(function, context, source, temp, start + 1, len); } } diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/PermutationFunction.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/PermutationFunction.java index cb1a467ba58943092587c8b0cdd80a77955e527c..eac2ec82fd02260972aa0a7fea7232d27ded622c 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/PermutationFunction.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/function/math/PermutationFunction.java @@ -9,10 +9,12 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.interpret.LambdaFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptResult; /** * 排列函数 + * * @author yancheng11334 * */ @@ -21,47 +23,48 @@ public class PermutationFunction extends AbstractScriptFunction { public String getNames() { return "permute"; } - + public String getBindingTypes() { return "java.util.Collection"; } @SuppressWarnings({ "unchecked", "rawtypes" }) - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ - if(parameters == null || parameters.length == 0){ - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); - }else if(checkParameters(parameters, 2)){ + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { + if (parameters == null || parameters.length == 0) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { Collection c = (Collection) parameters[0]; LambdaFunction function = (LambdaFunction) parameters[1]; Object[] source = new ArrayList(c).toArray(); - return getPermutations(function,context,source,source.length); - }else if(checkParameters(parameters, 3)){ + return getPermutations(function, context, source, source.length); + } else if (checkParameters(parameters, 3)) { Collection c = (Collection) parameters[0]; Integer length = (Integer) parameters[1]; - if(length<1){ - throw new ScriptException(String.format("%s函数的参数格式不正确:length超出正常范围", getNames(),length)); + if (length < 1) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("collection", "collection.length.error", getNames())); } LambdaFunction function = (LambdaFunction) parameters[2]; Object[] source = new ArrayList(c).toArray(); - return getPermutations(function,context,source,length); + return getPermutations(function, context, source, length); } - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); - }catch(ScriptException e){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected ScriptResult getPermutations(LambdaFunction function,ScriptContext context,Object[] source, int len) throws Exception { - getPermutations(len, function,context, source); + + protected ScriptResult getPermutations(LambdaFunction function, ScriptContext context, Object[] source, int len) + throws Exception { + getPermutations(len, function, context, source); return ScriptResult.VOID_RESULT; } - protected void getPermutations(int len, LambdaFunction function,ScriptContext context, - Object[] source, int... is) throws Exception { + protected void getPermutations(int len, LambdaFunction function, ScriptContext context, Object[] source, int... is) + throws Exception { if (is != null && is.length == len) { List list = new ArrayList(); for (int i = 0; i < len; i++) { @@ -72,12 +75,12 @@ public class PermutationFunction extends AbstractScriptFunction { return; } for (int i = 0; i < source.length; i++) { - getPermutations(len, function,context, source, merge(is, i)); + getPermutations(len, function, context, source, merge(is, i)); } } - protected int[] merge(int[] is, int item) { + protected int[] merge(int[] is, int item) { int[] nis; if (is == null) { nis = new int[1]; diff --git a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/objectitem/ListToListProcessor.java b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/objectitem/ListToListProcessor.java index d95c5426bcbe7489ecda32c4ab73284528d660df..77b04b582b33a659c51405ecd9b99374c984c667 100644 --- a/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/objectitem/ListToListProcessor.java +++ b/org.tinygroup.tinyscript.collection/src/main/java/org/tinygroup/tinyscript/collection/objectitem/ListToListProcessor.java @@ -7,36 +7,35 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptEngine; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.expression.ExpressionUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptContextUtil; import org.tinygroup.tinyscript.objectitem.ObjectSingleItemProcessor; -public class ListToListProcessor extends ObjectSingleItemProcessor{ +public class ListToListProcessor extends ObjectSingleItemProcessor { protected boolean isMatch(Object obj, Object item) { return obj instanceof List && item instanceof List; } - protected Object process(ScriptContext context, Object obj, Object item) - throws Exception { + protected Object process(ScriptContext context, Object obj, Object item) throws Exception { List list = (List) obj; List list2 = (List) item; - List result = new ArrayList(); //生成新的数组 + List result = new ArrayList(); // 生成新的数组 ScriptEngine engine = ScriptContextUtil.getScriptEngine(context); - for(int i=0;i T getData(int row, int col) throws Exception { if (absolute(row)) { - return getData(col); + return getData(col); } - throw new Exception("不存在" + row + "行的数据!"); + throw new Exception(ResourceBundleUtil.getResourceMessage("database", "database.data.noexists", row)); } public void setData(int row, int col, T data) throws Exception { @@ -136,12 +138,12 @@ public class ResultSetDataSet extends AbstractDataSet { @SuppressWarnings("unchecked") public T getData(int col) throws Exception { try { - if(isIndexFromOne()){ + if (isIndexFromOne()) { return (T) resultSet.getObject(col); - }else{ + } else { return (T) resultSet.getObject(col + 1); } - + } catch (SQLException e) { throw new Exception(e); } diff --git a/org.tinygroup.tinyscript.database/src/main/resources/database.properties b/org.tinygroup.tinyscript.database/src/main/resources/database.properties new file mode 100644 index 0000000000000000000000000000000000000000..772ced5abd5dcf85191893b2464b9f63ba8272b9 --- /dev/null +++ b/org.tinygroup.tinyscript.database/src/main/resources/database.properties @@ -0,0 +1,3 @@ +database.prev.error=The dataset only supports forward scrolling +database.method.nosupport=Unsupported method:[%s] +database.data.noexists=Non-existent row data:[%d] \ No newline at end of file diff --git a/org.tinygroup.tinyscript.database/src/main/resources/database_zh_CN.properties b/org.tinygroup.tinyscript.database/src/main/resources/database_zh_CN.properties new file mode 100644 index 0000000000000000000000000000000000000000..8962bdad2d1fb3396ddec5e29c8c3eda7005df00 --- /dev/null +++ b/org.tinygroup.tinyscript.database/src/main/resources/database_zh_CN.properties @@ -0,0 +1,3 @@ +database.prev.error=\u7ed3\u679c\u96c6\u53ea\u652f\u6301\u5411\u524d\u6eda\u52a8 +database.method.nosupport=\u4e0d\u652f\u6301\u7684\u65b9\u6cd5:[%s] +database.data.noexists=\u4e0d\u5b58\u5728\u7684\u884c\u6570\u636e:[%d] \ No newline at end of file diff --git a/org.tinygroup.tinyscript.template/src/main/java/org/tinygroup/tinyscript/template/TemplateEngineCustomProcessor.java b/org.tinygroup.tinyscript.template/src/main/java/org/tinygroup/tinyscript/template/TemplateEngineCustomProcessor.java index d33e4b852932cf245335ce85820275dacec0bb22..0138f8211665b9952cb5b2dc5c0a9af45e596c79 100644 --- a/org.tinygroup.tinyscript.template/src/main/java/org/tinygroup/tinyscript/template/TemplateEngineCustomProcessor.java +++ b/org.tinygroup.tinyscript.template/src/main/java/org/tinygroup/tinyscript/template/TemplateEngineCustomProcessor.java @@ -8,31 +8,33 @@ import org.tinygroup.template.impl.TemplateContextDefault; import org.tinygroup.template.impl.TemplateRenderDefault; import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.custom.CustomProcessor; /** * 模板引擎规则处理器 + * * @author yancheng11334 * */ -public class TemplateEngineCustomProcessor implements CustomProcessor{ +public class TemplateEngineCustomProcessor implements CustomProcessor { public boolean isMatch(Object obj) { return obj instanceof TemplateEngine; } - public Object executeRule(Object customObj, String customRule, - ScriptContext context) throws ScriptException { + public Object executeRule(Object customObj, String customRule, ScriptContext context) throws ScriptException { TemplateEngine engine = (TemplateEngine) customObj; TemplateRender render = new TemplateRenderDefault(); render.setTemplateEngine(engine); - + TemplateContext templateContext = new TemplateContextDefault(); templateContext.setParent(context); try { return render.renderTemplateContent(customRule, templateContext); } catch (TemplateException e) { - throw new ScriptException(String.format("模板引擎渲染[%s]发生异常:", customRule),e); + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("template", "template.render.error", customRule), e); } } diff --git a/org.tinygroup.tinyscript.template/src/main/resources/template.properties b/org.tinygroup.tinyscript.template/src/main/resources/template.properties new file mode 100644 index 0000000000000000000000000000000000000000..8a4a23e900b8c1e104087b8258811a9f0b4e8e33 --- /dev/null +++ b/org.tinygroup.tinyscript.template/src/main/resources/template.properties @@ -0,0 +1 @@ +template.render.error=The template engine renders an exception,rule:[%s] \ No newline at end of file diff --git a/org.tinygroup.tinyscript.template/src/main/resources/template_zh_CN.properties b/org.tinygroup.tinyscript.template/src/main/resources/template_zh_CN.properties new file mode 100644 index 0000000000000000000000000000000000000000..cd574d0160a180b1b894db964a5fc777d000ca76 --- /dev/null +++ b/org.tinygroup.tinyscript.template/src/main/resources/template_zh_CN.properties @@ -0,0 +1 @@ +template.render.error=\u6a21\u677f\u5f15\u64ce\u6e32\u67d3\u53d1\u751f\u5f02\u5e38\uff0c\u89c4\u5219\uff1a[%s] \ No newline at end of file diff --git a/org.tinygroup.tinyscript.text/src/main/java/org/tinygroup/tinyscript/text/function/ReadTxtFunction.java b/org.tinygroup.tinyscript.text/src/main/java/org/tinygroup/tinyscript/text/function/ReadTxtFunction.java index a00affd0e4e582bc2c708fdb68381337b3f69551..c2154a30390fd4cd8b45aa14cd40eb80d33cf435 100644 --- a/org.tinygroup.tinyscript.text/src/main/java/org/tinygroup/tinyscript/text/function/ReadTxtFunction.java +++ b/org.tinygroup.tinyscript.text/src/main/java/org/tinygroup/tinyscript/text/function/ReadTxtFunction.java @@ -14,10 +14,12 @@ import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.interpret.FileObjectUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.vfs.FileObject; /** * 增加读取char(9)分割记录,char(10)/char(13)换行的文本记录 + * * @author yancheng11334 * */ @@ -27,74 +29,73 @@ public class ReadTxtFunction extends AbstractScriptFunction { return "readTxt"; } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("readTxt函数的参数为空!"); - }else if(parameters.length == 1 && parameters[0]!=null){ - return readTxt((String)parameters[0],null,context); - }else if(parameters.length == 2 && parameters[0]!=null && parameters[1]!=null){ - return readTxt((String)parameters[0],(String)parameters[1],context); - }else{ - throw new ScriptException("readTxt函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (parameters.length == 1 && parameters[0] != null) { + return readTxt((String) parameters[0], null, context); + } else if (parameters.length == 2 && parameters[0] != null && parameters[1] != null) { + return readTxt((String) parameters[0], (String) parameters[1], context); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("readTxt函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - private DataSet readTxt(String path,String encode,ScriptContext context) throws Exception{ - if(StringUtil.isEmpty(encode)){ - encode = "utf-8"; + + private DataSet readTxt(String path, String encode, ScriptContext context) throws Exception { + if (StringUtil.isEmpty(encode)) { + encode = "utf-8"; } BufferedReader reader = null; FileObject fileObject = null; - try{ + try { fileObject = FileObjectUtil.findFileObject(path, false); - if(fileObject==null){ - throw new ScriptException(String.format("查找资源[%s]失败!", path)); + if (fileObject == null) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("text", "text.file.notfound", path)); } - reader = new BufferedReader(new InputStreamReader(fileObject.getInputStream(),encode)); - + reader = new BufferedReader(new InputStreamReader(fileObject.getInputStream(), encode)); + List fields = new ArrayList(); List values = new ArrayList(); - //解析标题 - String line = reader.readLine(); - String[] ss = line.split("\t"); - for(String s:ss){ - fields.add(new Field(s,s,"Object")); - } - - //解析字段 - while((line=reader.readLine())!=null){ + // 解析标题 + String line = reader.readLine(); + String[] ss = line.split("\t"); + for (String s : ss) { + fields.add(new Field(s, s, "Object")); + } + + // 解析字段 + while ((line = reader.readLine()) != null) { ss = line.split("\t"); - if(fields.size()!=ss.length){ - throw new ScriptException(String.format("解析行记录[%s]失败:值个数与标题列个数不匹配", line)); + if (fields.size() != ss.length) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("text", "text.row.error", line)); } values.add(ss); } - + Object[][] dataArray = new Object[values.size()][]; - for(int i=0;i