aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Hiesserich2013-06-16 08:24:40 (EDT)
committerFlorian Thienel2013-06-16 08:41:26 (EDT)
commit950628a7a27c10d91b647bdf31266fd516982164 (patch)
tree90a2259a1d06eab78875079d80ff0811fc250000
parentb12414371e1d91d8371858f9ba49a0aa2af8806e (diff)
downloadorg.eclipse.mylyn.docs.vex-950628a7a27c10d91b647bdf31266fd516982164.zip
org.eclipse.mylyn.docs.vex-950628a7a27c10d91b647bdf31266fd516982164.tar.gz
org.eclipse.mylyn.docs.vex-950628a7a27c10d91b647bdf31266fd516982164.tar.bz2
write DocumentFragment "as-is" without wrapping
https://bugs.eclipse.org/bugs/show_bug.cgi?id=408501
-rw-r--r--org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/io/DocumentWriterTest.java28
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/io/DocumentWriter.java24
2 files changed, 44 insertions, 8 deletions
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/io/DocumentWriterTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/io/DocumentWriterTest.java
index 001360a..5c976ab 100644
--- a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/io/DocumentWriterTest.java
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/io/DocumentWriterTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2008 John Krasnay and others.
+ * Copyright (c) 2004, 2013 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,10 +7,12 @@
*
* Contributors:
* John Krasnay - initial API and implementation
+ * Carsten Hiesserich - writeNoWrap(DocumentFragment)
*******************************************************************************/
package org.eclipse.vex.core.internal.io;
import static org.eclipse.vex.core.internal.io.RoundTrip.assertDocumentsEqual;
+import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -20,10 +22,15 @@ import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
+import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.vex.core.internal.css.CssWhitespacePolicy;
import org.eclipse.vex.core.internal.css.StyleSheet;
import org.eclipse.vex.core.internal.css.StyleSheetReader;
+import org.eclipse.vex.core.internal.dom.Document;
+import org.eclipse.vex.core.provisional.dom.ContentRange;
import org.eclipse.vex.core.provisional.dom.IDocument;
+import org.eclipse.vex.core.provisional.dom.IDocumentFragment;
+import org.eclipse.vex.core.provisional.dom.IElement;
import org.eclipse.vex.core.tests.TestResources;
import org.junit.Test;
import org.xml.sax.InputSource;
@@ -56,6 +63,25 @@ public class DocumentWriterTest {
assertWriteReadCycleWorks(TestResources.get("documentWithComments.xml"));
}
+ @Test
+ public void writeDocumentFragmentNoWrap() throws Exception {
+ final Document doc = new Document(new QualifiedName(null, "root"));
+ final IElement child1 = doc.insertElement(doc.getRootElement().getEndOffset(), new QualifiedName(null, "child"));
+ final IElement child2 = doc.insertElement(doc.getRootElement().getEndOffset(), new QualifiedName(null, "child"));
+ final IElement child3 = doc.insertElement(doc.getRootElement().getEndOffset(), new QualifiedName(null, "child"));
+ doc.insertText(child1.getEndOffset(), "a b");
+ doc.insertText(child2.getEndOffset(), "c d");
+ doc.insertText(child3.getEndOffset(), "e f");
+ final IDocumentFragment fragment = doc.getFragment(new ContentRange(child1.getStartOffset(), child3.getEndOffset()));
+
+ final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ final DocumentWriter writer = new DocumentWriter();
+ writer.writeNoWrap(fragment, buffer);
+ final String writtenFragment = new String(buffer.toByteArray());
+
+ assertEquals("<child>a b</child><child>c d</child><child>e f</child>", writtenFragment);
+ }
+
private static void assertWriteReadCycleWorks(final URL documentUrl) throws IOException, ParserConfigurationException, SAXException, Exception {
final StyleSheetReader reader = new StyleSheetReader();
final StyleSheet styleSheet = reader.read(TestResources.get("test.css"));
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/io/DocumentWriter.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/io/DocumentWriter.java
index b70ead2..5dd9ab4 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/io/DocumentWriter.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/io/DocumentWriter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2008 John Krasnay and others.
+ * Copyright (c) 2004, 2013 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
* Contributors:
* John Krasnay - initial API and implementation
* Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
+ * Carsten Hiesserich - writeNoWrap(DocumentFragment) method
*******************************************************************************/
package org.eclipse.vex.core.internal.io;
@@ -155,6 +156,15 @@ public class DocumentWriter {
printWriter.flush();
}
+ public void writeNoWrap(final IDocumentFragment fragment, final OutputStream out) throws IOException {
+ final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
+
+ for (final INode child : fragment.children()) {
+ writeNodeNoWrap(child, printWriter);
+ }
+ printWriter.flush();
+ }
+
// ====================================================== PRIVATE
private void writeNode(final INode node, final PrintWriter out, final String indent) {
@@ -255,7 +265,7 @@ public class DocumentWriter {
@Override
public void visit(final IComment comment) {
out.print(indent);
- out.println("<!-- ");
+ out.println("<!--");
final String childIndent = indent + DocumentWriter.this.indent;
final TextWrapper wrapper = new TextWrapper();
@@ -268,7 +278,7 @@ public class DocumentWriter {
}
out.print(indent);
- out.println(" -->");
+ out.println("-->");
}
@Override
@@ -307,9 +317,9 @@ public class DocumentWriter {
@Override
public void visit(final IComment comment) {
- out.print("<!-- ");
+ out.print("<!--");
out.print(escape(node.getText()));
- out.print(" -->");
+ out.print("-->");
}
@Override
@@ -360,9 +370,9 @@ public class DocumentWriter {
@Override
public void visit(final IComment comment) {
- wrapper.addNoSplit("<!-- ");
+ wrapper.addNoSplit("<!--");
wrapper.add(escape(node.getText()));
- wrapper.addNoSplit(" -->");
+ wrapper.addNoSplit("-->");
}
@Override