Author: rdonkin Date: Sun Nov 20 10:54:26 2005 New Revision: 345774 URL: http://svn.apache.org/viewcvs?rev=345774&view=rev Log: Added option inheritance. Submitted by Brian Ferris. Issue #37542.
Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/Options.java jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestOptions.java jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/Options.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/Options.java?rev=345774&r1=345773&r2=345774&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/Options.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/Options.java Sun Nov 20 10:54:26 2005 @@ -60,4 +60,16 @@ public void addOption(String name, String value) { valuesByName.put(name, value); } + + /** + * Adds multiple options from an existing <code>Options</code> collection. + * The rule with options is that the most recently set value for an option + * wins, so options are potentially overwritten by this call. + * + * @param options - + * an existing <code>Options</code> collection + */ + public void addOptions(Options options) { + valuesByName.putAll(options.valuesByName); + } } Modified: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java?rev=345774&r1=345773&r2=345774&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java Sun Nov 20 10:54:26 2005 @@ -25,6 +25,7 @@ import org.apache.commons.betwixt.BindingConfiguration; import org.apache.commons.betwixt.Descriptor; 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.expression.Context; @@ -320,7 +321,22 @@ if ( beanInfo != null ) { ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor(); if ( elementDescriptor != null ) { + + // Construct the options + Options combinedOptions = new Options(); + + // Add options defined by the current bean's element descriptor + combinedOptions.addOptions(elementDescriptor.getOptions()); + + // The parent descriptor may have defined options + // for the current bean. These options take precedence + // over the options of the current class descriptor + if( context.getOptions() != null) { + combinedOptions.addOptions(context.getOptions()); + } context = context.newContext( bean ); + context.pushOptions(combinedOptions); + if ( qualifiedName == null ) { qualifiedName = elementDescriptor.getQualifiedName(); } @@ -423,6 +439,8 @@ } popBean(); } + + context.popOptions(); } } } @@ -723,7 +741,6 @@ if ( log.isTraceEnabled() ) { log.trace( "Element " + elementDescriptor + " is empty." ); } - context.pushOptions(elementDescriptor.getOptions()); Attributes attributes = addNamespaceDeclarations( new ElementAttributes( elementDescriptor, context ), namespaceUri); @@ -738,7 +755,6 @@ writeElementContent( elementDescriptor, context ) ; writeContext.setCurrentDescriptor(elementDescriptor); endElement( writeContext, namespaceUri, localName, qualifiedName ); - context.popOptions(); } } @@ -803,7 +819,6 @@ IntrospectionException { if ( !ignoreElement( elementDescriptor, namespaceUri, localName, qualifiedName, context ) ) { - context.pushOptions(elementDescriptor.getOptions()); writeContext.setCurrentDescriptor(elementDescriptor); Attributes attributes = new IDElementAttributes( elementDescriptor, @@ -820,7 +835,6 @@ writeElementContent( elementDescriptor, context ) ; writeContext.setCurrentDescriptor(elementDescriptor); endElement( writeContext, namespaceUri, localName, qualifiedName ); - context.popOptions(); } else if ( log.isTraceEnabled() ) { log.trace( "Element " + qualifiedName + " is empty." ); } @@ -919,6 +933,7 @@ // Element content ElementDescriptor childDescriptor = (ElementDescriptor) childDescriptors[i]; Context childContext = context; + childContext.pushOptions(childDescriptor.getOptions()); Expression childExpression = childDescriptor.getContextExpression(); if ( childExpression != null ) { Object childBean = childExpression.evaluate( context ); @@ -959,6 +974,7 @@ childDescriptor, childContext ); } + childContext.popOptions(); } else { // Mixed text content // evaluate the body text Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestOptions.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestOptions.java?rev=345774&r1=345773&r2=345774&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestOptions.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestOptions.java Sun Nov 20 10:54:26 2005 @@ -73,4 +73,24 @@ } } + public void testAddOptions() { + Options a = new Options(); + a.addOption("A", "Alpha"); + a.addOption("B", "Beta"); + a.addOption("C", "Gamma"); + + Options b = new Options(); + b.addOption("A", "Apple"); + b.addOption("C", "Carrot"); + b.addOption("E", "Egg Plant"); + + a.addOptions(b); + + // Lat value set wins + assertEquals("Apple",a.getValue("A")); + assertEquals("Beta",a.getValue("B")); + assertEquals("Carrot",a.getValue("C")); + assertEquals("Egg Plant",a.getValue("E")); + } + } Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java?rev=345774&r1=345773&r2=345774&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/strategy/TestIdStorageStrategy.java Sun Nov 20 10:54:26 2005 @@ -17,12 +17,12 @@ import java.io.StringReader; import java.io.StringWriter; -import java.util.Iterator; import org.apache.commons.betwixt.AbstractTestCase; import org.apache.commons.betwixt.expression.Context; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter; +import org.xml.sax.InputSource; /** */ @@ -123,4 +123,251 @@ } + public void testWriteWithOptions() throws Exception { + + final Element alpha = new Element("ONE"); + Element beta = new Element("TWO"); + ElementsList elements = new ElementsList(); + elements.addElement(alpha); + elements.addElement(beta); + + String MAPPING = "<?xml version='1.0'?>" + + "<betwixt-config>" + + " <class name=\"" + ElementsList.class.getName() + "\">" + + " <element name=\"ElementsList\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>alice</value>" + + " </option>" + + " <element name=\"elements\">" + + " <element property=\"elements\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>bob</value>" + + " </option>" + + " </element>" + + " </element>" + + " </element>" + + " </class>" + + "</betwixt-config>"; + + IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() { + + public String getReferenceFor(Context context, Object bean) { + String result = null; + if( bean instanceof ElementsList) { + assertNotNull( context.getOptions() ); + assertEquals("Checking ElementsList option","alice",context.getOptions().getValue("id-strategy-prefix")); + } + if( bean instanceof Element) { + assertNotNull( context.getOptions() ); + assertEquals("Checking Element option","bob",context.getOptions().getValue("id-strategy-prefix")); + } + if (bean == alpha) { + result = "ALPHA"; + } + else + { + result = super.getReferenceFor(context, bean); + } + return result; + } + + public void setReference(Context context, Object bean, String id) { + if (bean != alpha) { + super.setReference(context, bean, id); + } + } + }; + + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy); + writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING))); + writer.write(elements); + + String expected = "<?xml version='1.0'?>" + + "<ElementsList id='1'>" + + " <elements>" + + " <Element idref='ALPHA'/>" + + " <Element id='2'>" + + " <value>TWO</value>" + + " </Element>" + + " </elements>" + + "</ElementsList>"; + + xmlAssertIsomorphicContent(parseString(expected), parseString(out)); + } + + public void testWriteWithParentOptions() throws Exception { + + AlphaBean alpha = new AlphaBean(); + alpha.setName("apple"); + BetaBean beta = new BetaBean(); + beta.setName("banana"); + alpha.setBetaBean(beta); + + String MAPPING = "<?xml version='1.0'?>" + + "<betwixt-config>" + + " <class name=\"" + AlphaBean.class.getName() + "\">" + + " <element name=\"alpha\">" + + " <element name=\"name\" property=\"name\" />" + + " <element property=\"betaBean\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>parent</value>" + + " </option>" + + " </element>" + + " </element>" + + " </class>" + + " <class name=\"" + BetaBean.class.getName() + "\">" + + " <element name=\"beta\">" + + " <element name=\"name\" property=\"name\" />" + + " </element>" + + " </class>" + + "</betwixt-config>"; + + IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() { + public String getReferenceFor(Context context, Object bean) { + if( bean instanceof BetaBean) { + assertNotNull( context.getOptions() ); + assertEquals("Checking BetaBean option","parent",context.getOptions().getValue("id-strategy-prefix")); + } + return super.getReferenceFor(context, bean); + } + }; + + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy); + writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING))); + writer.write(alpha); + + String expected = "<?xml version='1.0'?>" + + "<alpha id=\"1\">" + + " <name>apple</name>" + + " <beta id=\"2\">" + + " <name>banana</name>"+ + " </beta>" + + "</alpha>"; + + xmlAssertIsomorphicContent(parseString(expected), parseString(out)); + } + + public void testWriteWithTargetOptions() throws Exception { + + AlphaBean alpha = new AlphaBean(); + alpha.setName("apple"); + BetaBean beta = new BetaBean(); + beta.setName("banana"); + alpha.setBetaBean(beta); + + String MAPPING = "<?xml version='1.0'?>" + + "<betwixt-config>" + + " <class name=\"" + AlphaBean.class.getName() + "\">" + + " <element name=\"alpha\">" + + " <element name=\"name\" property=\"name\" />" + + " <element property=\"betaBean\" />" + + " </element>" + + " </class>" + + " <class name=\"" + BetaBean.class.getName() + "\">" + + " <element name=\"beta\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>target</value>" + + " </option>" + + " <element name=\"name\" property=\"name\" />" + + " </element>" + + " </class>" + + "</betwixt-config>"; + + IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() { + public String getReferenceFor(Context context, Object bean) { + if( bean instanceof BetaBean) { + assertNotNull( context.getOptions() ); + assertEquals("Checking BetaBean option","target",context.getOptions().getValue("id-strategy-prefix")); + } + return super.getReferenceFor(context, bean); + } + }; + + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy); + writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING))); + writer.write(alpha); + + String expected = "<?xml version='1.0'?>" + + "<alpha id=\"1\">" + + " <name>apple</name>" + + " <beta id=\"2\">" + + " <name>banana</name>"+ + " </beta>" + + "</alpha>"; + + xmlAssertIsomorphicContent(parseString(expected), parseString(out)); + } + + public void testWriteWithParentAndTargetOptions() throws Exception { + + AlphaBean alpha = new AlphaBean(); + alpha.setName("apple"); + BetaBean beta = new BetaBean(); + beta.setName("banana"); + alpha.setBetaBean(beta); + + String MAPPING = "<?xml version='1.0'?>" + + "<betwixt-config>" + + " <class name=\"" + AlphaBean.class.getName() + "\">" + + " <element name=\"alpha\">" + + " <element name=\"name\" property=\"name\" />" + + " <element property=\"betaBean\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>parent</value>" + + " </option>" + + " </element>" + + " </element>" + + " </class>" + + " <class name=\"" + BetaBean.class.getName() + "\">" + + " <element name=\"beta\">" + + " <option>" + + " <name>id-strategy-prefix</name>" + + " <value>target</value>" + + " </option>" + + " <element name=\"name\" property=\"name\" />" + + " </element>" + + " </class>" + + "</betwixt-config>"; + + IdStoringStrategy storingStrategy = new DefaultIdStoringStrategy() { + public String getReferenceFor(Context context, Object bean) { + if( bean instanceof BetaBean) { + assertNotNull( context.getOptions() ); + assertEquals("Checking BetaBean option","parent",context.getOptions().getValue("id-strategy-prefix")); + } + return super.getReferenceFor(context, bean); + } + }; + + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.getBindingConfiguration().setIdMappingStrategy(storingStrategy); + writer.getXMLIntrospector().register(new InputSource(new StringReader(MAPPING))); + writer.write(alpha); + + String expected = "<?xml version='1.0'?>" + + "<alpha id=\"1\">" + + " <name>apple</name>" + + " <beta id=\"2\">" + + " <name>banana</name>"+ + " </beta>" + + "</alpha>"; + + xmlAssertIsomorphicContent(parseString(expected), parseString(out)); + } } Modified: jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml?rev=345774&r1=345773&r2=345774&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml (original) +++ jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml Sun Nov 20 10:54:26 2005 @@ -199,6 +199,9 @@ <section name='Completed'> <subsection name='Since 0.7'> <ul> + <li> +Added support for option inheritance between parent and target mappings. Issue #37542. + </li> <li> Added <code>getInheritedOption</code> method to <code>Context</code> to assist with inheritance amongst options. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]