js.template.xhtml.SrcOperator 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.io.File;
import java.net.URL;
import js.converter.ConverterRegistry;
import js.dom.Element;
import js.template.TemplateException;
/**
* Set src attribute value.
*
*
* <img data-src="picture" />
*
*
* Operand is the property path used to get content value.
*
* @author Iulian Rotaru
*/
final class SrcOperator extends Operator {
/** Dynamic content reference. */
private Content content;
/**
* Construct SRC operator instance.
*
* @param content dynamic content.
*/
public SrcOperator(Content content) {
this.content = content;
}
/**
* Execute SRC operator. Uses property path to extract content value, convert it to string and set src attribute.
* If property value is null uses current element src attribute value, if any.
*
* @param element context element, unused,
* @param scope scope object,
* @param propertyPath property path,
* @param arguments optional arguments, unused.
* @return always returns null for void.
* @throws TemplateException if requested content value is undefined.
*/
@Override
protected Object doExec(Element element, Object scope, String propertyPath, Object... arguments) throws TemplateException {
if (!propertyPath.equals(".") && ConverterRegistry.hasType(scope.getClass())) {
throw new TemplateException("Operand is property path but scope is not an object.");
}
Object value = content.getObject(scope, propertyPath);
if (value == null) {
return null;
}
if (value instanceof URL) {
value = ((URL) value).toExternalForm();
}
if (value instanceof File) {
value = ((File) value).getPath();
}
if (!(value instanceof String)) {
throw new TemplateException("Invalid element |%s|. SRC operand should be URL, file or string.", element);
}
return new AttrImpl("src", (String) value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy