Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 76601 invoked from network); 27 Feb 2003 19:20:52 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 27 Feb 2003 19:20:52 -0000 Received: (qmail 15367 invoked by uid 97); 27 Feb 2003 19:22:29 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 15360 invoked from network); 27 Feb 2003 19:22:28 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 27 Feb 2003 19:22:28 -0000 Received: (qmail 74351 invoked by uid 500); 27 Feb 2003 19:20:22 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 74307 invoked by uid 500); 27 Feb 2003 19:20:21 -0000 Received: (qmail 74283 invoked from network); 27 Feb 2003 19:20:21 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 27 Feb 2003 19:20:21 -0000 Received: (qmail 41889 invoked by uid 1289); 27 Feb 2003 19:20:19 -0000 Date: 27 Feb 2003 19:20:19 -0000 Message-ID: <20030227192019.41882.qmail@icarus.apache.org> From: rdonkin@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/betwixt/xdocs tasks.xml X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N rdonkin 2003/02/27 11:20:18 Modified: betwixt/src/java/org/apache/commons/betwixt/io AbstractBeanWriter.java BeanWriter.java SAXBeanWriter.java betwixt/xdocs tasks.xml Log: Changed writers to use new Sax inspired API. This is a big change with lots of deprecated methods but the new code is less complex and should be easier to work on. Revision Changes Path 1.14 +674 -376 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java Index: AbstractBeanWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- AbstractBeanWriter.java 22 Feb 2003 16:25:24 -0000 1.13 +++ AbstractBeanWriter.java 27 Feb 2003 19:20:17 -0000 1.14 @@ -77,6 +77,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.xml.sax.SAXException; +import org.xml.sax.Attributes; +import org.xml.sax.helpers.AttributesImpl; // FIX ME!!! // Logging logic! @@ -111,8 +113,6 @@ private boolean writeIDs = true; /** Should empty elements be written out? */ private boolean writeEmptyElements = true; - /** indentation level */ - private int indentLevel; /** * Marks the start of the bean writing. @@ -340,16 +340,6 @@ } /** - * Get the indentation for the current element. - * Used for pretty priting. - * - * @return the amount that the current element is indented - */ - protected int getIndentLevel() { - return indentLevel; - } - - /** * Set IDGenerator implementation * used to generate ID attribute values. * This property can be used to customize the algorithm used for generation. @@ -457,165 +447,67 @@ public final void setAbstractBeanWriterLog(Log log) { this.log = log; } - - // Expression methods + // SAX-style methods //------------------------------------------------------------------------- - - /** - * Express an element tag start using given qualified name. - * - * @param qualifiedName the qualified name of the element to be expressed - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void expressElementStart(String qualifiedName) - throws IOException, SAXException { - // do nothing - } - - /** - * Express an element tag start using given qualified name. + + /** + * Writes the start tag for an element. * - * @param uri the namespace uri - * @param localName the local name for this element - * @param qualifiedName the qualified name of the element to be expressed - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void expressElementStart(String uri, String localName, String qualifiedName) - throws IOException, SAXException { - expressElementStart( qualifiedName ); - } - - /** - * Express a closing tag. + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qName the element's qualified name + * @param attr the element's attributes * * @throws IOException if an IO problem occurs during writing * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha-1 */ - protected abstract void expressTagClose() throws IOException, SAXException; + protected void startElement( + String uri, + String localName, + String qName, + Attributes attr) + throws + IOException, + SAXException {} - /** - * Express an element end tag (with given name) + /** + * Writes the end tag for an element * - * @param qualifiedName the qualified name for the element to be closed + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qName the element's qualified name * * @throws IOException if an IO problem occurs during writing * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha-1 */ - protected void expressElementEnd(String qualifiedName) - throws IOException, SAXException { - // do nothing - } + protected void endElement( + String uri, + String localName, + String qName) + throws + IOException, + SAXException {} /** - * Express an element end tag (with given name) - * - * @param uri the namespace uri of the element close tag - * @param localName the local name of the element close tag - * @param qualifiedName the qualified name for the element to be closed + * Writes body text * - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void expressElementEnd( - String uri, - String localName, - String qualifiedName) - throws - IOException, - SAXException { - expressElementEnd(qualifiedName); - } - - - /** - * Express an empty element end. - * - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected abstract void expressElementEnd() throws IOException, SAXException; - - /** - * Express body text + * @param text the body text to be written * - * @param text the string to write out as the body of the current element - * * @throws IOException if an IO problem occurs during writing * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha 1 */ - protected abstract void expressBodyText(String text) throws IOException, SAXException; + protected void bodyText(String text) throws IOException, SAXException {} - /** - * Express an attribute - * - * @param qualifiedName the qualified name of the attribute - * @param value the attribute value - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void expressAttribute( - String qualifiedName, - String value) - throws - IOException, - SAXException { - // Do nothing - } - - /** - * Express an attribute - * - * @param namespaceUri the namespace uri - * @param localName the local name - * @param qualifiedName the qualified name of the attribute - * @param value the attribute value - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void expressAttribute( - String namespaceUri, - String localName, - String qualifiedName, - String value) - throws - IOException, - SAXException { - expressAttribute(qualifiedName, value); - } - - // Implementation methods //------------------------------------------------------------------------- - /** * Writes the given element * - * @param qualifiedName qualified name to use for the element - * @param elementDescriptor the ElementDescriptor describing the element - * @param context the Context to use to evaluate the bean expressions - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - * @throws IntrospectionException if a java beans introspection problem occurs - * @deprecated refactoring means method no longer needed - */ - protected void write( - String qualifiedName, - ElementDescriptor elementDescriptor, - Context context ) - throws - IOException, - SAXException, - IntrospectionException { - writeElement( "", qualifiedName, qualifiedName, elementDescriptor, context ); - } - - /** - * Writes the given element - * * @param namespaceUri the namespace uri * @param localName the local name * @param qualifiedName qualified name to use for the element @@ -645,46 +537,18 @@ } if (elementDescriptor.isWrapCollectionsInElement()) { - expressElementStart( namespaceUri, localName, qualifiedName ); + startElement( + namespaceUri, + localName, + qualifiedName, + new ElementAttributes( elementDescriptor, context )); } - - writeRestOfElement( namespaceUri, localName, qualifiedName, elementDescriptor, context); - } - } - - - /** - * Writes the given element adding an ID attribute - * - * @param qualifiedName qualified name to use for the element - * @param elementDescriptor the ElementDescriptor describing the element - * @param context the Context to use to evaluate the bean expressions - * @param idAttribute the qualified name of the ID attribute - * @param idValue the value for the ID attribute - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - * @throws IntrospectionException if a java beans introspection problem occurs - * @deprecated refactoring means method no longer needed - */ - protected void write( - String qualifiedName, - ElementDescriptor elementDescriptor, - Context context, - String idAttribute, - String idValue ) - throws - IOException, - SAXException, - IntrospectionException { - writeElement( - "", - qualifiedName, - qualifiedName, - elementDescriptor, - context, - idAttribute, - idValue ); + writeElementContent( elementDescriptor, context ) ; + if ( elementDescriptor.isWrapCollectionsInElement() ) { + endElement( namespaceUri, localName, qualifiedName ); + } + } } /** @@ -716,44 +580,24 @@ if ( !ignoreElement( elementDescriptor, context ) ) { - expressElementStart( namespaceUri, localName, qualifiedName ); - - // XXX For the moment, assign ID attribute to default namespace - expressAttribute( "", idAttribute, idAttribute, idValue ); - - writeRestOfElement( - namespaceUri, - localName, - qualifiedName, - elementDescriptor, - context ); + startElement( + namespaceUri, + localName, + qualifiedName, + new ElementAttributes( + elementDescriptor, + context, + idAttribute, + idValue )); + + writeElementContent( elementDescriptor, context ) ; + endElement( namespaceUri, localName, qualifiedName ); } else if ( log.isTraceEnabled() ) { log.trace( "Element " + qualifiedName + " is empty." ); } } - /** - * Write attributes, child elements and element end - * - * @param qualifiedName qualified name to use for the element - * @param elementDescriptor the ElementDescriptor describing the element - * @param context the Context to use to evaluate the bean expressions - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - * @throws IntrospectionException if a java beans introspection problem occurs - * @deprecated refactored away - */ - protected void writeRestOfElement( - String qualifiedName, - ElementDescriptor elementDescriptor, - Context context ) - throws - IOException, - SAXException, - IntrospectionException { - writeRestOfElement( "", qualifiedName, qualifiedName, elementDescriptor, context ); - } /** * Write attributes, child elements and element end @@ -782,44 +626,11 @@ writeAttributes( elementDescriptor, context ); } - if ( writeContent( elementDescriptor, context ) ) { - if ( elementDescriptor.isWrapCollectionsInElement() ) { - expressElementEnd( uri, localName, qualifiedName ); - } - } else { - if ( elementDescriptor.isWrapCollectionsInElement() ) { - expressElementEnd(); - } + writeElementContent( elementDescriptor, context ); + if ( elementDescriptor.isWrapCollectionsInElement() ) { + endElement( uri, localName, qualifiedName ); } } - - - /** - * Writes an element with a IDREF attribute - * - * @param qualifiedName of the element with IDREF attribute - * @param idrefAttributeName the qualified name of the IDREF attribute - * @param idrefAttributeValue the value for the IDREF attribute - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - * @throws IntrospectionException if a java beans introspection problem occurs - * @deprecated refactored away - */ - protected void writeIDREFElement( - String qualifiedName, - String idrefAttributeName, - String idrefAttributeValue ) - throws - IOException, - SAXException, - IntrospectionException { - writeIDREFElement( - "", - qualifiedName, - qualifiedName, - idrefAttributeName, - idrefAttributeValue ); - } /** * Writes an element with a IDREF attribute @@ -844,34 +655,40 @@ SAXException, IntrospectionException { - // write IDREF element - expressElementStart( uri, localName, qualifiedName ); - // XXX for the moment, assgin IDREF to default namespace - expressAttribute( "", idrefAttributeName, idrefAttributeName, idrefAttributeValue ); - - expressElementEnd(); - } + // write IDREF element + AttributesImpl attributes = new AttributesImpl(); + // XXX for the moment, assign IDREF to default namespace + attributes.addAttribute( + "", + idrefAttributeName, + idrefAttributeName, + "IDREF", + idrefAttributeValue); + startElement( uri, localName, qualifiedName, attributes); + endElement( uri, localName, qualifiedName ); + } + /** * Writes the element content. * * @param elementDescriptor the ElementDescriptor to write as xml * @param context the Context to use to evaluate the bean expressions - * @return true if some content was written + * * @throws IOException if an IO problem occurs during writing * @throws SAXException if an SAX problem occurs during writing * @throws IntrospectionException if a java beans introspection problem occurs */ - protected boolean writeContent( + private void writeElementContent( ElementDescriptor elementDescriptor, Context context ) throws IOException, SAXException, - IntrospectionException { + IntrospectionException { + ElementDescriptor[] childDescriptors = elementDescriptor.getElementDescriptors(); - boolean writtenContent = false; if ( childDescriptors != null && childDescriptors.length > 0 ) { // process child elements for ( int i = 0, size = childDescriptors.length; i < size; i++ ) { @@ -891,51 +708,21 @@ if (object == null) { continue; } - if ( ! writtenContent ) { - writtenContent = true; - if (elementDescriptor.isWrapCollectionsInElement()) { - expressTagClose(); - } - } - ++indentLevel; writeBean( namespaceUri, localName, qualifiedName, object ); - --indentLevel; } } else { - if ( ! writtenContent ) { - writtenContent = true; - expressTagClose(); - } - ++indentLevel; writeBean( namespaceUri, localName, qualifiedName, childBean ); - --indentLevel; } } } else { - if ( ! writtenContent ) { - writtenContent = true; - expressTagClose(); - } - if (childDescriptor.isWrapCollectionsInElement()) { - ++indentLevel; - } - - writeElement( + writeElement( childDescriptor.getURI(), childDescriptor.getLocalName(), childDescriptor.getQualifiedName(), childDescriptor, childContext ); - - if (childDescriptor.isWrapCollectionsInElement()) { - --indentLevel; - } } } - if ( writtenContent ) { - writePrintln(); - writeIndent(); - } } else { // evaluate the body text Expression expression = elementDescriptor.getTextExpression(); @@ -944,96 +731,18 @@ if ( value != null ) { String text = value.toString(); if ( text != null && text.length() > 0 ) { - if ( ! writtenContent ) { - writtenContent = true; - expressTagClose(); - } - expressBodyText(text); + bodyText(text); } } } } - return writtenContent; - } - - /** - * Writes the attribute declarations - * - * @param elementDescriptor the ElementDescriptor to be written out as xml - * @param context the Context to use to evaluation bean expressions - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void writeAttributes( - ElementDescriptor elementDescriptor, - Context context ) - throws - IOException, SAXException { - if (!elementDescriptor.isWrapCollectionsInElement()) { - return; - } - - AttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributeDescriptors(); - if ( attributeDescriptors != null ) { - for ( int i = 0, size = attributeDescriptors.length; i < size; i++ ) { - AttributeDescriptor attributeDescriptor = attributeDescriptors[i]; - writeAttribute( attributeDescriptor, context ); - } - } } - - /** - * Writes an attribute declaration - * - * @param attributeDescriptor the AttributeDescriptor to be written as xml - * @param context the Context to use to evaluation bean expressions - * @throws IOException if an IO problem occurs during writing - * @throws SAXException if an SAX problem occurs during writing - */ - protected void writeAttribute( - AttributeDescriptor attributeDescriptor, - Context context ) - throws - IOException, SAXException { - Expression expression = attributeDescriptor.getTextExpression(); - if ( expression != null ) { - Object value = expression.evaluate( context ); - if ( value != null ) { - String text = value.toString(); - if ( text != null && text.length() > 0 ) { - expressAttribute( - attributeDescriptor.getURI(), - attributeDescriptor.getLocalName(), - attributeDescriptor.getQualifiedName(), - text); - } - } - } - } - - /** - * Writes a empty line. - * This implementation does nothing but can be overridden by subclasses. - * - * @throws IOException if the line cannot be written - */ - protected void writePrintln() throws IOException {} - - /** - * Writes an indentation. - * This implementation does nothing but can be overridden by subclasses. - * - * @throws IOException if the indent cannot be written - */ - protected void writeIndent() throws IOException {} - /** * Pushes the bean onto the ancestry stack. * If IDs are not being written, then check for cyclic references. * * @param bean push this bean onto the ancester stack - * @throws CyclicReferenceException if there is an identical bean already on the stack */ protected void pushBean( Object bean ) { // check that we don't have a cyclic reference when we're not writing IDs @@ -1138,4 +847,593 @@ return true; } + + + + + /** + * Attributes backed by attribute descriptors + */ + private class ElementAttributes implements Attributes { + /** Attribute descriptors backing the Attributes */ + private AttributeDescriptor[] attributes; + /** Context to be evaluated when finding values */ + private Context context; + /** ID attribute value */ + private String idValue; + /** ID attribute name */ + private String idAttributeName; + + + /** + * Construct attributes for element and context. + * + * @param descriptor the ElementDescriptor describing the element + * @param context evaluate against this context + */ + ElementAttributes( ElementDescriptor descriptor, Context context ) { + attributes = descriptor.getAttributeDescriptors(); + this.context = context; + } + + /** + * Construct attributes for element and context. + * + * @param descriptor the ElementDescriptor describing the element + * @param context evaluate against this context + * @param idAttributeName the name of the id attribute + * @param idValue the ID attribute value + */ + ElementAttributes( + ElementDescriptor descriptor, + Context context, + String idAttributeName, + String idValue) { + attributes = descriptor.getAttributeDescriptors(); + this.context = context; + this.idValue = idValue; + this.idAttributeName = idAttributeName; + } + + /** + * Gets the index of an attribute by qualified name. + * + * @param qName the qualified name of the attribute + * @return the index of the attribute - or -1 if there is no matching attribute + */ + public int getIndex( String qName ) { + for ( int i=0; i= 0 && index < attributes.length ); + } + } + + + // OLD API (DEPRECATED) + // -------------------------------------------------------------------------------------- + + + /** + * Get the indentation for the current element. + * Used for pretty priting. + * + * @return the amount that the current element is indented + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected int getIndentLevel() { + return 0; + } + + // Expression methods + //------------------------------------------------------------------------- + + /** + * Express an element tag start using given qualified name. + * + * @param qualifiedName the qualified name of the element to be expressed + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressElementStart(String qualifiedName) + throws IOException, SAXException { + // do nothing + } + + /** + * Express an element tag start using given qualified name. + * + * @param uri the namespace uri + * @param localName the local name for this element + * @param qualifiedName the qualified name of the element to be expressed + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressElementStart(String uri, String localName, String qualifiedName) + throws IOException, SAXException { + expressElementStart( qualifiedName ); + } + + /** + * Express a closing tag. + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressTagClose() throws IOException, SAXException {} + + /** + * Express an element end tag (with given name) + * + * @param qualifiedName the qualified name for the element to be closed + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressElementEnd(String qualifiedName) + throws IOException, SAXException { + // do nothing + } + + /** + * Express an element end tag (with given name) + * + * @param uri the namespace uri of the element close tag + * @param localName the local name of the element close tag + * @param qualifiedName the qualified name for the element to be closed + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressElementEnd( + String uri, + String localName, + String qualifiedName) + throws + IOException, + SAXException { + expressElementEnd(qualifiedName); + } + + + /** + * Express an empty element end. + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressElementEnd() throws IOException, SAXException {} + + /** + * Express body text + * + * @param text the string to write out as the body of the current element + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressBodyText(String text) throws IOException, SAXException {} + + /** + * Express an attribute + * + * @param qualifiedName the qualified name of the attribute + * @param value the attribute value + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressAttribute( + String qualifiedName, + String value) + throws + IOException, + SAXException { + // Do nothing + } + + /** + * Express an attribute + * + * @param namespaceUri the namespace uri + * @param localName the local name + * @param qualifiedName the qualified name of the attribute + * @param value the attribute value + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void expressAttribute( + String namespaceUri, + String localName, + String qualifiedName, + String value) + throws + IOException, + SAXException { + expressAttribute(qualifiedName, value); + } + + + /** + * Writes the given element + * + * @param qualifiedName qualified name to use for the element + * @param elementDescriptor the ElementDescriptor describing the element + * @param context the Context to use to evaluate the bean expressions + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @throws IntrospectionException if a java beans introspection problem occurs + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void write( + String qualifiedName, + ElementDescriptor elementDescriptor, + Context context ) + throws + IOException, + SAXException, + IntrospectionException { + writeElement( "", qualifiedName, qualifiedName, elementDescriptor, context ); + } + + /** + * Writes the given element adding an ID attribute + * + * @param qualifiedName qualified name to use for the element + * @param elementDescriptor the ElementDescriptor describing the element + * @param context the Context to use to evaluate the bean expressions + * @param idAttribute the qualified name of the ID attribute + * @param idValue the value for the ID attribute + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @throws IntrospectionException if a java beans introspection problem occurs + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void write( + String qualifiedName, + ElementDescriptor elementDescriptor, + Context context, + String idAttribute, + String idValue ) + throws + IOException, + SAXException, + IntrospectionException { + writeElement( + "", + qualifiedName, + qualifiedName, + elementDescriptor, + context, + idAttribute, + idValue ); + } + + /** + * Write attributes, child elements and element end + * + * @param qualifiedName qualified name to use for the element + * @param elementDescriptor the ElementDescriptor describing the element + * @param context the Context to use to evaluate the bean expressions + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @throws IntrospectionException if a java beans introspection problem occurs + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writeRestOfElement( + String qualifiedName, + ElementDescriptor elementDescriptor, + Context context ) + throws + IOException, + SAXException, + IntrospectionException { + writeRestOfElement( "", qualifiedName, qualifiedName, elementDescriptor, context ); + } + + /** + * Writes an element with a IDREF attribute + * + * @param qualifiedName of the element with IDREF attribute + * @param idrefAttributeName the qualified name of the IDREF attribute + * @param idrefAttributeValue the value for the IDREF attribute + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @throws IntrospectionException if a java beans introspection problem occurs + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writeIDREFElement( + String qualifiedName, + String idrefAttributeName, + String idrefAttributeValue ) + throws + IOException, + SAXException, + IntrospectionException { + writeIDREFElement( + "", + qualifiedName, + qualifiedName, + idrefAttributeName, + idrefAttributeValue ); + } + + + /** + * Writes the element content. + * + * @param elementDescriptor the ElementDescriptor to write as xml + * @param context the Context to use to evaluate the bean expressions + * @return true if some content was written + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @throws IntrospectionException if a java beans introspection problem occurs + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected boolean writeContent( + ElementDescriptor elementDescriptor, + Context context ) + throws + IOException, + SAXException, + IntrospectionException { + return false; + } + + + /** + * Writes the attribute declarations + * + * @param elementDescriptor the ElementDescriptor to be written out as xml + * @param context the Context to use to evaluation bean expressions + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writeAttributes( + ElementDescriptor elementDescriptor, + Context context ) + throws + IOException, SAXException { + if (!elementDescriptor.isWrapCollectionsInElement()) { + return; + } + + AttributeDescriptor[] attributeDescriptors = elementDescriptor.getAttributeDescriptors(); + if ( attributeDescriptors != null ) { + for ( int i = 0, size = attributeDescriptors.length; i < size; i++ ) { + AttributeDescriptor attributeDescriptor = attributeDescriptors[i]; + writeAttribute( attributeDescriptor, context ); + } + } + } + + + /** + * Writes an attribute declaration + * + * @param attributeDescriptor the AttributeDescriptor to be written as xml + * @param context the Context to use to evaluation bean expressions + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writeAttribute( + AttributeDescriptor attributeDescriptor, + Context context ) + throws + IOException, SAXException { + Expression expression = attributeDescriptor.getTextExpression(); + if ( expression != null ) { + Object value = expression.evaluate( context ); + if ( value != null ) { + String text = value.toString(); + if ( text != null && text.length() > 0 ) { + expressAttribute( + attributeDescriptor.getURI(), + attributeDescriptor.getLocalName(), + attributeDescriptor.getQualifiedName(), + text); + } + } + } + } + /** + * Writes a empty line. + * This implementation does nothing but can be overridden by subclasses. + * + * @throws IOException if the line cannot be written + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writePrintln() throws IOException {} + + /** + * Writes an indentation. + * This implementation does nothing but can be overridden by subclasses. + * + * @throws IOException if the indent cannot be written + * @deprecated after 1.0-Alpha-1 replaced by new BeanWriter API + */ + protected void writeIndent() throws IOException {} } 1.15 +193 -57 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java Index: BeanWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanWriter.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- BeanWriter.java 19 Jan 2003 23:22:47 -0000 1.14 +++ BeanWriter.java 27 Feb 2003 19:20:17 -0000 1.15 @@ -71,6 +71,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.xml.sax.SAXException; +import org.xml.sax.Attributes; import org.apache.commons.betwixt.XMLUtils; @@ -136,6 +137,12 @@ private boolean autoFlush; /** Log used for logging (Doh!) */ private Log log = LogFactory.getLog( BeanWriter.class ); + /** Has any content (excluding attributes) been written to the current element */ + private boolean currentElementIsEmpty = false; + /** Has the last start tag been closed */ + private boolean closedStartTag = true; + /** Current level of indentation (starts at 1 with the first element) */ + private int indentLevel; /** *

Constructor uses System.out for output.

@@ -174,7 +181,7 @@ */ public void writeXmlDeclaration(String xmlDeclaration) throws IOException { writer.write( xmlDeclaration ); - writePrintln(); + printLine(); } /** @@ -284,15 +291,193 @@ this.log = log; } + // New API + //------------------------------------------------------------------------------ + + + /** + * Writes the start tag for an element. + * + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qualifiedName the element's qualified name + * @param attr the element's attributes + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha 1 + */ + protected void startElement( + String uri, + String localName, + String qualifiedName, + Attributes attr) + throws + IOException, + SAXException { + if ( !closedStartTag ) { + writer.write( '>' ); + printLine(); + } + + indentLevel++; - // Expression methods - //------------------------------------------------------------------------- + indent(); + writer.write( '<' ); + writer.write( qualifiedName ); + + for ( int i=0; i< attr.getLength(); i++ ) { + writer.write( ' ' ); + writer.write( attr.getQName(i) ); + writer.write( "=\"" ); + writer.write( XMLUtils.escapeAttributeValue( attr.getValue(i) ) ); + writer.write( '\"' ); + } + closedStartTag = false; + currentElementIsEmpty = true; + } + + /** + * Writes the end tag for an element + * + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qualifiedName the element's qualified name + * + * @throws IOException if an IO problem occurs during writing + * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha 1 + */ + protected void endElement( + String uri, + String localName, + String qualifiedName) + throws + IOException, + SAXException { + if ( ( !closedStartTag ) && currentElementIsEmpty ) { + + writer.write( "/>" ); + closedStartTag = true; + + } else { + + writer.write( "' ); + + } + + indentLevel--; + printLine(); + } + + /** + * Write element body text + * + * @param text write out this body text + * @throws IOException when the stream write fails + * @since 1.0 Alpha 1 + */ + protected void bodyText(String text) throws IOException { + if ( text == null ) { + // XXX This is probably a programming error + log.error( "[expressBodyText]Body text is null" ); + + } else { + if ( !closedStartTag ) { + writer.write( '>' ); + closedStartTag = true; + } + writer.write( XMLUtils.escapeBodyValue(text) ); + currentElementIsEmpty = false; + } + } + + /** Writes out an empty line. + * Uses current endOfLine. + * + * @throws IOException when stream write fails + */ + private void printLine() throws IOException { + if ( endOfLine != null ) { + writer.write( endOfLine ); + } + } + + /** + * Writes out indent's to the current indentLevel + * + * @throws IOException when stream write fails + */ + private void indent() throws IOException { + if ( indent != null ) { + for ( int i = 0; i < indentLevel; i++ ) { + writer.write( getIndent() ); + } + } + } + + // OLD API (DEPRECATED) + //---------------------------------------------------------------------------- + + + /** Writes out an empty line. + * Uses current endOfLine. + * + * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writePrintln() throws IOException { + if ( endOfLine != null ) { + writer.write( endOfLine ); + } + } + + /** + * Writes out indent's to the current indentLevel + * + * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API + */ + protected void writeIndent() throws IOException { + if ( indent != null ) { + for ( int i = 0; i < indentLevel; i++ ) { + writer.write( getIndent() ); + } + } + } + + /** + *

Escape the toString of the given object. + * For use as body text.

+ * + * @param value escape value.toString() + * @return text with escaped delimiters + * @deprecated After 1.0-Alpha-1 moved into utility class {@link XMLUtils#escapeBodyValue} + */ + protected String escapeBodyValue(Object value) { + return XMLUtils.escapeBodyValue(value); + } + + /** + *

Escape the toString of the given object. + * For use in an attribute value.

+ * + * @param value escape value.toString() + * @return text with characters restricted (for use in attributes) escaped + * + * @deprecated After 1.0-Alpha-1 moved into utility class {@link XMLUtils#escapeAttributeValue} + */ + protected String escapeAttributeValue(Object value) { + return XMLUtils.escapeAttributeValue(value); + } /** * Express an element tag start using given qualified name * * @param qualifiedName the fully qualified name of the element to write * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressElementStart(String qualifiedName) throws IOException { if ( qualifiedName == null ) { @@ -311,6 +496,7 @@ * Write a tag close to the stream * * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressTagClose() throws IOException { writer.write( '>' ); @@ -321,6 +507,7 @@ * * @param qualifiedName the name of the element * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressElementEnd(String qualifiedName) throws IOException { if (qualifiedName == null) { @@ -338,6 +525,7 @@ * Write an empty element end to the stream * * @throws IOException when stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressElementEnd() throws IOException { writer.write( "/>" ); @@ -348,6 +536,7 @@ * * @param text write out this body text * @throws IOException when the stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressBodyText(String text) throws IOException { if ( text == null ) { @@ -365,6 +554,7 @@ * @param qualifiedName fully qualified attribute name * @param value attribute value * @throws IOException when the stream write fails + * @deprecated after 1.0-Alpha-1 replaced by new SAX inspired API */ protected void expressAttribute( String qualifiedName, @@ -389,58 +579,4 @@ writer.write( XMLUtils.escapeAttributeValue(value) ); writer.write( '\"' ); } - - - // Implementation methods - //------------------------------------------------------------------------- - - /** Writes out an empty line. - * Uses current endOfLine. - * - * @throws IOException when stream write fails - */ - protected void writePrintln() throws IOException { - if ( endOfLine != null ) { - writer.write( endOfLine ); - } - } - - /** - * Writes out indent's to the current indentLevel - * - * @throws IOException when stream write fails - */ - protected void writeIndent() throws IOException { - if ( indent != null ) { - for ( int i = 0; i < getIndentLevel(); i++ ) { - writer.write( getIndent() ); - } - } - } - - /** - *

Escape the toString of the given object. - * For use as body text.

- * - * @param value escape value.toString() - * @return text with escaped delimiters - * @deprecated After 1.0-Alpha-1 moved into utility class {@link XMLUtils#escapeBodyValue} - */ - protected String escapeBodyValue(Object value) { - return XMLUtils.escapeBodyValue(value); - } - - /** - *

Escape the toString of the given object. - * For use in an attribute value.

- * - * @param value escape value.toString() - * @return text with characters restricted (for use in attributes) escaped - * - * @deprecated After 1.0-Alpha-1 moved into utility class {@link XMLUtils#escapeAttributeValue} - */ - protected String escapeAttributeValue(Object value) { - return XMLUtils.escapeAttributeValue(value); - } - } 1.10 +50 -198 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/SAXBeanWriter.java Index: SAXBeanWriter.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/SAXBeanWriter.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- SAXBeanWriter.java 22 Feb 2003 16:25:25 -0000 1.9 +++ SAXBeanWriter.java 27 Feb 2003 19:20:17 -0000 1.10 @@ -61,13 +61,11 @@ */ package org.apache.commons.betwixt.io; -import java.util.Stack; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.Attributes; // FIX ME // At the moment, namespaces are NOT supported! @@ -85,15 +83,6 @@ private ContentHandler contentHandler; /** Log used for logging (Doh!) */ private Log log = LogFactory.getLog( SAXBeanWriter.class ); - - /** - * Place holder for elements that are started. - */ - private Stack elementStack; - /** Current element's attributes. */ - private AttributesImpl attributes; - /** Is there a element currently waiting to be written out? */ - private boolean elementWaiting = false; /** Should document events (ie. start and end) be called? */ private boolean callDocumentEvents = true; @@ -144,155 +133,74 @@ // Expression methods - //------------------------------------------------------------------------- + //------------------------------------------------------------------------- - /** - * Express an element tag start using given qualified name - * - * @param qualifiedName the fully qualified element name - * @throws SAXException if the ContentHandler has a problem - * @deprecated use {@link #expressElementStart(String, String, String)} - */ - protected void expressElementStart(String qualifiedName) throws SAXException { - expressElementStart("", qualifiedName, qualifiedName); - } - - /** - * Express an element tag start using given qualified name. + // Replaced by new API + + // New API + // ------------------------------------------------------------------------- + + + /** + * Writes the start tag for an element. * - * @param uri the namespace uri - * @param localName the local name for this element - * @param qualifiedName the qualified name of the element to be expressed + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qName the element's qualified name + * @param attributes the element's attributes * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha 1 */ - protected void expressElementStart(String uri, String localName, String qualifiedName) - throws SAXException { - if (elementStack == null) { - elementStack = new Stack(); - } - if (elementWaiting) { - sendElementStart(); - } - attributes = new AttributesImpl(); - elementStack.push(new ElementName(uri, localName, qualifiedName)); - elementWaiting = true; - } - - /** Element end */ - protected void expressTagClose() { - // using this could probably make life easier - // but i only know that i needed it after i'd written the rest + protected void startElement( + String uri, + String localName, + String qName, + Attributes attributes) + throws + SAXException { + contentHandler.startElement( + uri, + localName, + qName, + attributes); } - /** - * Express an element end tag - * - * @param qualifiedName the fully qualified name of the element - * @throws SAXException if the ContentHandler has a problem - * @deprecated use {@link #expressElementEnd(String, String, String)} - */ - protected void expressElementEnd(String qualifiedName) throws SAXException { - expressElementEnd("", qualifiedName, qualifiedName); - } - - /** - * Express an element tag start using given qualified name. + /** + * Writes the end tag for an element * - * @param uri the namespace uri - * @param localName the local name for this element - * @param qualifiedName the qualified name of the element to be expressed + * @param uri the element's namespace uri + * @param localName the element's local name + * @param qName the element's qualified name * @throws SAXException if an SAX problem occurs during writing + * @since 1.0 Alpha 1 */ - protected void expressElementEnd(String uri, String localName, String qualifiedName) - throws SAXException { - if (elementWaiting) { - elementWaiting = false; - sendElementStart(); - } - - contentHandler.endElement(uri, localName, qualifiedName); - } - - /** - * Express an empty element end - * @throws SAXException if the ContentHandler has a problem - */ - protected void expressElementEnd() throws SAXException { - // last element name must be correct since there haven't been any tag in between - ElementName lastElement = (ElementName) elementStack.peek(); + protected void endElement( + String uri, + String localName, + String qName) + throws + SAXException { contentHandler.endElement( - lastElement.getUri(), - lastElement.getLocalName() , - lastElement.getQName()); + uri, + localName, + qName); } /** * Express body text * @param text the element body text * @throws SAXException if the ContentHandler has a problem + * @since 1.0 Alpha 1 */ - protected void expressBodyText(String text) throws SAXException { + protected void bodyText(String text) throws SAXException { // FIX ME // CHECK UNICODE->CHAR CONVERSION! // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN - if (elementWaiting) { - elementWaiting = false; - sendElementStart(); - } char[] body = text.toCharArray(); contentHandler.characters(body, 0, body.length); - } - - /** - * Express an attribute - * @param qualifiedName the fully qualified attribute name - * @param value the attribute value - * @deprecated use {@link #expressAttribute(String, String, String, String)} - */ - protected void expressAttribute( - String qualifiedName, - String value) { - expressAttribute("", qualifiedName, qualifiedName, value); - } - - /** - * Express an attribute - * - * @param namespaceUri the namespace for the attribute - * @param localName the local name for the attribute - * @param qualifiedName the qualified name of the attribute - * @param value the attribute value - */ - protected void expressAttribute( - String namespaceUri, - String localName, - String qualifiedName, - String value) { - // FIX ME - // SHOULD PROBABLY SUPPORT ID IDREF HERE - attributes.addAttribute(namespaceUri, localName, qualifiedName, "CDATA", value); - } - - - // Implementation methods - //------------------------------------------------------------------------- + } /** - * Send the start element event to the ContentHandler - * @throws SAXException if the ContentHandler has a problem - */ - private void sendElementStart() throws SAXException { - ElementName lastElement = (ElementName) elementStack.peek(); - if (log.isTraceEnabled()) { - log.trace(lastElement); - } - contentHandler.startElement( - lastElement.getUri(), - lastElement.getLocalName(), - lastElement.getQName(), - attributes); - } - /** * This will announce the start of the document * to the contenthandler. * @@ -315,60 +223,4 @@ contentHandler.endDocument(); } } - - /** Used to store element names stored on the stack */ - private class ElementName { - /** Namespace uri */ - private String uri; - /** Local name */ - private String localName; - /** Qualified name */ - private String qName; - - /** - * Gets namespace uri - * @return the namespace uri - */ - String getUri() { - return uri; - } - - /** - * Gets local name - * @return the local name - */ - String getLocalName() { - return localName; - } - - /** - * Gets qualified name - * @return the qualified name - */ - String getQName() { - return qName; - } - - /** - * Base constructor - * @param uri the namespace uri - * @param localName the local name of this element - * @param qName the qualified name of this element - */ - ElementName(String uri, String localName, String qName) { - this.uri = uri; - this.localName = localName; - this.qName = qName; - } - - /** - * Return something useful for logging - * - * @return something useful for logging - */ - public String toString() { - return "[ElementName uri=" + uri + " ,lName=" + localName + " ,qName=" + qName + "]"; - } - } - } 1.9 +15 -0 jakarta-commons/betwixt/xdocs/tasks.xml Index: tasks.xml =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/xdocs/tasks.xml,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- tasks.xml 17 Feb 2003 19:41:57 -0000 1.8 +++ tasks.xml 27 Feb 2003 19:20:18 -0000 1.9 @@ -221,9 +221,18 @@ AbstractBeanWriter.writeIDREFElement( String, String, String) refactored away
  • + AbstractBeanWriter.writeAttributes() refactored away +
  • +
  • + AbstractBeanWriter.writeAttribute() refactored away +
  • +
  • AbstractBeanWriter.writeRestOfElement refactored away
  • + AbstractBeanWriter.getIndentLevel moved into BeanWriter +
  • +
  • SAXBeanWriter.expressElementEnd(String qualifiedName) deprecated
  • @@ -257,6 +266,12 @@ The public interface has been preserved but the protected API has been extensively modified. In order to preserve backwards compatibility, methods have been deprecated but are no longer called and so code that overrides then will break. +
  • +
  • + Cleaner API for SAXBeanWriter + The AbstractBeanWriter refactoring means that SAXBeanWriter now has a much cleaner + internal API. If anyone out there has been doing funky stuff by extending SAXBeanWriter + then i'm afraid that you'll need to rewrite.
  • --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org