aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-04-23 08:45:46 -0700
committerTor Norbye <tnorbye@google.com>2012-04-23 08:45:46 -0700
commitd0692238cf887345ab9224acbfba22c5fd895264 (patch)
tree633da72a7d02022f5f8fed343ffcecaec6663eb6 /eclipse
parent622512420652f1622a8d0ca9b64845e2a0288aa9 (diff)
downloadsdk-d0692238cf887345ab9224acbfba22c5fd895264.zip
sdk-d0692238cf887345ab9224acbfba22c5fd895264.tar.gz
sdk-d0692238cf887345ab9224acbfba22c5fd895264.tar.bz2
Improve XML CData formatting
Fix 29277: Strings XML formatter: unwanted whitespaces inserted when using CDATA section http://code.google.com/p/android/issues/detail?id=29277 This changes the XML formatter to keep CDATA sections not containing newlines to keep the CDATA on the same line without indentation or newlines, and multi-line CDATA sections to be separated on their own line and flushed left. Change-Id: I8576cff87e2880b0264479cc24c54cecc7841340
Diffstat (limited to 'eclipse')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java14
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java42
2 files changed, 51 insertions, 5 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
index 0cf08eb..754da29 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
@@ -264,11 +264,17 @@ public class XmlPrettyPrinter {
}
private void printCharacterData(int depth, Node node) {
- indent(depth);
+ String nodeValue = node.getNodeValue();
+ boolean separateLine = nodeValue.indexOf('\n') != -1;
+ if (separateLine && !endsWithLineSeparator()) {
+ mOut.append(mLineSeparator);
+ }
mOut.append("<![CDATA["); //$NON-NLS-1$
- mOut.append(node.getNodeValue());
- mOut.append("]]>"); //$NON-NLS-1$
- mOut.append(mLineSeparator);
+ mOut.append(nodeValue);
+ mOut.append("]]>"); //$NON-NLS-1$
+ if (separateLine) {
+ mOut.append(mLineSeparator);
+ }
}
private void printText(Node node) {
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
index fe7dcdd..736931b 100644
--- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
@@ -236,7 +236,7 @@ public class XmlPrettyPrinterTest extends TestCase {
"]>\n" +
"<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
" android:orientation=\"vertical\" >\n" +
- " <![CDATA[\n" +
+ " <![CDATA[\n" +
"This is character data!\n" +
"<!-- This is not a comment! -->\n" +
"and <this is not an element>\n" +
@@ -875,4 +875,44 @@ public class XmlPrettyPrinterTest extends TestCase {
"\n" +
"</resources>");
}
+
+ public void testCData1() throws Exception {
+ checkFormat(
+ "res/values/strings.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ " <string name=\"foo\"><![CDATA[bar]]></string>\n" +
+ "</resources>",
+
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ "\n" +
+ " <string name=\"foo\"><![CDATA[bar]]></string>\n" +
+ "\n" +
+ "</resources>");
+ }
+
+ public void testCData2() throws Exception {
+ checkFormat(
+ "res/values/strings.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ " <string name=\"foo1\"><![CDATA[bar1\n" +
+ "bar2\n" +
+ "bar3]]></string>\n" +
+ " <string name=\"foo2\"><![CDATA[bar]]></string>\n" +
+ "</resources>",
+
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ "\n" +
+ " <string name=\"foo1\">\n" +
+ "<![CDATA[bar1\n" +
+ "bar2\n" +
+ "bar3]]>\n" +
+ " </string>\n" +
+ " <string name=\"foo2\"><![CDATA[bar]]></string>\n" +
+ "\n" +
+ "</resources>");
+ }
}