Skip to content

Commit

Permalink
Fixes #54 - Outputers and Format
Browse files Browse the repository at this point in the history
Completes the code for SAXOutputter and Format, thus completing the issue.
Implements tests, but some tests are ignored because of two issues:
1. Cannot affect format of content outside of root element using Format
2. Cannot process DOCTYPE declarations properly.
  • Loading branch information
rolfl committed Jan 2, 2012
1 parent 24e6161 commit b1f779d
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 41 deletions.
2 changes: 1 addition & 1 deletion core/src/java/org/jdom2/input/sax/SAXHandler.java
Expand Up @@ -722,7 +722,7 @@ private void transferNamespaces(Element element) {
public void characters(char[] ch, int start, int length)
throws SAXException {

if (suppress || (length == 0))
if (suppress || (length == 0 && !inCDATA))
return;

if (previousCDATA != inCDATA) {
Expand Down
Expand Up @@ -295,8 +295,11 @@ protected void printDocument(final SAXTarget out, final FormatStack fstack,
// Handle root element, as well as any root level
// processing instructions and comments
// ignore DocType, if any.
for (Content obj : document.getContent()) {

int sz = document.getContentSize();
for (int c = 0; c < sz; c++) {

Content obj = document.getContent(c);
// update locator
out.getLocator().setNode(obj);

Expand Down Expand Up @@ -762,7 +765,7 @@ protected void printContent(final SAXTarget out, final FormatStack fstack,
// 'trimming' variants, if it is all whitespace we do
// not want to even print the indent/newline.
if (!isAllWhiteSpace(content, txti, index - txti)) {
printIndent(null, fstack);
printIndent(out, fstack);
helperTextType(out, fstack, content, txti, index - txti);
printEOL(out, fstack);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/java/org/jdom2/output/SAXOutputter.java
Expand Up @@ -600,7 +600,7 @@ public SAXOutputProcessor getSAXOutputProcessor() {
* the new SAXOutputProcessor
*/
public void setSAXOutputProcessor(SAXOutputProcessor processor) {
this.processor = processor;
this.processor = processor == null ? DEFAULT_PROCESSOR : processor;
}

/**
Expand All @@ -619,7 +619,7 @@ public Format getFormat() {
* the new Format
*/
public void setFormat(Format format) {
this.format = format;
this.format = format == null ? Format.getRawFormat() : format;
}

private final SAXTarget buildTarget(Document doc) {
Expand Down
3 changes: 2 additions & 1 deletion test/src/java/org/jdom2/test/cases/input/TestSAXBuilder.java
Expand Up @@ -97,6 +97,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.jdom2.Content;
import org.jdom2.DefaultJDOMFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.EntityRef;
import org.jdom2.JDOMException;
import org.jdom2.JDOMFactory;
Expand Down Expand Up @@ -836,7 +837,7 @@ private void assertXMLMatches(String baseuri, Document doc) {
}
}
}

@Test
public void testBuildInputSource() {
try {
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.jdom2.Parent;
import org.jdom2.ProcessingInstruction;
import org.jdom2.Text;
import org.jdom2.Verifier;
import org.jdom2.output.Format;
import org.jdom2.output.Format.TextMode;

Expand All @@ -35,14 +36,14 @@ protected static interface FormatSetup {
private final boolean pademptyelement;
private final boolean forceexpand;
private final boolean padpi;
private final boolean forceplatformeol;
//private final boolean rawoutsideroot;

public AbstractTestOutputter(boolean cr2xD, boolean padpreempty, boolean padpi, boolean forceexpand, boolean forceplatformeol) {
public AbstractTestOutputter(boolean cr2xD, boolean padpreempty, boolean padpi, boolean forceexpand, boolean rawoutsideroot) {
this.cr2xD = cr2xD;
this.pademptyelement = padpreempty;
this.forceexpand = forceexpand;
this.padpi = padpi;
this.forceplatformeol = forceplatformeol;
//this.rawoutsideroot = rawoutsideroot;
}

protected final String expect(String expect) {
Expand All @@ -66,9 +67,69 @@ protected final String expect(String expect) {
expect = expect.replaceAll("<(\\w+)(\\s+.+?)?\\s+/>", "<$1$2/>");
expect = expect.replaceAll("<(\\w+:\\w+)(\\s+.+?)?\\s+/>", "<$1$2/>");
}
if (forceplatformeol) {
//expect = expect.replaceAll("\n", System.getProperty("line.separator"));
}
// if (rawoutsideroot) {
// // outside the root element will be raw-formatted.
// StringBuilder sb = new StringBuilder(expect.length());
// int gotstuff = 0;
// boolean indoctype = false;
// boolean gotroot = false;
// int depth = 0;
// char[] chars = expect.toCharArray();
// int i = 0;
// while (i < chars.length && Verifier.isXMLWhitespace(chars[i])) {
// // skip initial whitespace.
// i++;
// }
// for (; i < chars.length; i++) {
// char c = chars[i];
// sb.append(c);
// if (!gotroot) {
// if (c == '<') {
// if (depth == 0) {
// if (i < chars.length - 2) {
// if (chars[i + 1] == '?') {
// // PI or XML Declaration
// gotstuff++;
// } else if (chars[i + 1] == '!') {
// // Comment of DOCTYPE
// gotstuff++;
// if (chars[i + 2] == 'D') {
// // DOCTYPE
// indoctype = true;
// }
// } else {
// // root element
// gotroot = true;
// }
// } else {
// gotroot = true;
// }
// }
// depth++;
// } else if (c == '>') {
// depth--;
// if (depth == 0) {
// if (indoctype) {
// sb.append('\n');
// indoctype = false;
// }
// while (i+1 < chars.length && Verifier.isXMLWhitespace(chars[i + 1])) {
// // skip whitespace after top-level content.
// i++;
// }
// }
// }
// }
// }
// while (Verifier.isXMLWhitespace(sb.charAt(sb.length() - 1))) {
// // eliminate trailing whitespace.
// sb.setLength(sb.length() - 1);
// }
// if (gotstuff > 1 || (gotroot && gotstuff > 0)) {
// // there is multiple content stuff, need to trim the whitespace....
// expect = sb.toString();
// }
// }
return expect;
}

Expand Down Expand Up @@ -380,14 +441,14 @@ public void testDocTypeSimple() {
@Test
public void testDocTypeSimpleISS() {
DocType content = new DocType("root");
content.setInternalSubset("internal");
assertEquals("<!DOCTYPE root [\ninternal]>",
content.setInternalSubset("<!ENTITY name \"value\">");
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
outputString(fraw, content));
assertEquals("<!DOCTYPE root [\ninternal]>",
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
outputString(fcompact, content));
assertEquals("<!DOCTYPE root [\ninternal]>",
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
outputString(fpretty, content));
assertEquals("<!DOCTYPE root [\ninternal]>",
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
outputString(ftfw, content));
}

Expand Down Expand Up @@ -986,6 +1047,10 @@ public void testOutputDocumentFull() {

@Test
public void testDeepNesting() {
testDeepNestingCore(true);
}

protected final void testDeepNestingCore(boolean startnewline) {
// need to get beyond 16 levels of XML.
DocType dt = new DocType("root");
Element root = new Element("root");
Expand All @@ -1000,7 +1065,9 @@ public void testDeepNesting() {
StringBuilder raw = new StringBuilder(base);
StringBuilder pretty = new StringBuilder(base);
raw.append("<root>");
pretty.append(lf);
if (startnewline) {
pretty.append(lf);
}
pretty.append("<root>");
pretty.append(lf);
final int depth = 40;
Expand Down

0 comments on commit b1f779d

Please sign in to comment.