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, packageBlock,
valueItem.getValueType(), valueItem.getValueType(),
valueItem.getData()); valueItem.getData());
value = XMLDecodeHelper.escapeXmlChars(value);
if(value == null){
System.err.println("\nNULL: " + valueItem);
}
writer.text(value); writer.text(value);
} }
} }

View File

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