11using System ;
22using System . IO ;
3+ using System . Xml ;
34
45namespace ApplicationUtility ;
56
@@ -8,6 +9,7 @@ public class AndroidManifest : IAspect
89 public string Description { get ; }
910
1011 AXMLParser ? binaryParser ;
12+ XmlDocument ? xmlDoc ;
1113
1214 AndroidManifest ( AXMLParser binaryParser , string ? description )
1315 {
@@ -35,6 +37,7 @@ public static IAspect LoadAspect (Stream stream, IAspectState state, string? des
3537
3638 public static IAspectState ProbeAspect ( Stream stream , string ? description )
3739 {
40+ Log . Debug ( $ "Checking if '{ description } ' is an Android binary XML document.") ;
3841 try {
3942 stream . Seek ( 0 , SeekOrigin . Begin ) ;
4043
@@ -44,15 +47,48 @@ public static IAspectState ProbeAspect (Stream stream, string? description)
4447 // We leave parsing of the data to `LoadAspect`, here we only detect the format
4548 return new AndroidManifestAspectState ( binaryParser ) ;
4649 } catch ( Exception ex ) {
47- Log . Debug ( $ "Failed to instantiate AXML binary parser for '{ description } '", ex ) ;
50+ Log . Debug ( $ "Failed to instantiate AXML binary parser for '{ description } '. Exception thrown: ", ex ) ;
4851 }
4952
50- // TODO: detect plain XML
51- throw new NotImplementedException ( ) ;
53+ Log . Debug ( $ "Checking if '{ description } ' is an plain XML document.") ;
54+ try {
55+ return new AndroidManifestAspectState ( ParsePlainXML ( stream ) ) ;
56+ } catch ( Exception ex ) {
57+ Log . Debug ( $ "Failed to parse '{ description } ' as XML document. Exception thrown:", ex ) ;
58+ }
59+
60+ // TODO: AndroidManifest.xml in AAB files is actually a protobuf data dump. Attempt to
61+ // deserialize it here.
62+ return new BasicAspectState ( success : false ) ;
5263 }
5364
5465 void Read ( )
5566 {
56- throw new NotImplementedException ( ) ;
67+ if ( binaryParser == null ) {
68+ throw new NotImplementedException ( ) ;
69+ }
70+
71+ xmlDoc = binaryParser . Parse ( ) ;
72+ if ( xmlDoc == null || ! binaryParser . IsValid ) {
73+ Log . Debug ( $ "AXML parser didn't render a valid document for '{ Description } '") ;
74+ return ;
75+ }
76+ Log . Debug ( $ "'{ Description } ' loaded and parsed correctly.") ;
77+ }
78+
79+ static XmlDocument ParsePlainXML ( Stream stream )
80+ {
81+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
82+ var settings = new XmlReaderSettings {
83+ IgnoreComments = true ,
84+ IgnoreProcessingInstructions = true ,
85+ IgnoreWhitespace = true ,
86+ } ;
87+
88+ using var reader = XmlReader . Create ( stream , settings ) ;
89+ var doc = new XmlDocument ( ) ;
90+ doc . Load ( reader ) ;
91+
92+ return doc ;
5793 }
5894}
0 commit comments