| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.res.decoder; |
| 18 |
|
| 19 |
import brut.androlib.AndrolibException; |
| 20 |
import brut.androlib.err.AXmlDecodingException; |
| 21 |
import brut.androlib.err.RawXmlEncounteredException; |
| 22 |
import brut.androlib.res.data.ResTable; |
| 23 |
import brut.androlib.res.util.ExtXmlSerializer; |
| 24 |
import org.xmlpull.v1.XmlPullParser; |
| 25 |
import org.xmlpull.v1.XmlPullParserException; |
| 26 |
import org.xmlpull.v1.wrapper.XmlPullParserWrapper; |
| 27 |
import org.xmlpull.v1.wrapper.XmlPullWrapperFactory; |
| 28 |
import org.xmlpull.v1.wrapper.XmlSerializerWrapper; |
| 29 |
import org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.io.InputStream; |
| 33 |
import java.io.OutputStream; |
| 34 |
|
| 35 |
public class XmlPullStreamDecoder implements ResStreamDecoder { |
| 36 |
public XmlPullStreamDecoder(XmlPullParser parser, |
| 37 |
ExtXmlSerializer serializer) { |
| 38 |
this.mParser = parser; |
| 39 |
this.mSerial = serializer; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void decode(InputStream in, OutputStream out) |
| 44 |
throws AndrolibException { |
| 45 |
try { |
| 46 |
XmlPullWrapperFactory factory = XmlPullWrapperFactory.newInstance(); |
| 47 |
XmlPullParserWrapper par = factory.newPullParserWrapper(mParser); |
| 48 |
final ResTable resTable = ((AXmlResourceParser) mParser).getAttrDecoder().getCurrentPackage().getResTable(); |
| 49 |
|
| 50 |
XmlSerializerWrapper ser = new StaticXmlSerializerWrapper(mSerial, factory) { |
| 51 |
boolean hideSdkInfo = false; |
| 52 |
boolean hidePackageInfo = false; |
| 53 |
|
| 54 |
@Override |
| 55 |
public void event(XmlPullParser pp) |
| 56 |
throws XmlPullParserException, IOException { |
| 57 |
int type = pp.getEventType(); |
| 58 |
|
| 59 |
if (type == XmlPullParser.START_TAG) { |
| 60 |
if ("manifest".equalsIgnoreCase(pp.getName())) { |
| 61 |
try { |
| 62 |
hidePackageInfo = parseManifest(pp); |
| 63 |
} catch (AndrolibException ignored) {} |
| 64 |
} else if ("uses-sdk".equalsIgnoreCase(pp.getName())) { |
| 65 |
try { |
| 66 |
hideSdkInfo = parseAttr(pp); |
| 67 |
if (hideSdkInfo) { |
| 68 |
return; |
| 69 |
} |
| 70 |
} catch (AndrolibException ignored) {} |
| 71 |
} |
| 72 |
} else if (hideSdkInfo && type == XmlPullParser.END_TAG |
| 73 |
&& "uses-sdk".equalsIgnoreCase(pp.getName())) { |
| 74 |
return; |
| 75 |
} else if (hidePackageInfo && type == XmlPullParser.END_TAG |
| 76 |
&& "manifest".equalsIgnoreCase(pp.getName())) { |
| 77 |
super.event(pp); |
| 78 |
return; |
| 79 |
} |
| 80 |
super.event(pp); |
| 81 |
} |
| 82 |
|
| 83 |
private boolean parseManifest(XmlPullParser pp) |
| 84 |
throws AndrolibException { |
| 85 |
String attr_name; |
| 86 |
|
| 87 |
// read <manifest> for package: |
| 88 |
for (int i = 0; i < pp.getAttributeCount(); i++) { |
| 89 |
attr_name = pp.getAttributeName(i); |
| 90 |
|
| 91 |
if (attr_name.equalsIgnoreCase(("package"))) { |
| 92 |
resTable.setPackageRenamed(pp.getAttributeValue(i)); |
| 93 |
} else if (attr_name.equalsIgnoreCase("versionCode")) { |
| 94 |
resTable.setVersionCode(pp.getAttributeValue(i)); |
| 95 |
} else if (attr_name.equalsIgnoreCase("versionName")) { |
| 96 |
resTable.setVersionName(pp.getAttributeValue(i)); |
| 97 |
} |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
private boolean parseAttr(XmlPullParser pp) |
| 103 |
throws AndrolibException { |
| 104 |
for (int i = 0; i < pp.getAttributeCount(); i++) { |
| 105 |
final String a_ns = "http://schemas.android.com/apk/res/android"; |
| 106 |
String ns = pp.getAttributeNamespace(i); |
| 107 |
|
| 108 |
if (a_ns.equalsIgnoreCase(ns)) { |
| 109 |
String name = pp.getAttributeName(i); |
| 110 |
String value = pp.getAttributeValue(i); |
| 111 |
if (name != null && value != null) { |
| 112 |
if (name.equalsIgnoreCase("minSdkVersion") |
| 113 |
|| name.equalsIgnoreCase("targetSdkVersion") |
| 114 |
|| name.equalsIgnoreCase("maxSdkVersion") |
| 115 |
|| name.equalsIgnoreCase("compileSdkVersion")) { |
| 116 |
resTable.addSdkInfo(name, value); |
| 117 |
} else { |
| 118 |
resTable.clearSdkInfo(); |
| 119 |
return false; // Found unknown flags |
| 120 |
} |
| 121 |
} |
| 122 |
} else { |
| 123 |
resTable.clearSdkInfo(); |
| 124 |
|
| 125 |
if (i >= pp.getAttributeCount()) { |
| 126 |
return false; // Found unknown flags |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return ! resTable.getAnalysisMode(); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
par.setInput(in, null); |
| 136 |
ser.setOutput(out, null); |
| 137 |
|
| 138 |
while (par.nextToken() != XmlPullParser.END_DOCUMENT) { |
| 139 |
ser.event(par); |
| 140 |
} |
| 141 |
ser.flush(); |
| 142 |
} catch (XmlPullParserException ex) { |
| 143 |
throw new AXmlDecodingException("Could not decode XML", ex); |
| 144 |
} catch (IOException ex) { |
| 145 |
throw new RawXmlEncounteredException("Could not decode XML", ex); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public void decodeManifest(InputStream in, OutputStream out) |
| 150 |
throws AndrolibException { |
| 151 |
decode(in, out); |
| 152 |
} |
| 153 |
|
| 154 |
private final XmlPullParser mParser; |
| 155 |
private final ExtXmlSerializer mSerial; |
| 156 |
} |