diff options
author | Tor Norbye <tnorbye@google.com> | 2011-05-12 17:11:25 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2011-05-17 13:17:01 -0700 |
commit | 8bcb3f7e8f2632f7d91cf75f3f637c006f4f6531 (patch) | |
tree | 45ac2ba96873ec189ba4392af2863573ce12098f /eclipse | |
parent | 6db93e9e7e8cb1281e3b75d6dfe467126cd9653f (diff) | |
download | sdk-8bcb3f7e8f2632f7d91cf75f3f637c006f4f6531.zip sdk-8bcb3f7e8f2632f7d91cf75f3f637c006f4f6531.tar.gz sdk-8bcb3f7e8f2632f7d91cf75f3f637c006f4f6531.tar.bz2 |
Escape Strings extracted with the Extract String refactoring
Escape any single or double quotes inside the string
value definition in strings.xml:
This'll work => "This'll work"
Escape '" => Escape \'\"
Change-Id: I21cb506e10e837feb0e435a21cb50aaa5342f0fa
Diffstat (limited to 'eclipse')
-rw-r--r-- | eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/extractstring/ExtractStringRefactoring.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/extractstring/ExtractStringRefactoring.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/extractstring/ExtractStringRefactoring.java index 955a0b2..dce8160 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/extractstring/ExtractStringRefactoring.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/refactorings/extractstring/ExtractStringRefactoring.java @@ -1202,6 +1202,9 @@ public class ExtractStringRefactoring extends Refactoring { IStructuredModel smodel = null; + // Single and double quotes must be escaped in the <string>value</string> declaration + tokenString = escapeString(tokenString); + try { IStructuredDocument sdoc = null; boolean checkTopElement = true; @@ -1432,6 +1435,34 @@ public class ExtractStringRefactoring extends Refactoring { } /** + * Escape a string value to be placed in a string resource file such that it complies with + * the escaping rules described here: + * http://developer.android.com/guide/topics/resources/string-resource.html + * This method assumes that the String is not escaped already. + */ + private static String escapeString(String s) { + if ((s.indexOf('"') != -1)) { + // Must escape each quote or apostrophe. + // (We cannot escape the string by surrounding it with apostrophes so we don't + // need to distinguish between containsQuotes and contains both.) + StringBuilder sb = new StringBuilder(s.length() + 2); + for (int i = 0, n = s.length(); i < n; i++) { + char c = s.charAt(i); + if (c == '\'' || c == '"') { + sb.append('\\'); + } + sb.append(c); + } + return sb.toString(); + } else if ((s.indexOf('\'') != -1)) { + // Surround string with quotes + return '"' + s + '"'; + } else { + return s; + } + } + + /** * Computes the changes to be made to the source Android XML file and * returns a list of {@link Change}. * <p/> |