You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Gregory M. Messner" <gm...@titan.com> on 2002/09/07 01:23:24 UTC
[PATCH][BETWIXT] Writing Repeating Choice
I have an XML document that contains a choice list that is unbounded,
the AbstractBeanWriter would output each item from the Collection with
the element name found for the first item. I corrected this problem in
the iteration loop by looking at the class of each item to see if it
changed from the previous item and if so I then determine the new
element name. The patch follows, as this is my first message to this
mail list I hope that I have submitted this correctly, if not please let
me know the proper way of doing things.
Greg
Index: AbstractBeanWriter.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v
retrieving revision 1.4
diff -u -r1.4 AbstractBeanWriter.java
--- AbstractBeanWriter.java 14 Aug 2002 18:50:21 -0000 1.4
+++ AbstractBeanWriter.java 6 Sep 2002 23:17:11 -0000
@@ -485,6 +485,8 @@
String qualifiedName = childDescriptor.getQualifiedName();
// XXXX: should we handle nulls better
if ( childBean instanceof Iterator ) {
+
+ int lastClassHash = 0;
for ( Iterator iter = (Iterator) childBean; iter.hasNext(); ) {
if ( ! writtenContent ) {
writtenContent = true;
@@ -492,8 +494,29 @@
expressTagClose();
}
}
+
++indentLevel;
- write( qualifiedName, iter.next() );
+ Object thisChild = iter.next();
+
+ // Determine if the element name has changed
+ if (lastClassHash == 0) {
+
+ if (thisChild != null) {
+ lastClassHash = thisChild.getClass().hashCode();
+ }
+
+ } else if (thisChild != null) {
+
+ int thisClassHash = thisChild.getClass().hashCode();
+ if (lastClassHash != thisClassHash) {
+
+ lastClassHash = thisClassHash;
+ qualifiedName =
+ getQualifiedName(thisChild, qualifiedName);
+ }
+ }
+
+ write( qualifiedName, thisChild );
--indentLevel;
}
}
@@ -548,6 +571,34 @@
}
return writtenContent;
}
+
+
+ /**
+ * Gets the qualifiedName for the specified bean, defaulting if the
+ * bean does not have a qualified name.
+ */
+ protected String getQualifiedName (Object bean, String qualifiedName ) {
+
+ try {
+
+ XMLBeanInfo beanInfo = introspector.introspect( bean );
+ if ( beanInfo != null ) {
+ ElementDescriptor elementDescriptor = beanInfo.getElementDescriptor();
+ if ( elementDescriptor != null ) {
+
+ String qname = elementDescriptor.getQualifiedName();
+ if (qname != null) {
+ return (qname);
+ }
+ }
+ }
+
+ } catch (Exception ignore) {
+ }
+
+ return (qualifiedName);
+ }
+
/** Writes the attribute declarations */
protected void writeAttributes(
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>