Skip to main content
This revision made August 03, 2011 23:11, by Kin-man Chung

API for Stand Alone EL

Introduction

This is the API for using EL stand-alone, outside of a web container. It provides a direct and simple interfaces to creating and evaluating EL expressions, while hiding low level details from the users. This API builds on the EL 2.2 API, and is therefore not independent from it.

ELProcessor is the main engine that EL users would be using most of the time. ELManager can be used to manage the environment/context for EL processing. StandardELContext provides the default implementation of an ELContext used here.

One design philosophy used here is to hide most of the implementation details from the users of this API. Things like ELContext, ELResolver, or even ValueExpression are not used.

The following example shows how the api can be used.

  // Get an instance of ELProcessor.  Container can inject one too.
  ELProcess el = ExpressionFactory.newInstance().newELProcessor();
  ELManager mgr = el.getELManager();
  mgr.addBeanNameResolver(new BeanNameResolver() {
    public Object getBean(String name) {
      if (name.equals("foo")
        return new Foo();
    }
  });
  // Evaluate an EL expression
  System.out.println("Name for foo is ",  el.getValue("#{foo.name}"));
  // Defines a el function  "sum" (org.acme.Foo.sum is a static method)
  el.defineFunction("x", "sum", org.acme.Foo", "sum");
  System.out.println("1 + 2 = ", el.getValue("#{x:sum(1,2)}"));
  // Defines a el variable
  el.setVariable("v", "#{foo.name}");
  System.out.println("name for foo is ", "#{v}");  // Same as the first example

ELProcessor.java

see

ELManager.java

see

BeanNameResolver.java

see

BeanNameELResolver.java

see

StandardELResolver.java

see

FunctionMapper.java

see

Difference compared to previous revision
== API for Stand Alone EL== === Introduction === This is the API for using EL stand-alone, outside of a web container. It provides a direct and simple interfaces to creating and evaluating EL expressions, while hiding low level details from the users. This API builds on the EL 2.2 API, and is therefore not independent from it. ELProcessor is the main engine that EL users would be using most of the time. ELManager can be used to manage the environment/context for EL processing. StandardELContext provides the default implementation of an ELContext used here.== API for Stand Alone EL== === Introduction === This is the API for using EL stand-alone, outside of a web container. It provides a direct and simple interfaces to creating and evaluating EL expressions, while hiding low level details from the users. This API builds on the EL 2.2 API, and is therefore not independent from it. ... ELProcessor is the main engine that EL users would be using most of the time. ELManager can be used to manage the environment/context for EL processing. One design philosophy used here is to hide most of the implementation details from the users of this API. Things like ELContext, ELResolver, or even ValueExpression are not used. '''To be revisited''' An instance of ELProcessor can be obtained from the ExpressionFactory. In it there is a default ELManager, which contains a default ELContext, which in term contains a default ELResolver, FunctionMapper, and VariableMapper. They are mostly empty, except for the default ELResover, which contain enough ELResolvers to handle beans properties, arrays etc. '''details to be defined.''' The follo One design philosophy used here is to hide most of the implementation details from the users of this API. Things like ELContext, ELResolver, or even ValueExpression are not used. ... ELManager mgr = el.getELManager(); mgr.addBeanNameResolver(new BeanNameResolver() { public Object getBean(String name) { if (name.equals("foo") wing example shows how the api can be used. // Get an instance of ELProcessor. Container can inject one too. ELProcess el = ExpressionFactory.newInstance().newELProcessor(); ... ELManager mgr = el.getELManager(); mgr.addBeanNameResolver(new BeanNameResolver() { public Object getBean(String name) { return myBeans.getreturn new Foo(name); } }); // Evaluate an EL expression Obj(); } }); // Evaluate an EL expression Systecm.out b.println("Name foar =r foo is ", el.getValue("#{foo.bel.getValue("#{foo.narme}"))}"); ===ELProcessor.java=== /* *; // Defines an APIDefines a el functi foron ELexpre "ssionum" pr(ocessinrg. */ interfg.acmace ELPre.Focessr { /* * Retoo.surnm the ELManager uised for EL processing. */ ELManager ges a stELManager(); /* * Evaluate an EL expression,tatic wimethout coercion.d) */ Object getValu el.de(Strfing expression); /* * EvalineFuate an EL expressunction("x"ion, with, "sum", coercion org. */ Objeact gmetValue(String expressi.Fono", Cla"ss expectedTypeum"); /* * Se); Syst a value ttem.o an EL exut.pression thatprintln("1 is+a2LHS =", */ void s el.geetValue(String etValue("#{xpre:ssion, Object valuesum(1,2)}"))); /* ; // * DefinesDefine a EL a el variable */ void defin variable el.seteVariable(String Variable("var", String expression); /* * De"#{fine a EL functifoo.on * @param prefixname}"); Theprefix of Systhe ftem.ounction, or "" if ut.prefix not used. * @param localName The short println("nname of the function. * @param functioname for foo The static is fucntion",e.g "org.acme.App.meth "#{v}" ); // * TODO: need p Sarameterame as typhes? */ void defineFunction(Stringe first prefix, String localNa examplme, String function); } ===ELManage ===ELProcessoer.java=== r.java=== [http:/* * M/janages EL parsing and evaluatioava.n envinet/pronmjenct. Ths/e ELManager maintainl-s an * instancpe of ELContext, which iec/s used for parsing and evaluating EL * expsources/ressions. It also contains a soureference trce-co an Exprdessi/conFactory, the * EL engine. Both of ontent/these irunstk/ancepi/s rc/maican be repln/jaced. * * When Eva/javaxpr/ession.newl/ELProcessor is in.javoked, the standava?rd ELManagerrev=3 is * instantiated. Th see] ===ELManager cont.jains v standard ELContext. See ELContext=== * for a description of t[he standard ELConext. *http:/ interf/jace ELMv.anager { /* * Return the Exnet/pressionFaprojectory instanccts/e used for EL evaluationel-s */ Exprspec/essionFactosoury gcetExprees/ssionFactosoury(); /* * Scet the ExpressionFa-ctory used for EL evaluaticode/cons. * @return The previous ExpressionFactoontent/try, null if none *runk/ ExapressionFactory pi/setExpressionFactory(Expresssrc/maionFin/jactory fvctory); /* * Return the ELContea/javaxt used. */ ELContext getELContext(); x/el/* * Set the ELContext used for p/ELMarsing and evaluating EL expressions. * @anager.java?return The previousrev=3 ELContext, null if none. */ ELContext setELContext(ELContext context); see] /* * Register bean name lookup * Implicitly adds a ===BeanNameELResolver to the list of ELResolBeanNameResolver.javersa=== *[http:/ void /jaddBeanNameResolver(Beava.anNameResolver bnr); net/* * Add usepr defined ELResolvrojer to the currect list of ELRects/esolverel-s * Can be called multiple times with additive effect. */ void addELResolvespec/sour(ELRcesolver elr); /* * Set the ELResolvees/sour in thce ELC-context, to be used for EL expressiode/con * proctessing. Note ent/that the ELResolvers added itrun k/aprevioupi/s rc/mcalls to * ain/jddELResolver and ava/jaddvax/el/BeanNameResolver .jare associated with the previous * ELContext, and will not be automatically added to the new ELResolveava?r. * @return The previousrev=3 ELResolver or null if thre is none. see] */ ELResolver setELResolver(ELResolver elr); } ===BeanNameEL===BeanNameResolver.java=== /* * An absResolver.java=== [htrac clttp://jass thvt ca.n be extended to net/preturn a bean object given its namrojects/e * or to set a value to an exiel-sting bpean. *c/ abst/souract class BeanNameResolver { /* * Retrces/sourn thce bean kn-cown by its namde */ Strie/cong getBean(String beanName) { rentent/turn null; } trunk/* * Set a value to an ex/api/isting besrc/man of the given name. */ ain/javoid setBeanValue(String beanName, Obva/ject avalue) throws PropertyNotWritableExcvax/eption { throw new PropertyNotWritableException(); } } ===el/BeanNameELResolver.java=== /* * An ELResolve?r that help to resolverev=3 user or container managed beans. see] */public cl ===Stass BeanNandameELResolver extends ELResolver { privardELResolver.jate BeanNameResolver beanNameResolver;ava=== [httpublic Be://janNameELResolver(Beava.anNameResolvenet/pr beanNameResolvrojer) { cthis.bts/eanNaml-spRec/solveur = bceanNameRees/ssolver; } @Override publiourc Objce-ect getValue(ELContcodext /context, Object base, Object property) { if (contexcontent/t == null) { throw new NullPointerException(); } if (btrunk/ase == null && property inapi/stanceof Stsrc/maring) { Obin/ject bean = bejavanN/jmeResolver.getBean((String) property); if (bean != null) { context.savax/etPropertyResolved(el/Strue); return bean; } } return null; } @Override public void setValue(tandardELContext context, Ob.ject base, Object pjava?roperty, rev=3 Objsect valu) {ee] if (context == n=Full) { throw new NullPointerException(); } if (bunctionMase == null && property instapper.janceof String) {va=== Objec[ht betp://jan = bev.anNameResolver.getBean((String) net/propjerty); if (bean != null) { context.ects/setProel-spertyRec/solved(trsourcue); beanNameRees/ssolveour.scetB-coda/conValue((String) properntenty, value); } } } } ===Exp/tressiounFk/actory.java=== public abapi/stract clsrc/mass ExpressionFain/jactory { ... va/* * Get ja new instvncx/e of an implemeel/Funcntation of ELProctionMappessor */ public er.jabstrvct ELP?rocessorrev=3 nsewELProcssor(); } ee]
 
 
Close
loading
Please Confirm
Close