AndrolibResources: cut escapeForResXml() to escapeCharsForResXml() and escapeTextForResXml().

StringBlock.getHTML() now properly escapes strings.
This commit is contained in:
Ryszard Wiśniewski 2010-06-14 09:11:55 +02:00
parent c781281e1c
commit 2ef8d48d96
3 changed files with 48 additions and 23 deletions

View File

@ -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("&amp;");
continue;
case '<':
out.append("&lt;");
continue;
}
out.append(c);
}
return out.toString();
}
private final static Logger LOGGER =
Logger.getLogger(AndrolibResources.class.getName());
}

View File

@ -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];

View File

@ -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("&", "&amp;").replace("<", "&lt;");
return AndrolibResources.escapeTextForResXml(html.toString(), false);
}
/**