Search in sources :

Example 1 with XmlSerializerWrapper

use of org.xmlpull.v1.wrapper.XmlSerializerWrapper in project Apktool by iBotPeaches.

the class XmlPullStreamDecoder method decode.

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {
        XmlPullWrapperFactory factory = XmlPullWrapperFactory.newInstance();
        XmlPullParserWrapper par = factory.newPullParserWrapper(mParser);
        final ResTable resTable = ((AXmlResourceParser) mParser).getAttrDecoder().getCurrentPackage().getResTable();
        XmlSerializerWrapper ser = new StaticXmlSerializerWrapper(mSerial, factory) {

            boolean hideSdkInfo = false;

            boolean hidePackageInfo = false;

            @Override
            public void event(XmlPullParser pp) throws XmlPullParserException, IOException {
                int type = pp.getEventType();
                if (type == XmlPullParser.START_TAG) {
                    if ("manifest".equalsIgnoreCase(pp.getName())) {
                        try {
                            hidePackageInfo = parseManifest(pp);
                        } catch (AndrolibException ignored) {
                        }
                    } else if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
                        try {
                            hideSdkInfo = parseAttr(pp);
                            if (hideSdkInfo) {
                                return;
                            }
                        } catch (AndrolibException ignored) {
                        }
                    }
                } else if (hideSdkInfo && type == XmlPullParser.END_TAG && "uses-sdk".equalsIgnoreCase(pp.getName())) {
                    return;
                } else if (hidePackageInfo && type == XmlPullParser.END_TAG && "manifest".equalsIgnoreCase(pp.getName())) {
                    super.event(pp);
                    return;
                }
                super.event(pp);
            }

            private boolean parseManifest(XmlPullParser pp) throws AndrolibException {
                String attr_name;
                // read <manifest> for package:
                for (int i = 0; i < pp.getAttributeCount(); i++) {
                    attr_name = pp.getAttributeName(i);
                    if (attr_name.equalsIgnoreCase(("package"))) {
                        resTable.setPackageRenamed(pp.getAttributeValue(i));
                    } else if (attr_name.equalsIgnoreCase("versionCode")) {
                        resTable.setVersionCode(pp.getAttributeValue(i));
                    } else if (attr_name.equalsIgnoreCase("versionName")) {
                        resTable.setVersionName(pp.getAttributeValue(i));
                    }
                }
                return true;
            }

            private boolean parseAttr(XmlPullParser pp) throws AndrolibException {
                for (int i = 0; i < pp.getAttributeCount(); i++) {
                    final String a_ns = "http://schemas.android.com/apk/res/android";
                    String ns = pp.getAttributeNamespace(i);
                    if (a_ns.equalsIgnoreCase(ns)) {
                        String name = pp.getAttributeName(i);
                        String value = pp.getAttributeValue(i);
                        if (name != null && value != null) {
                            if (name.equalsIgnoreCase("minSdkVersion") || name.equalsIgnoreCase("targetSdkVersion") || name.equalsIgnoreCase("maxSdkVersion")) {
                                resTable.addSdkInfo(name, value);
                            } else {
                                resTable.clearSdkInfo();
                                // Found unknown flags
                                return false;
                            }
                        }
                    } else {
                        resTable.clearSdkInfo();
                        if (i >= pp.getAttributeCount()) {
                            // Found unknown flags
                            return false;
                        }
                    }
                }
                return !resTable.getAnalysisMode();
            }
        };
        par.setInput(in, null);
        ser.setOutput(out, null);
        while (par.nextToken() != XmlPullParser.END_DOCUMENT) {
            ser.event(par);
        }
        ser.flush();
    } catch (XmlPullParserException ex) {
        throw new AndrolibException("Could not decode XML", ex);
    } catch (IOException ex) {
        throw new AndrolibException("Could not decode XML", ex);
    }
}
Also used : XmlPullWrapperFactory(org.xmlpull.v1.wrapper.XmlPullWrapperFactory) XmlPullParserWrapper(org.xmlpull.v1.wrapper.XmlPullParserWrapper) StaticXmlSerializerWrapper(org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper) XmlPullParser(org.xmlpull.v1.XmlPullParser) AndrolibException(brut.androlib.AndrolibException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializerWrapper(org.xmlpull.v1.wrapper.XmlSerializerWrapper) StaticXmlSerializerWrapper(org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper) IOException(java.io.IOException) ResTable(brut.androlib.res.data.ResTable)

Aggregations

AndrolibException (brut.androlib.AndrolibException)1 ResTable (brut.androlib.res.data.ResTable)1 IOException (java.io.IOException)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1 XmlPullParserWrapper (org.xmlpull.v1.wrapper.XmlPullParserWrapper)1 XmlPullWrapperFactory (org.xmlpull.v1.wrapper.XmlPullWrapperFactory)1 XmlSerializerWrapper (org.xmlpull.v1.wrapper.XmlSerializerWrapper)1 StaticXmlSerializerWrapper (org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper)1