-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpalXMLResourceParser.m
218 lines (192 loc) · 6.67 KB
/
OpalXMLResourceParser.m
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
//
// OpalXMLResourceParser.m
// Opal
//
// Created by Christian Niles on 5/22/10.
// Copyright 2010 Christian Niles. All rights reserved.
//
#import "OpalXMLResourceParser.h"
#import "OpalXMLScanner.h"
#import "OpalXMLParser.h"
#import "OpalXMLEvent.h"
#import "RegexKitLite.h"
@interface OpalXMLResourceParser ()
+ (NSDateFormatter *)dateFormatter;
@end
@implementation OpalXMLResourceParser
+ (NSDictionary *)parseResourceFromString:(NSString *)resourceXML
{
OpalXMLParser *parser = [OpalXMLParser parserWithString:resourceXML];
return [self parseResource:parser];
}
+ (NSDictionary *)parseResource:(OpalXMLParser *)parser
{
OpalXMLEvent *event = [parser nextStartTag];
if (event != nil) {
id value = [self parseResourceValue:parser];
return [NSDictionary dictionaryWithObject:value forKey:event.tagName];
} else {
return [NSDictionary dictionary];
}
}
+ (id)parseResourceValue:(OpalXMLParser *)parser
{
OpalXMLEvent *startTag = [parser currentEvent];
if (![startTag isStartTag]) return [NSNull null];
id value;
NSString *type = [startTag valueForAttribute:@"type"];
if (type && [type caseInsensitiveCompare:@"integer"] == NSOrderedSame) {
value = [self parseIntegerValue:parser];
} else if (type && [type caseInsensitiveCompare:@"datetime"] == NSOrderedSame) {
value = [self parseDateValue:parser];
} else if (type && [type caseInsensitiveCompare:@"array"] == NSOrderedSame) {
value = [self parseArrayValue:parser];
} else {
value = [self parseUntypedValue:parser];
}
if (value == nil) value = [NSNull null];
return value;
}
+ (NSNumber *)parseIntegerValue:(OpalXMLParser *)parser
{
// get the start tag's text value, then convert it to an integer
NSMutableString *content = [parser readElementText];
NSString *numberText = [content stringByMatching:@"\\d+"];
if (numberText) {
long long integer = [numberText longLongValue];
return [NSNumber numberWithLongLong:integer];
} else {
return nil;
}
}
+ (NSDate *)parseDateValue:(OpalXMLParser *)parser
{
NSString *content = [parser readElementText];
content = [content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// DateFormatter doesn't seem to like 'UTC'
content = [content stringByReplacingOccurrencesOfString:@"UTC" withString:@"GMT"];
return [[self dateFormatter] dateFromString:content];
}
+ (NSDateFormatter *)dateFormatter
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy/M/d HH:mm:ss zzz"];
return formatter;
}
+ (NSMutableArray *)parseArrayValue:(OpalXMLParser *)parser
{
// parse each item into an array
NSMutableArray *arrayContent = [NSMutableArray arrayWithCapacity:1];
OpalXMLEvent *item;
do {
item = [parser nextContentEvent];
if ([item isStartTag]) {
[arrayContent addObject:[self parseResourceValue:parser]];
}
} while (![item isEndTag]);
return arrayContent;
}
+ (id)parseUntypedValue:(OpalXMLParser *)parser
{
// examine the content, look for simple or complex content
OpalXMLEvent *contentEvent = [parser nextContentEvent];
if ([contentEvent isEndTag]) {
return nil;
} else if ([contentEvent isText]) {
NSString *simpleContent;
if ([[parser peek] isEndTag]) {
simpleContent = [contentEvent content];
[parser nextEvent];
} else {
// multiple values?
NSMutableString *mutableContent = [parser readElementText];
[mutableContent insertString:[contentEvent content] atIndex:0];
simpleContent = mutableContent;
}
return [simpleContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else {
// start tag
NSMutableDictionary *complexContent = [NSMutableDictionary dictionaryWithCapacity:1];
do {
NSString *fieldName = [contentEvent tagName];
id fieldValue = [self parseResourceValue:parser];
id existingValue = [complexContent objectForKey:fieldName];
if (existingValue == nil) {
// this is the first value, so create the key
[complexContent setObject:fieldValue forKey:[contentEvent tagName]];
} else if ([existingValue isKindOfClass:[NSMutableArray class]]) {
// this is the 3+ value, so append to the existing array
[existingValue addObject:fieldValue];
} else {
// this is the second value, so create an array
NSMutableArray *multipleValues = [NSMutableArray arrayWithCapacity:2];
[multipleValues addObject:existingValue];
[multipleValues addObject:fieldValue];
[complexContent setObject:multipleValues forKey:fieldName];
}
contentEvent = [parser nextContentEvent];
} while (![contentEvent isEndTag]);
return complexContent;
}
}
#pragma mark -
#pragma mark XML Serialization
+ (void)writeResource:(id)resource
toXMLString:(NSMutableString *)xmlString
{
if (resource == nil || resource == [NSNull null]) { return; }
if ([resource isKindOfClass:[NSString class]]) {
[xmlString appendString:[OpalXMLScanner escapeString:resource]];
} else if ([resource isKindOfClass:[NSDictionary class]]) {
[self writeResourceData:(NSDictionary *)resource
toXMLString:xmlString];
} else if ([resource isKindOfClass:[NSArray class]]) {
[self writeResources:[(NSArray *)resource objectEnumerator]
toXMLString:xmlString];
} else if ([resource respondsToSelector:@selector(stringValue)]) {
[xmlString appendString:[resource stringValue]];
} else {
[xmlString appendString:[OpalXMLScanner escapeString:[resource description]]];
}
}
+ (void)writeResourceData:(NSDictionary *)resourceData
toXMLString:(NSMutableString *)xmlString
{
NSEnumerator *keyEnumerator = [resourceData keyEnumerator];
for (NSString *key in keyEnumerator) {
id resourceValue = [resourceData objectForKey:key];
NSString *typeString = nil;
if ([resourceValue isKindOfClass:[NSArray class]]) {
typeString = @"array";
} else if ([resourceValue isKindOfClass:[NSDate class]]) {
typeString = @"datetime";
} else if ([resourceValue isKindOfClass:[NSNumber class]]) {
NSNumber *number = resourceValue;
const char *valueType = [number objCType];
switch (*valueType) {
case 'f':
case 'd':
typeString = @"float";
break;
default:
typeString = @"integer";
break;
}
}
if (typeString) {
[xmlString appendFormat:@"<%@ type='%@'>", key, typeString];
} else {
[xmlString appendFormat:@"<%@>", key];
}
[self writeResource:resourceValue toXMLString:xmlString];
[xmlString appendFormat:@"</%@>", key];
}
}
+ (void)writeResources:(NSEnumerator *)resourceEnumerator
toXMLString:(NSMutableString *)xmlString
{
for (id resource in resourceEnumerator) {
[self writeResource:resource toXMLString:xmlString];
}
}
@end