diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-20 13:19:55 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-03-20 13:19:55 -0700 |
commit | b1389c2bd06d56a081b4b43d1520b613eacce7b9 (patch) | |
tree | b158f225b3dbd4d44010b82e635d480c68a58aee /eclipse | |
parent | 7117fe536df6b9be019d628394ab1ab520a97c1f (diff) | |
download | sdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.zip sdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.tar.gz sdk-b1389c2bd06d56a081b4b43d1520b613eacce7b9.tar.bz2 |
27256: ADT XML Formatter Replaces >
Change-Id: I837371e862c596e1b9afc16ddfcac638bc92232a
Diffstat (limited to 'eclipse')
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 > 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\"><untitled2></string>\n" + + "<string name=\"untitled2\"><untitled2></string>\n" + + "<string name=\"untitled3\">'untitled3"</string></resources>\n", + + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + + "<resources>\n" + + "\n" + + " <string name=\"untitled\"><untitled2></string>\n" + + " <string name=\"untitled2\"><untitled2></string>\n" + + " <string name=\"untitled3\">'untitled3"</string>\n" + + "\n" + + "</resources>"); + } } |