rdonkin 2004/06/23 14:56:59
Modified: betwixt/src/java/org/apache/commons/betwixt/io
BeanWriter.java
Added: betwixt/src/test/org/apache/commons/betwixt/io
TestBeanWriter.java
Log:
Added ability to choose to write empty tags as two tags or onee.
Revision Changes Path
1.25 +41 -2
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.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- BeanWriter.java 31 Mar 2004 21:11:52 -0000 1.24
+++ BeanWriter.java 23 Jun 2004 21:56:59 -0000 1.25
@@ -98,6 +98,8 @@
private boolean currentElementHasBodyText = false;
/** Has the last start tag been closed */
private boolean closedStartTag = true;
+ /** Should an end tag be added for empty elements? */
+ private boolean addEndTagForEmptyElement = false;
/** Current level of indentation (starts at 1 with the first element) */
private int indentLevel;
/** USed to determine how body content should be encoded before being output*/
@@ -286,6 +288,34 @@
mixedContentEncodingStrategy = strategy;
}
+ /**
+ * Should an end tag be added for each empty element?
+ * When this property is false then empty elements will
+ * be written as <code><<em>element-name</em>/gt;</code>.
+ * When this property is true then empty elements will
+ * be written as <code><<em>element-name</em>gt;
+ * </<em>element-name</em>gt;</code>.
+ * @return true if an end tag should be added
+ */
+ public boolean isEndTagForEmptyElement() {
+ return addEndTagForEmptyElement;
+ }
+
+ /**
+ * Sets when an an end tag be added for each empty element?
+ * When this property is false then empty elements will
+ * be written as <code><<em>element-name</em>/gt;</code>.
+ * When this property is true then empty elements will
+ * be written as <code><<em>element-name</em>gt;
+ * </<em>element-name</em>gt;</code>.
+ * @param addEndTagForEmptyElement true if an end tag should be
+ * written for each empty element, false otherwise
+ */
+ public void setEndTagForEmptyElement(boolean addEndTagForEmptyElement) {
+ this.addEndTagForEmptyElement = addEndTagForEmptyElement;
+ }
+
+
// New API
//------------------------------------------------------------------------------
@@ -353,7 +383,10 @@
throws
IOException,
SAXException {
- if ( ( !closedStartTag ) && currentElementIsEmpty ) {
+ if (
+ !addEndTagForEmptyElement
+ && !closedStartTag
+ && currentElementIsEmpty ) {
writer.write( "/>" );
closedStartTag = true;
@@ -361,6 +394,12 @@
} else {
if (!currentElementHasBodyText) {
indent();
+ }
+ if (
+ addEndTagForEmptyElement
+ && !closedStartTag ) {
+ writer.write( ">" );
+ closedStartTag = true;
}
writer.write( "</" );
writer.write( qualifiedName );
1.1
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/io/TestBeanWriter.java
Index: TestBeanWriter.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.betwixt.io;
import java.io.StringWriter;
import org.apache.commons.betwixt.ElementDescriptor;
import org.xml.sax.helpers.AttributesImpl;
import junit.framework.TestCase;
/**
* Test for <code>BeanWriter</code>
* @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
* @version $Revision: 1.1 $
*/
public class TestBeanWriter extends TestCase {
public void testSetEndTagForEmptyElementTrue() throws Exception
{
StringWriter out = new StringWriter();
BeanWriter writer = new BeanWriter(out);
writer.setEndTagForEmptyElement(true);
WriteContext context = new WriteContext() {
public ElementDescriptor getCurrentDescriptor() {
return null;
}
};
writer.startElement(
context,
null,
null,
"element",
new AttributesImpl());
writer.endElement(
context,
null,
null,
"element");
assertEquals("<element></element>\n", out.getBuffer().toString());
}
public void testSetEndTagForEmptyElementFalse() throws Exception
{
StringWriter out = new StringWriter();
BeanWriter writer = new BeanWriter(out);
writer.setEndTagForEmptyElement(false);
WriteContext context = new WriteContext() {
public ElementDescriptor getCurrentDescriptor() {
return null;
}
};
writer.startElement(
context,
null,
null,
"element",
new AttributesImpl());
writer.endElement(
context,
null,
null,
"element");
assertEquals("<element/>\n", out.getBuffer().toString());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]