mirror of
https://github.com/ethauvin/rife2.git
synced 2025-05-01 02:58:12 -07:00
Added supported for template attributes.
Templates instantiated through the engine's Context, will have the context set as an attribute by default.
This commit is contained in:
parent
aab2eb7947
commit
f0b0fbce14
4 changed files with 207 additions and 8 deletions
|
@ -511,7 +511,9 @@ public class Context {
|
||||||
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
||||||
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
||||||
|
|
||||||
return TemplateFactory.HTML.get(name, encoding);
|
var template = TemplateFactory.HTML.get(name, encoding);
|
||||||
|
template.setAttribute(Context.class.getName(), this);
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -563,7 +565,9 @@ public class Context {
|
||||||
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
||||||
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
||||||
|
|
||||||
return TemplateFactory.TXT.get(name, encoding);
|
var template = TemplateFactory.TXT.get(name, encoding);
|
||||||
|
template.setAttribute(Context.class.getName(), this);
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -615,7 +619,9 @@ public class Context {
|
||||||
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
||||||
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
||||||
|
|
||||||
return TemplateFactory.XML.get(name, encoding);
|
var template = TemplateFactory.XML.get(name, encoding);
|
||||||
|
template.setAttribute(Context.class.getName(), this);
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -667,7 +673,9 @@ public class Context {
|
||||||
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
||||||
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
||||||
|
|
||||||
return TemplateFactory.JSON.get(name, encoding);
|
var template = TemplateFactory.JSON.get(name, encoding);
|
||||||
|
template.setAttribute(Context.class.getName(), this);
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -719,7 +727,9 @@ public class Context {
|
||||||
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
if (null == name) throw new IllegalArgumentException("name can't be null.");
|
||||||
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
if (0 == name.length()) throw new IllegalArgumentException("name can't be empty.");
|
||||||
|
|
||||||
return TemplateFactory.SVG.get(name, encoding);
|
var template = TemplateFactory.SVG.get(name, encoding);
|
||||||
|
template.setAttribute(Context.class.getName(), this);
|
||||||
|
return template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,6 +26,7 @@ public abstract class AbstractTemplate implements Template {
|
||||||
protected TemplateEncoder encoder_ = EncoderDummy.instance();
|
protected TemplateEncoder encoder_ = EncoderDummy.instance();
|
||||||
protected List<ResourceBundle> defaultResourceBundles_ = null;
|
protected List<ResourceBundle> defaultResourceBundles_ = null;
|
||||||
protected List<ResourceBundle> resourceBundles_ = null;
|
protected List<ResourceBundle> resourceBundles_ = null;
|
||||||
|
protected Map<String, Object> attributes_ = null;
|
||||||
protected String language_ = null;
|
protected String language_ = null;
|
||||||
protected String defaultContentType_ = null;
|
protected String defaultContentType_ = null;
|
||||||
|
|
||||||
|
@ -874,6 +875,52 @@ public abstract class AbstractTemplate implements Template {
|
||||||
return language_;
|
return language_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAttribute(String name, Object value) {
|
||||||
|
if (null == attributes_) {
|
||||||
|
attributes_ = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
attributes_.put(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttributes(Map<String, Object> map) {
|
||||||
|
if (null == attributes_) {
|
||||||
|
attributes_ = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
attributes_.putAll(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAttribute(String name) {
|
||||||
|
if (null == attributes_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
attributes_.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAttribute(String name) {
|
||||||
|
if (null == attributes_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return attributes_.containsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
if (null == attributes_) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return attributes_.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getAttributes() {
|
||||||
|
if (attributes_ == null) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.unmodifiableMap(attributes_);
|
||||||
|
}
|
||||||
|
|
||||||
final void initialize()
|
final void initialize()
|
||||||
throws TemplateException {
|
throws TemplateException {
|
||||||
evaluateL10nTags();
|
evaluateL10nTags();
|
||||||
|
@ -959,6 +1006,10 @@ public abstract class AbstractTemplate implements Template {
|
||||||
new_template.constructedValues_.put(constructed_value_id, constructedValues_.get(constructed_value_id));
|
new_template.constructedValues_.put(constructed_value_id, constructedValues_.get(constructed_value_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (attributes_ != null) {
|
||||||
|
new_template.attributes_ = new HashMap<>(attributes_);
|
||||||
|
}
|
||||||
|
|
||||||
return new_template;
|
return new_template;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1493,6 +1493,92 @@ public interface Template extends Cloneable {
|
||||||
*/
|
*/
|
||||||
String getLanguage();
|
String getLanguage();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an attribute that will be associated with this template instance.
|
||||||
|
* <p>
|
||||||
|
* For general purpose attributes of object instances, it's recommended to
|
||||||
|
* use the fully qualified classname of the object as the attribute name.
|
||||||
|
*
|
||||||
|
* @param name the name of the attribute
|
||||||
|
* @param value the value of the attribute
|
||||||
|
* @see #setAttributes
|
||||||
|
* @see #removeAttribute
|
||||||
|
* @see #hasAttribute
|
||||||
|
* @see #getAttribute
|
||||||
|
* @see #getAttributes
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
void setAttribute(String name, Object value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the given attributes to the corresponding values.
|
||||||
|
* Calling this method is equivalent to calling {@link #setAttribute
|
||||||
|
* setAttribute} for each entry in the given map.
|
||||||
|
*
|
||||||
|
* @param map a map of attribute name and values
|
||||||
|
* @see #setAttribute
|
||||||
|
* @see #removeAttribute
|
||||||
|
* @see #hasAttribute
|
||||||
|
* @see #getAttribute
|
||||||
|
* @see #getAttributes
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
void setAttributes(Map<String, Object> map);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an attribute from this template instance.
|
||||||
|
*
|
||||||
|
* @param name the name of the attribute
|
||||||
|
* @see #setAttribute
|
||||||
|
* @see #setAttributes
|
||||||
|
* @see #hasAttribute
|
||||||
|
* @see #getAttribute
|
||||||
|
* @see #getAttributes
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
void removeAttribute(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether an attribute exists in this template instance.
|
||||||
|
*
|
||||||
|
* @return {@code true} if the attribute exists; {@code false} otherwise
|
||||||
|
* @see #setAttribute
|
||||||
|
* @see #removeAttribute
|
||||||
|
* @see #setAttributes
|
||||||
|
* @see #getAttribute
|
||||||
|
* @see #getAttributes
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
boolean hasAttribute(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of an attribute that was {@linkplain #setAttribute set}
|
||||||
|
* in this template instance.
|
||||||
|
*
|
||||||
|
* @return the attributes value; or {@code null} of no such attribute could be found
|
||||||
|
* @see #setAttribute
|
||||||
|
* @see #removeAttribute
|
||||||
|
* @see #setAttributes
|
||||||
|
* @see #hasAttribute
|
||||||
|
* @see #getAttributes
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
Object getAttribute(String name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name and value of all attributes that
|
||||||
|
* have been {@linkplain #setAttribute set} in this template instance.
|
||||||
|
*
|
||||||
|
* @return the attributes currently set in this template
|
||||||
|
* @see #setAttribute
|
||||||
|
* @see #removeAttribute
|
||||||
|
* @see #setAttributes
|
||||||
|
* @see #hasAttribute
|
||||||
|
* @see #getAttribute
|
||||||
|
* @since 1.5
|
||||||
|
*/
|
||||||
|
Map<String, Object> getAttributes();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of URL's that this template depends on, and their last
|
* Returns a list of URL's that this template depends on, and their last
|
||||||
* modification dates (in milliseconds since the Unix epoch). This method
|
* modification dates (in milliseconds since the Unix epoch). This method
|
||||||
|
|
|
@ -13,9 +13,7 @@ import rife.tools.ExceptionUtils;
|
||||||
import rife.tools.StringUtils;
|
import rife.tools.StringUtils;
|
||||||
import rife.validation.ConstrainedProperty;
|
import rife.validation.ConstrainedProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@ -1080,4 +1078,58 @@ public class TestTemplate {
|
||||||
fail(ExceptionUtils.getExceptionStackTrace(e));
|
fail(ExceptionUtils.getExceptionStackTrace(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAttributes() {
|
||||||
|
var template = TemplateFactory.HTML.get("empty");
|
||||||
|
assertNotNull(template.getAttributes());
|
||||||
|
assertEquals(0, template.getAttributes().size());
|
||||||
|
|
||||||
|
assertNull(template.getAttribute("date"));
|
||||||
|
assertNull(template.getAttribute("template"));
|
||||||
|
assertFalse(template.hasAttribute("date"));
|
||||||
|
assertFalse(template.hasAttribute("template"));
|
||||||
|
var date = new Date();
|
||||||
|
template.setAttribute("date", date);
|
||||||
|
template.setAttribute("template", template);
|
||||||
|
|
||||||
|
assertSame(date, template.getAttribute("date"));
|
||||||
|
assertSame(template, template.getAttribute("template"));
|
||||||
|
assertTrue(template.hasAttribute("date"));
|
||||||
|
assertTrue(template.hasAttribute("template"));
|
||||||
|
|
||||||
|
assertEquals(2, template.getAttributes().size());
|
||||||
|
assertSame(date, template.getAttributes().get("date"));
|
||||||
|
assertSame(template, template.getAttributes().get("template"));
|
||||||
|
|
||||||
|
template.removeAttribute("date");
|
||||||
|
assertNull(template.getAttribute("date"));
|
||||||
|
assertSame(template, template.getAttribute("template"));
|
||||||
|
|
||||||
|
assertFalse(template.hasAttribute("date"));
|
||||||
|
assertTrue(template.hasAttribute("template"));
|
||||||
|
|
||||||
|
assertEquals(1, template.getAttributes().size());
|
||||||
|
assertNull(template.getAttributes().get("date"));
|
||||||
|
assertSame(template, template.getAttributes().get("template"));
|
||||||
|
|
||||||
|
var map = new HashMap<String, Object>();
|
||||||
|
var date2 = new Date();
|
||||||
|
var cal = Calendar.getInstance();
|
||||||
|
map.put("date", date2);
|
||||||
|
map.put("cal", cal);
|
||||||
|
|
||||||
|
assertFalse(template.hasAttribute("date"));
|
||||||
|
assertTrue(template.hasAttribute("template"));
|
||||||
|
assertFalse(template.hasAttribute("cal"));
|
||||||
|
|
||||||
|
template.setAttributes(map);
|
||||||
|
assertEquals(3, template.getAttributes().size());
|
||||||
|
assertTrue(template.hasAttribute("date"));
|
||||||
|
assertTrue(template.hasAttribute("template"));
|
||||||
|
assertTrue(template.hasAttribute("cal"));
|
||||||
|
assertSame(date2, template.getAttributes().get("date"));
|
||||||
|
assertSame(template, template.getAttributes().get("template"));
|
||||||
|
assertSame(cal, template.getAttributes().get("cal"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue