reusable bin xml parser

This commit is contained in:
REAndroid 2023-03-28 09:44:19 -04:00
parent 777c7f76ef
commit 0c4668ee7f

View File

@ -16,6 +16,7 @@
package com.reandroid.arsc.chunk.xml; package com.reandroid.arsc.chunk.xml;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import com.reandroid.arsc.ApkFile;
import com.reandroid.arsc.decoder.Decoder; import com.reandroid.arsc.decoder.Decoder;
import com.reandroid.arsc.value.ValueType; import com.reandroid.arsc.value.ValueType;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -33,11 +34,23 @@ public class ResXmlPullParser implements XmlResourceParser {
private ResXmlDocument mDocument; private ResXmlDocument mDocument;
private boolean mDocumentCreatedHere; private boolean mDocumentCreatedHere;
public ResXmlPullParser(Decoder decoder){
public ResXmlPullParser(){ this.mDecoder = decoder;
} }
public ResXmlPullParser(){
public void setResXmlDocument(ResXmlDocument xmlDocument){ this(null);
}
public synchronized ResXmlPullParser getParser(){
if(isBusy()){
return new ResXmlPullParser(getDecoder());
}
closeDocument();
return this;
}
public synchronized boolean isBusy() {
return !mEventList.hasNext();
}
public synchronized void setResXmlDocument(ResXmlDocument xmlDocument){
closeDocument(); closeDocument();
this.mDocument = xmlDocument; this.mDocument = xmlDocument;
initializeDecoder(xmlDocument); initializeDecoder(xmlDocument);
@ -54,9 +67,21 @@ public class ResXmlPullParser implements XmlResourceParser {
return mDecoder; return mDecoder;
} }
private void initializeDecoder(ResXmlDocument xmlDocument){ private void initializeDecoder(ResXmlDocument xmlDocument){
if(mDecoder!=null){ Decoder decoder = this.mDecoder;
if(decoder!=null){
if(decoder.getApkFile()==null){
decoder.setApkFile(xmlDocument.getApkFile());
}
return; return;
} }
ApkFile apkFile = xmlDocument.getApkFile();
if(apkFile!=null){
decoder = apkFile.getDecoder();
if(decoder!=null){
this.mDecoder = decoder;
return;
}
}
mDecoder = Decoder.create(xmlDocument); mDecoder = Decoder.create(xmlDocument);
} }
@ -320,16 +345,18 @@ public class ResXmlPullParser implements XmlResourceParser {
} }
@Override @Override
public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException { public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException {
ResXmlDocument xmlDocument = new ResXmlDocument(); synchronized (this){
try { ResXmlDocument xmlDocument = new ResXmlDocument();
xmlDocument.readBytes(inputStream); try {
} catch (IOException exception) { xmlDocument.readBytes(inputStream);
XmlPullParserException pullParserException = new XmlPullParserException(exception.getMessage()); } catch (IOException exception) {
pullParserException.initCause(exception); XmlPullParserException pullParserException = new XmlPullParserException(exception.getMessage());
throw pullParserException; pullParserException.initCause(exception);
throw pullParserException;
}
setResXmlDocument(xmlDocument);
this.mDocumentCreatedHere = true;
} }
setResXmlDocument(xmlDocument);
this.mDocumentCreatedHere = true;
} }
@Override @Override
public String getInputEncoding() { public String getInputEncoding() {