rdonkin 02/02/26 11:51:32
Modified: betwixt/src/java/org/apache/commons/betwixt XMLBeanInfo.java
betwixt/src/java/org/apache/commons/betwixt/io
BeanWriter.java
betwixt/src/test/org/apache/commons/betwixt
TestBeanWriter.java
Added: betwixt/src/java/org/apache/commons/betwixt/io
CyclicReferenceException.java
Removed: betwixt/src/java/org/apache/commons/betwixt/expression
CyclicReferenceException.java
Log:
For users which really don't want IDs added to their elements, i've added a property
which disables this and instead throws a CyclicReferenceException runtime whenever a
cyclic reference is encountered in the graph. The default is still to use ID/IDREFs.
Revision Changes Path
1.6 +23 -7
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java
Index: XMLBeanInfo.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XMLBeanInfo.java 25 Feb 2002 19:07:15 -0000 1.5
+++ XMLBeanInfo.java 26 Feb 2002 19:51:31 -0000 1.6
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java,v
1.5 2002/02/25 19:07:15 rdonkin Exp $
- * $Revision: 1.5 $
- * $Date: 2002/02/25 19:07:15 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/XMLBeanInfo.java,v
1.6 2002/02/26 19:51:31 rdonkin Exp $
+ * $Revision: 1.6 $
+ * $Date: 2002/02/26 19:51:31 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: XMLBeanInfo.java,v 1.5 2002/02/25 19:07:15 rdonkin Exp $
+ * $Id: XMLBeanInfo.java,v 1.6 2002/02/26 19:51:31 rdonkin Exp $
*/
package org.apache.commons.betwixt;
@@ -68,7 +68,7 @@
* or XSLT for example.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.5 $
+ * @version $Revision: 1.6 $
*/
public class XMLBeanInfo {
/** Descriptor for main element */
@@ -80,6 +80,10 @@
private String idAttributeName = "id";
/** <code>IDREF</code> attribute name */
private String idrefAttributeName = "idref";
+ /** Have we already cached the <code>idAttributeDescriptor</code>? */
+ private boolean cachedIDAttribute = false;
+ /** Cached <code>ID</code> attribute descriptor */
+ private AttributeDescriptor idAttributeDescriptor;
/** Base constructor */
public XMLBeanInfo( Class beanClass ) {
@@ -96,7 +100,11 @@
this.elementDescriptor = elementDescriptor;
}
- /** @return the beans class that this XML info refers to */
+ /**
+ * Gets the beans class that this XML info refers to
+ *
+ * @return the beans class that this XML info refers to
+ */
public Class getBeanClass() {
return beanClass;
}
@@ -108,7 +116,15 @@
/** Search attributes for one matching <code>ID</code> attribute name */
public AttributeDescriptor getIDAttribute() {
- // XXX this only need to be done once!
+ if ( cachedIDAttribute = false ) {
+ idAttributeDescriptor = findIDAttribute();
+ cachedIDAttribute = true;
+ }
+ return idAttributeDescriptor;
+ }
+
+ /** ID attribute search implementation */
+ private AttributeDescriptor findIDAttribute() {
// we'll check to see if the bean already has an id
if ( getElementDescriptor().hasAttributes() ) {
AttributeDescriptor[] attributes =
getElementDescriptor().getAttributeDescriptors();
1.16 +63 -13
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.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- BeanWriter.java 25 Feb 2002 19:07:15 -0000 1.15
+++ BeanWriter.java 26 Feb 2002 19:51:32 -0000 1.16
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
1.15 2002/02/25 19:07:15 rdonkin Exp $
- * $Revision: 1.15 $
- * $Date: 2002/02/25 19:07:15 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v
1.16 2002/02/26 19:51:32 rdonkin Exp $
+ * $Revision: 1.16 $
+ * $Date: 2002/02/26 19:51:32 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: BeanWriter.java,v 1.15 2002/02/25 19:07:15 rdonkin Exp $
+ * $Id: BeanWriter.java,v 1.16 2002/02/26 19:51:32 rdonkin Exp $
*/
package org.apache.commons.betwixt.io;
@@ -116,9 +116,16 @@
* BeanWriter defaults to using <code>SequentialIDGenerator</code>
* which supplies id values in numeric sequence.
*
+ * <p>If generated <code>ID</code> attribute values are not acceptable in the
output,
+ * then this can be disabled by setting the <code>WriteIDs</code> property to
false.
+ * If a cyclic reference is encountered in this case then a
+ * <code>CyclicReferenceException</code> will be thrown.
+ * When the <code>WriteIDs</code> property is set to false,
+ * it is recommended that this exception is caught by the caller.
+ *
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.15 $
+ * @version $Revision: 1.16 $
*/
public class BeanWriter {
@@ -151,6 +158,8 @@
private HashMap idMap = new HashMap();
/** Used to generate ID attribute values*/
private IDGenerator idGenerator = new SequentialIDGenerator();
+ /** Should generated <code>ID</code> attribute values be added to the elements?
*/
+ private boolean writeIDs = true;
/**
* <p> Constructor uses <code>System.out</code> for output.</p>
@@ -183,6 +192,12 @@
*
* <p> This writes an xml fragment representing the bean to the current
stream.</p>
*
+ * <p>This method will throw a <code>CyclicReferenceException</code> when a
cycle
+ * is encountered in the graph <strong>only</strong> if the
<code>WriteIDs</code>
+ * property is false.</p>
+ *
+ * @throws CyclicReferenceException when a cyclic reference is encountered
+ *
* @param bean write out representation of this bean
*/
public void write(Object bean) throws IOException, IntrospectionException {
@@ -198,7 +213,15 @@
log.debug( "Finished writing bean graph." );
}
- /** Writes the given bean to the current stream using the given
<code>qualifiedName</code> */
+ /**
+ * <p>Writes the given bean to the current stream using the given
<code>qualifiedName</code>.</p>
+ *
+ * <p>This method will throw a <code>CyclicReferenceException</code> when a
cycle
+ * is encountered in the graph <strong>only</strong> if the
<code>WriteIDs</code>
+ * property is false.</p>
+ *
+ * @throws CyclicReferenceException when a cyclic reference is encountered
+ */
public void write(
String qualifiedName,
Object bean)
@@ -244,13 +267,22 @@
id = new Integer( idGenerator.nextId() );
idMap.put( bean, id);
- // write element with id
- write(
- qualifiedName,
- elementDescriptor,
- context ,
- beanInfo.getIDAttributeName(),
- id.toString());
+ if ( writeIDs ) {
+ // write element with id
+ write(
+ qualifiedName,
+ elementDescriptor,
+ context ,
+ beanInfo.getIDAttributeName(),
+ id.toString());
+
+ } else {
+ // write element without ID
+ write(
+ qualifiedName,
+ elementDescriptor,
+ context );
+ }
} else {
// use id from bean property
@@ -267,6 +299,12 @@
}
}
else {
+ // we have a cyclic reference
+ if ( !writeIDs ) {
+ // if we're not writing IDs, then throw exception
+ throw new CyclicReferenceException();
+ }
+
// we've already written this bean so write an IDREF
writeIDREFElement(
qualifiedName,
@@ -319,7 +357,19 @@
this.idGenerator = idGenerator;
}
+ /** Get whether generated <code>ID</code> attribute values should be added to
the elements */
+ public boolean getWriteIDs() {
+ return writeIDs;
+ }
+ /**
+ * Set whether generated <code>ID</code> attribute values should be added to
the elements
+ * If this property is set to false, then <code>CyclicReferenceException</code>
+ * will be thrown whenever a cyclic occurs in the bean graph.
+ */
+ public void setWriteIDs(boolean writeIDs) {
+ this.writeIDs = writeIDs;
+ }
/**
* <p> Get the introspector used. </p>
1.1
jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/CyclicReferenceException.java
Index: CyclicReferenceException.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/io/CyclicReferenceException.java,v
1.1 2002/02/26 19:51:32 rdonkin Exp $
* $Revision: 1.1 $
* $Date: 2002/02/26 19:51:32 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* $Id: CyclicReferenceException.java,v 1.1 2002/02/26 19:51:32 rdonkin Exp $
*/
package org.apache.commons.betwixt.io;
/**
* <p>Thrown when bean evaluation finds a cycle reference.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Robert Burrell Donkin</a>
* @version $Revision: 1.1 $
*/
public class CyclicReferenceException extends RuntimeException {
/** Message used with empty constructor */
private static final String DEFAULT_MESSAGE
= "Bean graph contains a cyclic reference";
/** Construct exception with default message.
*/
public CyclicReferenceException() {
super(DEFAULT_MESSAGE);
}
/** Construct exception with given message
*/
public CyclicReferenceException(String message) {
super(message);
}
}
1.12 +19 -5
jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java
Index: TestBeanWriter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestBeanWriter.java 19 Feb 2002 06:10:27 -0000 1.11
+++ TestBeanWriter.java 26 Feb 2002 19:51:32 -0000 1.12
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v
1.11 2002/02/19 06:10:27 jstrachan Exp $
- * $Revision: 1.11 $
- * $Date: 2002/02/19 06:10:27 $
+ * $Header:
/home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/TestBeanWriter.java,v
1.12 2002/02/26 19:51:32 rdonkin Exp $
+ * $Revision: 1.12 $
+ * $Date: 2002/02/26 19:51:32 $
*
* ====================================================================
*
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * $Id: TestBeanWriter.java,v 1.11 2002/02/19 06:10:27 jstrachan Exp $
+ * $Id: TestBeanWriter.java,v 1.12 2002/02/26 19:51:32 rdonkin Exp $
*/
package org.apache.commons.betwixt;
@@ -69,6 +69,7 @@
import junit.textui.TestRunner;
import org.apache.commons.betwixt.io.BeanWriter;
+import org.apache.commons.betwixt.io.CyclicReferenceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -78,7 +79,7 @@
/** Test harness for the BeanWriter
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
- * @version $Revision: 1.11 $
+ * @version $Revision: 1.12 $
*/
public class TestBeanWriter extends AbstractTestCase {
@@ -111,6 +112,19 @@
writer.enablePrettyPrint();
writer.write( LoopBean.createNoLoopExampleBean() );
writer.write( LoopBean.createLoopExampleBean() );
+
+ // test not writing IDs
+ writer.setWriteIDs(false);
+
+ writer.write( LoopBean.createNoLoopExampleBean() );
+
+ try {
+ writer.write( LoopBean.createLoopExampleBean() );
+ fail("CyclicReferenceException not thrown!");
+
+ } catch (CyclicReferenceException e) {
+ // everything's fine
+ }
}
public void testEscaping() throws Exception {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>