fix: escaping xml characters

This commit is contained in:
REAndroid 2023-05-03 20:09:51 +02:00
parent b8d410803d
commit 45a1de1a08
2 changed files with 6 additions and 24 deletions

View File

@ -45,12 +45,6 @@ abstract class DecoderTableEntry<INPUT extends TableEntry<?, ?>, OUTPUT> {
packageBlock,
valueItem.getValueType(),
valueItem.getData());
value = XMLDecodeHelper.escapeXmlChars(value);
if(value == null){
System.err.println("\nNULL: " + valueItem);
}
writer.text(value);
}
}

View File

@ -15,6 +15,7 @@
*/
package com.reandroid.apk.xmldecoder;
import com.reandroid.arsc.decoder.ValueDecoder;
import com.reandroid.arsc.item.StringItem;
import com.reandroid.xml.*;
import com.reandroid.xml.parser.XMLSpanParser;
@ -28,7 +29,7 @@ public class XMLDecodeHelper {
return;
}
if(!stringItem.hasStyle()){
writer.text(escapeXmlChars(stringItem.get()));
writer.text(ValueDecoder.escapeSpecialCharacter(stringItem.get()));
}else {
String xml = stringItem.getXml();
XMLElement element = parseSpanSafe(xml);
@ -43,11 +44,13 @@ public class XMLDecodeHelper {
public static void writeElement(EntryWriter<?> writer, XMLElement element) throws IOException {
writer.startTag(element.getTagName());
for(XMLAttribute xmlAttribute : element.listAttributes()){
writer.attribute(xmlAttribute.getName(), xmlAttribute.getValue());
writer.attribute(xmlAttribute.getName(),
ValueDecoder.escapeSpecialCharacter(xmlAttribute.getValue()));
}
for(XMLNode xmlNode : element.getChildNodes()){
if(xmlNode instanceof XMLText){
writer.text(((XMLText)xmlNode).getText(false));
String text = ((XMLText)xmlNode).getText(false);
writer.text(ValueDecoder.escapeSpecialCharacter(text));
}else if(xmlNode instanceof XMLElement){
writeElement(writer, (XMLElement) xmlNode);
}
@ -65,20 +68,5 @@ public class XMLDecodeHelper {
return null;
}
}
public static String escapeXmlChars(String str){
if(str == null){
return null;
}
if(str.indexOf('&') < 0 && str.indexOf('<') < 0 && str.indexOf('>') < 0){
return str;
}
str=str.replaceAll("&amp;", "&");
str=str.replaceAll("&lt;", "<");
str=str.replaceAll("&gt;", ">");
str=str.replaceAll("&", "&amp;");
str=str.replaceAll("<", "&lt;");
str=str.replaceAll(">", "&gt;");
return str;
}
}