Beetl to get variables in Java via Java interface

Source: Internet
Author: User

The template language binding variable is a program that actively binds to beetl, and is now implementing the binding by actively making the template language unsolicited request on beetl.


Recently done projects to Beetl, because the template needs to use a lot of variables, if directly bound, the system consumes a lot.

Generates an idea that when beetl resolves to a variable, fetching the object directly through the Java interface is returned to Beetl to continue parsing.

Finally, the function of Beetl to initiate bound variables is realized.


The revised code is shown in the last.

The code at the time of invocation is as follows:

Stringtemplateresourceloader Resourceloader = new Stringtemplateresourceloader ();
Configuration conf = configuration.defaultconfiguration ();
Conf.setengine ("Org.koron.ebs.permission.beetl.MyEngine");//This is a rewritten engine.
GroupTemplate GT = new GroupTemplate (resourceloader, conf);
Template t = gt.gettemplate ("${sdf.name}<%for (U in user) {print (u);} %> ");
T.binding ("User", New string[]{"IBM", "GOOGLE"});
T.binding ("var_not_defined", new Varlistener () {//bind a global variable used to parse variable undefined variable
@Override
Public Object Parse (String var) {
if (Java.util.regex.Pattern.compile ("sdf\\.?"). Matcher (Var). Find ())
{
POJO PJ = new POJO ();
Pj.name= "I try";
Pj.id= "1111";
return PJ;
}
return null;
}
});
T.renderto (System.out);


Package org.koron.ebs.permission.beetl;


Import Java.util.EventListener;


Public interface Varlistener extends eventlistener{//used to parse variables
/**
* When parsing a variable into an instance, NULL, the expression does not analyze
* @param var variable name
* @return Instance
*/
Public Object Parse (String var);
}

The first implementation by the template language to actively tune the Java method, feel good:)



Modified code:

Package org.koron.ebs.permission.beetl;



Import Java.io.Reader;
Import Java.util.Map;
Import Java.util.Stack;


Import org.beetl.core.*;
Import Org.beetl.core.engine.FastRuntimeEngine;
Import Org.beetl.core.engine.FilterProgram;
Import Org.beetl.core.engine.StatementParser;
Import org.beetl.core.exception.BeetlException;
Import org.beetl.core.statement.*;


public class Myengine extends Fastruntimeengine {


/*
* (Non-javadoc)
*
* @see Org.beetl.core.engine.defaulttemplateengine#createprogram (org.beetl
*. Core. Resource, Java.io.Reader, Java.util.Map, java.lang.String,
* org.beetl.core.GroupTemplate)
*/
@Override
Public program Createprogram (Resource Resource, Reader Reader, Map<integer, string> textmap, String CR, Grouptempla Te gt) {
Filterprogram program = (filterprogram) super.createprogram (Resource, Reader, Textmap, CR, GT);
MODIFYSTATEMETN (Resource, program, GT);
MODIFYSTATEMETN (Resource, program.getcopy (), GT);
return program;


}


private void Modifystatemetn (Resource Resource, program program, GroupTemplate GT) {
statement[] sts = program.metaData.statements;
Statementparser parser = new Statementparser (STS, GT, Resource.getid ());
Parser.addlistener (Varref.class, New Varreflistener ());
Parser.parse ();
}


Class Varreflistener implements Listener {
@Override
Public Object onEvent (Event e) {
stack<?> stack = (stack<?>) e.geteventtaget ();
Object o = Stack.peek ();
if (o instanceof varref)
return new Myvarref ((VARREF) o);
return null;


}
}


Class Myvarref extends Varref implements Ivarindex {
Private Grammartoken Firsttoken;
Private Varref var;


Public Myvarref (varref var) {
This (Var.attributes, Var.hassafe, Var.safe, Var.token);
This.var = var;
This.varindex = Var.getvarindex ();
}


Private Myvarref (varattribute[] attributes, Boolean hassafe, Expression safe, Grammartoken token) {
This (attributes, Hassafe, safe, token, token);
}


Private Myvarref (varattribute[] attributes, Boolean hassafe, Expression safe, Grammartoken token, Grammartoken Firsttoken) {
Super (attributes, Hassafe, safe, token, firsttoken);
}


@Override
Public Object Evaluate (Context ctx) {
Object value = Ctx.vars[varindex];
if (value = = Context.not_exist_object) {
if (Hassafe) {
return safe = = null? Null:safe.evaluate (CTX);
} else {
Object o = Ctx.globalVar.get ("var_not_defined");
if (o = = NULL | |! ( o instanceof Varlistener)) {
Beetlexception ex = new Beetlexception (beetlexception.var_not_defined);
Ex.pushtoken (Firsttoken);
Throw ex;
}
o = ((Varlistener) O). Parse (Var.token.text);
if (o! = null) {
Ctx.vars[varindex] = o;
Value = Ctx.vars[varindex];
} else {
Beetlexception ex = new Beetlexception (beetlexception.var_not_defined);
Ex.pushtoken (Firsttoken);
Throw ex;
}
}
}


if (value = = null) {
if (Hassafe) {
return safe = = null? Null:safe.evaluate (CTX);
}
}


if (Attributes.length = = 0) {
return value;
}


for (int i = 0; i < attributes.length; i++) {
Varattribute attr = attributes[i];
if (value = = null) {
if (Hassafe) {
return safe = = null? Null:safe.evaluate (CTX);
} else {
Beetlexception be = new Beetlexception (beetlexception.null, "null pointer");
if (i = = 0) {
Be.pushtoken (This.firsttoken);
} else {
Be.pushtoken (Attributes[i-1].token);
}


throw be;
}


}


try {
Value = Attr.evaluate (ctx, value);
} catch (Beetlexception ex) {
Ex.pushtoken (Attr.token);
Throw ex;


} catch (RuntimeException ex) {
Beetlexception be = new Beetlexception (Beetlexception.attribute_invalid, "Property access Error", ex);
Be.pushtoken (Attr.token);
throw be;
}


}


if (value = = null && hassafe) {
return safe = = null? Null:safe.evaluate (CTX);
} else {
return value;
}


}


@Override
public void Setvarindex (int index) {
This.varindex = index;


}


@Override
public int Getvarindex () {
return this.varindex;
}


@Override
public void Infer (Infercontext inferctx) {


Type type = Inferctx.types[this.varindex];
Type lasttype = type;
Type t = null;
for (Varattribute attr:attributes) {
Inferctx.temp = Lasttype;
Attr.infer (INFERCTX);
t = Lasttype;
Lasttype = Attr.type;
Attr.type = t;


}
This.type = Lasttype;
if (safe! = null) {
Safe.infer (INFERCTX);
if (!safe.type.equals (This.type)) {
This.type = Type.objecttype;
}
}
}


/**
* @return Get #{bare_field_comment}
*/
Public Grammartoken Getfirsttoken () {
return firsttoken;
}
}
}

Beetl to get variables in Java via Java interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.