org.beetl.core.Context Java Examples

The following examples show how to use org.beetl.core.Context. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonMapExpression.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Object evaluate(Context ctx)
{
	if (map.size() == 0)
	{
		return new LinkedHashMap();
	}
	else
	{
		Map values = new LinkedHashMap(map.size());
		for (Entry<String, Expression> entry : map.entrySet())
		{
			values.put(entry.getKey(), entry.getValue().evaluate(ctx));
		}
		return values;

	}
}
 
Example #2
Source File: IfStatement.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void execute(Context ctx)
{
	// TODO Auto-generated method stub
	Object value = condtion.evaluate(ctx);

	if (ALU.isTrue(value, this))
	{
		ifStatement.execute(ctx);
	}
	else
	{
		if (elseStatement != null)
		{
			elseStatement.execute(ctx);
		}
	}

}
 
Example #3
Source File: VarRefAssignStatement.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void execute(Context ctx)
{
	Object value =  exp.evaluate(ctx);
	Object obj = varRef.evaluateUntilLast(ctx);
	Object key = null;
	if(lastVarAttribute instanceof VarSquareAttribute){
		key  = (((VarSquareAttribute)lastVarAttribute).exp).evaluate(ctx);
		
		
	}else {
		key = lastVarAttribute.name;
	}
	try{
		ObjectAA.defaultObjectAA().setValue(obj, key, value);
	}catch(ClassCastException ex){
		BeetlException bx = new BeetlException(BeetlException.ATTRIBUTE_INVALID,ex);
		bx.pushToken(lastVarAttribute.token);
		throw bx;
	}catch(BeetlException be){
		be.pushToken(lastVarAttribute.token);
		throw be;
	}
	
	

}
 
Example #4
Source File: VerifyForamtFunction.java    From springboot-plus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
 public String call(Object[] arg0, Context arg1) {
 	
 	StringBuilder sb = new StringBuilder("");
 	
 	if(arg0[0] instanceof List){
 		List<Verify> list = (List)arg0[0];
 		for (int i = 0; i < list.size(); i++) {
 			Verify verify = list.get(i);
 			if(i < list.size() - 1){
 				sb.append(verify.getName()+"|");
 			}else{
 				sb.append(verify.getName());
 			}
}
 	}
 	
     return sb.toString();
 }
 
Example #5
Source File: CookieFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Object call(Object[] paras, Context ctx)
{
	
	 HttpServletRequest request = (HttpServletRequest)ctx.getGlobal(WebVariable.REQUEST);
	 Cookie[] cookies = request.getCookies();
	 if(paras.length==0){
		 return cookies;
	 }else{
		 String name = (String)paras[0];
		 for(Cookie cookie:cookies){
			 if(cookie.getName().equals(name)){
				 return cookie;
			 }
		 }
		 return null;
		 
	 }

}
 
Example #6
Source File: CheckExistFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Boolean call(Object[] paras, Context ctx)
{

	if (ctx.globalVar == null)
		return false;
	String key = null;
	for (Object o : paras)
	{
		key = (String) o;

		if (ctx.globalVar.containsKey(key))
		{
			continue;
		}
		else
		{
			return false;
		}
	}
	return true;

}
 
Example #7
Source File: ParseDouble.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args)
{
	Double d = 1232323232323.89;
	System.out.println(d);
	String str = d.toString();
	double c = Double.parseDouble(str);
	System.out.println(c);
	ParseDouble pDouble = new ParseDouble();
	Context ctx = new Context();
	System.out.println(pDouble.call(new Object[]
	{ -01.}, ctx));
	System.out.println(pDouble.call(new Object[]
	{ 2332.23213 }, ctx));
	System.out.println(pDouble.call(new Object[]
	{ "-1.023" }, ctx));
	System.out.println(pDouble.call(new Object[]
	{ "abcd" }, ctx));
}
 
Example #8
Source File: AccessUrlIfFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Boolean call(Object[] paras, Context ctx) {
	// 如果没有安全上下文,固定返回true
	SecurityContext securityContext = SecurityContextHolder.getContext();
	if (securityContext == null) {
		return true;
	}
	// 用户未登录
	Authentication authentication = getAuthentication(securityContext);

	// 如果未提供URL,固定返回true
	if ((paras.length == 0) || (paras[0] == null) || !(paras[0] instanceof String)) {
		return true;
	}
	String url = (String) paras[0];
	// 默认请求方式为GET,由第二参数给定
	String method = "GET";
	if ((paras.length > 1) && (paras[1] != null) && (paras[1] instanceof String)) {
		method = (String) paras[1];
	}
	return privilegeEvaluator.isAllowed(servletContext.getContextPath(), url, method, authentication);
}
 
Example #9
Source File: TypeNewFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object call(Object[] paras, Context ctx) {
	String clsName = (String)paras[0];
	Class cls = ctx.gt.getClassSearch().getClassByName(clsName);
	if(cls!=null){
		try{
			return cls.newInstance();
		}catch(Exception ex){
			throw new RuntimeException("创建类实例失败:"+cls+",error:"+ex.getMessage(),ex);
		}
		
	}else{
		throw new RuntimeException("不能加载类:"+clsName+" classloader:"+ctx.gt.getClassLoader());
	}
	

}
 
Example #10
Source File: SpELFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 方法调用
 *
 * @param args
 *            方法参数列表
 * @param context
 *            Beetl执行上下文
 * @return SpEL表达式执行结果
 */
@Override
public Object call(Object[] args, Context context) {
	// 只能有一个参数且只能为字符串
	if ((args.length >= 1) && (args[0] instanceof String) && (args[0] != null)) {
		String spel = (String) args[0];
		Expression expression = getExpression(spel);
		// 如果有第二参数,取他作为根对象
		if ((args.length >= 2) && (args[1] != null)) {
			return expression.getValue(createEvaluationContext(args[1], context));
		} else {
			return expression.getValue(createEvaluationContext(Collections.emptyMap(), context));
		}
	}
	return null;
}
 
Example #11
Source File: HasAttributeFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object call(Object[] paras, Context ctx) {
	Object  o = paras[0];
	if(o==null) {
		throw new NullPointerException();
	}
	if(o instanceof Map) {
		return false;
	}
	Class type  = o.getClass();
	for(int i=1;i<paras.length;i++) {
		String key = (String)paras[i];
		MethodInvoker invoke = ObjectUtil.getInvokder(type, key);
		if(invoke==null) {
			return false;
		}else if(invoke instanceof GeneralGetMethodInvoker) {
			return false ;
		}
	}
	return true;
}
 
Example #12
Source File: EmptyFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args)
{
	EmptyFunction fn = new EmptyFunction();
	Context ctx = new Context();
	ctx.set("list", new ArrayList());
	ctx.set("array", new Object[]
	{});

	ctx.set("array1", new Object[]
	{ 1, 2 });
	System.out.println(fn.call(new Object[]
	{ "lijz" }, ctx));
	System.out.println(fn.call(new Object[]
	{ "list" }, ctx));
	System.out.println(fn.call(new Object[]
	{ "array" }, ctx));
	System.out.println(fn.call(new Object[]
	{ "array1" }, ctx));

}
 
Example #13
Source File: VarVirtualAttribute.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void infer(InferContext inferCtx)
{
	Type type = (Type) inferCtx.temp;
	VirtualClassAttribute vae = inferCtx.gt.getVirtualAttributeEval(type.cls, name);
	if (vae == null)
	{
		this.type = Type.ObjectType;
	}
	else
	{
		Method m = ObjectUtil.getGetMethod(vae.getClass(), "eval", new Class[]
		{ Object.class, String.class, Context.class });
		this.type = new Type(m.getReturnType());
	}

}
 
Example #14
Source File: BasicProgramOptProbe.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void check(Context ctx)
{
	StatementParser seacher = new StatementParser(program.metaData.statements, program.gt, program.res.getId());
	Map<Class, Listener> map = initProbeNode();
	for (Entry<Class, Listener> entry : map.entrySet())
	{
		seacher.addListener(entry.getKey(), entry.getValue());
	}

	this.initProbeNode();
	seacher.parse();
	this.program.gt.getProgramCache().set(program.res.getId(), program);
	ProgramReplaceEvent event = new ProgramReplaceEvent(program);
	this.program.gt.fireEvent(event);

}
 
Example #15
Source File: Print.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public String call(Object[] paras, Context ctx)
{
	Object o = paras[0];

	if (o != null)
	{
		try
		{
			ctx.byteWriter.writeString(o.toString());
		}
		catch (IOException e)
		{
			BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR);
			throw be;
		}
	}
	return "";

}
 
Example #16
Source File: SafePlaceholderST.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void execute(Context ctx)
{
	try
	{
		Object value = exp.evaluate(ctx);
		if (format != null)
		{
			value = format.evaluateValue(value, ctx);
		}
		ctx.byteWriter.writeObject(value);
	}
	catch (Exception ex)
	{

	}

}
 
Example #17
Source File: AssertFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public String call(Object[] paras, Context ctx)
{

	if (ASSERT)
	{
		boolean result = (Boolean) paras[0];
		String msg = null;
		if (paras.length > 1)
		{
			msg = (String) paras[1];
		}
		if (!result)
		{
			throw new RuntimeException(msg == null ? "断言异常" : msg);
		}
		return "";
	}
	else
	{
		return "";
	}

}
 
Example #18
Source File: ContentBodyExpression.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object evaluate(Context ctx)
{
	ByteWriter real = ctx.byteWriter;
	ByteWriter temp = real.getTempWriter(real);
	ctx.byteWriter = temp;
	block.execute(ctx);
	ctx.byteWriter = real;
	return temp.getTempConent();

}
 
Example #19
Source File: ReturnStatement.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void execute(Context ctx)
{
	ctx.gotoFlag = IGoto.RETURN;
	if (this.exp != null)
	{
		Object value = exp.evaluate(ctx);
		//最后一个存放返回值
		ctx.vars[ctx.vars.length - 1] = value;
	}

}
 
Example #20
Source File: ErrorGrammarProgram.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ErrorGrammarProgram(Resource res, GroupTemplate gt, String cr)
{
	super();
	this.res = res;
	this.rs = res;
	this.gt = gt;
	this.metaData = new ProgramMetaData() {
		public void initContext(Context ctx)
		{
			//do nothing;
		}

		protected void putGlobaToArray(Context ctx)
		{
			//do nothing;
		}

		public AjaxStatement getAjax(String anchor)
		{
			//创建一个临时的ajax片段语句
			return new AjaxStatement(null, GrammarToken.createToken(anchor, 0),true) {
				public void execute(Context ctx)
				{
					ErrorGrammarProgram.this.execute(ctx);

				}
			};
		}
	};
	this.metaData.lineSeparator = cr;
}
 
Example #21
Source File: ErrorGrammarProgram.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void execute(Context ctx)
{
	ErrorHandler errorHandler = this.gt.getErrorHandler();
	if (errorHandler == null)
		throw exception;
	//		Writer w = BeetlUtil.getWriterByByteWriter(ctx.byteWriter);
	//		errorHandler.processExcption(exception, w);
	throw exception;
}
 
Example #22
Source File: NativeCallExpression.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkPermit(Context ctx, Class targetCls, Object targetObj, String method)
{
	if (targetCls == null)
		return;
	if (!ctx.gt.getNativeSecurity().permit(ctx.template.program.res.getId(), targetCls, targetObj, method))
	{
		BeetlException be = new BeetlException(BeetlException.NATIVE_SECUARITY_EXCEPTION);
		be.pushToken(GrammarToken.createToken(method, token.line));
		throw be;
	}
}
 
Example #23
Source File: AuthenticationFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object call(Object[] paras, Context ctx) {
	// 获取安全上下文
	SecurityContext securityContext = SecurityContextHolder.getContext();
	Authentication authentication = securityContext != null ? securityContext.getAuthentication() : null;
	// 获取认证凭证
	return ((authentication == null) || (authentication instanceof AnonymousAuthenticationToken)) ? null : authentication;
}
 
Example #24
Source File: VarVirtualAttribute.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object evaluate(Context ctx, Object o)
{

	VirtualClassAttribute vae = ctx.gt.getVirtualAttributeEval(o.getClass(), name);
	if (vae == null)
	{
		BeetlException be = new BeetlException(BeetlException.VIRTUAL_NOT_FOUND);
		be.pushToken(token);
		throw be;
	}
	return vae.eval(o, name, ctx);

}
 
Example #25
Source File: DecodeFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object call(Object[] paras, Context ctx)
{
	
	Object ret = null;
	try
	{
		Object o = paras[0];
		int i = 1;
		while (true)
		{
			if (same(o, paras[i],ctx))
			{
				ret = paras[i + 1];
				break ;
			}
			else
			{
				if (paras.length - 1 == i + 2)
				{
					//default
					ret =  paras[i + 2];
					break ;
				}
				else
				{
					i = i + 2;
					continue;
				}
			}
		}
	}
	catch (ArrayIndexOutOfBoundsException ex)
	{

		throw new RuntimeException("decode函数使用错误:DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )");
	}
	return unwrap(ret,ctx);
	

}
 
Example #26
Source File: FunctionWrapper.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Object[] getContextParas(Object[] paras, Context ctx)
{
	Object[] newParas = new Object[paras.length + 1];
	System.arraycopy(paras, 0, newParas, 0, paras.length);
	newParas[paras.length] = ctx;
	return newParas;

}
 
Example #27
Source File: ProgramMetaData.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 将模板全局变量转为数组
 * 
 * @param ctx
 */
protected void putGlobaToArray(Context ctx)
{
	Map<String, Object> globalVar = ctx.globalVar;
	if (globalVar == null)
	{
		for (int i = 0; i < this.tempVarStartIndex; i++)
		{
			ctx.vars[i] = ctx.NOT_EXIST_OBJECT;
		}
		return;
	}

	for (Entry<String, Integer> entry : globalIndexMap.entrySet())
	{
		String key = entry.getKey();
		int index = entry.getValue();
		if (globalVar.containsKey(key))
		{
			ctx.vars[index] = globalVar.get(key);
		}
		else
		{
			// 不存在
			ctx.vars[index] = ctx.NOT_EXIST_OBJECT;
		}
	}
}
 
Example #28
Source File: GetValueFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Object call(Object[] paras, Context ctx)
{
	int len = paras.length;
	Map<String, Object> map = (Map<String, Object>) ctx.globalVar.get("beetlKitMap");
	for (int i = 0; i < len; i++)
	{
		String key = (String) paras[i];
		Object value = paras[++i];
		map.put(key, value);
	}
	return map;

}
 
Example #29
Source File: CheckExistFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args)
{
	CheckExistFunction fn = new CheckExistFunction();
	Context ctx = new Context();
	ctx.set("list", null);

	System.out.println(fn.call(new Object[]
	{ "list" }, ctx));

}
 
Example #30
Source File: MutipleFunctionWrapper.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MutipleFunctionWrapper(String funName, Class cls, Object target, Method[] ms)
{
	super(funName);
	this.ms = ms;
	this.target = target;
	this.cls = cls;
	int index = this.functionName.lastIndexOf(".");
	if (index != -1)
	{
		methodName = functionName.substring(index + 1);
	}
	else
	{
		methodName = functionName;
	}
	List<MethodContext> list = new ArrayList<MethodContext>();
	for (Method m : ms)
	{
		Class[] paraType = m.getParameterTypes();
		MethodContext mc = new MethodContext();
		mc.m = m;
		mc.parasType = paraType;

		if (paraType.length != 0 && paraType[paraType.length - 1] == Context.class)
		{
			mc.contextRequired = true;

		}

		list.add(mc);

	}
	mcs = (MethodContext[]) list.toArray(new MethodContext[0]);

}