diff --git a/src/brut/androlib/res/AndrolibResources.java b/src/brut/androlib/res/AndrolibResources.java index 9bec86eb..66d062f4 100644 --- a/src/brut/androlib/res/AndrolibResources.java +++ b/src/brut/androlib/res/AndrolibResources.java @@ -470,11 +470,20 @@ final public class AndrolibResources { } } - public static String escapeForResXml(String value) { + public static String escapeTextForResXml(String value) { + return escapeTextForResXml(value, true); + } + + public static String escapeTextForResXml(String value, + boolean escapeChars) { if (value.isEmpty()) { return value; } + if (escapeChars) { + value = escapeCharsForResXml(value); + } + StringBuilder out = new StringBuilder(value.length() + 10); char[] chars = value.toCharArray(); @@ -486,9 +495,7 @@ final public class AndrolibResources { } boolean space = true; - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; - + for (char c : chars) { if (c == ' ') { if (space) { out.append("\\u0020"); @@ -500,16 +507,6 @@ final public class AndrolibResources { } space = false; - switch (c) { - case '\\': - case '\'': - case '"': - out.append('\\'); - break; - case '\n': - out.append("\\n"); - continue; - } out.append(c); } @@ -521,6 +518,35 @@ final public class AndrolibResources { return out.toString(); } + public static String escapeCharsForResXml(String value) { + if (value.isEmpty()) { + return value; + } + + StringBuilder out = new StringBuilder(value.length() + 10); + for (char c : value.toCharArray()) { + switch (c) { + case '\\': + case '\'': + case '"': + out.append('\\'); + break; + case '\n': + out.append("\\n"); + continue; + case '&': + out.append("&"); + continue; + case '<': + out.append("<"); + continue; + } + out.append(c); + } + + return out.toString(); + } + private final static Logger LOGGER = Logger.getLogger(AndrolibResources.class.getName()); } diff --git a/src/brut/androlib/res/decoder/AXmlResourceParser.java b/src/brut/androlib/res/decoder/AXmlResourceParser.java index 02efaad9..b9d2333d 100644 --- a/src/brut/androlib/res/decoder/AXmlResourceParser.java +++ b/src/brut/androlib/res/decoder/AXmlResourceParser.java @@ -301,7 +301,7 @@ public class AXmlResourceParser implements XmlResourceParser { int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE]; if (valueType == TypedValue.TYPE_STRING) { int valueString = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING]; - return AndrolibResources.escapeForResXml( + return AndrolibResources.escapeTextForResXml( m_strings.getString(valueString)); } int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA]; diff --git a/src/brut/androlib/res/decoder/StringBlock.java b/src/brut/androlib/res/decoder/StringBlock.java index e695d8e1..c08d5659 100644 --- a/src/brut/androlib/res/decoder/StringBlock.java +++ b/src/brut/androlib/res/decoder/StringBlock.java @@ -16,6 +16,7 @@ */ package brut.androlib.res.decoder; +import brut.androlib.res.AndrolibResources; import brut.util.ExtDataInput; import java.io.IOException; import java.nio.ByteBuffer; @@ -125,7 +126,7 @@ public class StringBlock { } int[] style = getStyle(index); if (style == null) { - return escapeForXml(raw); + return AndrolibResources.escapeTextForResXml(raw); } StringBuilder html = new StringBuilder(raw.length() + 32); int[] opened = new int[style.length / 3]; @@ -148,7 +149,8 @@ public class StringBlock { break; } if (offset <= end) { - html.append(escapeForXml(raw.substring(offset, end + 1))); + html.append(AndrolibResources.escapeCharsForResXml( + raw.substring(offset, end + 1))); offset = end + 1; } html.append('<'); @@ -158,7 +160,8 @@ public class StringBlock { } depth = j + 1; if (offset < start) { - html.append(escapeForXml(raw.substring(offset, start))); + html.append(AndrolibResources.escapeCharsForResXml( + raw.substring(offset, start))); offset = start; } if (i == -1) { @@ -170,11 +173,7 @@ public class StringBlock { style[i + 1] = -1; opened[depth++] = i; } - return html.toString(); - } - - private String escapeForXml(String txt) { - return txt.replace("&", "&").replace("<", "<"); + return AndrolibResources.escapeTextForResXml(html.toString(), false); } /**