js.template.xhtml.AttrOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of js-xhtml-template Show documentation
Show all versions of js-xhtml-template Show documentation
Reference implementation for j(s)-lib template API, declarative, natural and based on X(HT)ML language.
The newest version!
package js.template.xhtml;
import java.util.HashSet;
import java.util.Set;
import js.dom.Attr;
import js.dom.Element;
import js.lang.Pair;
import js.lang.PairsList;
import js.template.TemplateException;
/**
* Set one or more element's attributes values. This operator is the main means to set element attributes value. There are also
* specialized, convenient operators for common HTML attributes: id, value, src, href and title.
*
*
* <img data-attr="src:picture;title:description;" />
*
*
* Operand is an expression describing attribute name/property path, with next syntax:
*
*
* expression := attrProperty (';' attrProperty)* ';'?
* attrProperty : = attributeName ':' propertyPath
* propertyPath := used to extract content value
*
*
* @author Iulian Rotaru
*/
final class AttrOperator extends Operator
{
/**
* Dynamic content reference.
*/
private Content content;
/**
* Construct ATTR operator instance.
*
* @param content dynamic content.
*/
AttrOperator(Content content)
{
this.content = content;
}
/**
* Execute ATTR operator. Expression argument is set of attribute name / property path pairs. Property path is used to
* retrieve content value that is converted to string and used as attribute value.
*
* @param element context element, unused,
* @param scope scope object,
* @param expression set of attribute name / property path pairs,
* @param arguments optional arguments, unused.
* @return always returns null for void.
* @throws TemplateException if expression is empty or not well formatted or if requested content value is undefined.
*/
@Override
protected Object doExec(Element element, Object scope, String expression, Object... arguments) throws TemplateException
{
if(expression.isEmpty()) {
throw new TemplateException("Invalid ATTR operand. Attribute property path expression is empty.");
}
Set syntheticAttributes = new HashSet();
PairsList pairs = new PairsList(expression);
for(Pair pair : pairs) {
// accordingly this operator expression syntax first value is attribute name and second is property path
String value = content.getString(scope, pair.second());
if(value != null) {
syntheticAttributes.add(new AttrImpl(pair.first(), value));
}
}
return syntheticAttributes;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy