@@ -16,12 +16,20 @@ namespace Xamarin.Android.Tasks
1616{
1717 class FileResourceParser : ResourceParser
1818 {
19+ public string JavaPlatformDirectory { get ; set ; }
20+
21+ public string ResourceFlagFile { get ; set ; }
22+
1923 Dictionary < R , R [ ] > arrayMapping = new Dictionary < R , R [ ] > ( ) ;
20- public Dictionary < string , R > Parse ( string resourceDirectory , IEnumerable < string > additionalResourceDirectories , Dictionary < string , string > resourceMap )
24+ public IDictionary < string , R > Parse ( string resourceDirectory , IEnumerable < string > additionalResourceDirectories , Dictionary < string , string > resourceMap )
2125 {
22- Log . LogDebugMessage ( $ "Processing Directory { resourceDirectory } ") ;
23- var result = new Dictionary < string , R > ( ) ;
26+ Log . LogDebugMessage ( $ "Parsing Directory { resourceDirectory } ") ;
27+ var result = new SortedDictionary < string , R > ( ) ;
2428 Dictionary < string , SortedSet < R > > resources = new Dictionary < string , SortedSet < R > > ( ) ;
29+ foreach ( var kvp in RtxtParser . knownTypes ) {
30+ Log . LogDebugMessage ( $ "Adding { kvp . Key } ") ;
31+ resources . Add ( kvp . Key , new SortedSet < R > ( ) ) ;
32+ }
2533 foreach ( var dir in Directory . EnumerateDirectories ( resourceDirectory , "*" , SearchOption . TopDirectoryOnly ) ) {
2634 foreach ( var file in Directory . EnumerateFiles ( dir , "*.*" , SearchOption . AllDirectories ) ) {
2735 ProcessResourceFile ( file , resources ) ;
@@ -39,11 +47,25 @@ public Dictionary<string, R> Parse (string resourceDirectory, IEnumerable<string
3947 }
4048 }
4149 }
50+ foreach ( var kvp in resources ) {
51+ foreach ( R r in kvp . Value ) {
52+ if ( ! result . ContainsKey ( r . ToSortedString ( ) ) )
53+ result . Add ( r . ToSortedString ( ) , r ) ;
54+ }
55+ }
4256 return result ;
4357 }
4458
59+ HashSet < string > resourceNamesToUseDirectly = new HashSet < string > ( ) {
60+ "integer-array" ,
61+ "string-array" ,
62+ "declare-styleable" ,
63+ "add-resource" ,
64+ } ;
65+
4566 void ProcessResourceFile ( string file , Dictionary < string , SortedSet < R > > resources )
4667 {
68+ Log . LogDebugMessage ( $ "{ nameof ( ProcessResourceFile ) } { file } ") ;
4769 var fileName = Path . GetFileNameWithoutExtension ( file ) ;
4870 if ( string . IsNullOrEmpty ( fileName ) )
4971 return ;
@@ -65,22 +87,55 @@ void ProcessResourceFile (string file, Dictionary<string, SortedSet<R>> resource
6587 default :
6688 break ;
6789 }
68- if ( ! resources . ContainsKey ( path ) )
69- resources [ path ] = new SortedSet < R > ( ) ;
90+ // if (!resources.ContainsKey (path)) {
91+ // Log.LogDebugMessage ($"Registering {path}");
92+ // resources[path] = new SortedSet<R>();
93+ // }
94+ CreateResourceField ( path , fileName , resources ) ;
95+ }
96+
97+ void CreateResourceField ( string root , string id , Dictionary < string , SortedSet < R > > resources ) {
98+ var i = root . IndexOf ( '-' ) ;
99+ var item = i < 0 ? root : root . Substring ( 0 , i ) ;
100+ item = resourceNamesToUseDirectly . Contains ( root ) ? root : item ;
101+ switch ( item . ToLower ( ) ) {
102+ case "array" :
103+ case "string-array" :
104+ case "integer-array" :
105+ item = "array" ;
106+ break ;
107+ case "enum" :
108+ case "flag" :
109+ item = "id" ;
110+ break ;
111+ }
70112 var r = new R ( ) {
71- ResourceTypeName = path ,
72- Identifier = fileName ,
113+ ResourceTypeName = item ,
114+ Identifier = id ,
73115 Id = - 1 ,
74116 } ;
75- resources [ path ] . Add ( r ) ;
117+ if ( ! resources . ContainsKey ( item ) ) {
118+ Log . LogDebugMessage ( $ "Ignoring path:{ item } r:{ r } ") ;
119+ return ;
120+ }
121+ Log . LogDebugMessage ( $ "Adding 5 path:{ item } r:{ r } ") ;
122+ resources [ item ] . Add ( r ) ;
76123 }
77124
78125 void ProcessStyleable ( XmlReader reader , Dictionary < string , SortedSet < R > > resources )
79126 {
127+ Log . LogDebugMessage ( $ "{ nameof ( ProcessStyleable ) } ") ;
80128 string topName = null ;
81129 int fieldCount = 0 ;
82130 List < R > fields = new List < R > ( ) ;
83131 List < string > attribs = new List < string > ( ) ;
132+ if ( reader . HasAttributes ) {
133+ while ( reader . MoveToNextAttribute ( ) ) {
134+ if ( reader . Name . Replace ( "android:" , "" ) == "name" )
135+ topName = reader . Value ;
136+ Log . LogDebugMessage ( $ "found top:{ topName } ") ;
137+ }
138+ }
84139 while ( reader . Read ( ) ) {
85140 if ( reader . NodeType == XmlNodeType . Whitespace || reader . NodeType == XmlNodeType . Comment )
86141 continue ;
@@ -90,10 +145,11 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
90145 while ( reader . MoveToNextAttribute ( ) ) {
91146 if ( reader . Name . Replace ( "android:" , "" ) == "name" )
92147 topName = reader . Value ;
148+ Log . LogDebugMessage ( $ "found { topName } ") ;
93149 }
94150 }
95151 }
96- if ( ! reader . IsStartElement ( ) || reader . LocalName == "declare-styleable" )
152+ if ( ! reader . IsStartElement ( ) )
97153 continue ;
98154 if ( reader . HasAttributes ) {
99155 while ( reader . MoveToNextAttribute ( ) ) {
@@ -111,6 +167,7 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
111167 Identifier = name ,
112168 Id = - 1 ,
113169 } ;
170+ Log . LogDebugMessage ( $ "Adding { r } ") ;
114171 resources [ r . ResourceTypeName ] . Add ( r ) ;
115172 }
116173 }
@@ -124,20 +181,22 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
124181 attribs . Sort ( StringComparer . OrdinalIgnoreCase ) ;
125182 for ( int i = 0 ; i < attribs . Count ; i ++ ) {
126183 string name = attribs [ i ] ;
184+ Log . LogDebugMessage ( $ "Checking { name } ") ;
127185 if ( ! name . StartsWith ( "android:" , StringComparison . OrdinalIgnoreCase ) ) {
128186 var r = new R ( ) {
129- ResourceTypeName = "attrib " ,
130- Identifier = name ,
187+ ResourceTypeName = "attr " ,
188+ Identifier = $ " { name } " ,
131189 Id = - 1 ,
132190 } ;
191+ Log . LogDebugMessage ( $ "Adding { r } ") ;
133192 resources [ r . ResourceTypeName ] . Add ( r ) ;
134193 fields . Add ( r ) ;
135194 } else {
136195 // this is an android:xxx resource, we should not calculate the id
137196 // we should get it from "somewhere" maybe the pubic.xml
138197 var r = new R ( ) {
139- ResourceTypeName = "attrib " ,
140- Identifier = name ,
198+ ResourceTypeName = "attr " ,
199+ Identifier = $ " { name } " ,
141200 Id = 0 ,
142201 } ;
143202 fields . Add ( r ) ;
@@ -146,17 +205,31 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
146205 if ( field . Type != RType . Array )
147206 return ;
148207 arrayMapping . Add ( field , fields . ToArray ( ) ) ;
208+ field . Ids = new int [ fields . Count ] ;
209+ resources [ field . ResourceTypeName ] . Add ( field ) ;
210+ foreach ( R r in fields ) {
211+ resources [ field . ResourceTypeName ] . Add ( new R ( ) {
212+ ResourceTypeName = field . ResourceTypeName ,
213+ Identifier = $ "{ field . Identifier } _{ r . Identifier . Replace ( ":" , "_" ) } ",
214+ Id = 0 ,
215+ } ) ;
216+ }
149217 }
150218 }
151219
152220 void ProcessXmlFile ( string file , Dictionary < string , SortedSet < R > > resources )
153221 {
222+ Log . LogDebugMessage ( $ "{ nameof ( ProcessXmlFile ) } ") ;
154223 using ( var reader = XmlReader . Create ( file ) ) {
155224 while ( reader . Read ( ) ) {
156225 if ( reader . NodeType == XmlNodeType . Whitespace || reader . NodeType == XmlNodeType . Comment )
157226 continue ;
158227 if ( reader . IsStartElement ( ) ) {
159228 var elementName = reader . Name ;
229+ if ( elementName == "declare-styleable" || elementName == "configVarying" || elementName == "add-resource" ) {
230+ ProcessStyleable ( reader , resources ) ;
231+ continue ;
232+ }
160233 if ( reader . HasAttributes ) {
161234 string name = null ;
162235 string type = null ;
@@ -186,17 +259,20 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
186259 Identifier = inflateId ,
187260 Id = - 1 ,
188261 } ;
262+ Log . LogDebugMessage ( $ "Adding 1 { r } ") ;
189263 resources [ r . ResourceTypeName ] . Add ( r ) ;
190264 }
191265 }
266+ Log . LogDebugMessage ( $ "DEBUG! name:{ name } id:{ id } type:{ type } elementName:{ elementName } ") ;
192267 if ( name ? . Contains ( "android:" ) ?? false )
193268 continue ;
194269 if ( id ? . Contains ( "android:" ) ?? false )
195270 continue ;
196271 // Move the reader back to the element node.
197272 reader . MoveToElement ( ) ;
198- //if (!string.IsNullOrEmpty (name))
199- //CreateResourceField (type ?? elementName, name, reader.ReadSubtree ());
273+ if ( ! string . IsNullOrEmpty ( name ) ) {
274+ CreateResourceField ( type ?? elementName , name , resources ) ;
275+ }
200276 //if (!string.IsNullOrEmpty (custom_id) && !custom_types.TryGetValue (custom_id, out customClass)) {
201277 //customClass = CreateClass (custom_id);
202278 //custom_types.Add (custom_id, customClass);
@@ -208,6 +284,7 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
208284 Identifier = id ,
209285 Id = - 1 ,
210286 } ;
287+ Log . LogDebugMessage ( $ "Adding 2 { r } ") ;
211288 resources [ r . ResourceTypeName ] . Add ( r ) ;
212289 }
213290 }
0 commit comments