-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved pull parser with decoder #18
- Loading branch information
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/com/reandroid/arsc/chunk/xml/ParserEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
103
src/main/java/com/reandroid/arsc/chunk/xml/ParserEventList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |