All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xson.web.ognl.var.VariableParser Maven / Gradle / Ivy

Go to download

xco-web is an easy to use control layer framework, is part of the SOA system, using xml language to describe the controller.

The newest version!
package org.xson.web.ognl.var;

import java.util.ArrayList;
import java.util.List;

import org.xson.web.ognl.OgnlException;
import org.xson.web.util.NumberUtils;

/**
 * 属性表达式采集
 * 
 * 
 * 	1:	a.b
 * 	2:	a["b"]
 * 	3:	a.b[0]
 * 	4:	a.b.c
 * 
* */ public class VariableParser { /** * 判断字符串是否是静态属性'xxx' | "xxxx" */ private static boolean isStaticProperty(String property) { if (property.length() > 2 && ((property.startsWith("'") && property.endsWith("'")) || (property.startsWith("\"") && property.endsWith("\"")))) { return true; } return false; } /** * 获取一个属性表达式对象 * * @param builder * @param isInternalProperty * 是否是方括号内部属性 * @return */ private static VariableUnitVo getVariableUnitVo(StringBuilder builder, boolean isInternalProperty) { String property = builder.toString().trim(); if (!isInternalProperty) { return new VariableUnitVo(property, false); } else { // boolean isNum = isNumeric(builder); boolean isNum = NumberUtils.isInteger(property); if (isNum) { return new VariableUnitVo(Integer.parseInt(property)); } else { if (isStaticProperty(property)) { property = property.substring(1, property.length() - 1); return new VariableUnitVo(property, false); } // 动态的 return new VariableUnitVo(property, true); } } } /** * 解析属性表达式结构 * *
	 * a["b"]==>a.b(p)
	 * a[n]==>a.0(n)
	 * 
* * @param propertyExpr * @return */ private static List parseComplexProperty(String propertyExpr) { List list = new ArrayList(); char[] src = propertyExpr.toCharArray(); int srcLength = src.length; StringBuilder builder = new StringBuilder(); boolean isInternalProperty = false; // 是否进入内部属性采集 for (int i = 0; i < srcLength; i++) { char key = src[i]; switch (key) { case '.': // 前面采集告一段落 if (builder.length() > 0) { list.add(getVariableUnitVo(builder, isInternalProperty)); builder = new StringBuilder(); } break; case '[': // 进入括弧模式 if (builder.length() > 0) { list.add(getVariableUnitVo(builder, isInternalProperty)); builder = new StringBuilder(); } isInternalProperty = true; break; case ']': if (builder.length() > 0) { list.add(getVariableUnitVo(builder, isInternalProperty)); builder = new StringBuilder(); } isInternalProperty = false; break; // 退出括弧模式 case '\'': // 属性采集 case '"': // break; default: builder.append(key); } } if (builder.length() > 0) { list.add(getVariableUnitVo(builder, isInternalProperty)); } return list; } /** * 是否是一个简单的属性表达式 * * @param propertyExpr * @return */ private static boolean isSimplePropertyExpr(String propertyExpr) { int dotIndex = propertyExpr.indexOf("."); int squareBracketsIndex = propertyExpr.indexOf("["); if (dotIndex < 0 && squareBracketsIndex < 0) { return true; } return false; } /** * 检测是否存在默认 */ private static boolean existDefaultValue(String property) { if (property.indexOf("|") > -1) { return true; } return false; } /** * 获取刨除默认值的属性值 */ private static String getPropertyWithOutDefault(String property) { return property.substring(0, property.indexOf("|")); } enum defaultValueEnum { NOW, DATE, TIME } private static Object getPropertyDefaultValue(String property) { int pos = property.indexOf("|"); String defaultString = property.substring(pos + 1, property.length()).trim(); if (0 == defaultString.length()) { throw new OgnlException("属性的默认值不合法:" + property); } // 属性的默认值 #{abccc|0, null, 'xxx', now(), date(), time()}, // 只有在变量[#{}|${}]里边才可以存在 if ("null".equalsIgnoreCase(defaultString)) { return "null"; } else if (defaultString.length() > 1 && defaultString.startsWith("'") && defaultString.endsWith("'")) {// fix // bug return defaultString.substring(1, defaultString.length() - 1); } else if (NumberUtils.isNumber(defaultString)) { return NumberUtils.parseNumber(defaultString); } else if ("now()".equalsIgnoreCase(defaultString)) { return defaultValueEnum.NOW; } else if ("date()".equalsIgnoreCase(defaultString)) { return defaultValueEnum.DATE; } else if ("time()".equalsIgnoreCase(defaultString)) { return defaultValueEnum.TIME; } throw new OgnlException("属性的默认值不合法:" + property); } /** * 解析表达式 * * @param property * @param allowDefault * 是否允许有默认值 * @return */ public static VariableVo parse(String property, boolean allowDefault) { Object defaultValue = null; String original = property; boolean hasDefault = false; int defaultValueType = 0; if (allowDefault && existDefaultValue(property)) { defaultValue = getPropertyDefaultValue(property); property = getPropertyWithOutDefault(property); hasDefault = true; if (defaultValueEnum.NOW == defaultValue) { defaultValueType = 1; defaultValue = null; } else if (defaultValueEnum.DATE == defaultValue) { defaultValueType = 2; defaultValue = null; } else if (defaultValueEnum.TIME == defaultValue) { defaultValueType = 3; defaultValue = null; } } if (isSimplePropertyExpr(property)) { return new VariableVo(original, new VariableUnitVo(property, false), hasDefault, defaultValue, defaultValueType); } return new VariableVo(original, parseComplexProperty(property), hasDefault, defaultValue, defaultValueType); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy