Skip to content

Commit

Permalink
improved pull parser with decoder #18
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Mar 27, 2023
1 parent 9cc7852 commit a7b3a46
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/com/reandroid/arsc/chunk/xml/ParserEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.chunk.xml;

import org.xmlpull.v1.XmlPullParser;

public class ParserEvent {
private final int event;
private final ResXmlNode xmlNode;
private final String comment;
private final boolean endComment;
public ParserEvent(int event, ResXmlNode xmlNode, String comment, boolean endComment){
this.event = event;
this.xmlNode = xmlNode;
this.comment = comment;
this.endComment = endComment;
}
public ParserEvent(int event, ResXmlNode xmlNode){
this(event, xmlNode, null, false);
}
public int getEvent() {
return event;
}
public ResXmlNode getXmlNode() {
return xmlNode;
}
public String getComment() {
return comment;
}
public boolean isEndComment() {
return endComment;
}

public static final int START_DOCUMENT = XmlPullParser.START_DOCUMENT;
public static final int END_DOCUMENT = XmlPullParser.END_DOCUMENT;
public static final int START_TAG = XmlPullParser.START_TAG;
public static final int END_TAG = XmlPullParser.END_TAG;
public static final int TEXT = XmlPullParser.TEXT;
public static final int CDSECT = XmlPullParser.CDSECT;
public static final int ENTITY_REF = XmlPullParser.ENTITY_REF;
public static final int IGNORABLE_WHITESPACE = XmlPullParser.IGNORABLE_WHITESPACE;
public static final int PROCESSING_INSTRUCTION = XmlPullParser.PROCESSING_INSTRUCTION;
public static final int COMMENT = XmlPullParser.COMMENT;
public static final int DOCDECL = XmlPullParser.DOCDECL;

}
103 changes: 103 additions & 0 deletions src/main/java/com/reandroid/arsc/chunk/xml/ParserEventList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.chunk.xml;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ParserEventList implements Iterator<ParserEvent> {
private final List<ParserEvent> eventList;
private int index;
private ParserEvent mCurrent;
private int type = -1;
public ParserEventList(){
this.eventList = new ArrayList<>();
}
public void clear(){
this.eventList.clear();
reset();
}
public int getType(){
return type;
}
public String getText(){
if(type == ParserEvent.COMMENT){
return mCurrent.getComment();
}
if(type == ParserEvent.START_TAG || type == ParserEvent.END_TAG){
return getElement().getTag();
}
return null;
}
public int getLineNumber(){
if(type!=ParserEvent.COMMENT
&& type!=ParserEvent.START_TAG
&& type!=ParserEvent.END_TAG){
return 0;
}
ResXmlNode xmlNode = getXmlNode();
if(mCurrent.isEndComment() || type==ParserEvent.END_TAG){
return ((ResXmlElement)xmlNode).getEndLineNumber();
}
if(type==ParserEvent.TEXT){
return ((ResXmlTextNode)xmlNode).getLineNumber();
}
return ((ResXmlElement)xmlNode).getStartLineNumber();
}
public ResXmlNode getXmlNode(){
return mCurrent.getXmlNode();
}
public ResXmlElement getElement(){
return (ResXmlElement) mCurrent.getXmlNode();
}
@Override
public ParserEvent next(){
if(!hasNext()){
return null;
}
ParserEvent event = get(index);
index++;
mCurrent = event;
type = event.getEvent();
return event;
}
@Override
public boolean hasNext(){
return index < size();
}
public int size(){
return eventList.size();
}
public int getIndex() {
return index;
}
public void reset(){
index = 0;
mCurrent = null;
type = -1;
}
void add(ParserEvent parserEvent){
if(parserEvent==null){
return;
}
eventList.add(parserEvent);
}
private ParserEvent get(int i){
return eventList.get(i);
}
}

0 comments on commit a7b3a46

Please sign in to comment.