org.apache.commons.digester.xmlrules

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 » XML » digester » org.apache.commons.digester.xmlrules 
org.apache.commons.digester.xmlrules
Documentation for org.apache.commons.digester.xmlrules

The xmlrules package provides for XML-based definition of rules for Digester. This improves maintainability of Java code, as rules are now defined in XML and read into Digester at run-time.



Introduction

This is a brief overview of the digester-rules-in-XML feature. Briefly, this feature lets you define Digester rules in XML, instead of creating and initializing the Rules objects programmatically, which can become tedious. In addition, it allows for including of one XML rules file within another, inclusion of programmatically created rule sets within an XML file (via reflection), and recursively nested matching pattern specifications.

Overview of digester-rules.dtd

A DTD, named digester-rules.dtd has been defined to help in the understanding of how the loader operates.

The DTD is distributed in the commons-digester.jar. It can be found at org/apache/commons/digester/xmlrules/digester-rules.dtd. It is not available for download from the Apache website since users are best advised to use a copy stored on their local system.

Digester input documents wishing to cite this DTD should include the following DOCTYPE declaration:

  <!DOCTYPE digester-rules PUBLIC
   "-//Jakarta Apache //DTD digester-rules XML V1.0//EN"
   "digester-rules.dtd">

Rule elements:

The DTD defines an element type corresponding to each predefined Digester rule. Each rule element type includes attributes for values needed to initialize the rule, and an optional pattern attribute specifying the pattern to associate with the rule.

The DigesterLoader adds the rules to the digester in the order in which they occur in the XML.

The use of each rule element type should be self-explanatory, if you compare them to the API documentation for the Digester rules classes.

Defining matching patterns:

The matching pattern is a simple, xpath-like string which the Digester uses to determine which elements to apply each rule to. See the Digester documentation for more details.
There are two methods for associating patterns to rules in the XML file. One is for each rule element to directly define its pattern in a pattern attribute. An example would like something like:

      <digester-rules>
        <object-create-rule pattern="*/foo" classname="Foo"/>
        <set-properties-rule pattern="*/foo"/>
      </digester-rules>

In the above example, an ObjectCreateRule is created and associated with the pattern "*/foo"; then a SetPropertiesRule is created and associated with the pattern "*/foo".

The other method is to nest rules elements inside a <pattern> element. In this way, the same pattern can be defined for a group of rules. The following example has the same effect as the previous example:
       <digester-rules>
         <pattern value="*/foo">
           <object-create-rule classname="Foo"/>
           <set-properties-rule/>
         </pattern>
       </digester-rules>


Pattern elements can be recursively nested. If patterns are nested, the pattern string is formed by concatenating all the patterns together. Example:
       <digester-rules>
         <pattern value="*/foo">
           <pattern value="bar">
             <object-create-rule classname="Foobar"/>
             <set-properties-rule/>
           </pattern>
         </pattern>
       </digester-rules>

In the above example, an ObjectCreateRule and a SetPropertiesRule are associated with the matching pattern "*/foo/bar".
The use of pattern elements and the use of the pattern attribute inside rules elements can be freely mixed. The next example has the same effect as the previous example:
       <digester-rules>
         <pattern value="*/foo">
           <object-create-rule pattern="bar" classname="Foobar"/>
           <set-properties-rule pattern="bar"/>
         </pattern>
       </digester-rules>

Including rules XML files within other rules XML files:

The <include> element lets you include one rules file within another. With respect to pattern concatenation, the DigesterLoader behaves as if the include file was 'macro-expanded'. Example:

      File rules1.xml:
         <?xml version="1.0"?>
         <!DOCTYPE digester-rules SYSTEM "digester-rules.dtd">

         <digester-rules>
           <pattern value="root/foo">
             <object-create-rule classname="Foo"/>

             <include path="rules2.xml"/>
           </pattern>
         </digester-rules>


      File rules2.xml:
         <?xml version="1.0"?>
         <!DOCTYPE digester-rules SYSTEM "digester-rules.dtd">

         <digester-rules>
           <pattern value="bar">
             <object-create-rule classname="Bar"/>
           </pattern>
         </digester-rules>

Parsing rule1.xml would result in a Digester initialized with these pattern/rule pairs:

    root/foo -> ObjectCreateRule(Foo)
    root/foo/bar -> ObjectCreateRule(Bar)

Note that the pattern for the 'bar' rule has been prepended with the 'root/foo' pattern. If rule2.xml was parsed by itself, it would yield a Digester initialized with this pattern/rule:

    bar -> ObjectCreateRule(Bar)

Including programmatically-created rules:

Sometimes rules cannot be easily defined via XML. Rule sets that are created programmatically can still be included within a digester-rules XML file. This is done by using an <include> element with a class attribute, containing the name of a class that implements org.apache.commons.digester.xmlrules.DigesterRulesSource. This interface defines one method, getRules(Digester), which creates rules and adds them to the supplied Digester. The pattern concatenation works exactly as if the rules had been included from an XML file. Example:

      File rules3.xml:
         <?xml version="1.0"?>
         <!DOCTYPE digester-rules SYSTEM "digester-rules.dtd">

         <digester-rules>
           <pattern value="root/foo">
             <object-create-rule classname="Foo"/>

             <include class="BarRuleCreator"/>
           </pattern>
         </digester-rules>

BarRuleCreator class definition:
          public class BarRuleCreator implements DigesterRulesSource {
              public void getRules(Digester digester) {
                  digester.addObjectCreate("bar", "Bar");
              }
          }

Parsing rules3.xml yields the same results as rules1.xml above:

    root/foo -> ObjectCreateRule(Foo)
    root/foo/bar -> ObjectCreateRule(Bar)

Creating a digester from XML:

FromXmlRuleSet is a RuleSet implementation that initializes its Digester from rules defined in an XML file. The path to the XML file is passed to constructor.

Alternatively, the convenience class DigesterLoader defines a static method, Digester createDigester(String rulesXml) throws DigesterLoaderException". When passing the name of the file that contains your digester rules, this method returns a Digester instance initialized with the rules.

To add your own rules, you need to:

  • Update the DTD
    You should add an element type for your rule. The element should have an attribute corresponding to each of the rule's initialization parameters.
  • Define an ObjectCreationFactory
  • Extend DigesterRuleParser
    DigesterRuleParser is a RuleSet for parsing a rules XML file. You should extend this, and override the addRuleInstances() method to add the rules for parsing your new element. Look in DigesterRuleParser.java to see how its done.

Java Source File NameTypeComment
CallParamTestObject.javaClass Object for use with testing call param rules.
CircularIncludeException.javaClass Thrown when parsing XML into Digester rules, if a circular inclusion occurred in the xml digester rules files.
DigesterLoader.javaClass This class manages the creation of Digester instances from XML digester rules files.
DigesterLoaderRulesTest.javaClass
DigesterLoaderTest.javaClass Tests loading Digester rules from an XML file.
author:
   David H.
DigesterLoaderTestSuite.javaClass Tests loading Digester rules from an XML file.
DigesterLoadingException.javaClass Thrown when an error occurs while parsing XML into Digester rules.
DigesterPatternStackTest.javaClass This test case tests the behavior of DigesterRuleParser.PatternStack, a specialized stack whose toString() method returns a /-separated representation of the stack's elements.
DigesterRuleParser.javaClass This is a RuleSet that parses XML into Digester rules, and then adds those rules to a 'target' Digester.
DigesterRulesSource.javaInterface Interface for classes that initialize a Digester Rules object with Digester Rules.
FromXmlRuleSet.javaClass A Digester rule set where the rules come from an XML file.
FromXmlRuleSetTest.javaClass Tests loading Digester rules from an XML file.
IncludeTest.javaClass
TestDigesterRulesSource.javaClass A test class, for validating FromXmlRuleSet's ability to 'include' programmatically-created rules from within an XML rules file.
author:
   David H.
TestObject.javaClass Test harness object for holding results of digestion.
author:
   David H.
ThrowExceptionCreationFactory.javaClass Object creation factory used for testing exception propagation.
XmlLoadException.javaClass Thrown when an error occurs while parsing XML into Digester rules.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.