-
Notifications
You must be signed in to change notification settings - Fork 5
/
CPManagedObjectModel+XCDataModel.j
364 lines (292 loc) · 15.3 KB
/
CPManagedObjectModel+XCDataModel.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// CPManagedObjectModel+XCDataModel.j
//
// Created by Raphael Bartolome on 23.11.09.
//
@import <Foundation/Foundation.j>
@import "CPManagedObjectModel.j"
@implementation CPManagedObjectModel (XCDataModel)
+ (void)modelWithContentsOfURL:(CPString)aPath completionHandler:(Function/*(CPManagedObjectModel)*/)completionBlock
{
var request = [CPURLRequest requestWithURL:[self modelFilePathFromModelPath:aPath]];
[CPURLConnection sendAsynchronousRequest:request queue:[CPOperationQueue mainQueue] completionHandler:function(response, result, error) {
var managedObjectModel = [self objectModelFromXMLData:[CPData dataWithRawString:result]];
[managedObjectModel setNameFromFilePath:aPath];
if (completionBlock) completionBlock(managedObjectModel);
}];
}
+ (id)parseCoreDataModel:(CPString)aModelName
{
var modelPath = [self modelFilePathFromModelPath:aModelName],
data = [CPURLConnection sendSynchronousRequest: [CPURLRequest requestWithURL:modelPath] returningResponse:nil];
if (data == nil)
{
console.log("Can't find model at path '" + modelPath + "'");
return nil;
} else
{
var managedObjectModel = [self objectModelFromXMLData:data];
[managedObjectModel setNameFromFilePath:modelPath];
return managedObjectModel;
}
}
+ (id)objectModelFromXMLData:(CPData)modelData withName:(CPString)aName
{
var managedObjectModel = [self objectModelFromXMLData:data];
[managedObjectModel setName:aName];
return managedObjectModel;
}
+ (id)objectModelFromXMLData:(CPData)modelData
{
return [self objectModelFromXMLString:[modelData rawString]];
}
+ (id)objectModelFromXMLString:(CPString)modelString
{
var plistContents = modelString.replace(/\<key\>\s*CF\$UID\s*\<\/key\>/g, "<key>CP$UID</key>");
var unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:[CPData dataWithRawString:plistContents]];
var managedObjectModel = [unarchiver decodeObjectForKey:@"root"];
if (!managedObjectModel) {
managedObjectModel = modelFromXML(modelString);
}
return managedObjectModel;
}
+ (CPString)modelFilePathFromModelPath:(CPString)modelPath
{
var pathExtension = [modelPath pathExtension];
if(pathExtension === @"xcdatamodeld")
{
var lastPathComponent = [modelPath lastPathComponent];
lastPathComponent = [lastPathComponent stringByDeletingPathExtension];
modelPath = [[[modelPath stringByAppendingPathComponent:lastPathComponent] stringByAppendingPathExtension:@"xcdatamodel"] stringByAppendingPathComponent:@"contents"];
}
else if (pathExtension === @"xcdatamodel")
{
modelPath = [modelPath stringByAppendingPathComponent:@"contents"];
}
return modelPath;
}
@end
var XML_XML = "xml",
XML_DOCUMENT = "#document",
MODEL_MODEL = "model",
MODEL_ENTITY = "entity",
MODEL_ATTRIBUTE = "attribute",
MODEL_RELATIONSHIP = "relationship",
MODEL_USERINFO = "userInfo",
MODEL_ENTRY = "entry",
MODEL_ELEMENTS = "elements",
MODEL_ELEMENT = "element",
PLIST_KEY = "key",
PLIST_DICTIONARY = "dict",
PLIST_ARRAY = "array",
PLIST_STRING = "string",
PLIST_DATE = "date",
PLIST_BOOLEAN_TRUE = "true",
PLIST_BOOLEAN_FALSE = "false",
PLIST_NUMBER_REAL = "real",
PLIST_NUMBER_INTEGER = "integer",
PLIST_DATA = "data";
#define NODE_NAME(anXMLNode) (String(anXMLNode.nodeName))
#define NODE_TYPE(anXMLNode) (anXMLNode.nodeType)
#define TEXT_CONTENT(anXMLNode) (anXMLNode.textContent || (anXMLNode.textContent !== "" && textContent([anXMLNode])))
#define FIRST_CHILD(anXMLNode) (anXMLNode.firstChild)
#define NEXT_SIBLING(anXMLNode) (anXMLNode.nextSibling)
#define PARENT_NODE(anXMLNode) (anXMLNode.parentNode)
#define DOCUMENT_ELEMENT(aDocument) (aDocument.documentElement)
#define ATTRIBUTE_VALUE(anXMLNode, anAttributeName) (anXMLNode.getAttribute(anAttributeName))
#define HAS_ATTRIBUTE_VALUE(anXMLNode, anAttributeName, aValue) (ATTRIBUTE_VALUE(anXMLNode, anAttributeName) === aValue)
#define IS_OF_TYPE(anXMLNode, aType) (NODE_NAME(anXMLNode) === aType)
#define IS_MODEL(anXMLNode) IS_OF_TYPE(anXMLNode, MODEL_MODEL)
#define IS_WHITESPACE(anXMLNode) (NODE_TYPE(anXMLNode) === 8 || NODE_TYPE(anXMLNode) === 3)
#define IS_DOCUMENTTYPE(anXMLNode) (NODE_TYPE(anXMLNode) === 10)
#define PLIST_NEXT_SIBLING(anXMLNode) while ((anXMLNode = NEXT_SIBLING(anXMLNode)) && IS_WHITESPACE(anXMLNode));
#define PLIST_FIRST_CHILD(anXMLNode) { anXMLNode = FIRST_CHILD(anXMLNode); if (anXMLNode !== NULL && IS_WHITESPACE(anXMLNode)) PLIST_NEXT_SIBLING(anXMLNode) }
var textContent = function(nodes)
{
var text = "",
index = 0,
count = nodes.length;
for (; index < count; ++index)
{
var node = nodes[index];
if (node.nodeType === 3 || node.nodeType === 4)
text += node.nodeValue;
else if (node.nodeType !== 8)
text += textContent(node.childNodes);
}
return text;
}
var _plist_traverseNextNode = function(anXMLNode, stayWithin, stack)
{
var node = anXMLNode;
/// PLIST_FIRST_CHILD(node);
node = node.firstChild;
if (node !== NULL && (node.nodeType === 8 || node.nodeType === 3))
while ((node = node.nextSibling) && (node.nodeType === 8 || node.nodeType === 3));
// If this element has a child, traverse to it.
if (node)
return node;
// If not, first check if it is a container class (as opposed to a designated leaf).
// If it is, then we have to pop this container off the stack, since it is empty.
/// if (NODE_NAME(anXMLNode) === PLIST_ARRAY || NODE_NAME(anXMLNode) === PLIST_DICTIONARY)
if (anXMLNode.nodeName == PLIST_ARRAY || anXMLNode.nodeName == PLIST_DICTIONARY)
stack.pop();
// If not, next check whether it has a sibling.
else
{
if (node === stayWithin)
return NULL;
node = anXMLNode;
/// PLIST_NEXT_SIBLING(node);
while ((node = node.nextSibling) && (node.nodeType === 8 || node.nodeType === 3));
if (node)
return node;
}
// If it doesn't, start working our way back up the node tree.
node = anXMLNode;
// While we have a node and it doesn't have a sibling (and we're within our stayWithin),
// keep moving up.
while (node)
{
var next = node;
/// PLIST_NEXT_SIBLING(next);
while ((next = next.nextSibling) && (next.nodeType === 8 || next.nodeType === 3));
// If we have a next sibling, just go to it.
if (next)
return next;
/// var node = PARENT_NODE(node);
var node = node.parentNode;
// If we are being asked to move up, and our parent is the stay within, then just
if (stayWithin && node === stayWithin)
return NULL;
// Pop the stack if we have officially "moved up"
stack.pop();
}
return NULL;
}
//function encodeHTMLComponent(/*String*/ aString)
//{
// return aString.replace(/&/g,'&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g,'<').replace(/>/g,'>');
//}
var decodeHTMLComponent = function(/*String*/ aString)
{
return aString.replace(/"/g, '"').replace(/'/g, '\'').replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
}
var parseXML = function(/*String*/ aString)
{
if (window.DOMParser)
/// return DOCUMENT_ELEMENT(new window.DOMParser().parseFromString(aString, "text/xml"));
return new window.DOMParser().parseFromString(aString, "text/xml").documentElement;
else if (window.ActiveXObject)
{
XMLNode = new ActiveXObject("Microsoft.XMLDOM");
// Extract the DTD, which confuses IE.
var matches = aString.match(CFPropertyList.DTDRE);
if (matches)
aString = aString.substr(matches[0].length);
XMLNode.loadXML(aString);
return XMLNode
}
return NULL;
}
var modelFromXML = function(/*String | XMLNode*/ aStringOrXMLNode)
{
var XMLNode = aStringOrXMLNode;
if (aStringOrXMLNode.valueOf && typeof aStringOrXMLNode.valueOf() === "string")
XMLNode = parseXML(aStringOrXMLNode);
// Skip over DOCTYPE and so forth.
/// while (IS_OF_TYPE(XMLNode, XML_DOCUMENT) || IS_OF_TYPE(XMLNode, XML_XML))
/// PLIST_FIRST_CHILD(XMLNode);
while (String(XMLNode.nodeName) === XML_DOCUMENT || String(XMLNode.nodeName) === XML_XML)
{
XMLNode = XMLNode.firstChild;
if (XMLNode !== NULL && (XMLNode.nodeType === 8 || XMLNode.nodeType === 3))
while ((XMLNode = XMLNode.nextSibling) && (XMLNode.nodeType === 8 || XMLNode.nodeType === 3));
}
// Skip over the DOCTYPE... see a pattern?
/// if (IS_DOCUMENTTYPE(XMLNode))
if (XMLNode.nodeType === 10)
/// PLIST_NEXT_SIBLING(XMLNode);
while ((XMLNode = XMLNode.nextSibling) && (XMLNode.nodeType === 8 || XMLNode.nodeType === 3));
// If this is not a PLIST, bail.
/// if (!IS_MODEL(XMLNode))
if (XMLNode.nodeName != MODEL_MODEL)
return NULL;
var key = "",
object = NULL,
modelObject = [[CPManagedObjectModel alloc] init],
modelNode = XMLNode,
containers = [],
currentContainer = NULL;
while (XMLNode = _plist_traverseNextNode(XMLNode, modelNode, containers))
{
var count = containers.length;
//console.log(new Array(count + 1).join(" ") + NODE_NAME(XMLNode));
if (count)
currentContainer = containers[count - 1];
/// switch (NODE_NAME(XMLNode))
switch (String(XMLNode.nodeName))
{
case MODEL_ENTITY: object = [[CPEntityDescription alloc] init];
/// [object setName:ATTRIBUTE_VALUE(XMLNode, "name")];
[object setName:XMLNode.getAttribute("name")];
/// [object setExternalName:ATTRIBUTE_VALUE(XMLNode, "representedClassName") || @"CPMutableDictionary"];
[object setExternalName:XMLNode.getAttribute("representedClassName") || @"CPMutableDictionary"];
/// [object setAbstract:ATTRIBUTE_VALUE(XMLNode, "isAbstract") === 'YES'];
[object setAbstract:XMLNode.getAttribute("isAbstract") === 'YES'];
[modelObject addEntity:object];
/// if (FIRST_CHILD(XMLNode)) containers.push(object);
if (XMLNode.firstChild) containers.push(object);
break;
case MODEL_ATTRIBUTE: object = [[CPAttributeDescription alloc] init];
/// [object setName:ATTRIBUTE_VALUE(XMLNode, "name")];
[object setName:XMLNode.getAttribute("name")];
/// [object setOptional:ATTRIBUTE_VALUE(XMLNode, "optional") === 'YES'];
[object setOptional:XMLNode.getAttribute("optional") === 'YES'];
/// [object setTransient:ATTRIBUTE_VALUE(XMLNode, "transient") === 'YES'];
[object setTransient:XMLNode.getAttribute("transient") === 'YES'];
/// var typeValueString = ATTRIBUTE_VALUE(XMLNode, "attributeType").replace(/\s+/g, ''); // Remove spaces
var typeValueString = XMLNode.getAttribute("attributeType").replace(/\s+/g, ''); // Remove spaces
if (typeValueString === "Binary") typeValueString = "BinaryData";
var typeValue = global["CPD" + typeValueString + "AttributeType"];
//console.log("ValueType for " + ATTRIBUTE_VALUE(XMLNode, "attributeType") + " : " + typeValue);
[object setTypeValue:typeValue || CPDUndefinedAttributeType];
/// [object setDefaultValue:ATTRIBUTE_VALUE(XMLNode, "defaultValueString")];
[object setDefaultValue:XMLNode.getAttribute("defaultValueString")];
[currentContainer addProperty:object];
[object setEntity:currentContainer];
/// if (FIRST_CHILD(XMLNode)) containers.push(object);
if (XMLNode.firstChild) containers.push(object);
break;
case MODEL_RELATIONSHIP: object = [[CPRelationshipDescription alloc] init];
/// [object setName:ATTRIBUTE_VALUE(XMLNode, "name")];
[object setName:XMLNode.getAttribute("name")];
/// [object setOptional:ATTRIBUTE_VALUE(XMLNode, "optional") === 'YES'];
[object setOptional:XMLNode.getAttribute("optional") === 'YES'];
/// [object setIsToMany:ATTRIBUTE_VALUE(XMLNode, "toMany") === 'YES'];
[object setIsToMany:XMLNode.getAttribute("toMany") === 'YES'];
/// [object setDestinationEntityName: ATTRIBUTE_VALUE(XMLNode, "destinationEntity")];
[object setDestinationEntityName: XMLNode.getAttribute("destinationEntity")];
/// [object setInversePropertyName: ATTRIBUTE_VALUE(XMLNode, "inverseName")];
[object setInversePropertyName: XMLNode.getAttribute("inverseName")];
[currentContainer addProperty:object];
[object setEntity:currentContainer];
/// if (FIRST_CHILD(XMLNode)) containers.push(object);
if (XMLNode.firstChild) containers.push(object);
break;
case MODEL_USERINFO: object = new CFMutableDictionary();
[currentContainer setUserInfo:object];
/// if (FIRST_CHILD(XMLNode)) containers.push(object);
if (XMLNode.firstChild) containers.push(object);
break;
/// case MODEL_ENTRY: currentContainer.setValueForKey(ATTRIBUTE_VALUE(XMLNode, "key"), ATTRIBUTE_VALUE(XMLNode, "value"));
case MODEL_ENTRY: currentContainer.setValueForKey(XMLNode.getAttribute("key"), XMLNode.getAttribute("value"));
break;
case MODEL_ELEMENTS: break;
case MODEL_ELEMENT: break;
/// default: throw new Error("*** '" + NODE_NAME(XMLNode) + "' tag not recognized in model file.");
default: throw new Error("*** '" + XMLNode.nodeName + "' tag not recognized in model file.");
}
}
return modelObject;
}