A simple Java Template Tool Class (ii)-Simple expression parsing implementation

Source: Internet
Author: User

Previously written one, with regular comparison is not efficient, so with the expression of the analytic way to achieve a, practiced hand.

Previous: http://my.oschina.net/haogrgr/blog/222349

Today's:

import java.util.arraylist;import java.util.list;import java.util.map;import  java.util.objects;/** *  very very simple template implementation  *  *  @author  desheng.tu * @ date 2015 June 23   PM 4:38:05 * */public class simpletmpl {public static  void main (String[] args)  {String t1 =  "Hello  $ $name $$,  your captcha is: $$ Code$$ "; string t2 =  "Hello  #{name},  Your Verification code is: ${code}"; Map<string, object> param = mapbuilder.makeo ("name",  "Haogrgr"). Build ("Code",   "1314");for  (int i = 0; i < 100; i++)  { Simpletempletutil.render (T1, param); Templ.of (T2). Render (param);} string result =  ""; Long start = 0;start = system.currenttimemillis (); for  (int i = 0; i < 100000; i++)  {param.put ("code",  i); result = Simpletempletutil.render (T1, param);} System.out.println (System.currenttimemillis ()  - start); SYSTEM.OUT.PRINTLN (result); Start = system.currenttimemillis (); Templ templ = templ.of (T2);for  (int i = 0; i < 100000;  i++)  {param.put ("code",  i); Result = templ.render (param);} System.out.println (System.currenttimemillis ()  - start); SYSTEM.OUT.PRINTLN (result);}} Abstract class exp {abstract string resolve (map<string, object>  context), Static exp of (string exp)  {objects.requirenonnull (EXP);if  (Exp.startsWith ("# {")  | |  exp.startswith ("${")) &NBSP;{RETURN&NBSP;NEW&NBSP;VAREXP (exp);} RETURN&NBSP;NEW&NBSP;STREXP (exp);}} class strexp extends exp {string value; Strexp (String exp)  {this.value = exp;} @Overridepublic  string resolve (Map<string, object> context) &nbsp {Return this.value;} @Overridepublic  string tostring ()  {return  "strexp [value="  + value +   "]";}} class varexp extends exp {string varname; string defaultvalue; boolean nullable = false; Varexp (string varname, string defaultvalue, boolean nullable)  {this.varName  = varname;this.defaultvalue = defaultvalue;this.nullable = nullable;} Varexp (STRING&NBSP;EXP)  {objects.requirenonnull (exp);if  (! ( Exp.startswith ("#{")  | |  exp.startswith ("${"))  | |  !exp.endswith ("}"))  {throw new illegalargumentexception ("Expression ["  + exp +   "] must resemble #{} or ${}");} String[] nodes = exp.substring (2, exp.length ()  - 1). Split (",");if  ( NODES.LENGTH&NBSP;&GT;&NBSP;2)  {throw new illegalargumentexception ("Expression ["  + exp  +  "] can only appear one ', '");} this.varname = nodes[0].trim (); This.defaultvalue = nodes.length == 2 ? nodes[1].trim ()  :  ""; This.nullable = exp.startswith ("$");} @Overridepublic  string resolve (Map<string, object> context)  {object value  = context.get (VarName);if  (value == null && nullable)  {value  = defaultValue == null ?  ""  : defaultvalue; if  (value == null)  {throw new nullpointerexception ("There are no variables specified in the context: var="  +  varName +  " map="  + context);} Return value.tostring ();} @Overridepublic  string tostring ()  {return  "Varexp [varname="  + varname  +  ",  defaultvalue="  + defaultValue +  ",  nullable=" + nullable  +  "]";}} Class templ {list<exp> exps = new arraylist<> (); static&nbSp Templ of (STRING&NBSP;TEMPLSTR)  {objects.requirenonnull (templstr,  "template is empty"); Templ templ = new templ (); Stringbuilder sb = new stringbuilder (); Char[] chars = templstr.tochararray ( );for  (int i = 0; i < chars.length; i++)  {switch  (chars[i ])  {case  ' # ':case  ' $ ': if (i < chars.length - 1 && chars [i+1] ==  ' {') {Templ.addexp (Exp.of (sb.tostring ())); Sb.setlength (0);} Sb.append (Chars[i]);break;case  '} ': Sb.append ('} '); if (Sb.charat (1)  ==  ' {') {Templ.addexp (Exp.of ( Sb.tostring ())); Sb.setlength (0);} Break;default:sb.append (Chars[i]); break;}} if (Sb.length ()  > 0) {Templ.addexp (Exp.of (sb.tostring ()));} Return templ;} Templ addexp (EXP&NBSP;EXP)  {objects.requirenonnull (exp,  "expression is empty"); Exps.add (EXP); return this ;} String render (Map<string, object> context)  {sTringbuilder sb = new stringbuilder (;for ) (exp exp : exps)  { Sb.append (Exp.resolve (context));} Return sb.tostring ();}}


A simple Java Template Tool Class (ii)-Simple expression parsing implementation

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.