aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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 ">" 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 > 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>");
+ }
}