Author: rdonkin Date: Tue Apr 26 14:48:43 2005 New Revision: 164896 URL: http://svn.apache.org/viewcvs?rev=164896&view=rev Log: Fixed nested empty element bug.
Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/TestIgnoreEmptyElements.java jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SidekickBean.java jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SuperheroBean.java Modified: 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/TestBeanWriter.java 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=164896&r1=164895&r2=164896&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 Tue Apr 26 14:48:43 2005 @@ -270,22 +270,34 @@ } // introspect to obtain bean info - XMLBeanInfo beanInfo = null; - Class introspectedBindType = parentDescriptor.getSingularPropertyType(); - if ( introspectedBindType == null ) { - introspectedBindType = parentDescriptor.getPropertyType(); - } - if ( parentDescriptor.isUseBindTimeTypeForMapping() || introspectedBindType == null ) { - beanInfo = introspector.introspect( bean ); - } else { - beanInfo = introspector.introspect( introspectedBindType ); - } + XMLBeanInfo beanInfo = findXMLBeanInfo(bean, parentDescriptor); writeBean(namespaceUri, localName, qualifiedName, bean, context, beanInfo); log.trace( "Finished writing bean graph." ); } /** + * Finds the appropriate bean info for the given (hollow) element. + * @param bean + * @param parentDescriptor <code>ElementDescriptor</code>, not null + * @return <code>XMLBeanInfo</code>, not null + * @throws IntrospectionException + */ + private XMLBeanInfo findXMLBeanInfo(Object bean, ElementDescriptor parentDescriptor) throws IntrospectionException { + XMLBeanInfo beanInfo = null; + Class introspectedBindType = parentDescriptor.getSingularPropertyType(); + if ( introspectedBindType == null ) { + introspectedBindType = parentDescriptor.getPropertyType(); + } + if ( parentDescriptor.isUseBindTimeTypeForMapping() || introspectedBindType == null ) { + beanInfo = introspector.introspect( bean ); + } else { + beanInfo = introspector.introspect( introspectedBindType ); + } + return beanInfo; + } + + /** * <p>Writes the given bean to the current stream * using the given mapping.</p> * @@ -1035,8 +1047,9 @@ * @param descriptor the <code>ElementDescriptor</code> to evaluate * @param context the <code>Context</code> against which the element will be evaluated * @return true if this element should be written out + * @throws IntrospectionException */ - private boolean ignoreElement( ElementDescriptor descriptor, Context context ) { + private boolean ignoreElement( ElementDescriptor descriptor, Context context ) throws IntrospectionException { if ( ! getWriteEmptyElements() ) { return isEmptyElement( descriptor, context ); } @@ -1050,16 +1063,19 @@ * and no body text. * For example, <code><element/></code> is an empty element but * <code><element attr='value'/></code> is not.</p> - * + * * @param descriptor the <code>ElementDescriptor</code> to evaluate * @param context the <code>Context</code> against which the element will be evaluated * @return true if this element is empty on evaluation + * @throws IntrospectionException */ - private boolean isEmptyElement( ElementDescriptor descriptor, Context context ) { + private boolean isEmptyElement( ElementDescriptor descriptor, Context context ) throws IntrospectionException { + //TODO: this design isn't too good + // to would be much better to render just once if ( log.isTraceEnabled() ) { log.trace( "Is " + descriptor + " empty?" ); } - + // an element which has attributes is not empty if ( descriptor.hasAttributes() ) { log.trace( "Element has attributes." ); @@ -1090,6 +1106,23 @@ if ( ! isEmptyElement( descriptor.getElementDescriptors()[i], context ) ) { log.trace( "Element has child which isn't empty." ); return false; + } + } + } + + if ( descriptor.isHollow() ) + { + Expression contentExpression = descriptor.getContextExpression(); + if (contentExpression != null) { + Object childBean = contentExpression.evaluate(context); + if (childBean != null) + { + XMLBeanInfo xmlBeanInfo = findXMLBeanInfo(childBean, descriptor); + Object currentBean = context.getBean(); + context.setBean(childBean); + boolean result = isEmptyElement(xmlBeanInfo.getElementDescriptor(), context); + context.setBean(currentBean); + return result; } } } Added: jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/TestIgnoreEmptyElements.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/TestIgnoreEmptyElements.java?rev=164896&view=auto ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/TestIgnoreEmptyElements.java (added) +++ jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/io/TestIgnoreEmptyElements.java Tue Apr 26 14:48:43 2005 @@ -0,0 +1,80 @@ +/* + * Copyright 2005 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.io; + +import java.io.StringWriter; + +import org.apache.commons.betwixt.AbstractTestCase; +import org.apache.commons.betwixt.LoopBean; + +/** + */ +public class TestIgnoreEmptyElements extends AbstractTestCase { + + + public TestIgnoreEmptyElements(String testName) { + super(testName); + } + + public void testWritePersonBean() throws Exception { + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.setWriteEmptyElements(false); + SidekickBean sidekick = new SidekickBean("Robin"); + SuperheroBean superhero = new SuperheroBean(sidekick); + writer.write(superhero); + String expected = "<?xml version='1.0'?>" + + "<SuperheroBean id='1'>" + + " <sidekick id='2'><nickname>Robin</nickname></sidekick>" + + "</SuperheroBean>"; + String xml = out.toString(); + xmlAssertIsomorphic(parseString(expected), parseString(xml)); + } + + + /** Test nested case for writing empty elements */ + public void testNestedWriteEmptyElements() throws Exception{ + + // write same bean both times + LoopBean root = new LoopBean("base"); + LoopBean middle = new LoopBean(null); + root.setFriend(middle); + middle.setFriend(new LoopBean(null)); + + // test output when writing empty elements + StringWriter out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + BeanWriter writer = new BeanWriter(out); + writer.setWriteEmptyElements(true); + writer.getBindingConfiguration().setMapIDs(false); + writer.write(root); + String xml = "<?xml version='1.0'?><LoopBean><name>base</name><friend><name/><friend><name/></friend>" + + "</friend></LoopBean>"; + xmlAssertIsomorphicContent(parseString(out.getBuffer().toString()),parseString(xml), true); + + // test output when not writing empty elements + out = new StringWriter(); + out.write("<?xml version='1.0'?>"); + writer = new BeanWriter(out); + writer.setWriteEmptyElements(false); + writer.getBindingConfiguration().setMapIDs(false); + writer.write(root); + xml = "<?xml version='1.0'?><LoopBean><name>base</name></LoopBean>"; + xmlAssertIsomorphicContent(parseString(out.getBuffer().toString()),parseString(xml), true); + + } +} Modified: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestBeanWriter.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestBeanWriter.java?rev=164896&r1=164895&r2=164896&view=diff ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestBeanWriter.java (original) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestBeanWriter.java Tue Apr 26 14:48:43 2005 @@ -311,45 +311,7 @@ // baseLog.debug(out.getBuffer().toString()); xmlAssertIsomorphicContent(parseString(out.getBuffer().toString()),parseString(xml), true); } - - /** Test nested case for writing empty elements */ - public void testNestedWriteEmptyElements() throws Exception{ - - // write same bean both times - LoopBean root = new LoopBean("base"); - LoopBean middle = new LoopBean(null); - root.setFriend(middle); - middle.setFriend(new LoopBean(null)); - -// SimpleLog baseLog = new SimpleLog( "[NestedEmpty]" ); -// baseLog.setLevel(SimpleLog.LOG_LEVEL_TRACE); - - // test output when writing empty elements - StringWriter out = new StringWriter(); - out.write("<?xml version='1.0'?>"); - BeanWriter writer = new BeanWriter(out); - writer.setWriteEmptyElements(true); - writer.getBindingConfiguration().setMapIDs(false); - writer.write(root); -// baseLog.debug("NESTED EMPTY"); -// baseLog.debug(out.getBuffer().toString()); - String xml = "<?xml version='1.0'?><LoopBean><name>base</name><friend><name/><friend><name/></friend>" - + "</friend></LoopBean>"; - xmlAssertIsomorphicContent(parseString(out.getBuffer().toString()),parseString(xml), true); - - // test output when not writing empty elements - out = new StringWriter(); - out.write("<?xml version='1.0'?>"); - writer = new BeanWriter(out); - writer.setWriteEmptyElements(false); - writer.getBindingConfiguration().setMapIDs(false); - writer.write(root); -// baseLog.debug("NESTED NOT EMPTY"); -// baseLog.debug(out.getBuffer().toString()); - xml = "<?xml version='1.0'?><LoopBean><name>base</name></LoopBean>"; - xmlAssertIsomorphicContent(parseString(out.getBuffer().toString()),parseString(xml), true); - - } + public void testArrayWrite() throws Exception { ArrayBean bean = new ArrayBean("Rob"); Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SidekickBean.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SidekickBean.java?rev=164896&view=auto ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SidekickBean.java (added) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SidekickBean.java Tue Apr 26 14:48:43 2005 @@ -0,0 +1,38 @@ +/* + * Copyright 2005 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.io; + +/** + */ +public class SidekickBean { + + private String nickname; + + public SidekickBean() {} + + public SidekickBean(String name) { + setNickname(name); + } + + public String getNickname() { + return nickname; + } + + + public void setNickname(String nickname) { + this.nickname = nickname; + } +} Added: jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SuperheroBean.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SuperheroBean.java?rev=164896&view=auto ============================================================================== --- jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SuperheroBean.java (added) +++ jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/io/SuperheroBean.java Tue Apr 26 14:48:43 2005 @@ -0,0 +1,55 @@ +/* + * Copyright 2005 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.io; + +/** + */ +public class SuperheroBean { + + private String moniker; + private SidekickBean sidekick; + + public SuperheroBean() { + super(); + } + + public SuperheroBean(String moniker, SidekickBean sidekick) { + super(); + this.moniker = moniker; + this.sidekick = sidekick; + } + + public SuperheroBean(SidekickBean sidekick) { + super(); + this.sidekick = sidekick; + } + + public String getMoniker() { + return moniker; + } + + public void setMoniker(String moniker) { + this.moniker = moniker; + } + + public SidekickBean getSidekick() { + return sidekick; + } + + public void setSidekick(SidekickBean sidekick) { + this.sidekick = sidekick; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]