@@ -16,12 +16,20 @@ namespace Xamarin.Android.Tasks
16
16
{
17
17
class FileResourceParser : ResourceParser
18
18
{
19
+ public string JavaPlatformDirectory { get ; set ; }
20
+
21
+ public string ResourceFlagFile { get ; set ; }
22
+
19
23
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 )
21
25
{
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 > ( ) ;
24
28
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
+ }
25
33
foreach ( var dir in Directory . EnumerateDirectories ( resourceDirectory , "*" , SearchOption . TopDirectoryOnly ) ) {
26
34
foreach ( var file in Directory . EnumerateFiles ( dir , "*.*" , SearchOption . AllDirectories ) ) {
27
35
ProcessResourceFile ( file , resources ) ;
@@ -39,11 +47,25 @@ public Dictionary<string, R> Parse (string resourceDirectory, IEnumerable<string
39
47
}
40
48
}
41
49
}
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
+ }
42
56
return result ;
43
57
}
44
58
59
+ HashSet < string > resourceNamesToUseDirectly = new HashSet < string > ( ) {
60
+ "integer-array" ,
61
+ "string-array" ,
62
+ "declare-styleable" ,
63
+ "add-resource" ,
64
+ } ;
65
+
45
66
void ProcessResourceFile ( string file , Dictionary < string , SortedSet < R > > resources )
46
67
{
68
+ Log . LogDebugMessage ( $ "{ nameof ( ProcessResourceFile ) } { file } ") ;
47
69
var fileName = Path . GetFileNameWithoutExtension ( file ) ;
48
70
if ( string . IsNullOrEmpty ( fileName ) )
49
71
return ;
@@ -65,22 +87,55 @@ void ProcessResourceFile (string file, Dictionary<string, SortedSet<R>> resource
65
87
default :
66
88
break ;
67
89
}
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
+ }
70
112
var r = new R ( ) {
71
- ResourceTypeName = path ,
72
- Identifier = fileName ,
113
+ ResourceTypeName = item ,
114
+ Identifier = id ,
73
115
Id = - 1 ,
74
116
} ;
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 ) ;
76
123
}
77
124
78
125
void ProcessStyleable ( XmlReader reader , Dictionary < string , SortedSet < R > > resources )
79
126
{
127
+ Log . LogDebugMessage ( $ "{ nameof ( ProcessStyleable ) } ") ;
80
128
string topName = null ;
81
129
int fieldCount = 0 ;
82
130
List < R > fields = new List < R > ( ) ;
83
131
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
+ }
84
139
while ( reader . Read ( ) ) {
85
140
if ( reader . NodeType == XmlNodeType . Whitespace || reader . NodeType == XmlNodeType . Comment )
86
141
continue ;
@@ -90,10 +145,11 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
90
145
while ( reader . MoveToNextAttribute ( ) ) {
91
146
if ( reader . Name . Replace ( "android:" , "" ) == "name" )
92
147
topName = reader . Value ;
148
+ Log . LogDebugMessage ( $ "found { topName } ") ;
93
149
}
94
150
}
95
151
}
96
- if ( ! reader . IsStartElement ( ) || reader . LocalName == "declare-styleable" )
152
+ if ( ! reader . IsStartElement ( ) )
97
153
continue ;
98
154
if ( reader . HasAttributes ) {
99
155
while ( reader . MoveToNextAttribute ( ) ) {
@@ -111,6 +167,7 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
111
167
Identifier = name ,
112
168
Id = - 1 ,
113
169
} ;
170
+ Log . LogDebugMessage ( $ "Adding { r } ") ;
114
171
resources [ r . ResourceTypeName ] . Add ( r ) ;
115
172
}
116
173
}
@@ -124,20 +181,22 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
124
181
attribs . Sort ( StringComparer . OrdinalIgnoreCase ) ;
125
182
for ( int i = 0 ; i < attribs . Count ; i ++ ) {
126
183
string name = attribs [ i ] ;
184
+ Log . LogDebugMessage ( $ "Checking { name } ") ;
127
185
if ( ! name . StartsWith ( "android:" , StringComparison . OrdinalIgnoreCase ) ) {
128
186
var r = new R ( ) {
129
- ResourceTypeName = "attrib " ,
130
- Identifier = name ,
187
+ ResourceTypeName = "attr " ,
188
+ Identifier = $ " { name } " ,
131
189
Id = - 1 ,
132
190
} ;
191
+ Log . LogDebugMessage ( $ "Adding { r } ") ;
133
192
resources [ r . ResourceTypeName ] . Add ( r ) ;
134
193
fields . Add ( r ) ;
135
194
} else {
136
195
// this is an android:xxx resource, we should not calculate the id
137
196
// we should get it from "somewhere" maybe the pubic.xml
138
197
var r = new R ( ) {
139
- ResourceTypeName = "attrib " ,
140
- Identifier = name ,
198
+ ResourceTypeName = "attr " ,
199
+ Identifier = $ " { name } " ,
141
200
Id = 0 ,
142
201
} ;
143
202
fields . Add ( r ) ;
@@ -146,17 +205,31 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, SortedSet<R>> resour
146
205
if ( field . Type != RType . Array )
147
206
return ;
148
207
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
+ }
149
217
}
150
218
}
151
219
152
220
void ProcessXmlFile ( string file , Dictionary < string , SortedSet < R > > resources )
153
221
{
222
+ Log . LogDebugMessage ( $ "{ nameof ( ProcessXmlFile ) } ") ;
154
223
using ( var reader = XmlReader . Create ( file ) ) {
155
224
while ( reader . Read ( ) ) {
156
225
if ( reader . NodeType == XmlNodeType . Whitespace || reader . NodeType == XmlNodeType . Comment )
157
226
continue ;
158
227
if ( reader . IsStartElement ( ) ) {
159
228
var elementName = reader . Name ;
229
+ if ( elementName == "declare-styleable" || elementName == "configVarying" || elementName == "add-resource" ) {
230
+ ProcessStyleable ( reader , resources ) ;
231
+ continue ;
232
+ }
160
233
if ( reader . HasAttributes ) {
161
234
string name = null ;
162
235
string type = null ;
@@ -186,17 +259,20 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
186
259
Identifier = inflateId ,
187
260
Id = - 1 ,
188
261
} ;
262
+ Log . LogDebugMessage ( $ "Adding 1 { r } ") ;
189
263
resources [ r . ResourceTypeName ] . Add ( r ) ;
190
264
}
191
265
}
266
+ Log . LogDebugMessage ( $ "DEBUG! name:{ name } id:{ id } type:{ type } elementName:{ elementName } ") ;
192
267
if ( name ? . Contains ( "android:" ) ?? false )
193
268
continue ;
194
269
if ( id ? . Contains ( "android:" ) ?? false )
195
270
continue ;
196
271
// Move the reader back to the element node.
197
272
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
+ }
200
276
//if (!string.IsNullOrEmpty (custom_id) && !custom_types.TryGetValue (custom_id, out customClass)) {
201
277
//customClass = CreateClass (custom_id);
202
278
//custom_types.Add (custom_id, customClass);
@@ -208,6 +284,7 @@ void ProcessXmlFile (string file, Dictionary<string, SortedSet<R>> resources)
208
284
Identifier = id ,
209
285
Id = - 1 ,
210
286
} ;
287
+ Log . LogDebugMessage ( $ "Adding 2 { r } ") ;
211
288
resources [ r . ResourceTypeName ] . Add ( r ) ;
212
289
}
213
290
}
0 commit comments