Custom evaluation of EL expressions - extend ExpressionEvaluator?

I have a model class (corresponding to a database table) with some exclusion flags, where the flags indicated that a field should not be displayed on a page where we choose to apply exclusions. For example, a person may request that their username and date of birth not be listed in a directory search, yet we would still want to show the username/birthdate on a page where the user is looking at their own information. For example:

publicclass Person{

private String name;

private Date birthDate;

private String username;

privateboolean excludeBirthDate;

privateboolean excludeUsername;

// getters and setters

}

I want to write a tag such that I can say in my JSP:

<page:applyExclusions>

...

${person.username} ${person.name} ${person.birthDate}

...

</page:applyExclusions>

and have only the person's name display (if they have requested that the other fields be excluded).

Looking at the tags API, it seems like I would want to have my own ExpressionEvaluator whose evaluate method says something like

public Object evaluate(String expr, Class type, VariableResolver resolver, FunctionMapper mapper){

if (type.equals(Person.class) &&

expr.equals([i]someExcludedField[/i])){

// return empty String or something to say "don't display the actual value"

}

else{

// ok to display value, evaluate like normal

return super.evaluate(expr, type, resolver, mapper);

}

}

Not sure if I'm on the right track with my idea or if there's a better way to accomplish this?

[2545 byte] By [ima.developera] at [2007-10-2 18:32:26]
# 1

I think the better place to put this would be the get/set method for this class.

so for instance

public Date getBirthDate(){

if (excludeBirthDate && Security.isExclusionActivated()){

// exclusion is on, so don't return this field.

return null;

}

else{

return birthDate;

}

}

The question is, how would Security.isExclusionActivated() be able to tell if it was activated or not? You don't normally have access to request/session attributes which would be the easiest place to put a flag.

Good luck,

evnafets

evnafetsa at 2007-7-13 19:53:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

An expression evaluator is exactly that - searches for some variables in scope and performs evaluation on them according to a set of rules.

IMO you shouldnt be changing this functionality to include your business logic there. You should be writing your own custom tags for such pruposes, something like

<myTag:applyExclusions username="${userName}" name="${name}" birthDate="${brithDate}/>

And in your tag code, you would have

public int doEndTag() throws JspException {

try {

pageContext.getOut().println("UserName "+ userName);

if(user is viewing the page)//your logic here{

pageContext.getOut().println("Name "+ name);

pageContext.getOut().println("Birth Date "+ birthDate);

}

} catch (IOException exp) {

throw new JspException(exp);

}

return EVAL_PAGE;

}

where userName, name and birthDate are attribute properties of the tag with get and set methods as evnafets suggested.

Hope that helps.

cheers,

ram.>

Madathil_Prasada at 2007-7-13 19:53:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...