mirror of
https://github.com/revanced/ARSCLib.git
synced 2025-05-05 16:24:30 +02:00
escape xml characters
This commit is contained in:
parent
291628b140
commit
4566824570
@ -162,7 +162,7 @@ import java.util.*;
|
||||
}
|
||||
}
|
||||
private void addStyleElement(XMLElement element){
|
||||
styleList.add(element.buildTextContent());
|
||||
styleList.add(element.buildTextContent(false));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -96,9 +96,7 @@ public class XMLFileEncoder {
|
||||
}
|
||||
}
|
||||
private void buildAttributes(XMLElement element, ResXmlElement resXmlElement){
|
||||
int count=element.getAttributeCount();
|
||||
for(int i=0;i<count;i++){
|
||||
XMLAttribute attribute=element.getAttributeAt(i);
|
||||
for(XMLAttribute attribute:element.listAttributes()){
|
||||
if(attribute instanceof SchemaAttr){
|
||||
continue;
|
||||
}
|
||||
@ -176,9 +174,7 @@ public class XMLFileEncoder {
|
||||
resXmlElement.calculatePositions();
|
||||
}
|
||||
private void ensureNamespaces(XMLElement element, ResXmlElement resXmlElement){
|
||||
int count=element.getAttributeCount();
|
||||
for(int i=0;i<count;i++){
|
||||
XMLAttribute attribute = element.getAttributeAt(i);
|
||||
for(XMLAttribute attribute:element.listAttributes()){
|
||||
String prefix = SchemaAttr.getPrefix(attribute.getName());
|
||||
if(prefix==null){
|
||||
continue;
|
||||
@ -194,12 +190,10 @@ public class XMLFileEncoder {
|
||||
idBuilder.buildTo(resXmlBlock.getResXmlIDMap());
|
||||
}
|
||||
private void searchResIds(ResIdBuilder idBuilder, XMLElement element){
|
||||
int count=element.getAttributeCount();
|
||||
for(int i=0;i<count;i++){
|
||||
XMLAttribute attribute = element.getAttributeAt(i);
|
||||
for(XMLAttribute attribute : element.listAttributes()){
|
||||
addResourceId(idBuilder, attribute);
|
||||
}
|
||||
count=element.getChildesCount();
|
||||
int count=element.getChildesCount();
|
||||
for(int i=0;i<count;i++){
|
||||
searchResIds(idBuilder, element.getChildAt(i));
|
||||
}
|
||||
|
@ -46,7 +46,6 @@ class XMLValuesEncoderAttr extends XMLValuesEncoderBag{
|
||||
resValueBag.getEntryBlock().setPublic(true);
|
||||
}
|
||||
private void encodeAttributes(XMLElement parentElement, ResValueBag resValueBag){
|
||||
int count=parentElement.getAttributeCount();
|
||||
ResValueBagItemArray bagItemArray = resValueBag.getResValueBagItemArray();
|
||||
|
||||
int bagIndex=0;
|
||||
@ -66,8 +65,7 @@ class XMLValuesEncoderAttr extends XMLValuesEncoderBag{
|
||||
|
||||
bagIndex++;
|
||||
|
||||
for(int i=0;i<count;i++){
|
||||
XMLAttribute attribute = parentElement.getAttributeAt(i);
|
||||
for(XMLAttribute attribute : parentElement.listAttributes()){
|
||||
String name = attribute.getName();
|
||||
if("name".equals(name) || "formats".equals(name)){
|
||||
continue;
|
||||
|
@ -36,15 +36,44 @@ class XMLValuesEncoderStyle extends XMLValuesEncoderBag{
|
||||
ResValueBagItemArray itemArray = resValueBag.getResValueBagItemArray();
|
||||
for(int i=0;i<count;i++){
|
||||
XMLElement child=parentElement.getChildAt(i);
|
||||
ResValueBagItem item = itemArray.get(i);
|
||||
String name=child.getAttributeValue("name");
|
||||
int id=decodeUnknownAttributeHex(name);
|
||||
if(id!=0){
|
||||
String value = child.getTextContent();
|
||||
if(ValueDecoder.isReference(value)){
|
||||
item.setTypeAndData(ValueType.REFERENCE,
|
||||
getMaterials().resolveReference(value));
|
||||
}else {
|
||||
ValueDecoder.EncodeResult encodeResult = ValueDecoder.encodeGuessAny(value);
|
||||
if(encodeResult!=null){
|
||||
item.setTypeAndData(encodeResult.valueType, encodeResult.value);
|
||||
}else {
|
||||
item.setValueAsString(value);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
EntryBlock attributeEntry=getMaterials()
|
||||
.getAttributeBlock(child.getAttributeValue("name"));
|
||||
.getAttributeBlock(name);
|
||||
if(attributeEntry==null){
|
||||
throw new EncodeException("Unknown attribute name: '"+child.toText()
|
||||
+"', for style: "+parentElement.getAttributeValue("name"));
|
||||
}
|
||||
encodeChild(parentElement.getChildAt(i), attributeEntry, itemArray.get(i));
|
||||
encodeChild(child, attributeEntry, item);
|
||||
}
|
||||
}
|
||||
private int decodeUnknownAttributeHex(String name){
|
||||
if(name.length()==0||name.charAt(0)!='@'){
|
||||
return 0;
|
||||
}
|
||||
name=name.substring(1);
|
||||
if(!ValueDecoder.isHex(name)){
|
||||
return 0;
|
||||
}
|
||||
return ValueDecoder.parseHex(name);
|
||||
}
|
||||
private void encodeChild(XMLElement child, EntryBlock attributeEntry, ResValueBagItem bagItem){
|
||||
|
||||
bagItem.setId(attributeEntry.getResourceId());
|
||||
|
@ -94,15 +94,9 @@ public class XMLAttribute extends XMLNode{
|
||||
public void setValue(String val){
|
||||
mValue= XMLUtil.escapeXmlChars(val);
|
||||
}
|
||||
public boolean isEmpty(){
|
||||
return XMLUtil.isEmpty(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean write(Writer writer, boolean newLineAttributes) throws IOException {
|
||||
if(isEmpty()){
|
||||
return false;
|
||||
}
|
||||
writer.write(getName());
|
||||
writer.write("=\"");
|
||||
String val= XMLUtil.trimQuote(getValue());
|
||||
@ -114,9 +108,6 @@ public class XMLAttribute extends XMLNode{
|
||||
}
|
||||
@Override
|
||||
public String toText(int indent, boolean newLineAttributes) {
|
||||
if(isEmpty()){
|
||||
return null;
|
||||
}
|
||||
StringWriter writer=new StringWriter();
|
||||
try {
|
||||
write(writer);
|
||||
@ -131,16 +122,12 @@ public class XMLAttribute extends XMLNode{
|
||||
if(name==null){
|
||||
name="";
|
||||
}
|
||||
name=getClass().getName()+name;
|
||||
return name.hashCode();
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof XMLAttribute){
|
||||
XMLAttribute attr=(XMLAttribute)obj;
|
||||
if(isEmpty()){
|
||||
return attr.isEmpty();
|
||||
}
|
||||
return getName().equals(attr.getName());
|
||||
}
|
||||
return false;
|
||||
|
@ -26,7 +26,7 @@ import java.util.*;
|
||||
public class XMLElement extends XMLNode{
|
||||
static final long DEBUG_TO_STRING=500;
|
||||
private String mTagName;
|
||||
private final List<XMLAttribute> mAttributes = new ArrayList<>();
|
||||
private final LinkedHashMap<String, XMLAttribute> mAttributes = new LinkedHashMap<>();
|
||||
private final List<XMLElement> mChildElements = new ArrayList<>();
|
||||
private final List<XMLComment> mComments = new ArrayList<>();
|
||||
private final List<XMLText> mTexts = new ArrayList<>();
|
||||
@ -94,7 +94,7 @@ public class XMLElement extends XMLNode{
|
||||
if(exist!=null){
|
||||
exist.setValue(schemaAttr.getValue());
|
||||
}else {
|
||||
addAttributeNoCheck(schemaAttr);
|
||||
addAttribute(schemaAttr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -254,8 +254,8 @@ public class XMLElement extends XMLNode{
|
||||
mComments.clear();
|
||||
mTexts.clear();
|
||||
}
|
||||
public List<XMLAttribute> listAttributes(){
|
||||
return mAttributes;
|
||||
public Collection<XMLAttribute> listAttributes(){
|
||||
return mAttributes.values();
|
||||
}
|
||||
public int getChildesCount(){
|
||||
return mChildElements.size();
|
||||
@ -272,12 +272,6 @@ public class XMLElement extends XMLNode{
|
||||
public int getAttributeCount(){
|
||||
return mAttributes.size();
|
||||
}
|
||||
public XMLAttribute getAttributeAt(int index){
|
||||
if(index>=mAttributes.size()){
|
||||
return null;
|
||||
}
|
||||
return mAttributes.get(index);
|
||||
}
|
||||
public String getAttributeValue(String name){
|
||||
XMLAttribute attr=getAttribute(name);
|
||||
if (attr==null){
|
||||
@ -303,48 +297,11 @@ public class XMLElement extends XMLNode{
|
||||
throw new XMLException(ex.getMessage()+": "+" '"+toString()+"'");
|
||||
}
|
||||
}
|
||||
public boolean getAttributeValueBool(String name, boolean def){
|
||||
XMLAttribute attr=getAttribute(name);
|
||||
if (attr==null){
|
||||
return def;
|
||||
}
|
||||
if(!attr.isValueBool()){
|
||||
return def;
|
||||
}
|
||||
return attr.getValueBool();
|
||||
}
|
||||
public boolean getAttributeValueBool(String name) throws XMLException {
|
||||
XMLAttribute attr=getAttribute(name);
|
||||
if (attr==null || !attr.isValueBool()){
|
||||
throw new XMLException("Expecting boolean for attr <"+name+ "> at '"+toString()+"'");
|
||||
}
|
||||
return attr.getValueBool();
|
||||
}
|
||||
public XMLAttribute getAttribute(String name){
|
||||
if(XMLUtil.isEmpty(name)){
|
||||
return null;
|
||||
}
|
||||
for(XMLAttribute attr:mAttributes){
|
||||
if(name.equals(attr.getName())){
|
||||
return attr;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return mAttributes.get(name);
|
||||
}
|
||||
public XMLAttribute removeAttribute(String name){
|
||||
if(XMLUtil.isEmpty(name)){
|
||||
return null;
|
||||
}
|
||||
XMLAttribute attr=getAttribute(name);
|
||||
if(attr==null){
|
||||
return null;
|
||||
}
|
||||
int i=mAttributes.indexOf(attr);
|
||||
if(i<0){
|
||||
return null;
|
||||
}
|
||||
mAttributes.remove(i);
|
||||
return attr;
|
||||
return mAttributes.remove(name);
|
||||
}
|
||||
public XMLAttribute setAttribute(String name, int value){
|
||||
return setAttribute(name, String.valueOf(value));
|
||||
@ -364,7 +321,7 @@ public class XMLElement extends XMLNode{
|
||||
}else{
|
||||
attr=new XMLAttribute(name,value);
|
||||
}
|
||||
addAttributeNoCheck(attr);
|
||||
addAttribute(attr);
|
||||
}else {
|
||||
attr.setValue(value);
|
||||
}
|
||||
@ -382,20 +339,15 @@ public class XMLElement extends XMLNode{
|
||||
if(attr==null){
|
||||
return;
|
||||
}
|
||||
if(XMLUtil.isEmpty(attr.getName())){
|
||||
String name = attr.getName();
|
||||
if(XMLUtil.isEmpty(name)){
|
||||
return;
|
||||
}
|
||||
XMLAttribute exist=getAttribute(attr.getName());
|
||||
XMLAttribute exist = mAttributes.get(name);
|
||||
if(exist!=null){
|
||||
return;
|
||||
}
|
||||
mAttributes.add(attr);
|
||||
}
|
||||
private void addAttributeNoCheck(XMLAttribute attr){
|
||||
if(attr==null || attr.isEmpty()){
|
||||
return;
|
||||
}
|
||||
mAttributes.add(attr);
|
||||
mAttributes.put(name, attr);
|
||||
}
|
||||
public void sortChildes(Comparator<XMLElement> comparator){
|
||||
if(comparator==null){
|
||||
@ -403,12 +355,6 @@ public class XMLElement extends XMLNode{
|
||||
}
|
||||
mChildElements.sort(comparator);
|
||||
}
|
||||
public void sortAttributes(Comparator<XMLAttribute> comparator){
|
||||
if(comparator==null){
|
||||
return;
|
||||
}
|
||||
mAttributes.sort(comparator);
|
||||
}
|
||||
public XMLElement getParent(){
|
||||
return mParent;
|
||||
}
|
||||
@ -534,13 +480,13 @@ public class XMLElement extends XMLNode{
|
||||
if(!hasTextContent()){
|
||||
return null;
|
||||
}
|
||||
return buildTextContent();
|
||||
return buildTextContent(true);
|
||||
}
|
||||
public String buildTextContent(){
|
||||
public String buildTextContent(boolean unEscape){
|
||||
StringWriter writer=new StringWriter();
|
||||
try {
|
||||
for(XMLNode node:getChildNodes()){
|
||||
node.buildTextContent(writer);
|
||||
node.buildTextContent(writer, unEscape);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
@ -548,7 +494,7 @@ public class XMLElement extends XMLNode{
|
||||
}
|
||||
return writer.toString();
|
||||
}
|
||||
void buildTextContent(Writer writer) throws IOException {
|
||||
void buildTextContent(Writer writer, boolean unEscape) throws IOException {
|
||||
writer.write("<");
|
||||
writer.write(getTagName());
|
||||
appendAttributes(writer, false);
|
||||
@ -558,7 +504,7 @@ public class XMLElement extends XMLNode{
|
||||
}
|
||||
writer.write('>');
|
||||
for(XMLNode node:getChildNodes()){
|
||||
node.buildTextContent(writer);
|
||||
node.buildTextContent(writer, unEscape);
|
||||
}
|
||||
if(hasChildNodes()){
|
||||
writer.write("</");
|
||||
@ -611,10 +557,7 @@ public class XMLElement extends XMLNode{
|
||||
}
|
||||
private boolean appendAttributes(Writer writer, boolean newLineAttributes) throws IOException {
|
||||
boolean addedOnce=false;
|
||||
for(XMLAttribute attr:mAttributes){
|
||||
if(attr.isEmpty()){
|
||||
continue;
|
||||
}
|
||||
for(XMLAttribute attr:listAttributes()){
|
||||
if(addedOnce){
|
||||
if(newLineAttributes){
|
||||
writer.write(XMLUtil.NEW_LINE);
|
||||
|
@ -78,7 +78,7 @@ public abstract class XMLNode {
|
||||
boolean hasChildNodes(){
|
||||
return mChildNodes.size()>0;
|
||||
}
|
||||
void buildTextContent(Writer writer) throws IOException{
|
||||
void buildTextContent(Writer writer, boolean unEscape) throws IOException{
|
||||
|
||||
}
|
||||
public boolean write(Writer writer) throws IOException {
|
||||
|
@ -21,7 +21,7 @@ import java.io.Writer;
|
||||
public class XMLText extends XMLNode{
|
||||
private String text;
|
||||
public XMLText(String text){
|
||||
this.text=text;
|
||||
this.text=XMLUtil.escapeXmlChars(text);
|
||||
}
|
||||
public XMLText(){
|
||||
this(null);
|
||||
@ -44,8 +44,8 @@ public class XMLText extends XMLNode{
|
||||
this.text=XMLUtil.escapeXmlChars(text);
|
||||
}
|
||||
@Override
|
||||
void buildTextContent(Writer writer) throws IOException{
|
||||
writer.write(this.text);
|
||||
void buildTextContent(Writer writer, boolean unEscape) throws IOException{
|
||||
writer.write(getText(unEscape));
|
||||
}
|
||||
@Override
|
||||
public boolean write(Writer writer, boolean newLineAttributes) throws IOException {
|
||||
|
@ -29,7 +29,7 @@ public class XMLUtil {
|
||||
if(str==null){
|
||||
return null;
|
||||
}
|
||||
if(!PATTERN_ESCAPE.matcher(str).matches()){
|
||||
if(str.indexOf('&')<0 && str.indexOf('<')<0 && str.indexOf('>')<0){
|
||||
return str;
|
||||
}
|
||||
str=str.replaceAll("&", "&");
|
||||
|
@ -35,9 +35,7 @@ package com.reandroid.xml;
|
||||
if(element==null){
|
||||
return;
|
||||
}
|
||||
int max=element.getAttributeCount();
|
||||
for(int i=0;i<max;i++){
|
||||
XMLAttribute exist=element.getAttributeAt(i);
|
||||
for(XMLAttribute exist : element.listAttributes()){
|
||||
setAttribute(exist.getName(), exist.getValue());
|
||||
}
|
||||
}
|
||||
@ -104,7 +102,7 @@ package com.reandroid.xml;
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
void buildTextContent(Writer writer) throws IOException {
|
||||
void buildTextContent(Writer writer, boolean unEscape) throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user