The problem I was having was that IE doesn't resolve entities within event attributes properly, such as onclick. Instead, it sends the raw string to the interpreter which throws an error. XMLWriter always escapes quote characters, even though attriubtes are always written with double-quotes and single-quotes don't actually need to be escaped. Thus, any string literals in my event code caused IE to mess up.
Since it's really common to use string literals in these attributes to do mouse-overs and such, the patch changes XMLWriter to only escape the quote character used to write the attribute value. In addition, it adds a field to OutputFormat to control which quote-character is used when writing attributes.
best, christian.
Index: OutputFormat.java =================================================================== RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/io/OutputFormat.java,v retrieving revision 1.9 diff -u -r1.9 OutputFormat.java --- OutputFormat.java 20 May 2002 08:14:16 -0000 1.9 +++ OutputFormat.java 26 Nov 2003 14:32:47 -0000 @@ -53,6 +53,8 @@ /** Controls when to output a line.separtor every so many tags in case of no lines and total text trimming.*/ private int newLineAfterNTags = 0; //zero means don't bother. + /** Quote character to use when writing attributes. */ + private char attributeQuoteChar = '\"'; /** Creates an <code>OutputFormat</code> with * no additional whitespace (indent or new lines) added. @@ -309,6 +311,36 @@ public void setNewLineAfterNTags(int tagCount){ newLineAfterNTags = tagCount; } + + public char getAttributeQuoteCharacter() { + + return attributeQuoteChar; + + } + + /** + * Sets the character used to quote attribute values. The specified + * character must be a valid XML attribute quote character, otherwise an + * <code>IllegalArgumentException</code> will be thrown. + * + * @param quoteChar The character to use when quoting attribute values. + * @throws IllegalArgumentException If the specified character is not a + * valid XML attribute quote character. + */ + public void setAttributeQuoteCharacter(char quoteChar) { + + if (quoteChar == '\'' || quoteChar == '"') { + + attributeQuoteChar = quoteChar; + + } else { + + throw new IllegalArgumentException( + "Invalid attribute quote character (" + quoteChar + ")"); + + } + + } /** Parses command line arguments of the form * <code>-omitEncoding -indentSize 3 -newlines -trimText</code> Index: XMLWriter.java =================================================================== RCS file: /cvsroot/dom4j/dom4j/src/java/org/dom4j/io/XMLWriter.java,v retrieving revision 1.62 diff -u -r1.62 XMLWriter.java --- XMLWriter.java 14 Apr 2003 15:41:00 -0000 1.62 +++ XMLWriter.java 26 Nov 2003 14:32:48 -0000 @@ -1150,11 +1150,13 @@ } } + char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); - writer.write("=\""); + writer.write("="); + writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); - writer.write("\""); + writer.write(quote); } } @@ -1163,11 +1165,12 @@ writer.write(attribute.getQualifiedName()); writer.write("="); - writer.write("\""); + char quote = format.getAttributeQuoteCharacter(); + writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); - writer.write("\""); + writer.write(quote); lastOutputNodeType = Node.ATTRIBUTE_NODE; } @@ -1178,11 +1181,13 @@ } protected void writeAttribute(Attributes attributes, int index) throws IOException { + char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attributes.getQName(index)); - writer.write("=\""); + writer.write("="); + writer.write(quote); writeEscapeAttributeEntities(attributes.getValue(index)); - writer.write("\""); + writer.write(quote); } @@ -1336,6 +1341,8 @@ * entity reference, suitable for XML attributes. */ protected String escapeAttributeEntities(String text) { + char quote = format.getAttributeQuoteCharacter(); + char[] block = null; int i, last = 0, size = text.length(); for ( i = 0; i < size; i++ ) { @@ -1349,9 +1356,14 @@ entity = ">"; break; case '\'' : - entity = "'"; + if (quote == '\'') { + entity = "'"; + } break; case '\"' : + if (quote == '\"') { + entity = """; + } entity = """; break; case '&' :