Rule implementation that sets properties on the object at the top of the
stack, based on child elements with names matching properties on that
object.
Example input that can be processed by this rule:
[widget]
[height]7[/height]
[width]8[/width]
[label]Hello, world[/label]
[/widget]
This rule supports custom mapping of attribute names to property names.
The default mapping for particular attributes can be overridden by using
SetNestedPropertiesRule(String[] elementNames,
String[] propertyNames)
.
This allows child elements to be mapped to properties with different names.
Certain elements can also be marked to be ignored.
A very similar effect can be achieved using a combination of the
BeanPropertySetterRule
and the
ExtendedBaseRules
rules manager; this
Rule
, however, works fine with the default
RulesBase
rules manager.
Implementation Notes
This class works by creating its own simple Rules implementation. When
begin is invoked on this rule, the digester's current rules object is
replaced by a custom one. When end is invoked for this rule, the original
rules object is restored. The digester rules objects therefore behave in
a stack-like manner.
For each child element encountered, the custom Rules implementation
ensures that a special AnyChildRule instance is included in the matches
returned to the digester, and it is this rule instance that is responsible
for setting the appropriate property on the target object (if such a property
exists). The effect is therefore like a "trailing wildcard pattern". The
custom Rules implementation also returns the matches provided by the
underlying Rules implementation for the same pattern, so other rules
are not "disabled" during processing of a SetNestedPropertiesRule.
addAlias
public void addAlias(String elementName,
String propertyName)
Add an additional element name to property name mapping.
This is intended to be used from the xml rules.
begin
public void begin(String namespace,
String name,
Attributes attributes)
throws Exception
Process the beginning of this element.
- begin in interface Rule
namespace
- is the namespace this attribute is in, or nullname
- is the name of the current xml elementattributes
- is the attribute list of this element
body
public void body(String bodyText)
throws Exception
This is only invoked after all child elements have been processed,
so we can remove the custom Rules object that does the
child-element-matching.
- body in interface Rule
getAllowUnknownChildElements
public boolean getAllowUnknownChildElements()
getTrimData
public boolean getTrimData()
setAllowUnknownChildElements
public void setAllowUnknownChildElements(boolean allowUnknownChildElements)
When set to true, any child element for which there is no
corresponding object property will cause an error to be reported.
The default value of this attribute is false (not allowed).
setDigester
public void setDigester(Digester digester)
Invoked when rule is added to digester.
- setDigester in interface Rule
setTrimData
public void setTrimData(boolean trimData)
When set to true, any text within child elements will have leading
and trailing whitespace removed before assignment to the target
object. The default value for this attribute is true.
toString
public String toString()
Render a printable version of this Rule.