implement xml pp namespace features #18

This commit is contained in:
REAndroid 2023-05-02 16:41:33 +02:00
parent e8fff620f7
commit 0ec2b3fe1c
6 changed files with 1224 additions and 1105 deletions

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -123,12 +123,13 @@ public class ResXmlAttribute extends ValueItem implements AttributeValue, Compar
startNamespace.addAttributeReference(this);
}
private void unLinkStartNameSpace(){
ResXmlElement xmlElement=getParentResXmlElement();
if(xmlElement==null){
ResXmlElement xmlElement = getParentResXmlElement();
if(xmlElement == null){
return;
}
ResXmlStartNamespace startNamespace=xmlElement.getStartNamespaceByUriRef(getNamespaceReference());
if(startNamespace==null){
ResXmlStartNamespace startNamespace =
xmlElement.getStartNamespaceByUriRef(getNamespaceReference());
if(startNamespace == null){
return;
}
startNamespace.removeAttributeReference(this);

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -34,9 +34,13 @@ public class ResXmlPullParser implements XmlResourceParser {
private ResXmlDocument mDocument;
private boolean mDocumentCreatedHere;
private DocumentLoadedListener documentLoadedListener;
private boolean processNamespaces;
private boolean reportNamespaceAttrs;
public ResXmlPullParser(Decoder decoder){
this.mDecoder = decoder;
this.processNamespaces = true;
this.reportNamespaceAttrs = true;
}
public ResXmlPullParser(){
this(null);
@ -54,6 +58,7 @@ public class ResXmlPullParser implements XmlResourceParser {
public synchronized void setResXmlDocument(ResXmlDocument xmlDocument){
closeDocument();
this.mDocument = xmlDocument;
initDefaultFeatures();
initializeDecoder(xmlDocument);
xmlDocument.addEvents(mEventList);
}
@ -109,17 +114,33 @@ public class ResXmlPullParser implements XmlResourceParser {
@Override
public int getAttributeCount() {
ResXmlElement element = getCurrentElement();
if(element!=null){
return element.getAttributeCount();
if(element == null){
return 0;
}
return 0;
int count = element.getAttributeCount();
if(reportNamespaceAttrs){
count += element.getNamespaceCount();
}
return count;
}
@Override
public String getAttributeName(int index) {
if(reportNamespaceAttrs){
int nsCount = getNamespaceCountInternal();
if(index < nsCount){
return getNamespaceAttributeName(index);
}
}
return decodeAttributeName(getResXmlAttributeAt(index));
}
@Override
public String getAttributeValue(int index) {
if(reportNamespaceAttrs){
int nsCount = getNamespaceCountInternal();
if(index < nsCount){
return getNamespaceAttributeValue(index);
}
}
return decodeAttributeValue(getResXmlAttributeAt(index));
}
@Override
@ -324,9 +345,22 @@ public class ResXmlPullParser implements XmlResourceParser {
@Override
public void setFeature(String name, boolean state) throws XmlPullParserException {
if(FEATURE_PROCESS_NAMESPACES.equals(name)) {
processNamespaces = state;
}else if(FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
reportNamespaceAttrs = state;
}else {
throw new XmlPullParserException("Unsupported feature: " + name);
}
}
@Override
public boolean getFeature(String name) {
if(FEATURE_PROCESS_NAMESPACES.equals(name)) {
return processNamespaces;
}else if(FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
return reportNamespaceAttrs;
}
return false;
}
@Override
@ -358,12 +392,15 @@ public class ResXmlPullParser implements XmlResourceParser {
}
@Override
public int getNamespaceCount(int depth) throws XmlPullParserException {
if(reportNamespaceAttrs){
return 0;
}
ResXmlElement element = getCurrentElement();
while(element!=null && element.getDepth()>depth){
element=element.getParentResXmlElement();
}
if(element!=null){
return element.getStartNamespaceList().size();
return element.getNamespaceCount();
}
return 0;
}
@ -371,7 +408,7 @@ public class ResXmlPullParser implements XmlResourceParser {
public String getNamespacePrefix(int pos) throws XmlPullParserException {
ResXmlElement element = getCurrentElement();
if(element!=null){
return element.getStartNamespaceList().get(pos).getPrefix();
return element.getNamespace(pos).getPrefix();
}
return null;
}
@ -379,7 +416,7 @@ public class ResXmlPullParser implements XmlResourceParser {
public String getNamespaceUri(int pos) throws XmlPullParserException {
ResXmlElement element = getCurrentElement();
if(element!=null){
return element.getStartNamespaceList().get(pos).getUri();
return element.getNamespace(pos).getUri();
}
return null;
}
@ -466,10 +503,13 @@ public class ResXmlPullParser implements XmlResourceParser {
if(element!=null){
return element.countResXmlNodes() == 0 && element.getAttributeCount()==0;
}
return false;
return true;
}
@Override
public String getAttributeNamespace(int index) {
if(processNamespaces){
return null;
}
ResXmlAttribute attribute = getResXmlAttributeAt(index);
if(attribute != null){
return attribute.getUri();
@ -478,6 +518,9 @@ public class ResXmlPullParser implements XmlResourceParser {
}
@Override
public String getAttributePrefix(int index) {
if(processNamespaces){
return null;
}
ResXmlAttribute attribute = getResXmlAttributeAt(index);
if(attribute != null){
return attribute.getNamePrefix();
@ -493,7 +536,7 @@ public class ResXmlPullParser implements XmlResourceParser {
return false;
}
private String decodeAttributeName(ResXmlAttribute attribute){
if(attribute==null){
if(attribute == null){
return null;
}
String name;
@ -502,6 +545,9 @@ public class ResXmlPullParser implements XmlResourceParser {
name = attribute.getName();
}else {
name = mDecoder.decodeResourceName(attribute.getNameResourceID(), true);
if(processNamespaces){
name = attribute.getNamePrefix() + ":" + name;
}
}
return name;
}
@ -512,6 +558,7 @@ public class ResXmlPullParser implements XmlResourceParser {
return mDecoder.decodeAttributeValue(attribute);
}
public ResXmlAttribute getResXmlAttributeAt(int index){
index = getRealAttributeIndex(index);
ResXmlElement element = getCurrentElement();
if(element == null){
return null;
@ -545,6 +592,33 @@ public class ResXmlPullParser implements XmlResourceParser {
}
return null;
}
private int getRealAttributeIndex(int index){
if(reportNamespaceAttrs){
index = index - getNamespaceCountInternal();
}
return index;
}
private int getNamespaceCountInternal(){
ResXmlElement element = getCurrentElement();
if(element != null){
return element.getNamespaceCount();
}
return 0;
}
private String getNamespaceAttributeName(int index){
ResXmlStartNamespace namespace = getCurrentElement()
.getNamespace(index);
String prefix = namespace.getPrefix();
if(processNamespaces){
prefix = "xmlns:" + prefix;
}
return prefix;
}
private String getNamespaceAttributeValue(int index){
ResXmlStartNamespace namespace = getCurrentElement()
.getNamespace(index);
return namespace.getUri();
}
@Override
public int getEventType() throws XmlPullParserException {
return mEventList.getType();
@ -552,6 +626,10 @@ public class ResXmlPullParser implements XmlResourceParser {
@Override
public int next() throws XmlPullParserException, IOException {
mEventList.next();
int type = mEventList.getType();
if(type == START_TAG){
onStartTag();
}
return mEventList.getType();
}
@Override
@ -629,6 +707,13 @@ public class ResXmlPullParser implements XmlResourceParser {
this.mDocumentCreatedHere = true;
}
}
private void initDefaultFeatures(){
processNamespaces = true;
reportNamespaceAttrs = true;
}
private void onStartTag(){
}
public static interface DocumentLoadedListener{
public ResXmlDocument onDocumentLoaded(ResXmlDocument resXmlDocument);

View File

@ -1,89 +1,90 @@
/*
* 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;
/*
* 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 com.reandroid.arsc.chunk.ChunkType;
import com.reandroid.xml.SchemaAttr;
import com.reandroid.xml.XMLAttribute;
import com.reandroid.arsc.chunk.ChunkType;
import com.reandroid.xml.SchemaAttr;
import com.reandroid.xml.XMLAttribute;
import java.util.HashSet;
import java.util.Set;
import java.util.HashSet;
import java.util.Set;
public class ResXmlStartNamespace extends ResXmlNamespace {
private final Set<ResXmlAttribute> mReferencedAttributes;
public ResXmlStartNamespace() {
super(ChunkType.XML_START_NAMESPACE);
this.mReferencedAttributes = new HashSet<>();
}
public ResXmlEndNamespace getEnd(){
return (ResXmlEndNamespace) getPair();
}
public void setEnd(ResXmlEndNamespace namespace){
setPair(namespace);
}
@Override
void linkStringReferences(){
super.linkStringReferences();
ResXmlEndNamespace end = getEnd();
if(end!=null){
end.linkStringReferences();
}
}
@Override
void onRemoved(){
ResXmlEndNamespace end = getEnd();
if(end!=null){
end.onRemoved();
}
mReferencedAttributes.clear();
}
public boolean hasReferencedAttributes(){
return mReferencedAttributes.size()>0;
}
public void clearReferencedAttributes(){
mReferencedAttributes.clear();
}
public Set<ResXmlAttribute> getReferencedAttributes(){
return mReferencedAttributes;
}
void addAttributeReference(ResXmlAttribute attribute){
if(attribute!=null){
mReferencedAttributes.add(attribute);
}
}
void removeAttributeReference(ResXmlAttribute attribute){
if(attribute!=null){
mReferencedAttributes.remove(attribute);
}
}
public XMLAttribute decodeToXml(){
String uri=getUri();
String prefix=getPrefix();
if(isEmpty(uri) || isEmpty(prefix)){
return null;
}
SchemaAttr schemaAttr=new SchemaAttr(prefix, uri);
schemaAttr.setLineNumber(getLineNumber());
return schemaAttr;
}
private boolean isEmpty(String txt){
if(txt==null){
return true;
}
txt=txt.trim();
return txt.length()==0;
}
}
public class ResXmlStartNamespace extends ResXmlNamespace {
private final Set<ResXmlAttribute> mReferencedAttributes;
public ResXmlStartNamespace() {
super(ChunkType.XML_START_NAMESPACE);
this.mReferencedAttributes = new HashSet<>();
}
public ResXmlEndNamespace getEnd(){
return (ResXmlEndNamespace) getPair();
}
public void setEnd(ResXmlEndNamespace namespace){
setPair(namespace);
}
@Override
void linkStringReferences(){
super.linkStringReferences();
ResXmlEndNamespace end = getEnd();
if(end!=null){
end.linkStringReferences();
}
}
@Override
void onRemoved(){
ResXmlEndNamespace end = getEnd();
if(end!=null){
end.onRemoved();
}
mReferencedAttributes.clear();
}
public boolean hasReferencedAttributes(){
return mReferencedAttributes.size()>0;
}
public void clearReferencedAttributes(){
mReferencedAttributes.clear();
}
public Set<ResXmlAttribute> getReferencedAttributes(){
return mReferencedAttributes;
}
void addAttributeReference(ResXmlAttribute attribute){
if(attribute!=null){
mReferencedAttributes.add(attribute);
}
}
void removeAttributeReference(ResXmlAttribute attribute){
if(attribute!=null){
mReferencedAttributes.remove(attribute);
}
}
public XMLAttribute decodeToXml(){
String uri=getUri();
String prefix=getPrefix();
if(isEmpty(uri) || isEmpty(prefix)){
return null;
}
SchemaAttr schemaAttr=new SchemaAttr(prefix, uri);
schemaAttr.setLineNumber(getLineNumber());
return schemaAttr;
}
private boolean isEmpty(String txt){
if(txt==null){
return true;
}
txt=txt.trim();
return txt.length()==0;
}
}

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -16,6 +16,7 @@
package com.reandroid.xml;
import android.content.res.XmlResourceParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@ -24,10 +25,18 @@ import java.io.IOException;
public class XmlParserToSerializer {
private final XmlSerializer serializer;
private final XmlResourceParser parser;
private boolean enableIndent;
public XmlParserToSerializer(XmlResourceParser parser, XmlSerializer serializer){
this.parser = parser;
this.serializer = serializer;
this.enableIndent = true;
}
public void setEnableIndent(boolean enableIndent) {
this.enableIndent = enableIndent;
}
public void write() throws IOException, XmlPullParserException {
XmlResourceParser parser = this.parser;
int event = parser.next();
@ -71,17 +80,24 @@ public class XmlParserToSerializer {
private void onStartTag() throws IOException, XmlPullParserException {
XmlResourceParser parser = this.parser;
XmlSerializer serializer = this.serializer;
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
int nsCount = parser.getNamespaceCount(parser.getDepth());
for(int i=0; i<nsCount; i++){
String prefix = parser.getNamespacePrefix(i);
String namespace = parser.getNamespaceUri(i);
serializer.setPrefix(prefix, namespace);
boolean processNamespace = parser.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES);
boolean reportNamespaceAttrs = parser.getFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES);
serializer.setFeature(FEATURE_INDENT_OUTPUT, enableIndent);
if(!reportNamespaceAttrs){
int nsCount = parser.getNamespaceCount(parser.getDepth());
for(int i=0; i<nsCount; i++){
String prefix = parser.getNamespacePrefix(i);
String namespace = parser.getNamespaceUri(i);
serializer.setPrefix(prefix, namespace);
}
}
serializer.startTag(parser.getNamespace(), parser.getName());
int attrCount = parser.getAttributeCount();
for(int i=0; i<attrCount; i++){
serializer.attribute(parser.getAttributeNamespace(i),
String namespace = processNamespace ?
parser.getAttributeNamespace(i) : null;
serializer.attribute(namespace,
parser.getAttributeName(i),
parser.getAttributeValue(i));
}
@ -98,4 +114,6 @@ public class XmlParserToSerializer {
private void onEndDocument() throws IOException{
serializer.endDocument();
}
private static final String FEATURE_INDENT_OUTPUT = "http://xmlpull.org/v1/doc/features.html#indent-output";
}