commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1128431 - /commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml
Date Fri, 27 May 2011 19:28:22 GMT
Author: simonetripodi
Date: Fri May 27 19:28:22 2011
New Revision: 1128431

URL: http://svn.apache.org/viewvc?rev=1128431&view=rev
Log:
re-re-reformatted code samples

Modified:
    commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml

Modified: commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml?rev=1128431&r1=1128430&r2=1128431&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml (original)
+++ commons/sandbox/digester3/trunk/src/site/xdoc/guide/binder.xml Fri May 27 19:28:22 2011
@@ -134,23 +134,24 @@ class EmployeeModule
   extends HttpServlet
 {
 
-  public void doGet(HttpServletRequest req, HttpServletResponse res)
-      throws ServletException, IOException {
-    Digester digester = new Digester();
-    digester.setNamespaceAware( true );
-    digester.setXIncludeAware( true );
-    digester.addObjectCreate( "employee", Employee.class );
-    digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
-    digester.addCallMethod( "employee/lastName", "setLastName", 0 );
-
-    digester.addObjectCreate( "employee/address", Address.class );
-    digester.addCallMethod( "employee/address/type", "setType", 0 );
-    digester.addCallMethod( "employee/address/city", "setCity", 0 );
-    digester.addCallMethod( "employee/address/state", "setState", 0 );
-    digester.addSetNext( "employee/address", "addAddress" );
+    public void doGet(HttpServletRequest req, HttpServletResponse res)
+        throws ServletException, IOException
+    {
+        Digester digester = new Digester();
+        digester.setNamespaceAware( true );
+        digester.setXIncludeAware( true );
+        digester.addObjectCreate( "employee", Employee.class );
+        digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
+        digester.addCallMethod( "employee/lastName", "setLastName", 0 );
+
+        digester.addObjectCreate( "employee/address", Address.class );
+        digester.addCallMethod( "employee/address/type", "setType", 0 );
+        digester.addCallMethod( "employee/address/city", "setCity", 0 );
+        digester.addCallMethod( "employee/address/state", "setState", 0 );
+        digester.addSetNext( "employee/address", "addAddress" );
 
-    Employee employee = digester.parse( openStream( req.getParameter( "employeeId" ) ) );
-    ...
+        Employee employee = digester.parse( openStream( req.getParameter( "employeeId" )
) );
+        ...
 }</source>
       <p>Nothing wrong with that approach but configuration is not reusable; the <i>RuleSet</i>
       interface fills in some way the reuse of configurations lack:</p>
@@ -158,18 +159,18 @@ class EmployeeModule
   implements RuleSet
 {
 
-  public void addRuleInstances( Digester digester )
-  {
-    digester.addObjectCreate( "employee", Employee.class );
-    digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
-    digester.addCallMethod( "employee/lastName", "setLastName", 0 );
-
-    digester.addObjectCreate( "employee/address", Address.class );
-    digester.addCallMethod( "employee/address/type", "setType", 0 );
-    digester.addCallMethod( "employee/address/city", "setCity", 0 );
-    digester.addCallMethod( "employee/address/state", "setState", 0 );
-    digester.addSetNext( "employee/address", "addAddress" );
-  }
+    public void addRuleInstances( Digester digester )
+    {
+        digester.addObjectCreate( "employee", Employee.class );
+        digester.addCallMethod( "employee/firstName", "setFirstName", 0 );
+        digester.addCallMethod( "employee/lastName", "setLastName", 0 );
+
+        digester.addObjectCreate( "employee/address", Address.class );
+        digester.addCallMethod( "employee/address/type", "setType", 0 );
+        digester.addCallMethod( "employee/address/city", "setCity", 0 );
+        digester.addCallMethod( "employee/address/state", "setState", 0 );
+        digester.addSetNext( "employee/address", "addAddress" );
+    }
 
 }</source>
       <p>then, in our sample servlet</p>
@@ -177,19 +178,20 @@ class EmployeeModule
   extends HttpServlet
 {
 
-  private final RuleSet employeeRuleSet = new EmployeeRuleSet();
+    private final RuleSet employeeRuleSet = new EmployeeRuleSet();
+
+    public void doGet(HttpServletRequest req, HttpServletResponse res)
+        throws ServletException, IOException
+    {
+        Digester digester = new Digester();
+        digester.setNamespaceAware( true );
+        digester.setXIncludeAware( true );
+
+        employeeRuleSet.addRuleInstances( digester );
 
-  public void doGet(HttpServletRequest req, HttpServletResponse res)
-      throws ServletException, IOException {
-    Digester digester = new Digester();
-    digester.setNamespaceAware( true );
-    digester.setXIncludeAware( true );
-
-    employeeRuleSet.addRuleInstances( digester );
-
-    Employee employee = digester.parse( openStream( req.getParameter( "employeeId" ) ) );
-    ...
-  }
+        Employee employee = digester.parse( openStream( req.getParameter( "employeeId" )
) );
+        ...
+    }
 
 }</source>
       <p>Nothing wrong again, but:</p>
@@ -202,41 +204,44 @@ class EmployeeModule
         methods/constructors arguments.</li>
       </ol>
       <p>In the new Digester, <i>RuleSet</i> has been suppressed in favor
of <i>RulesModule</i></p>
-      <source>class EmployeeModule extends AbstractRulesModule {
+      <source>class EmployeeModule
+    extends AbstractRulesModule
+{
 
-  @Override
-  protected void configure()
-  {
-    forPattern( "employee" ).createObject().ofType( Employee.class );
-    forPattern( "employee/firstName" ).setBeanProperty();
-    forPattern( "employee/lastName" ).setBeanProperty();
-
-    forPattern( "employee/address" )
-      .createObject().ofType( Address.class )
-      .then()
-      .setNext( "addAddress");
-    forPattern( "employee/address/type" ).setBeanProperty();
-    forPattern( "employee/address/city" ).setBeanProperty();
-    forPattern( "employee/address/state" ).setBeanProperty();
-  }
+    @Override
+    protected void configure()
+    {
+        forPattern( "employee" ).createObject().ofType( Employee.class );
+        forPattern( "employee/firstName" ).setBeanProperty();
+        forPattern( "employee/lastName" ).setBeanProperty();
+
+        forPattern( "employee/address" )
+            .createObject().ofType( Address.class )
+            .then()
+            .setNext( "addAddress");
+        forPattern( "employee/address/type" ).setBeanProperty();
+        forPattern( "employee/address/city" ).setBeanProperty();
+        forPattern( "employee/address/state" ).setBeanProperty();
+    }
 
 }</source>
       <p>Then, our sample Servlet become:</p>
       <source>public class EmployeeServlet
-  extends HttpServlet
+    extends HttpServlet
 {
 
-  private final DigesterLoader loader = newLoader( new EmployeeModule() )
-    .setNamespaceAware( true )
-    .setXIncludeAware( true );
-
-  public void doGet(HttpServletRequest req, HttpServletResponse res)
-      throws ServletException, IOException {
-    Digester digester = loader.newDigester()
-
-    Employee employee = digester.parse( openStream( req.getParameter("employeeId") ) );
-    ...
-  }
+    private final DigesterLoader loader = newLoader( new EmployeeModule() )
+        .setNamespaceAware( true )
+        .setXIncludeAware( true );
+
+    public void doGet(HttpServletRequest req, HttpServletResponse res)
+        throws ServletException, IOException
+    {
+        Digester digester = loader.newDigester()
+
+        Employee employee = digester.parse( openStream( req.getParameter("employeeId") )
);
+        ...
+    }
 
 }</source>
 
@@ -249,20 +254,20 @@ class EmployeeModule
     <section name="One single configuration point, one single universal loader">
       <p>As shown above, basic Digester2.X usage would be creating a Digester then
setting the rules:</p>
       <source>Digester digester = new Digester();
-  digester.addObjectCreate( "root", "org.apache.commons.digester.SimpleTestBean" );
-  digester.addBeanPropertySetter( "root", "alpha" );
-  digester.addBeanPropertySetter( "root/alpha", "beta" );
-  digester.addBeanPropertySetter( "root/delta", "delta" );</source>
+digester.addObjectCreate( "root", "org.apache.commons.digester.SimpleTestBean" );
+digester.addBeanPropertySetter( "root", "alpha" );
+digester.addBeanPropertySetter( "root/alpha", "beta" );
+digester.addBeanPropertySetter( "root/delta", "delta" );</source>
 
       <p>Alternatively, users can create the <code>Rules</code> instance,
set the rules and pass it to the Digester:</p>
       <source>ExtendedBaseRules rules = new ExtendedBaseRules();
-  rules.addRule( "root", new ObjectCreateRule( "org.apache.commons.digester.SimpleTestBean"
) );
-  rules.addRule( "root", new BeanPropertySetterRule( "alpha" ) );
-  rules.addRule( "root/alpha", new BeanPropertySetterRule( "beta" ) );
-  rules.addRule( "root/delta", new BeanPropertySetterRule( "delta" ) );
+rules.addRule( "root", new ObjectCreateRule( "org.apache.commons.digester.SimpleTestBean"
) );
+rules.addRule( "root", new BeanPropertySetterRule( "alpha" ) );
+rules.addRule( "root/alpha", new BeanPropertySetterRule( "beta" ) );
+rules.addRule( "root/delta", new BeanPropertySetterRule( "delta" ) );
 
-  Digester digester = new Digester();
-  digester.setRules( rules );</source>
+Digester digester = new Digester();
+digester.setRules( rules );</source>
 
       <p>Last, but not least, special loader classes have been created to gain more
benefits from <code>RuleSet</code>:
 like the <code>annotations</code> package <code>DigesterLoader</code>,
to avoid scanning class elements each time
@@ -270,8 +275,8 @@ users want to create a new Digester inst
       <source>import org.apache.commons.digester.annotations.*;
 
 DigesterLoader digesterLoader = new DigesterLoaderBuilder()
-  .useDefaultAnnotationRuleProviderFactory()
-  .useDefaultDigesterLoaderHandlerFactory();
+    .useDefaultAnnotationRuleProviderFactory()
+    .useDefaultDigesterLoaderHandlerFactory();
 Digester digester = digesterLoader.createDigester( Channel.class );</source>
 
       <p>In Digester3 there is just one universal loader that aggregates all the power
of the components described above,
@@ -280,16 +285,16 @@ configurations are expressed via <code>(
   extends AbstractRulesModule
 {
 
-  @Override
-  protected void configure()
-  {
-    forPattern( "root" )
-      .createObject().ofType( "org.apache.commons.digester.SimpleTestBean" )
-      .then()
-      .setBeanProperty( "alpha" );
-    forPattern( "root/alpha" ).setBeanProperty( "beta" );
-    forPattern( "root/delta" ).setBeanProperty( "delta" );
-  }
+    @Override
+    protected void configure()
+    {
+        forPattern( "root" )
+            .createObject().ofType( "org.apache.commons.digester.SimpleTestBean" )
+            .then()
+            .setBeanProperty( "alpha" );
+        forPattern( "root/alpha" ).setBeanProperty( "beta" );
+        forPattern( "root/delta" ).setBeanProperty( "delta" );
+    }
 
 }</source>
       <p>Users can simply create new Digester instances:</p>
@@ -302,15 +307,15 @@ Digester digester = loader.newDigester()
 
       <p>An, by the nature of the universal loader, auxiliary optimizations are not
needed:</p>
       <source>DigesterLoader loader = newLoader( new FromAnnotationsRuleModule()
-{
+    {
 
-  @Override
-  protected void configureRules()
-  {
-      bindRulesFrom( Channel.class );
-  }
+        @Override
+        protected void configureRules()
+        {
+            bindRulesFrom( Channel.class );
+        }
 
-} );
+    } );
 ...
 Digester digester = loader.newDigester();
 ...
@@ -331,15 +336,15 @@ ruleSet.addRuleInstances( newDigester );
       <p>In Digester3 there's only one <code>RulesModule</code>s loading,
so in the case of
       <code>FromXmlRulesModule</code>, the XML rules will be parsed only once:</p>
       <source>DigesterLoader loader = newLoader( new FromXmlRulesModule()
-  {
-
-    @Override
-    protected void loadRules()
     {
-        loadXMLRulesFromText( MyClass.class.getResource( "myrule.xml" ) );
-    }
 
-  } );
+        @Override
+        protected void loadRules()
+        {
+            loadXMLRulesFromText( MyClass.class.getResource( "myrule.xml" ) );
+        }
+
+    } );
 ...
 Digester digester = loader.newDigester(); // myrule.xml already parsed
 ...
@@ -362,16 +367,18 @@ Digester newDigester = loader.newDigeste
       runtime error will be thrown as soon as the <i>root/child</i> pattern will
match, and so on.</p>
 
       <p>The new Digester tries to report all patterns/rules binding error in one single
detailed report, i.e.</p>
-      <source>class SampleModule extends AbstractRulesModule {
+      <source>class SampleModule
+    extends AbstractRulesModule
+{
 
-  @Override
-  protected void configure()
-  {
-    forPattern( "root" ).createObject().ofType( "com.acme.InOtherClassLoader" );
-    ...
-    forPattern( "root/child" ).createObject().ofType( "foo.bar.DoesNotExist" );
-    ...
-  }
+    @Override
+    protected void configure()
+    {
+        forPattern( "root" ).createObject().ofType( "com.acme.InOtherClassLoader" );
+        ...
+        forPattern( "root/child" ).createObject().ofType( "foo.bar.DoesNotExist" );
+        ...
+    }
 
 }</source>
       <p>The <code>DigesterLoader</code> will report problems in the following
way:</p>



Mime
View raw message