commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1140140 - in /commons/proper/digester/trunk: ./ src/changes/ src/main/java/org/apache/commons/digester3/ src/main/java/org/apache/commons/digester3/binder/ src/main/java/org/apache/commons/digester3/xmlrules/ src/main/resources/org/apache/...
Date Mon, 27 Jun 2011 12:34:57 GMT
Author: simonetripodi
Date: Mon Jun 27 12:34:56 2011
New Revision: 1140140

URL: http://svn.apache.org/viewvc?rev=1140140&view=rev
Log:
[DIGESTER-105] Need to process [attribute id=name]somename[/attribute]

Added:
    commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
  (with props)
    commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
  (with props)
    commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
  (with props)
Modified:
    commons/proper/digester/trunk/RELEASE-NOTES.txt
    commons/proper/digester/trunk/src/changes/changes.xml
    commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/BeanPropertySetterRule.java
    commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/binder/BeanPropertySetterBuilder.java
    commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRule.java
    commons/proper/digester/trunk/src/main/resources/org/apache/commons/digester3/xmlrules/digester-rules.dtd
    commons/proper/digester/trunk/src/site/resources/dtds/digester-rules-3.0.dtd
    commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java

Modified: commons/proper/digester/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/RELEASE-NOTES.txt?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/digester/trunk/RELEASE-NOTES.txt Mon Jun 27 12:34:56 2011
@@ -91,6 +91,8 @@ IMPROVEMENTS OVER PREVIOUS RELEASE
 
  * [DIGESTER-90] xmlrules does not support setNamespaceURI
 
+ * [DIGESTER-105] Need to process [attribute id="name"]somename[/attribute]
+
  * [DIGESTER-127] Allow DigesterLoader to accept an instance of a preconfigured Digester
 
  * [DIGESTER-131] Allow recursive match in ExtendedBaseRules

Modified: commons/proper/digester/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/changes/changes.xml?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/changes/changes.xml (original)
+++ commons/proper/digester/trunk/src/changes/changes.xml Mon Jun 27 12:34:56 2011
@@ -35,6 +35,9 @@
     <action dev="simonetripodi" type="fix" issue="DIGESTER-103">
       xmlrules does not support NodeCreateRule
     </action>
+    <action dev="simonetripodi" type="add" issue="DIGESTER-105">
+      Need to process [attribute id="name"]somename[/attribute]
+    </action>
     <action dev="simonetripodi" type="fix" issue="DIGESTER-118">
       ObjectCreateRule shouldn't keep className as a field.
     </action>

Modified: commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/BeanPropertySetterRule.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/BeanPropertySetterRule.java?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/BeanPropertySetterRule.java
(original)
+++ commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/BeanPropertySetterRule.java
Mon Jun 27 12:34:56 2011
@@ -27,6 +27,7 @@ import org.apache.commons.beanutils.Bean
 import org.apache.commons.beanutils.DynaBean;
 import org.apache.commons.beanutils.DynaProperty;
 import org.apache.commons.beanutils.PropertyUtils;
+import org.xml.sax.Attributes;
 
 /**
  * <p>
@@ -70,7 +71,7 @@ public class BeanPropertySetterRule
      */
     public BeanPropertySetterRule()
     {
-        this( (String) null );
+        this( null );
     }
 
     // ----------------------------------------------------- Instance Variables
@@ -78,7 +79,12 @@ public class BeanPropertySetterRule
     /**
      * Set this property on the top object.
      */
-    private final String propertyName;
+    private String propertyName;
+
+    /**
+     * Extract the property name from attribute
+     */
+    private String propertyNameFromAttribute;
 
     /**
      * The body text used to set the property.
@@ -98,6 +104,17 @@ public class BeanPropertySetterRule
     }
 
     /**
+     * Sets the attribute name from which the property name has to be extracted.
+     *
+     * @param propertyNameFromAttribute the attribute name from which the property name has
to be extracted.
+     * @since 3.0
+     */
+    public void setPropertyNameFromAttribute( String propertyNameFromAttribute )
+    {
+        this.propertyNameFromAttribute = propertyNameFromAttribute;
+    }
+
+    /**
      * Returns the body text used to set the property.
      *
      * @return The body text used to set the property
@@ -111,6 +128,22 @@ public class BeanPropertySetterRule
      * {@inheritDoc}
      */
     @Override
+    public void begin( String namespace, String name, Attributes attributes )
+        throws Exception
+    {
+        if ( propertyNameFromAttribute != null )
+        {
+            propertyName = attributes.getValue( propertyNameFromAttribute );
+
+            getDigester().getLogger().warn( format( "[BeanPropertySetterRule]{%s} Attribute
'%s' not found in matching element '%s'",
+                                                    getDigester().getMatch(), propertyNameFromAttribute,
name ) );
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
     public void body( String namespace, String name, String text )
         throws Exception
     {

Modified: commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/binder/BeanPropertySetterBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/binder/BeanPropertySetterBuilder.java?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/binder/BeanPropertySetterBuilder.java
(original)
+++ commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/binder/BeanPropertySetterBuilder.java
Mon Jun 27 12:34:56 2011
@@ -30,6 +30,8 @@ public final class BeanPropertySetterBui
 
     private String propertyName;
 
+    private String attribute;
+
     BeanPropertySetterBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
                                LinkedRuleBuilder mainBuilder )
     {
@@ -49,12 +51,31 @@ public final class BeanPropertySetterBui
     }
 
     /**
+     * Sets the attribute name from which the property name has to be extracted.
+     *
+     * @param propertyName The name of property to set
+     * @return this builder instance
+     */
+    public BeanPropertySetterBuilder extractPropertyNameFromAttribute( String attribute )
+    {
+        if ( attribute == null )
+        {
+            reportError( "setBeanProperty().extractPropertyNameFromAttribute( String )",
+                         "Attribute name can not be null" );
+        }
+        this.attribute = attribute;
+        return this;
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
     protected BeanPropertySetterRule createRule()
     {
-        return new BeanPropertySetterRule( propertyName );
+        BeanPropertySetterRule rule = new BeanPropertySetterRule( propertyName );
+        rule.setPropertyNameFromAttribute( attribute );
+        return rule;
     }
 
 }

Modified: commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRule.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRule.java?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRule.java
(original)
+++ commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRule.java
Mon Jun 27 12:34:56 2011
@@ -19,6 +19,7 @@ package org.apache.commons.digester3.xml
  * under the License.
  */
 
+import org.apache.commons.digester3.binder.BeanPropertySetterBuilder;
 import org.apache.commons.digester3.binder.LinkedRuleBuilder;
 import org.apache.commons.digester3.binder.RulesBinder;
 import org.xml.sax.Attributes;
@@ -42,7 +43,14 @@ final class BeanPropertySetterRule
     protected void bindRule( LinkedRuleBuilder linkedRuleBuilder, Attributes attributes )
         throws Exception
     {
-        linkedRuleBuilder.setBeanProperty().withName( attributes.getValue( "propertyname"
) );
+        BeanPropertySetterBuilder builder =
+            linkedRuleBuilder.setBeanProperty().withName( attributes.getValue( "propertyname"
) );
+
+        int propertyNameFromAttributeIndex = -1;
+        if ( ( propertyNameFromAttributeIndex = attributes.getIndex( "propertynameFromAttribute"
) ) >= 0 )
+        {
+            builder.extractPropertyNameFromAttribute( attributes.getValue( propertyNameFromAttributeIndex
) );
+        }
     }
 
 }

Modified: commons/proper/digester/trunk/src/main/resources/org/apache/commons/digester3/xmlrules/digester-rules.dtd
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/main/resources/org/apache/commons/digester3/xmlrules/digester-rules.dtd?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/main/resources/org/apache/commons/digester3/xmlrules/digester-rules.dtd
(original)
+++ commons/proper/digester/trunk/src/main/resources/org/apache/commons/digester3/xmlrules/digester-rules.dtd
Mon Jun 27 12:34:56 2011
@@ -95,8 +95,9 @@
 <!-- Bean Property Setter Rule -->
 <!ELEMENT bean-property-setter-rule EMPTY>
 <!ATTLIST bean-property-setter-rule
-    pattern      CDATA #IMPLIED
-    propertyname CDATA #IMPLIED>
+    pattern                   CDATA #IMPLIED
+    propertyname              CDATA #IMPLIED
+    propertynameFromAttribute CDATA #IMPLIED>
 
 <!-- CallMethodRule
   -

Modified: commons/proper/digester/trunk/src/site/resources/dtds/digester-rules-3.0.dtd
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/site/resources/dtds/digester-rules-3.0.dtd?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/site/resources/dtds/digester-rules-3.0.dtd (original)
+++ commons/proper/digester/trunk/src/site/resources/dtds/digester-rules-3.0.dtd Mon Jun 27
12:34:56 2011
@@ -95,8 +95,9 @@
 <!-- Bean Property Setter Rule -->
 <!ELEMENT bean-property-setter-rule EMPTY>
 <!ATTLIST bean-property-setter-rule
-    pattern      CDATA #IMPLIED
-    propertyname CDATA #IMPLIED>
+    pattern                   CDATA #IMPLIED
+    propertyname              CDATA #IMPLIED
+    propertynameFromAttribute CDATA #IMPLIED>
 
 <!-- CallMethodRule
   -

Modified: commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java?rev=1140140&r1=1140139&r2=1140140&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java
(original)
+++ commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/BeanPropertySetterRuleTestCase.java
Mon Jun 27 12:34:56 2011
@@ -264,6 +264,29 @@ public class BeanPropertySetterRuleTestC
 
     }
 
+    @Test
+    public void extractPropertyNameFromAttribute() throws Exception
+    {
+        Employee expected = new Employee( "John", "Doe" );
+
+        Employee actual = newLoader( new AbstractRulesModule()
+        {
+
+            @Override
+            protected void configure()
+            {
+                forPattern( "employee" ).createObject().ofType( Employee.class );
+                forPattern( "employee/property" ).setBeanProperty().extractPropertyNameFromAttribute(
"name" );
+            }
+
+        } )
+        .newDigester()
+        .parse( getClass().getResource( "extractPropertyNameFromAttribute.xml" ) );
+
+        assertEquals( expected.getFirstName(), actual.getFirstName() );
+        assertEquals( expected.getLastName(), actual.getLastName() );
+    }
+
     /**
      * Get input stream from {@link #TEST_XML}.
      */

Added: commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java?rev=1140140&view=auto
==============================================================================
--- commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
(added)
+++ commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
Mon Jun 27 12:34:56 2011
@@ -0,0 +1,53 @@
+package org.apache.commons.digester3.xmlrules;
+
+import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.digester3.Employee;
+import org.junit.Test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public final class BeanPropertySetterRuleTestCase
+{
+
+    @Test
+    public void extractPropertyNameFromAttribute() throws Exception
+    {
+        Employee expected = new Employee( "John", "Doe" );
+
+        Employee actual = newLoader( new FromXmlRulesModule()
+        {
+
+            @Override
+            protected void loadRules()
+            {
+                loadXMLRules( getClass().getResource( "extractPropertyNameFromAttribute-rules.xml"
) );
+            }
+
+        } )
+        .newDigester()
+        .parse( getClass().getResource( "../extractPropertyNameFromAttribute.xml" ) );
+
+        assertEquals( expected.getFirstName(), actual.getFirstName() );
+        assertEquals( expected.getLastName(), actual.getLastName() );
+    }
+
+}

Propchange: commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/digester/trunk/src/test/java/org/apache/commons/digester3/xmlrules/BeanPropertySetterRuleTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml?rev=1140140&view=auto
==============================================================================
--- commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
(added)
+++ commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
Mon Jun 27 12:34:56 2011
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+  
+      http://www.apache.org/licenses/LICENSE-2.0
+  
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<employee>
+  <property name="firstName">John</property>
+  <property name="lastName">Doe</property>
+</employee>

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/extractPropertyNameFromAttribute.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml?rev=1140140&view=auto
==============================================================================
--- commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
(added)
+++ commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
Mon Jun 27 12:34:56 2011
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE digester-rules PUBLIC "-//Apache Commons //DTD digester-rules XML V1.0//EN"
"http://commons.apache.org/digester/dtds/digester-rules-3.0.dtd">
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+  
+      http://www.apache.org/licenses/LICENSE-2.0
+  
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<digester-rules>
+  <pattern value="employee">
+    <object-create-rule classname="org.apache.commons.digester3.Employee" />
+    <bean-property-setter-rule pattern="property" propertynameFromAttribute="name" />
+  </pattern>
+</digester-rules>

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: commons/proper/digester/trunk/src/test/resources/org/apache/commons/digester3/xmlrules/extractPropertyNameFromAttribute-rules.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message