[prev in list] [next in list] [prev in thread] [next in thread]
List: jakarta-commons-dev
Subject: cvs commit: jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt RSSBeanReader.java A
From: jstrachan () apache ! org
Date: 2002-01-31 21:24:43
[Download RAW message or body]
jstrachan 02/01/31 13:24:43
Modified: betwixt TODO.txt build.xml
betwixt/src/java/org/apache/commons/betwixt
ElementDescriptor.java
betwixt/src/java/org/apache/commons/betwixt/expression
MethodUpdater.java Updater.java
betwixt/src/java/org/apache/commons/betwixt/io
BeanCreateRule.java BeanReader.java
betwixt/src/test/org/apache/commons/betwixt AddressBean.java
CustomerBean.java RSSBeanWriter.java customer.xml
Added: betwixt/src/test/org/apache/commons/betwixt
RSSBeanReader.java
Log:
Got the early BeanReader code working for simple getters and setters such as for \
1-1 relationships of beans. Also added an early RSS reader sample program which is \
some ways off working but will hopefully test out the reading capabilities of \
betwixt+digester
Revision Changes Path
1.2 +28 -0 jakarta-commons-sandbox/betwixt/TODO.txt
Index: TODO.txt
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/TODO.txt,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TODO.txt 19 Nov 2001 16:22:11 -0000 1.1
+++ TODO.txt 31 Jan 2002 21:24:42 -0000 1.2
@@ -4,3 +4,31 @@
* consider using betwixt to generate digester
rules for a java bean using introspection
+* should the NodeDescriptors keep a reference to
+ the introspector, so that navigating properties happens
+ automatically.
+
+e.g.
+
+Customer {
+ Address address;
+}
+
+Address {
+ String name;
+}
+
+By default then customer's XMLInfo looks like
+
+<xmlInfo class="Customer">
+ <element name="address" type="Address"/>
+</xmlInfo>
+
+it should expand to...
+
+<xmlInfo class="Customer">
+ <element name="address" type="Address" expression="address">
+ <attribute name="name" type="String" expression="name"/>
+ </element>
+</xmlInfo>
+
1.9 +13 -2 jakarta-commons-sandbox/betwixt/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/build.xml,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- build.xml 31 Jan 2002 19:56:02 -0000 1.8
+++ build.xml 31 Jan 2002 21:24:42 -0000 1.9
@@ -3,7 +3,7 @@
<!--
"Digester" component of the Jakarta Commons Subproject
- $Id: build.xml,v 1.8 2002/01/31 19:56:02 jstrachan Exp $
+ $Id: build.xml,v 1.9 2002/01/31 21:24:42 jstrachan Exp $
-->
@@ -256,7 +256,10 @@
<java classname="org.apache.commons.betwixt.TestBeanReader" fork="yes">
<classpath refid="test.classpath"/>
<sysproperty key="org.apache.commons.logging.log" \
value="org.apache.commons.logging.SimpleLog"/>
- <sysproperty key="org.apache.commons.logging.simplelog.defaultlog" \
value="info"/> + <sysproperty \
key="org.apache.commons.logging.simplelog.defaultlog" value="debug"/> + \
<sysproperty key="org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester" \
value="warn"/> + <sysproperty \
key="org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester.sax" \
value="warn"/> + <sysproperty \
key="org.apache.commons.logging.simplelog.defaultlog" value="debug"/> </java>
</target>
@@ -282,6 +285,14 @@
description="Runs the RSS sample">
<echo message="Running the RSS sample..."/>
<java classname="org.apache.commons.betwixt.RSSBeanWriter" fork="yes">
+ <classpath refid="test.classpath"/>
+ </java>
+ </target>
+
+ <target name="demo.rss2" depends="compile.tests"
+ description="Runs the RSS read and write sample">
+ <echo message="Running the RSS read and write sample..."/>
+ <java classname="org.apache.commons.betwixt.RSSBeanReader" fork="yes">
<classpath refid="test.classpath"/>
</java>
</target>
1.7 +9 -5 \
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java
Index: ElementDescriptor.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ElementDescriptor.java 31 Jan 2002 19:56:02 -0000 1.6
+++ ElementDescriptor.java 31 Jan 2002 21:24:42 -0000 1.7
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v \
1.6 2002/01/31 19:56:02 jstrachan Exp $
- * $Revision: 1.6 $
- * $Date: 2002/01/31 19:56:02 $
+ * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v \
1.7 2002/01/31 21:24:42 jstrachan Exp $ + * $Revision: 1.7 $
+ * $Date: 2002/01/31 21:24:42 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: ElementDescriptor.java,v 1.6 2002/01/31 19:56:02 jstrachan Exp $
+ * $Id: ElementDescriptor.java,v 1.7 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -70,7 +70,7 @@
* and <code>ElementDescriptor</code>'s for it's child elements.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.6 $
+ * @version $Revision: 1.7 $
*/
public class ElementDescriptor extends NodeDescriptor {
@@ -99,6 +99,10 @@
super( localName );
}
+ public String toString() {
+ return "ElementDescriptor[qname=" + getQualifiedName() + ",class=" + \
getPropertyType(); + }
+
/** Creates a <code>ElementDescriptor</code> with namespace URI and qualified \
name */
public ElementDescriptor(String localName, String qualifiedName, String uri) {
super(localName, qualifiedName, uri);
1.2 +12 -3 \
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java
Index: MethodUpdater.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/MethodUpdater.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MethodUpdater.java 31 Jan 2002 19:56:03 -0000 1.1
+++ MethodUpdater.java 31 Jan 2002 21:24:42 -0000 1.2
@@ -5,21 +5,27 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: MethodUpdater.java,v 1.1 2002/01/31 19:56:03 jstrachan Exp $
+ * $Id: MethodUpdater.java,v 1.2 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt.expression;
import java.lang.reflect.Method;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogSource;
+
/** <p><code>MethodUpdater</code> updates the current bean context
* by calling a WriteMethod with the String value from the XML attribute
* or element.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class MethodUpdater implements Updater {
+ /** Logger */
+ private static final Log log = LogSource.getInstance( MethodUpdater.class );
+
/** The method to call on the bean */
private Method method;
@@ -33,13 +39,16 @@
}
/** Updates the current bean context with the given String value */
- public void update(Context context, String newValue) {
+ public void update(Context context, Object newValue) {
Object bean = context.getBean();
if ( bean != null ) {
// XXX: should use the primitive type converters from
// commons-beanutils either BeanUtils or PropertyUtils
Object[] arguments = { newValue };
try {
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Calling setter method: " + method.getName() + " on \
bean: " + bean + " with new value: " + newValue ); + }
method.invoke( bean, arguments );
}
catch (Exception e) {
1.2 +2 -2 \
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Updater.java
Index: Updater.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/Updater.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Updater.java 31 Jan 2002 19:56:03 -0000 1.1
+++ Updater.java 31 Jan 2002 21:24:42 -0000 1.2
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: Updater.java,v 1.1 2002/01/31 19:56:03 jstrachan Exp $
+ * $Id: Updater.java,v 1.2 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt.expression;
@@ -13,7 +13,7 @@
* context bean from some text from an XML attribute or element.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public interface Updater {
@@ -21,5 +21,5 @@
* This is typically used when parsing XML and updating a beans value
* from XML
*/
- public void update(Context context, String newValue);
+ public void update(Context context, Object newValue);
}
1.2 +74 -14 \
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java
Index: BeanCreateRule.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanCreateRule.java 31 Jan 2002 19:56:03 -0000 1.1
+++ BeanCreateRule.java 31 Jan 2002 21:24:42 -0000 1.2
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: BeanCreateRule.java,v 1.1 2002/01/31 19:56:03 jstrachan Exp $
+ * $Id: BeanCreateRule.java,v 1.2 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt.io;
@@ -15,6 +15,8 @@
import org.apache.commons.betwixt.AttributeDescriptor;
import org.apache.commons.betwixt.ElementDescriptor;
+import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
import org.apache.commons.betwixt.expression.Context;
import org.apache.commons.betwixt.expression.Updater;
@@ -30,25 +32,32 @@
* from the betwixt XML metadata.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class BeanCreateRule extends Rule {
/** Logger */
- private static final Log log = LogSource.getInstance( \
BeanCreateRule.class.getName() ); + private static final Log log = \
LogSource.getInstance( BeanCreateRule.class );
/** The descriptor of this element */
private ElementDescriptor descriptor;
/** The Context used when evaluating Updaters */
- private Context context = new Context();
+ private Context context;
/** Have we added our child rules to the digester? */
private boolean addedChildren;
/** In this begin-end loop did we actually create a new bean */
private boolean createdBean;
- public BeanCreateRule(Digester digester, ElementDescriptor descriptor) {
+ public BeanCreateRule(BeanReader digester, ElementDescriptor descriptor) {
super( digester );
this.descriptor = descriptor;
+ this.context = new Context();
+ }
+
+ public BeanCreateRule(BeanReader digester, ElementDescriptor descriptor, \
Context context) { + super( digester );
+ this.descriptor = descriptor;
+ this.context = context;
}
@@ -68,14 +77,32 @@
// XXX: or let digester take nulls, which would be easier for us ;-)
createdBean = false;
+ Object instance = null;
if ( descriptor.getPropertyType() != null ) {
- Object instance = createBean(attributes);
+ instance = createBean(attributes);
if ( instance != null ) {
createdBean = true;
- context.setBean( instance );
+ // force any setters of the parent bean to be called for this new \
(property value) object + Updater updater = descriptor.getUpdater();
+ if ( updater != null ) {
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Calling updater for: " + descriptor + " with: \
" + instance ); + }
+ updater.update( context, instance );
+ }
+
+ context.setBean( instance );
+
+
+ // if we are a reference to a type we should lookup the original
+ // as this ElementDescriptor will be 'hollow' and have no child \
attributes/elements. + // XXX: this should probably be done by the \
NodeDescriptors... + ElementDescriptor typeDescriptor = \
getElementDescriptor( descriptor ); + //ElementDescriptor \
typeDescriptor = descriptor; +
// iterate through all attributes
- AttributeDescriptor[] attributeDescriptors = \
descriptor.getAttributeDescriptors(); + AttributeDescriptor[] \
attributeDescriptors = typeDescriptor.getAttributeDescriptors(); if ( \
attributeDescriptors != null ) {
for ( int i = 0, size = attributeDescriptors.length; i < size; \
i++ ) {
AttributeDescriptor attributeDescriptor = \
attributeDescriptors[i]; @@ -85,18 +112,19 @@
attributeDescriptor.getLocalName()
);
- Updater updater = attributeDescriptor.getUpdater();
+ updater = attributeDescriptor.getUpdater();
if ( updater != null ) {
updater.update( context, value );
}
}
}
-
+
addChildRules();
digester.push(instance);
}
}
+
}
@@ -106,6 +134,7 @@
public void end() throws Exception {
if ( createdBean ) {
Object top = digester.pop();
+ context.setBean( digester.peek() );
}
}
@@ -119,7 +148,7 @@
return descriptor.getPropertyType().newInstance();
}
catch (Exception e) {
- log.debug( "Could not create instance of type: " + \
descriptor.getPropertyType().getName(), e ); + log.debug( "Could not \
create instance of type: " + descriptor.getPropertyType().getName() ); return null;
}
}
@@ -129,9 +158,16 @@
if ( ! addedChildren ) {
addedChildren = true;
- Digester digester = getDigester();
+ BeanReader digester = getBeanReader();
String prefix = descriptor.getQualifiedName() + "/";
- ElementDescriptor[] childDescriptors = \
descriptor.getElementDescriptors(); +
+ // if we are a reference to a type we should lookup the original
+ // as this ElementDescriptor will be 'hollow' and have no child \
attributes/elements. + // XXX: this should probably be done by the \
NodeDescriptors... + ElementDescriptor typeDescriptor = \
getElementDescriptor( descriptor ); + //ElementDescriptor typeDescriptor \
= descriptor; +
+ ElementDescriptor[] childDescriptors = \
typeDescriptor.getElementDescriptors(); if ( childDescriptors != null ) {
for ( int i = 0, size = childDescriptors.length; i < size; i++ ) {
@@ -148,10 +184,34 @@
digester.addRule( path, rule );
}
else {
- digester.addRule( path, new BeanCreateRule( digester, \
childDescriptor ) ); + digester.addRule( path, new \
BeanCreateRule( digester, childDescriptor, context ) ); }
}
}
}
+ }
+
+ protected BeanReader getBeanReader() {
+ return (BeanReader) getDigester();
+ }
+
+ /** Allows the navigation from a reference to a property object to the \
descriptor defining what + * the property is. i.e. doing the join from a \
reference to a type to lookup its descriptor. + * This could be done \
automatically by the NodeDescriptors. Refer to TODO.txt for more info. + */
+ protected ElementDescriptor getElementDescriptor( ElementDescriptor \
propertyDescriptor ) { + Class beanClass = \
propertyDescriptor.getPropertyType(); + if ( beanClass != null ) {
+ XMLIntrospector introspector = getBeanReader().getXMLIntrospector();
+ try {
+ XMLBeanInfo xmlInfo = introspector.introspect( beanClass );
+ return xmlInfo.getElementDescriptor();
+ }
+ catch (Exception e) {
+ log.warn( "Could not introspect class: " + beanClass, e );
+ }
+ }
+ // could not find a better descriptor so use the one we've got
+ return propertyDescriptor;
}
}
1.2 +14 -2 \
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java
Index: BeanReader.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanReader.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanReader.java 31 Jan 2002 19:56:03 -0000 1.1
+++ BeanReader.java 31 Jan 2002 21:24:42 -0000 1.2
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: BeanReader.java,v 1.1 2002/01/31 19:56:03 jstrachan Exp $
+ * $Id: BeanReader.java,v 1.2 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt.io;
@@ -32,7 +32,7 @@
/** <p><code>BeanReader</code> reads a tree of beans from an XML document.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class BeanReader extends Digester {
@@ -77,6 +77,18 @@
ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); \
String path = elementDescriptor.getQualifiedName();
+
+ Rule rule = new BeanCreateRule( this, elementDescriptor );
+ addRule( path, rule );
+
+ log.info( "Added rule: " + rule + " to path: " + path );
+ }
+
+ /** Registers a bean class for use by the writer at the given path */
+ public void registerBeanClass(String path, Class beanClass) throws \
IntrospectionException { + // introspect and find the ElementDescriptor to \
use as the root + XMLBeanInfo xmlInfo = introspector.introspect( beanClass );
+ ElementDescriptor elementDescriptor = xmlInfo.getElementDescriptor(); \
Rule rule = new BeanCreateRule( this, elementDescriptor );
addRule( path, rule );
1.3 +8 -5 \
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AddressBean.java \
Index: AddressBean.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AddressBean.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AddressBean.java 30 Jan 2002 19:35:37 -0000 1.2
+++ AddressBean.java 31 Jan 2002 21:24:42 -0000 1.3
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AddressBean.java,v \
1.2 2002/01/30 19:35:37 rdonkin Exp $
- * $Revision: 1.2 $
- * $Date: 2002/01/30 19:35:37 $
+ * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/AddressBean.java,v \
1.3 2002/01/31 21:24:42 jstrachan Exp $ + * $Revision: 1.3 $
+ * $Date: 2002/01/31 21:24:42 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: AddressBean.java,v 1.2 2002/01/30 19:35:37 rdonkin Exp $
+ * $Id: AddressBean.java,v 1.3 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -66,7 +66,7 @@
/** <p><code>CustomerBean</code> is a sample bean for use by the test cases.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class AddressBean implements Serializable {
@@ -110,4 +110,7 @@
this.country = country;
}
+ public String toString() {
+ return super.toString() + "[street=" + street + ", city=" + city+ \
",country=" + country + "]"; + }
}
1.8 +19 -6 \
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java \
Index: CustomerBean.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- CustomerBean.java 30 Jan 2002 19:35:37 -0000 1.7
+++ CustomerBean.java 31 Jan 2002 21:24:42 -0000 1.8
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v \
1.7 2002/01/30 19:35:37 rdonkin Exp $
- * $Revision: 1.7 $
- * $Date: 2002/01/30 19:35:37 $
+ * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v \
1.8 2002/01/31 21:24:42 jstrachan Exp $ + * $Revision: 1.8 $
+ * $Date: 2002/01/31 21:24:42 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: CustomerBean.java,v 1.7 2002/01/30 19:35:37 rdonkin Exp $
+ * $Id: CustomerBean.java,v 1.8 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -70,13 +70,19 @@
import org.apache.commons.collections.IteratorEnumeration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogSource;
+
/** <p><code>CustomerBean</code> is a sample bean for use by the test cases.</p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.7 $
+ * @version $Revision: 1.8 $
*/
public class CustomerBean implements Serializable {
+ /** Logger */
+ private static final Log log = LogSource.getInstance( CustomerBean.class );
+
private String id;
private String name;
private String[] emails;
@@ -149,6 +155,10 @@
public void setAddress(AddressBean address) {
this.address = address;
+
+ if ( log.isDebugEnabled() ) {
+ log.debug( "Setting the address to be: " + address );
+ }
}
public void setProjectMap(Map projectMap) {
@@ -164,5 +174,8 @@
locations.set(index, location);
}
}
-
+
+ public String toString() {
+ return super.toString() + "[ID=" + id + ", name=" + name + ",address=" + \
address + "]"; + }
}
1.2 +3 -2 \
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/RSSBeanWriter.java
Index: RSSBeanWriter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/RSSBeanWriter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RSSBeanWriter.java 31 Jan 2002 19:56:03 -0000 1.1
+++ RSSBeanWriter.java 31 Jan 2002 21:24:42 -0000 1.2
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: RSSBeanWriter.java,v 1.1 2002/01/31 19:56:03 jstrachan Exp $
+ * $Id: RSSBeanWriter.java,v 1.2 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -20,10 +20,11 @@
import org.apache.commons.digester.rss.RSSDigester;
-/** A sample program to output a bean as pretty printed XML
+/** Reads an RSS file using Digesters's RSS demo then uses Betwixt
+ * to output it as XML again.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class RSSBeanWriter extends AbstractTestCase {
1.2 +1 -1 \
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/customer.xml
Index: customer.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/customer.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- customer.xml 31 Jan 2002 19:56:03 -0000 1.1
+++ customer.xml 31 Jan 2002 21:24:42 -0000 1.2
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<CustomerBean name="James" ID="1">
+ <address code="N5" city="London" country="UK" street="Near the park"/>
<projectMap>
<entry>
<key>jaxen</key>
@@ -34,7 +35,6 @@
<String>http://dom4j.org</String>
<String>http://jakarta.apache.org/commons/</String>
</projectURLs>
- <address code="N5" city="London" country="UK" street="Near the park"/>
<numbers>
<Integer>3</Integer>
<Integer>4</Integer>
1.1 \
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/RSSBeanReader.java
Index: RSSBeanReader.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
* $Id: RSSBeanReader.java,v 1.1 2002/01/31 21:24:42 jstrachan Exp $
*/
package org.apache.commons.betwixt;
import java.net.URL;
import java.io.InputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.digester.rss.Channel;
import org.apache.commons.digester.rss.RSSDigester;
/** Reads an RSS file using Betwixt's auto-digester rules then
* outputs it again.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @version $Revision: 1.1 $
*/
public class RSSBeanReader extends AbstractTestCase {
/**
* The set of public identifiers, and corresponding resource names,
* for the versions of the DTDs that we know about.
*/
protected static final String registrations[] = {
"-//Netscape Communications//DTD RSS 0.9//EN",
"/org/apache/commons/digester/rss/rss-0.9.dtd",
"-//Netscape Communications//DTD RSS 0.91//EN",
"/org/apache/commons/digester/rss/rss-0.91.dtd",
};
public RSSBeanReader(String testName) {
super(testName);
}
public static void main(String[] args) throws Exception {
RSSBeanReader sample = new RSSBeanReader("RSS");
sample.run( args );
}
public void run(String[] args) throws Exception {
BeanReader reader = new BeanReader();
reader.registerBeanClass( "rss/channel", Channel.class );
// Register local copies of the DTDs we understand
for (int i = 0; i < registrations.length; i += 2) {
URL url = RSSDigester.class.getResource(registrations[i + 1]);
if (url != null) {
reader.register(registrations[i], url.toString());
}
}
Object bean = null;
if ( args.length > 0 ) {
bean = reader.parse( args[0] );
}
else {
InputStream in = getClass().getResourceAsStream( "rss-example.xml" );
bean = reader.parse( in );
in.close();
}
write( bean );
}
public void write(Object bean) throws Exception {
if ( bean == null ) {
throw new Exception( "No bean read from the XML document!" );
}
BeanWriter writer = new BeanWriter();
writer.getXMLIntrospector().setAttributesForPrimitives(false);
writer.enablePrettyPrint();
writer.write( bean );
}
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic