aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-03-20 13:19:55 -0700
committerTor Norbye <tnorbye@google.com>2012-03-20 13:19:55 -0700
commitb1389c2bd06d56a081b4b43d1520b613eacce7b9 (patch)
treeb158f225b3dbd4d44010b82e635d480c68a58aee /eclipse/plugins
parent7117fe536df6b9be019d628394ab1ab520a97c1f (diff)
downloadsdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.zip
sdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.tar.gz
sdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.tar.bz2
27256: ADT XML Formatter Replaces &gt;
Change-Id: I837371e862c596e1b9afc16ddfcac638bc92232a
Diffstat (limited to 'eclipse/plugins')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java19
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java20
2 files changed, 38 insertions, 1 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 cdb3fa4..0cf08eb 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
@@ -27,6 +27,7 @@ import com.android.ide.eclipse.adt.internal.editors.layout.gle2.DomUtilities;
import org.eclipse.wst.xml.core.internal.document.DocumentTypeImpl;
import org.eclipse.wst.xml.core.internal.document.ElementImpl;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -271,8 +272,19 @@ public class XmlPrettyPrinter {
}
private void printText(Node node) {
+ boolean escape = true;
String text = node.getNodeValue();
+ if (node instanceof IDOMNode) {
+ // Get the original source string. This will contain the actual entities
+ // such as "&gt;" instead of ">" which it gets turned into for the DOM nodes.
+ // By operating on source we can preserve the user's entities rather than
+ // having &gt; for example always turned into >.
+ IDOMNode textImpl = (IDOMNode) node;
+ text = textImpl.getSource();
+ escape = false;
+ }
+
// Most text nodes are just whitespace for formatting (which we're replacing)
// so look for actual text content and extract that part out
String trimmed = text.trim();
@@ -311,7 +323,12 @@ public class XmlPrettyPrinter {
text = text.substring(lastPrefixNewline + 1, firstSuffixNewline);
}
- DomUtilities.appendXmlTextValue(mOut, text);
+ if (escape) {
+ DomUtilities.appendXmlTextValue(mOut, text);
+ } else {
+ // Text is already escaped
+ mOut.append(text);
+ }
if (mStyle != XmlFormatStyle.RESOURCE) {
mOut.append(mLineSeparator);
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 5b44ed2..fe7dcdd 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
@@ -855,4 +855,24 @@ public class XmlPrettyPrinterTest extends TestCase {
"\n" +
"</resources>");
}
+
+ public void testPreserveEntities() throws Exception {
+ // Ensure that entities such as &gt; in the input string are preserved in the output
+ // format
+ checkFormat(
+ "res/values/strings.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources><string name=\"untitled\">&lt;untitled2></string>\n" +
+ "<string name=\"untitled2\">&lt;untitled2&gt;</string>\n" +
+ "<string name=\"untitled3\">&apos;untitled3&quot;</string></resources>\n",
+
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ "\n" +
+ " <string name=\"untitled\">&lt;untitled2></string>\n" +
+ " <string name=\"untitled2\">&lt;untitled2&gt;</string>\n" +
+ " <string name=\"untitled3\">&apos;untitled3&quot;</string>\n" +
+ "\n" +
+ "</resources>");
+ }
}