Java Doc for SetNestedPropertiesRule.java in  » Library » Apache-commons-digester-1.8-src » org » apache » commons » digester » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons digester 1.8 src » org.apache.commons.digester 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.digester.Rule
      org.apache.commons.digester.SetNestedPropertiesRule

SetNestedPropertiesRule
public class SetNestedPropertiesRule extends Rule (Code)

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]
 

For each child element of [widget], a corresponding setter method is located on the object on the top of the digester stack, the body text of the child element is converted to the type specified for the (sole) parameter to the setter method, then the setter method is invoked.

This rule supports custom mapping of xml element names to property names. The default mapping for particular elements can be overridden by using SetNestedPropertiesRule.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.

Note that this rule is designed to be used to set only "primitive" bean properties, eg String, int, boolean. If some of the child xml elements match ObjectCreateRule rules (ie cause objects to be created) then you must use one of the more complex constructors to this rule to explicitly skip processing of that xml element, and define a SetNextRule (or equivalent) to handle assigning the child object to the appropriate property instead.

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.

TODO: Optimise this class. Currently, each time begin is called, new AnyChildRules and AnyChildRule objects are created. It should be possible to cache these in normal use (though watch out for when a rule instance is invoked re-entrantly!).


since:
   1.6



Constructor Summary
public  SetNestedPropertiesRule()
     Base constructor, which maps every child element into a bean property with the same name as the xml element.
public  SetNestedPropertiesRule(String elementName, String propertyName)
    

Convenience constructor which overrides the default mappings for just one property.

For details about how this works, see SetNestedPropertiesRule.SetNestedPropertiesRule(String[] elementNames,String[] propertyNames) .


Parameters:
  elementName - is the child xml element to match
Parameters:
  propertyName - is the java bean property to be assigned the value of the specified xml element.
public  SetNestedPropertiesRule(String[] elementNames, String[] propertyNames)
    

Constructor which allows element->property mapping to be overridden.

Two arrays are passed in.


Method Summary
public  voidaddAlias(String elementName, String propertyName)
     Add an additional custom xml-element -> property mapping.

This is primarily intended to be used from the xml rules module (as it is not possible there to pass the necessary parameters to the constructor for this class).

public  voidbegin(String namespace, String name, Attributes attributes)
     Process the beginning of this element.
public  voidbody(String bodyText)
     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.
public  booleangetAllowUnknownChildElements()
     See SetNestedPropertiesRule.setAllowUnknownChildElements .
public  booleangetTrimData()
     See SetNestedPropertiesRule.setTrimData .
public  voidsetAllowUnknownChildElements(boolean allowUnknownChildElements)
     Determines whether an error is reported when a nested element is encountered for which there is no corresponding property-setter method.
public  voidsetDigester(Digester digester)
     Invoked when rule is added to digester.
public  voidsetTrimData(boolean trimData)
     When set to true, any text within child elements will have leading and trailing whitespace removed before assignment to the target object.
public  StringtoString()
     Render a printable version of this Rule.


Constructor Detail
SetNestedPropertiesRule
public SetNestedPropertiesRule()(Code)
Base constructor, which maps every child element into a bean property with the same name as the xml element.

It is an error if a child xml element exists but the target java bean has no such property (unless setAllowUnknownChildElements has been set to true).




SetNestedPropertiesRule
public SetNestedPropertiesRule(String elementName, String propertyName)(Code)

Convenience constructor which overrides the default mappings for just one property.

For details about how this works, see SetNestedPropertiesRule.SetNestedPropertiesRule(String[] elementNames,String[] propertyNames) .


Parameters:
  elementName - is the child xml element to match
Parameters:
  propertyName - is the java bean property to be assigned the value of the specified xml element. This may be null, in which case the specified xml element will be ignored.



SetNestedPropertiesRule
public SetNestedPropertiesRule(String[] elementNames, String[] propertyNames)(Code)

Constructor which allows element->property mapping to be overridden.

Two arrays are passed in. One contains xml element names and the other java bean property names. The element name / property name pairs are matched by position; in order words, the first string in the element name array corresponds to the first string in the property name array and so on.

If a property name is null or the xml element name has no matching property name due to the arrays being of different lengths then this indicates that the xml element should be ignored.

Example One

The following constructs a rule that maps the alt-city element to the city property and the alt-state to the state property. All other child elements are mapped as usual using exact name matching.

 SetNestedPropertiesRule(
 new String[] {"alt-city", "alt-state"}, 
 new String[] {"city", "state"});
 

Example Two

The following constructs a rule that maps the class xml element to the className property. The xml element ignore-me is not mapped, ie is ignored. All other elements are mapped as usual using exact name matching.

 SetPropertiesRule(
 new String[] {"class", "ignore-me"}, 
 new String[] {"className"});
 


Parameters:
  elementNames - names of elements to map
Parameters:
  propertyNames - names of properties mapped to




Method Detail
addAlias
public void addAlias(String elementName, String propertyName)(Code)
Add an additional custom xml-element -> property mapping.

This is primarily intended to be used from the xml rules module (as it is not possible there to pass the necessary parameters to the constructor for this class). However it is valid to use this method directly if desired.




begin
public void begin(String namespace, String name, Attributes attributes) throws Exception(Code)
Process the beginning of this element.
Parameters:
  namespace - is the namespace this attribute is in, or null
Parameters:
  name - is the name of the current xml element
Parameters:
  attributes - is the attribute list of this element



body
public void body(String bodyText) throws Exception(Code)
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.



getAllowUnknownChildElements
public boolean getAllowUnknownChildElements()(Code)
See SetNestedPropertiesRule.setAllowUnknownChildElements .



getTrimData
public boolean getTrimData()(Code)
See SetNestedPropertiesRule.setTrimData .



setAllowUnknownChildElements
public void setAllowUnknownChildElements(boolean allowUnknownChildElements)(Code)
Determines whether an error is reported when a nested element is encountered for which there is no corresponding property-setter method.

When set to false, any child element for which there is no corresponding object property will cause an error to be reported.

When set to true, any child element for which there is no corresponding object property will simply be ignored.

The default value of this attribute is false (unknown child elements are not allowed).




setDigester
public void setDigester(Digester digester)(Code)
Invoked when rule is added to digester.



setTrimData
public void setTrimData(boolean trimData)(Code)
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()(Code)
Render a printable version of this Rule.



Fields inherited from org.apache.commons.digester.Rule
protected Digester digester(Code)(Java Doc)
protected String namespaceURI(Code)(Java Doc)

Methods inherited from org.apache.commons.digester.Rule
public void begin(Attributes attributes) throws Exception(Code)(Java Doc)
public void begin(String namespace, String name, Attributes attributes) throws Exception(Code)(Java Doc)
public void body(String text) throws Exception(Code)(Java Doc)
public void body(String namespace, String name, String text) throws Exception(Code)(Java Doc)
public void end() throws Exception(Code)(Java Doc)
public void end(String namespace, String name) throws Exception(Code)(Java Doc)
public void finish() throws Exception(Code)(Java Doc)
public Digester getDigester()(Code)(Java Doc)
public String getNamespaceURI()(Code)(Java Doc)
public void setDigester(Digester digester)(Code)(Java Doc)
public void setNamespaceURI(String namespaceURI)(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.