You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2006/05/24 23:35:20 UTC

svn commit: r409255 - in /jakarta/commons/proper/betwixt/trunk: ./ src/java/org/apache/commons/betwixt/ src/java/org/apache/commons/betwixt/digester/ src/java/org/apache/commons/betwixt/strategy/ src/test/org/apache/commons/betwixt/versioning/

Author: rdonkin
Date: Wed May 24 14:35:19 2006
New Revision: 409255

URL: http://svn.apache.org/viewvc?rev=409255&view=rev
Log:
New introspection time suppression strategies for attributes and elements. JIRA-BETWIXT-51. Contributed by Holger Haag.

Added:
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/AttributeSuppressionStrategy.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ElementSuppressionStrategy.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestVersioning.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite1.xml
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite2.xml
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite3.xml
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite4.xml
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningStrategy.java
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.betwixt
    jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.java
Modified:
    jakarta/commons/proper/betwixt/trunk/project.xml
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/IntrospectionConfiguration.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AttributeRule.java
    jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/ElementRule.java

Modified: jakarta/commons/proper/betwixt/trunk/project.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/project.xml?rev=409255&r1=409254&r2=409255&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/project.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/project.xml Wed May 24 14:35:19 2006
@@ -157,6 +157,10 @@
       <name>Brian Pugh</name>
       <email></email>
     </contributor>
+    <contributor>
+      <name>Holger Haag</name>
+      <email></email>
+    </contributor>
   </contributors>
 
   <dependencies>

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java?rev=409255&r1=409254&r2=409255&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/ElementDescriptor.java Wed May 24 14:35:19 2006
@@ -230,7 +230,17 @@
         attributeDescriptors = null;
     }
     
-    
+
+    /**
+     * Removes an attribute descriptor from this element descriptor. 
+     * @param descriptor the <code>AttributeDescriptor</code> that will be removed.
+     * 
+     * @param descriptor
+     */
+    public void removeAttributeDescriptor(AttributeDescriptor descriptor) {
+        getAttributeList().remove(descriptor);
+    }
+
     /** 
      * Returns the attribute descriptors for this element 
      *
@@ -252,6 +262,23 @@
         return attributeDescriptors;
     }
     
+    /**
+     * Returns an attribute descriptor with a given name or null.
+     *  
+     * @param name to search for; will be checked against the attributes' qualified name.
+     * @return
+     */
+    public AttributeDescriptor getAttributeDescriptor(final String name) {
+        for (int i = 0, size = attributeDescriptors.length; i < size; i++) {
+            AttributeDescriptor descr = attributeDescriptors[i];
+            if (descr.getQualifiedName().equals(name)) {
+                return descr;
+            }
+        }
+
+        return null;
+    }
+
     /** 
      * Sets the <code>AttributesDescriptors</code> for this element.
      * This sets descriptors for the attributes of the element describe by the 
@@ -277,6 +304,17 @@
         getElementList().add( descriptor );
         elementDescriptors = null;
         addContentDescriptor( descriptor );
+    }
+
+    /**
+     * Removes an element descriptor from this element descriptor. 
+     * @param descriptor the <code>ElementDescriptor</code> that will be removed.
+     * 
+     * @param descriptor
+     */
+    public void removeElementDescriptor(ElementDescriptor descriptor) {
+        getElementList().remove(descriptor);
+        getContentList().remove(descriptor);
     }
     
     /** 

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/IntrospectionConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/IntrospectionConfiguration.java?rev=409255&r1=409254&r2=409255&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/IntrospectionConfiguration.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/IntrospectionConfiguration.java Wed May 24 14:35:19 2006
@@ -16,10 +16,12 @@
 
 package org.apache.commons.betwixt;
 
+import org.apache.commons.betwixt.strategy.AttributeSuppressionStrategy;
 import org.apache.commons.betwixt.strategy.ClassNormalizer;
 import org.apache.commons.betwixt.strategy.CollectiveTypeStrategy;
 import org.apache.commons.betwixt.strategy.DefaultNameMapper;
 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
+import org.apache.commons.betwixt.strategy.ElementSuppressionStrategy;
 import org.apache.commons.betwixt.strategy.MappingDerivationStrategy;
 import org.apache.commons.betwixt.strategy.NameMapper;
 import org.apache.commons.betwixt.strategy.NamespacePrefixMapper;
@@ -90,6 +92,12 @@
     private TypeBindingStrategy typeBindingStrategy = TypeBindingStrategy.DEFAULT;
     /** Strategy used for determining which types are collective */
     private CollectiveTypeStrategy collectiveTypeStrategy = CollectiveTypeStrategy.DEFAULT;
+
+    /** Strategy for suppressing attributes */
+    private AttributeSuppressionStrategy attributeSuppressionStrategy = AttributeSuppressionStrategy.DEFAULT;   
+    /** Strategy for suppressing elements */
+    private ElementSuppressionStrategy elementSuppressionStrategy = ElementSuppressionStrategy.DEFAULT;
+
     
     /** 
      * Strategy used to determine whether the bind or introspection time type is to be used to  
@@ -432,5 +440,52 @@
      */
     public boolean isLoopType(Class type) {
         return getCollectiveTypeStrategy().isCollective(type);
+    }
+
+
+    /**
+     * Returns the <code>AttributeSuppressionStrategy</code>. 
+     * This is used to suppress attributes, e.g. for versioning.
+     * 
+     * @since 0.8
+     * @return the strategy
+     */
+    public AttributeSuppressionStrategy getAttributeSuppressionStrategy() {
+        return attributeSuppressionStrategy;
+    }
+
+    /**
+     * Sets the <code>AttributeSuppressionStrategy</code>. 
+     * This is used to suppress attributes, e.g. for versioning.
+     * 
+     * @since 0.8
+     * @param the strategy 
+     */
+    public void setAttributeSuppressionStrategy(
+            AttributeSuppressionStrategy attributeSuppressionStrategy) {
+        this.attributeSuppressionStrategy = attributeSuppressionStrategy;
+    }
+
+    /**
+     * Returns the <code>ElementSuppressionStrategy</code>. 
+     * This is used to suppress elements, e.g. for versioning.
+     * 
+     * @since 0.8
+     * @return the strategy
+     */
+    public ElementSuppressionStrategy getElementSuppressionStrategy() {
+        return elementSuppressionStrategy;
+    }
+
+    /**
+     * Sets the <code>ElementSuppressionStrategy</code>. 
+     * This is used to suppress elements, e.g. for versioning.
+     * 
+     * @since 0.8
+     * @param the strategy 
+     */
+    public void setElementSuppressionStrategy(
+            ElementSuppressionStrategy elementSuppressionStrategy) {
+        this.elementSuppressionStrategy = elementSuppressionStrategy;
     }
 }

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AttributeRule.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AttributeRule.java?rev=409255&r1=409254&r2=409255&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AttributeRule.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AttributeRule.java Wed May 24 14:35:19 2006
@@ -112,7 +112,13 @@
      * Process the end of this element.
      */
     public void end(String name, String namespace) {
-        Object top = digester.pop();
+        AttributeDescriptor descriptor = (AttributeDescriptor)digester.pop();
+        ElementDescriptor parent = (ElementDescriptor)digester.peek();
+
+        // check for attribute suppression
+        if( getXMLIntrospector().getConfiguration().getAttributeSuppressionStrategy().suppress(descriptor)) {
+            parent.removeAttributeDescriptor(descriptor);
+        }
     }
 
     

Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/ElementRule.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/ElementRule.java?rev=409255&r1=409254&r2=409255&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/ElementRule.java (original)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/ElementRule.java Wed May 24 14:35:19 2006
@@ -20,6 +20,7 @@
 import java.lang.reflect.Modifier;
 import java.util.Map;
 
+import org.apache.commons.betwixt.AttributeDescriptor;
 import org.apache.commons.betwixt.ElementDescriptor;
 import org.apache.commons.betwixt.XMLBeanInfo;
 import org.apache.commons.betwixt.XMLUtils;
@@ -193,7 +194,18 @@
      * Process the end of this element.
      */
     public void end(String name, String namespace) {
-        Object top = digester.pop();
+        ElementDescriptor descriptor = (ElementDescriptor)digester.pop();
+        
+        final Object peek = digester.peek();
+        
+        if(peek instanceof ElementDescriptor) {
+            ElementDescriptor parent = (ElementDescriptor)digester.peek();
+
+            // check for element suppression
+            if( getXMLIntrospector().getConfiguration().getElementSuppressionStrategy().suppress(descriptor)) {
+                parent.removeElementDescriptor(descriptor);
+            }
+        }
     }
 
     // Implementation methods

Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/AttributeSuppressionStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/AttributeSuppressionStrategy.java?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/AttributeSuppressionStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/AttributeSuppressionStrategy.java Wed May 24 14:35:19 2006
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ */
+package org.apache.commons.betwixt.strategy;
+
+import org.apache.commons.betwixt.AttributeDescriptor;
+
+/**
+ * Strategy to determine whether to show an attribute at all.
+ * 
+ */
+public interface AttributeSuppressionStrategy 
+{
+    /**
+     * Should the attribute described as given be suppressed during introspection?
+     * @param descriptor <code>AttributeDescriptor</code>, not null
+     * @return true if the attribute should be ignore,
+     * false otherwise
+     */
+    public boolean suppress(AttributeDescriptor descriptor);
+    
+    /**
+     * Default strategy: show all attributes.
+     */
+    public final static AttributeSuppressionStrategy DEFAULT = new AttributeSuppressionStrategy() {
+        public boolean suppress(AttributeDescriptor description) {
+            return false;
+        }
+
+    };
+}

Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ElementSuppressionStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ElementSuppressionStrategy.java?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ElementSuppressionStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/strategy/ElementSuppressionStrategy.java Wed May 24 14:35:19 2006
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ */ 
+package org.apache.commons.betwixt.strategy;
+
+import org.apache.commons.betwixt.ElementDescriptor;
+
+/**
+ * Strategy to determine whether to show an element at all.
+ * 
+ */
+public interface ElementSuppressionStrategy {
+    /**
+     * Should the element described as given be suppressed?
+     * @param descriptor <code>ElementDescriptor</code>, not null
+     * @return true if the desciptor should be ignored during introspection,
+     * false otherwise
+     */
+    public boolean suppress(ElementDescriptor descriptor);
+    
+    /**
+     * Default strategy: show all elements.
+     */
+    public final static ElementSuppressionStrategy DEFAULT = new ElementSuppressionStrategy() {
+        public boolean suppress(ElementDescriptor descr) {
+            return false;
+        }
+    };
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestVersioning.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestVersioning.java?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestVersioning.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestVersioning.java Wed May 24 14:35:19 2006
@@ -0,0 +1,277 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ */
+
+package org.apache.commons.betwixt.versioning;
+
+import java.io.StringWriter;
+
+import org.apache.commons.betwixt.AbstractTestCase;
+import org.apache.commons.betwixt.AttributeDescriptor;
+import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.Options;
+import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
+import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TestVersioning extends AbstractTestCase {
+    public static Log log = LogFactory.getLog(TestVersioning.class);
+
+    public TestVersioning(String testName) {
+        super(testName);
+    }
+
+    private void configure(BindingConfiguration configuration) {
+        configuration.setMapIDs(false);
+    }
+
+    public void testIntrospection() throws Exception {
+        log.info("testIntrospection() started");
+
+        XMLIntrospector introspector = new XMLIntrospector();
+        XMLBeanInfo beanInfo = introspector
+                .introspect(VersioningTestData.class);
+
+        // 2 Element descriptors
+        ElementDescriptor[] elementDescriptors = beanInfo
+                .getElementDescriptor().getElementDescriptors();
+        assertEquals("Need 2 element descriptors", 2, elementDescriptors.length);
+
+        ElementDescriptor element1Descriptor = beanInfo.getElementDescriptor()
+                .getElementDescriptor("element1");
+        log.info("element1Descriptor: " + element1Descriptor);
+        debugOptions(element1Descriptor.getOptions());
+        assertNotNull(element1Descriptor);
+        assertEquals("1", element1Descriptor.getOptions().getValue(
+                "version-from"));
+        assertNull(element1Descriptor.getOptions().getValue("version-until"));
+
+        ElementDescriptor element2Descriptor = beanInfo.getElementDescriptor()
+                .getElementDescriptor("element2");
+        log.info("element2Descriptor: " + element2Descriptor);
+        debugOptions(element2Descriptor.getOptions());
+        assertNotNull(element2Descriptor);
+        assertEquals("2", element2Descriptor.getOptions().getValue(
+                "version-from"));
+        assertNull(element2Descriptor.getOptions().getValue("version-until"));
+
+        // 2 Attribute descriptors
+        AttributeDescriptor[] attributeDescriptors = beanInfo
+                .getElementDescriptor().getAttributeDescriptors();
+        assertEquals("Need 2 attribute descriptors", 2,
+                attributeDescriptors.length);
+
+        AttributeDescriptor attribute1Descriptor = beanInfo
+                .getElementDescriptor().getAttributeDescriptor("attribute1");
+        log.info("attribute1Descriptor: " + attribute1Descriptor);
+        debugOptions(attribute1Descriptor.getOptions());
+        assertNotNull(attribute1Descriptor);
+        assertEquals("2", attribute1Descriptor.getOptions().getValue(
+                "version-from"));
+        assertNull(attribute1Descriptor.getOptions().getValue("version-until"));
+
+        AttributeDescriptor attribute2Descriptor = beanInfo
+                .getElementDescriptor().getAttributeDescriptor("attribute2");
+        log.info("attribute2Descriptor: " + attribute2Descriptor);
+        debugOptions(attribute2Descriptor.getOptions());
+        assertNotNull(attribute2Descriptor);
+        assertEquals("1", attribute2Descriptor.getOptions().getValue(
+                "version-from"));
+        assertEquals("2", attribute2Descriptor.getOptions().getValue(
+                "version-until"));
+
+        log.info("testIntrospection() complete");
+    }
+
+    /**
+     * Simple test case with no version specified: All elements/attributes will
+     * be written.
+     * 
+     * @throws Exception
+     */
+    public void testWrite1() throws Exception {
+        log.info("testWrite1() started");
+
+        final VersioningTestData data = new VersioningTestData();
+        data.setAttribute1("attributevalue1");
+        data.setAttribute2("attributevalue2");
+        data.setElement1("elementvalue1");
+        data.setElement2("elementvalue2");
+
+        StringWriter out = new StringWriter();
+        BeanWriter writer = new BeanWriter(out);
+        configure(writer.getBindingConfiguration());
+        writer.write(data);
+
+        final String written = out.toString();
+        log.info("Written:\n" + written);
+
+        final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
+        xmlAssertIsomorphicContent(parseString(expected), parseString(written),
+                true);
+
+        log.info("testWrite1() complete");
+    }
+
+    /**
+     * Version = 1
+     * 
+     * <ul>
+     * <li>Attribute1 (2-/): Not written
+     * <li>Attribute2 (1-2): Written
+     * <li>Element1 (1-/): Written
+     * <li>Element2 (2-/): Not written
+     * </ul>
+     * 
+     * @throws Exception
+     */
+    public void testWrite2() throws Exception {
+        log.info("testWrite2() started");
+
+        final VersioningTestData data = new VersioningTestData();
+        data.setAttribute1("attributevalue1");
+        data.setAttribute2("attributevalue2");
+        data.setElement1("elementvalue1");
+        data.setElement2("elementvalue2");
+
+        StringWriter out = new StringWriter();
+        BeanWriter writer = new BeanWriter(out);
+
+        final VersioningStrategy versioningStrategy = new VersioningStrategy(
+                "1");
+        writer.getXMLIntrospector().getConfiguration()
+                .setAttributeSuppressionStrategy(versioningStrategy);
+        writer.getXMLIntrospector().getConfiguration()
+                .setElementSuppressionStrategy(versioningStrategy);
+
+        configure(writer.getBindingConfiguration());
+        writer.write(data);
+
+        final String written = out.toString();
+        log.info("Written:\n" + written);
+
+        final String expected = "<VersioningTestData attribute2=\"attributevalue2\"><element1>elementvalue1</element1></VersioningTestData>";
+        xmlAssertIsomorphicContent(parseString(expected), parseString(written),
+                true);
+
+        log.info("testWrite1() complete");
+    }
+
+    private final void debugOptions(final Options options) {
+        final String[] names = options.getNames();
+
+        log.info("Names:");
+
+        for (int ii = 0; ii < names.length; ii++) {
+            final String name = names[ii];
+
+            log.info("  Name " + ii + ": " + name + "="
+                    + options.getValue(name));
+        }
+    }
+
+
+    /**
+     * Version = 2
+     * 
+     * <ul>
+     * <li>Attribute1 (2-/): written
+     * <li>Attribute2 (1-2): Written
+     * <li>Element1 (1-/): Written
+     * <li>Element2 (2-/): written
+     * </ul>
+     * 
+     * @throws Exception
+     */
+    public void testWrite3() throws Exception {
+        log.info("testWrite2() started");
+
+        final VersioningTestData data = new VersioningTestData();
+        data.setAttribute1("attributevalue1");
+        data.setAttribute2("attributevalue2");
+        data.setElement1("elementvalue1");
+        data.setElement2("elementvalue2");
+
+        StringWriter out = new StringWriter();
+        BeanWriter writer = new BeanWriter(out);
+
+        final VersioningStrategy versioningStrategy = new VersioningStrategy(
+                "2");
+        writer.getXMLIntrospector().getConfiguration()
+                .setAttributeSuppressionStrategy(versioningStrategy);
+        writer.getXMLIntrospector().getConfiguration()
+                .setElementSuppressionStrategy(versioningStrategy);
+
+        configure(writer.getBindingConfiguration());
+        writer.write(data);
+
+        final String written = out.toString();
+        log.info("Written:\n" + written);
+
+        final String expected = "<VersioningTestData attribute1=\"attributevalue1\" attribute2=\"attributevalue2\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
+        xmlAssertIsomorphicContent(parseString(expected), parseString(written),
+                true);
+
+        log.info("testWrite1() complete");
+    }
+
+
+    /**
+     * Version = 3
+     * 
+     * <ul>
+     * <li>Attribute1 (2-/): written
+     * <li>Attribute2 (1-2): Not Written
+     * <li>Element1 (1-/): Written
+     * <li>Element2 (2-/): written
+     * </ul>
+     * 
+     * @throws Exception
+     */
+    public void testWrite4() throws Exception {
+        log.info("testWrite2() started");
+
+        final VersioningTestData data = new VersioningTestData();
+        data.setAttribute1("attributevalue1");
+        data.setAttribute2("attributevalue2");
+        data.setElement1("elementvalue1");
+        data.setElement2("elementvalue2");
+
+        StringWriter out = new StringWriter();
+        BeanWriter writer = new BeanWriter(out);
+
+        final VersioningStrategy versioningStrategy = new VersioningStrategy(
+                "3");
+        writer.getXMLIntrospector().getConfiguration()
+                .setAttributeSuppressionStrategy(versioningStrategy);
+        writer.getXMLIntrospector().getConfiguration()
+                .setElementSuppressionStrategy(versioningStrategy);
+
+        configure(writer.getBindingConfiguration());
+        writer.write(data);
+
+        final String written = out.toString();
+        log.info("Written:\n" + written);
+
+        final String expected = "<VersioningTestData attribute1=\"attributevalue1\"><element1>elementvalue1</element1><element2>elementvalue2</element2></VersioningTestData>";
+        xmlAssertIsomorphicContent(parseString(expected), parseString(written),
+                true);
+
+        log.info("testWrite1() complete");
+    }
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite1.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite1.xml?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite1.xml (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite1.xml Wed May 24 14:35:19 2006
@@ -0,0 +1,21 @@
+<?xml version='1.0'?>
+<!-- 
+	Copyright 2006 The Apache Software Foundation.
+	
+	Licensed 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.
+-->
+<VersioningTestData attribute1="attributevalue1"
+	attribute2="attributevalue2">
+	<element1>elementvalue1</element1>	
+	<element2>elementvalue2</element2>
+</VersioningTestData>
\ No newline at end of file

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite2.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite2.xml?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite2.xml (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite2.xml Wed May 24 14:35:19 2006
@@ -0,0 +1,19 @@
+<?xml version='1.0'?>
+<!-- 
+  Copyright 2006 The Apache Software Foundation.
+  
+  Licensed 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.
+-->
+<VersioningTestData attribute2="attributevalue2">
+    <element1>elementvalue1</element1>
+</VersioningTestData>

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite3.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite3.xml?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite3.xml (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite3.xml Wed May 24 14:35:19 2006
@@ -0,0 +1,20 @@
+<?xml version='1.0'?>
+<!-- 
+  Copyright 2006 The Apache Software Foundation.
+  
+  Licensed 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.
+-->
+<VersioningTestData attribute1="attributevalue1" attribute2="attributevalue2">
+    <element1>elementvalue1</element1>
+    <element2>elementvalue2</element2>
+</VersioningTestData>

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite4.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite4.xml?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite4.xml (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/TestWrite4.xml Wed May 24 14:35:19 2006
@@ -0,0 +1,20 @@
+<?xml version='1.0'?>
+<!-- 
+  Copyright 2006 The Apache Software Foundation.
+  
+  Licensed 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.
+-->
+<VersioningTestData attribute1="attributevalue1">
+    <element1>elementvalue1</element1>
+    <element2>elementvalue2</element2>
+</VersioningTestData>

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningStrategy.java?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningStrategy.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningStrategy.java Wed May 24 14:35:19 2006
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ */
+package org.apache.commons.betwixt.versioning;
+
+import org.apache.commons.betwixt.AttributeDescriptor;
+import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.Options;
+import org.apache.commons.betwixt.strategy.AttributeSuppressionStrategy;
+import org.apache.commons.betwixt.strategy.ElementSuppressionStrategy;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class VersioningStrategy implements ElementSuppressionStrategy,
+        AttributeSuppressionStrategy {
+    public static Log log = LogFactory.getLog(VersioningStrategy.class);
+
+    public final static String VERSION_FROM = "version-from";
+
+    public final static String VERSION_UNTIL = "version-until";
+
+    private String version;
+
+    public String getVersion() {
+        return version;
+    }
+
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    public boolean suppress(ElementDescriptor descr) {
+        log.info("Checking element " + descr.getLocalName() + " (" + descr + ")");
+
+        if (false == checkVersionFrom(descr.getOptions())) {
+            log.info("Suppressing element (invalid version/from)");
+            return true;
+        }
+
+        if (false == checkVersionUntil(descr.getOptions())) {
+            log.info("Suppressing element (invalid version/until)");
+            return true;
+        }
+
+        log.info("Showing element");
+        return false;
+    }
+
+    public boolean suppress(final AttributeDescriptor descr) {
+        log.info("Checking attribute " + descr.getLocalName() + " (" + descr + ")");
+
+        if (false == checkVersionFrom(descr.getOptions())) {
+            log.info("Suppressing attribute (invalid version/from)");
+            return true;
+        }
+
+        if (false == checkVersionUntil(descr.getOptions())) {
+            log.info("Suppressing attribute (invalid version/until)");
+            return true;
+        }
+
+        log.info("Showing attribute");
+        return false;
+    }
+
+    private boolean checkVersionFrom(final Options options) {
+        log.info("Checking version/from");
+
+        if (options == null) {
+            log.info("No options");
+            return true;
+        }
+
+        final String value = options.getValue(VERSION_FROM);
+
+        log.info("value=" + value);
+        log.info("version=" + version);
+        debugOptions(options);
+
+        if (value == null || value.trim().length() == 0) {
+            log.info("No attribute \"Version from\"");
+            return true;
+        }
+
+        final boolean versionOk = value.compareTo(version) <= 0;
+        log.info("versionOk=" + versionOk);
+
+        return versionOk;
+    }
+
+    private boolean checkVersionUntil(final Options options) {
+        log.info("Checking version/until");
+
+        if (options == null) {
+            log.info("No options");
+            return true;
+        }
+
+        final String value = options.getValue(VERSION_UNTIL);
+
+        log.info("value=" + value);
+        log.info("version=" + version);
+        debugOptions(options);
+
+        if (value == null || value.trim().length() == 0) {
+            log.info("No attribute \"Version until\"");
+            return true;
+        }
+
+        final boolean versionOk = value.compareTo(version) >= 0;
+        log.info("versionOk=" + versionOk);
+
+        return versionOk;
+    }
+
+    public VersioningStrategy() {
+        super();
+    }
+
+    public VersioningStrategy(final String version) {
+        super();
+        setVersion(version);
+    }
+
+    private final void debugOptions(final Options options) {
+        final String[] names = options.getNames();
+
+        log.info("Names:");
+
+        for (int ii = 0; ii < names.length; ii++) {
+            final String name = names[ii];
+
+            log.info("  " + ii + ": " + name + "=" + options.getValue(name));
+        }
+    }
+}

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.betwixt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.betwixt?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.betwixt (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.betwixt Wed May 24 14:35:19 2006
@@ -0,0 +1,49 @@
+<?xml version='1.0'?>
+<!-- 
+  Copyright 2006 The Apache Software Foundation.
+  
+  Licensed 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.
+-->
+<info>
+	<element name='VersioningTestData'>
+	    <addDefaults add-properties="false" />
+		<attribute name='attribute1' property='attribute1'>
+			<option>
+				<name>version-from</name>
+				<value>2</value>
+			</option>
+		</attribute>
+		<attribute name='attribute2' property='attribute2'>
+			<option>
+				<name>version-from</name>
+				<value>1</value>
+			</option>
+			<option>
+				<name>version-until</name>
+				<value>2</value>
+			</option>
+		</attribute>
+		<element name='element1' property='element1'>
+			<option>
+				<name>version-from</name>
+				<value>1</value>
+			</option>
+		</element>
+		<element name='element2' property='element2'>
+			<option>
+				<name>version-from</name>
+				<value>2</value>
+			</option>
+		</element>
+	</element>
+</info>
\ No newline at end of file

Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.java?rev=409255&view=auto
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.java (added)
+++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/versioning/VersioningTestData.java Wed May 24 14:35:19 2006
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ */
+package org.apache.commons.betwixt.versioning;
+
+/**
+ * Holder for versioned test data. 
+ */
+public class VersioningTestData {
+    private String attribute1;
+
+    public String getAttribute1() {
+        return attribute1;
+    }
+
+    public void setAttribute1(String attribute1) {
+        this.attribute1 = attribute1;
+    }
+    
+    
+    private String attribute2;
+
+    public String getAttribute2() {
+        return attribute2;
+    }
+
+    public void setAttribute2(String attribute2) {
+        this.attribute2 = attribute2;
+    }
+    
+    
+    private String element1;
+
+    public String getElement1() {
+        return element1;
+    }
+
+    public void setElement1(String element1) {
+        this.element1 = element1;
+    }
+    
+ 
+    private String element2;
+
+    public String getElement2() {
+        return element2;
+    }
+
+    public void setElement2(String element2) {
+        this.element2 = element2;
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org