From b1389c2bd06d56a081b4b43d1520b613eacce7b9 Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Tue, 20 Mar 2012 13:19:55 -0700 Subject: 27256: ADT XML Formatter Replaces > Change-Id: I837371e862c596e1b9afc16ddfcac638bc92232a --- .../editors/formatting/XmlPrettyPrinter.java | 19 ++++++++++++++++++- .../editors/formatting/XmlPrettyPrinterTest.java | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) (limited to 'eclipse') 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" + ""); } + + 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", + "\n" + + "<untitled2>\n" + + "<untitled2>\n" + + "'untitled3"\n", + + "\n" + + "\n" + + "\n" + + " <untitled2>\n" + + " <untitled2>\n" + + " 'untitled3"\n" + + "\n" + + ""); + } } -- cgit v1.1