jstrachan 01/08/22 21:34:40
Modified: betwixt build.xml
betwixt/src/java/org/apache/commons/betwixt
XMLIntrospector.java
betwixt/src/java/org/apache/commons/betwixt/io
BeanWriter.java
betwixt/src/test/org/apache/commons/betwixt
CustomerBean.java
Added: betwixt/src/java/org/apache/commons/betwixt/expression
StringExpression.java
betwixt/src/test/org/apache/commons/betwixt
SampleBeanWriter.java
Log:
Navigating iterators and arrays appears to work
Revision Changes Path
1.2 +10 -1 jakarta-commons-sandbox/betwixt/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/build.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- build.xml 2001/08/22 12:25:01 1.1
+++ build.xml 2001/08/23 04:34:40 1.2
@@ -1,4 +1,4 @@
-<!-- $Id: build.xml,v 1.1 2001/08/22 12:25:01 jstrachan Exp $ -->
+<!-- $Id: build.xml,v 1.2 2001/08/23 04:34:40 jstrachan Exp $ -->
<project name="betwixt" default="test" basedir=".">
<!-- patternset describing files to be copied from the doc directory -->
@@ -236,4 +236,13 @@
<!-- ######################################################### -->
+ <target name="demo" depends="build-test" description="runs sample program">
+ <java classname="org.apache.commons.betwixt.SampleBeanWriter" fork="yes">
+ <classpath>
+ <pathelement location="${dest.classes}" />
+ <pathelement path="${classpath}" />
+ <pathelement path="${java.class.path}" />
+ </classpath>
+ </java>
+ </target>
</project>
1.4 +40 -29
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
Index: XMLIntrospector.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- XMLIntrospector.java 2001/08/22 19:44:55 1.3
+++ XMLIntrospector.java 2001/08/23 04:34:40 1.4
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: XMLIntrospector.java,v 1.3 2001/08/22 19:44:55 jstrachan Exp $
+ * $Id: XMLIntrospector.java,v 1.4 2001/08/23 04:34:40 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
+import org.apache.commons.betwixt.expression.StringExpression;
import org.apache.commons.betwixt.expression.Expression;
import org.apache.commons.betwixt.expression.IteratorExpression;
import org.apache.commons.betwixt.expression.MethodExpression;
@@ -33,7 +34,7 @@
/** <p><code>XMLIntrospector</code> an introspector of beans to create a
XMLBeanInfo instance.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class XMLIntrospector {
@@ -56,36 +57,42 @@
public XMLBeanInfo introspect(BeanInfo beanInfo) throws IntrospectionException
{
XMLBeanInfo answer = createXMLBeanInfo( beanInfo );
- ArrayList elements = new ArrayList();
- ArrayList attributes = new ArrayList();
-
- addProperties( beanInfo, elements, attributes );
-
- BeanInfo[] additionals = beanInfo.getAdditionalBeanInfo();
- if ( additionals != null ) {
- for ( int i = 0, size = additionals.length; i < size; i++ ) {
- BeanInfo otherInfo = additionals[i];
- addProperties( otherInfo, elements, attributes );
- }
- }
-
ElementDescriptor elementDescriptor = new ElementDescriptor();
elementDescriptor.setLocalName( beanInfo.getBeanDescriptor().getName() );
- int size = elements.size();
- if ( size > 0 ) {
- ElementDescriptor[] descriptors = new ElementDescriptor[size];
- elements.toArray( descriptors );
- elementDescriptor.setElementDescriptors( descriptors );
- }
- size = attributes.size();
- if ( size > 0 ) {
- AttributeDescriptor[] descriptors = new AttributeDescriptor[size];
- attributes.toArray( descriptors );
- elementDescriptor.setAttributeDescriptors( descriptors );
+ // add default string value for primitive types
+ if ( isPrimitiveType( beanInfo.getBeanDescriptor().getBeanClass() ) ) {
+ elementDescriptor.setTextExpression( StringExpression.getInstance() );
+ }
+ else {
+ ArrayList elements = new ArrayList();
+ ArrayList attributes = new ArrayList();
+
+ addProperties( beanInfo, elements, attributes );
+
+ BeanInfo[] additionals = beanInfo.getAdditionalBeanInfo();
+ if ( additionals != null ) {
+ for ( int i = 0, size = additionals.length; i < size; i++ ) {
+ BeanInfo otherInfo = additionals[i];
+ addProperties( otherInfo, elements, attributes );
+ }
+ }
+
+ int size = elements.size();
+ if ( size > 0 ) {
+ ElementDescriptor[] descriptors = new ElementDescriptor[size];
+ elements.toArray( descriptors );
+ elementDescriptor.setElementDescriptors( descriptors );
+ }
+ size = attributes.size();
+ if ( size > 0 ) {
+ AttributeDescriptor[] descriptors = new AttributeDescriptor[size];
+ attributes.toArray( descriptors );
+ elementDescriptor.setAttributeDescriptors( descriptors );
+ }
}
- answer.setElementDescriptor( elementDescriptor );
+ answer.setElementDescriptor( elementDescriptor );
return answer;
}
@@ -111,7 +118,10 @@
Expression loop = createLoopExpression( propertyDescriptor, type );
if ( loop != null ) {
ElementDescriptor elementDescriptor = new ElementDescriptor();
- elementDescriptor.setContextExpression( loop );
+
+ ElementDescriptor loopDescriptor = new ElementDescriptor();
+ loopDescriptor.setContextExpression( loop );
+ elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
nodeDescriptor = elementDescriptor;
elements.add( nodeDescriptor );
@@ -164,6 +174,7 @@
/** Returns true for primitive types */
protected boolean isPrimitiveType(Class type) {
- return type.getName().startsWith( "java.lang." ) || type.isAssignableFrom(
Date.class );
+ String name = type.getName();
+ return name.startsWith( "java.lang." ) || type.isAssignableFrom( Date.class
);
}
}
1.1
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/expression/StringExpression.java
Index: StringExpression.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: StringExpression.java,v 1.1 2001/08/23 04:34:40 jstrachan Exp $
*/
package org.apache.commons.betwixt.expression;
/** <p><code>StringExpression</code> returns the current context object as a
string.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @version $Revision: 1.1 $
*/
public class StringExpression implements Expression {
private static final StringExpression singleton = new StringExpression();
public static StringExpression getInstance() {
return singleton;
}
public StringExpression() {
}
public Object evaluate(Context context) {
Object value = context.getBean();
if ( value != null ) {
return value.toString();
}
return null;
}
}
1.4 +3 -3
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java
Index: BeanWriter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BeanWriter.java 2001/08/22 19:44:55 1.3
+++ BeanWriter.java 2001/08/23 04:34:40 1.4
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: BeanWriter.java,v 1.3 2001/08/22 19:44:55 jstrachan Exp $
+ * $Id: BeanWriter.java,v 1.4 2001/08/23 04:34:40 jstrachan Exp $
*/
package org.apache.commons.betwixt.io;
@@ -27,7 +27,7 @@
/** <p><code>BeanWriter</code> outputs a bean as XML.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class BeanWriter {
@@ -105,6 +105,7 @@
/** Writes the given element */
protected void write( ElementDescriptor elementDescriptor, Context context )
throws IOException, IntrospectionException {
String qualifiedName = elementDescriptor.getQualifiedName();
+ writePrintln();
writeIndent();
writer.write( "<" );
writer.write( qualifiedName );
@@ -151,7 +152,6 @@
}
}
- writePrintln();
write( childDescriptor, childContext );
}
--indentLevel;
1.4 +4 -4
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CustomerBean.java 2001/08/22 19:44:56 1.3
+++ CustomerBean.java 2001/08/23 04:34:40 1.4
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: CustomerBean.java,v 1.3 2001/08/22 19:44:56 jstrachan Exp $
+ * $Id: CustomerBean.java,v 1.4 2001/08/23 04:34:40 jstrachan Exp $
*/
package org.apache.commons.betwixt;
@@ -19,7 +19,7 @@
/** <p><code>CustomerBean</code> is a sample bean for use by the test cases.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class CustomerBean implements Serializable {
@@ -52,7 +52,8 @@
public Map getProjectMap() {
return projectMap;
}
-
+*/
+
public Iterator getProjectNames() {
if ( projectMap == null ) {
return null;
@@ -66,7 +67,6 @@
}
return new IteratorEnumeration( projectMap.values().iterator() );
}
-*/
public void setID(String id) {
this.id = id;
1.1
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/SampleBeanWriter.java
Index: SampleBeanWriter.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: SampleBeanWriter.java,v 1.1 2001/08/23 04:34:40 jstrachan Exp $
*/
package org.apache.commons.betwixt;
import java.io.StringWriter;
import junit.framework.*;
import org.apache.commons.betwixt.io.BeanWriter;
/** A sample program to output a bean as pretty printed XML
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
* @version $Revision: 1.1 $
*/
public class SampleBeanWriter extends AbstractTestCase {
public SampleBeanWriter(String testName) {
super(testName);
}
public static void main(String[] args) throws Exception {
SampleBeanWriter sample = new SampleBeanWriter("foo");
sample.run( args );
}
public void run(String[] args) throws Exception {
Object bean = null;
if ( args.length > 0 ) {
bean = Class.forName( args[0] ).newInstance();
}
else {
bean = createBean();
}
write( bean );
}
public void write(Object bean) throws Exception {
BeanWriter writer = new BeanWriter();
writer.enablePrettyPrint();
writer.write( bean );
}
}