-
Notifications
You must be signed in to change notification settings - Fork 10
/
GSMarkupBundleAdditions.j
255 lines (205 loc) · 7.59 KB
/
GSMarkupBundleAdditions.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*-objc-*-
Author: Nicola Pero <[email protected]>
Date: January 2003
Author of Cappuccino port: Daniel Boehringer (2012)
This file is part of GNUstep Renaissance
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
@import <Foundation/CPBundle.j>
@import "GSMarkupDecoder.j"
@import "GSMarkupAwaker.j"
@import "GSMarkupLocalizer.j"
@import "GSMarkupConnector.j"
GSMarkupBundleDidLoadGSMarkupNotification= @"GSMarkupBundleDidLoadGSMarkupNotification";
var staticNameTable;
var __sharedDecoder;
@implementation CPBundle (GSMarkupBundleStaticObjects)
+ sharedGSMarkupDecoder
{
return __sharedDecoder;
}
- (CPString)localizedStringForKey:(CPString )key value:(CPString)value table:(CPString)tableName
{ if(!key || !tableName) return value;
return key;
}
+ (void) registerStaticObject: (id)object
withName: (CPString)itsId
{
if (staticNameTable == nil)
{
staticNameTable = [CPMutableDictionary new];
}
[staticNameTable setObject: object forKey: itsId];
}
+ (id) loadGSMarkupData: (CPData)data
externalNameTable: (CPDictionary)context
localizableStringsTable: (CPString)table
inBundle: (CPBundle)bundle
tagMapping: (CPDictionary)mapping;
{
[self registerStaticObject: [CPApplication sharedApplication] withName: @"CPApp"];
var success = NO;
if (data == nil)
{
return NO;
}
/* If bundle is specified, use it; otherwise, use mainBundle. */
if (bundle == nil)
{
bundle = [CPBundle mainBundle];
}
var nameTable;
var outputTable;
var connectors;
var platformObjects;
var i, count;
var e;
var key;
var topLevelObjects = nil;
var awaker = [GSMarkupAwaker new];
var decoder = __sharedDecoder = [[GSMarkupDecoder alloc] initWithXMLString:[data rawString]];
[decoder setExternalNameTable:context];
console.log("Will parse");
[decoder parse];
console.log("Did parse");
var objects=[decoder objects];
console.log("Will postparse");
if (mapping != nil)
{ e = [mapping keyEnumerator];
while ((key = [e nextObject]) != nil)
{ var value = [mapping objectForKey:key];
[decoder setObjectClass: value forTagName: key];
}
}
platformObjects = [];
var localizer = [[GSMarkupLocalizer alloc] initWithTable:table
bundle:bundle];
nameTable = [[decoder nameTable] mutableCopy];
connectors = [decoder connectors];
count = [objects count];
for (i = 0; i < count; i++)
{ var o;
var platformObject;
o = objects[i];
[o setLocalizer: localizer];
[o setAwaker:awaker];
/* platformObject is autoreleased. */
platformObject = [o platformObject];
if (platformObject)
platformObjects.push(platformObject);
}
/* Now update the nameTable replacing each decoded object with
* its platformObject in the nameTable.
*/
/* Note that we can not use [nameTable keyEnumerator] because we
* will be modifying the nameTable dictionary. So we first get
* an array with all the keys, then we enumerate that one.
*/
e = [[nameTable allKeys] objectEnumerator];
while ((key = [e nextObject]) != nil)
{
var object = [nameTable objectForKey: key];
var platformObject = [object platformObject];
if (platformObject)
[nameTable setObject:platformObject forKey:key];
else
[nameTable removeObjectForKey: key];
}
/* Now extend the nameTable by adding the externalNameTable
* (which contains references to object outside the GSMarkup
* file). */
e = [context keyEnumerator];
while ((key = [e nextObject]) != nil)
{ var object = [context objectForKey:key];
/* CPTopLevelObjects is special ... if it exists, it is a
* key to a mutable array where we store the top-level
* objects so that the caller can access them. Inspired by
* an undocumented feature of nib loading on other
* platforms. */
if (key == "CPTopLevelObjects" && [object isKindOfClass:[CPMutableArray class]])
topLevelObjects = object;
else
[nameTable setObject:object forKey:key];
}
/* Now extend the nameTable adding the static objects (for example,
* NSApp if it's a gui application).
*/
if (staticNameTable != nil)
[nameTable addEntriesFromDictionary: staticNameTable];
/* Now establish the connectors. Our connectors can manage
* the nameTable automatically. */
count = [connectors count];
for (i = 0; i < count; i++)
[connectors[i] establishConnectionUsingNameTable:nameTable];
/* Register the NSOwner, if any, in the list of objects to
* awake. */
var fileOwner = [nameTable objectForKey: @"CPOwner"];
if (fileOwner)
[awaker registerObject:fileOwner];
/* Now awake the objects. */
[awaker awakeObjects];
/* Done - finally send the notification that we loaded the
* file. */
var fileOwner = [nameTable objectForKey:@"CPOwner"];
var objects = [];
var n;
/* Build the array of top-level objects for the
* notification. */
count = [platformObjects count];
for (i = 0; i < count; i++)
objects.push(platformObjects[i]);
/* Create the notification. */
n = [CPNotification
notificationWithName: GSMarkupBundleDidLoadGSMarkupNotification
object: fileOwner
userInfo: [CPDictionary dictionaryWithObject: objects
forKey: @"CPTopLevelObjects"]];
/* Send the notification to the file owner manually. */
if (fileOwner)
if ([fileOwner respondsToSelector:@selector (bundleDidLoadGSMarkup:)])
[fileOwner bundleDidLoadGSMarkup: n];
[[CPNotificationCenter defaultCenter] postNotification: n];
if (topLevelObjects != nil)
{
count = [platformObjects count];
for (i = 0; i < count; i++)
topLevelObjects.push(platformObjects[i]);
}
/*
* Finally, pass back name table contents in the context if possible.
*/
outputTable = [context objectForKey: @"GSMarkupNameTable"];
if (outputTable && [outputTable isKindOfClass: [CPMutableDictionary class]])
{
var k;
[outputTable removeAllObjects];
e = [nameTable keyEnumerator];
while ((k = [e nextObject]) != nil)
{
if ([context objectForKey: k] == nil)
[outputTable setObject: [nameTable objectForKey: k] forKey: k];
}
}
console.log("Did postparse");
success = YES;
return success? decoder:nil;
}
// convenience method
+ (id) loadRessourceNamed: (CPString) fileName owner:(id) anOwner
{ var configData=[[CPData alloc]
initWithContentsOfURL: [CPURL URLWithString:[CPString stringWithFormat:@"%@/%@", [[CPBundle mainBundle] resourcePath], fileName ]]];
return [CPBundle loadGSMarkupData: configData externalNameTable: [CPDictionary dictionaryWithObject: anOwner forKey:"CPOwner"]
localizableStringsTable: nil inBundle: nil tagMapping: nil];
}
@end