--- src/java/org/dom4j/io/XMLWriter.java.orig Mon May 16 07:28:42 2005
+++ src/java/org/dom4j/io/XMLWriter.java Wed Feb 3 11:57:30 2010
@@ -17,6 +17,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Stack;
import java.util.StringTokenizer;
import org.dom4j.Attribute;
@@ -123,6 +124,9 @@
private char lastChar;
+
+ private Stack hadContent = new Stack();
+
private boolean autoFlush;
@@ -641,6 +645,7 @@
public void startDocument() throws SAXException {
try {
+ hadContent.clear();
writeDeclaration();
super.startDocument();
} catch (IOException e) {
@@ -677,15 +682,20 @@
String qName, Attributes attributes) throws SAXException {
try {
charsAdded = false;
+ if (hadContent.size() > 0 && !((Boolean)hadContent.peek()).booleanValue()) {
+ writer.write('>');
+ hadContent.pop();
+ hadContent.push(Boolean.TRUE);
+ }
writePrintln();
indent();
- writer.write("<");
+ writer.write('<');
writer.write(qName);
writeNamespaces();
writeAttributes(attributes);
- writer.write(">");
++indentLevel;
+ hadContent.push(Boolean.FALSE);
lastOutputNodeType = Node.ELEMENT_NODE;
lastElementClosed = false;
@@ -706,11 +716,10 @@
indent();
}
- - - boolean hadContent = true;
-
- if (hadContent) {
+ if (hadContent.isEmpty()) {
+ throw new SAXException("Empty stack; endElement without matching startElement?");
+ }
+ if (((Boolean)hadContent.pop()).booleanValue()) {
writeClose(qName);
} else {
writeEmptyElementClose(qName);
@@ -732,6 +741,12 @@
}
try {
+ if (!hadContent.isEmpty() && !((Boolean)hadContent.peek()).booleanValue()) {
+ writer.write('>');
+ hadContent.pop();
+ hadContent.push(Boolean.TRUE);
+ }
+
/*
* we can't use the writeString method here because it's possible we
* don't receive all characters at once and calling writeString
kohsuke pointed me to the org.dom4j.io.HTMLWriter class which can help with these tags.
I modified stapler's DefaultScriptInvoker.createXMLOutput() to use an HTMLWriter instead of the default XMLWriter.. however, jelly's impl.StaticTag class calls startElement and endElement directly rather than going through writeElement(Element). The former does not detect when the tag is empty and use the writeEmptyElementClose method that HTMLWriter overrides (the latter does). There is a comment in the XMLWriter source about this ("need to determine this using a stack and checking for content/children".. hadContent is hardcoded to true instead).
End result with the change I tried: The tags that should end in "/>" simply end in ">" (ie bad XHTML, but firefox seemed to display it ok).