-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDataEntryContent.m
266 lines (198 loc) · 6.75 KB
/
GDataEntryContent.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
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
/* Copyright (c) 2007-2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// GDataEntryContent.m
//
#define GDATAENTRYCONTENT_DEFINE_GLOBALS 1
#import "GDataEntryContent.h"
static NSString* const kLangAttr = @"xml:lang";
static NSString* const kTypeAttr = @"type";
static NSString* const kSourceAttr = @"src";
static BOOL IsTypeEqualToText(NSString *str) {
// internal utility routine
return (str == nil)
|| [str isEqual:@"text"]
|| [str hasPrefix:@"text/"]
|| [str isEqual:@"html"]
|| [str isEqual:@"xhtml"];
}
@implementation GDataEntryContent
+ (NSString *)extensionElementURI { return kGDataNamespaceAtom; }
+ (NSString *)extensionElementPrefix { return kGDataNamespaceAtomPrefix; }
+ (NSString *)extensionElementLocalName { return @"content"; }
+ (id)contentWithString:(NSString *)str {
// RFC4287 Sec 4.1.3.1. says that omitted type attributes are assumed to be
// "text", so we don't need to explicitly set it to text
GDataEntryContent *obj = [self object];
[obj setStringValue:str];
return obj;
}
+ (id)contentWithSourceURI:(NSString *)str type:(NSString *)type {
GDataEntryContent *obj = [self object];
[obj setSourceURI:str];
[obj setType:type];
return obj;
}
+ (id)contentWithXMLValue:(NSXMLNode *)node type:(NSString *)type {
GDataEntryContent *obj = [self object];
// declare that we'll be using child elements as XML values
[obj addChildXMLElementsDeclaration];
[obj setType:type];
if (node != nil) {
[obj addXMLValue:node];
}
return obj;
}
+ (id)textConstructWithString:(NSString *)str {
// deprecated; kept for compatibility with the previous
// implementation of GDataEntryContent
GDATA_DEBUG_LOG(@"GDataEntryContent: +textConstructWithString deprecated, use +contentWithString");
return [self contentWithString:str];
}
- (void)addParseDeclarations {
NSArray *attrs = [NSArray arrayWithObjects:
kTypeAttr, kLangAttr, kSourceAttr, nil];
[self addLocalAttributeDeclarations:attrs];
// we're not calling -addContentValueDeclaration since the content may not
// be plain text but rather XML for an entry or feed that we will parse
}
- (NSArray *)attributesIgnoredForEquality {
// ignore the "type" attribute since we test for it uniquely below
return [NSArray arrayWithObject:kTypeAttr];
}
- (void)parseAttributesForElement:(NSXMLElement *)element {
// override the attribute parsing method
//
// once we have parsed the attributes, we can decide
// how to parse the contents or children
[super parseAttributesForElement:element];
NSString *type = [self type];
if (IsTypeEqualToText(type)) {
// content is plain text
[self addContentValueDeclaration];
} else if ([type hasPrefix:@"application/atom+xml;"]) {
// content is a feed or entry stored in unknownChildren
GDataObject *obj = [self objectForChildOfElement:element
qualifiedName:@"*"
namespaceURI:@"*"
objectClass:nil];
[self setChildObject:obj];
} else if ([type hasPrefix:kGDataContentTypeKML]) {
// content is KML
[self addChildXMLElementsDeclaration];
}
}
- (void)dealloc {
[childObject_ release];
[super dealloc];
}
- (NSXMLElement *)XMLElement {
NSXMLElement *element = [self XMLElementWithExtensionsAndDefaultName:nil];
GDataObject *obj = [self childObject];
if (obj) {
NSXMLElement *elem = [obj XMLElement];
[element addChild:elem];
}
return element;
}
- (id)copyWithZone:(NSZone *)zone {
GDataEntryContent* newObj = [super copyWithZone:zone];
[newObj setChildObject:[[[self childObject] copy] autorelease]];
return newObj;
}
- (BOOL)isEqual:(GDataEntryContent *)other {
// override isEqual: to allow nil types to be considered equal to "text"
return [super isEqual:other]
// a missing type attribute is equal to "text" per RFC 4287 3.1.1
//
// consider them equal if both are some flavor of "text"
&& (AreEqualOrBothNil([self type], [other type])
|| (IsTypeEqualToText([self type]) && IsTypeEqualToText([other type])))
&& AreEqualOrBothNil([self childObject], [other childObject]);
}
#if !GDATA_SIMPLE_DESCRIPTIONS
- (NSMutableArray *)itemsForDescription {
NSMutableArray *items = [super itemsForDescription];
// if the base class is not storing the content value, we must provide the
// description item here
if (![self hasDeclaredContentValue]) {
[self addToArray:items objectDescriptionIfNonNil:[self stringValue] withName:@"content"];
}
[self addToArray:items objectDescriptionIfNonNil:[self XMLValues] withName:@"xml"];
GDataObject *obj = [self childObject];
if (obj != nil) {
NSString *className = NSStringFromClass([obj class]);
[self addToArray:items objectDescriptionIfNonNil:className withName:@"childObject"];
}
return items;
}
#endif
#pragma mark -
- (NSString *)lang {
return [self stringValueForAttribute:kLangAttr];
}
- (void)setLang:(NSString *)str {
[self setStringValue:str forAttribute:kLangAttr];
}
- (NSString *)type {
return [self stringValueForAttribute:kTypeAttr];
}
- (NSString *)sourceURI {
return [self stringValueForAttribute:kSourceAttr];
}
- (void)setSourceURI:(NSString *)str {
[self setStringValue:str forAttribute:kSourceAttr];
}
- (NSURL *)sourceURL {
NSString *sourceURI = [self sourceURI];
if ([sourceURI length] > 0) {
NSURL *url = [NSURL URLWithString:sourceURI];
return url;
}
return nil;
}
- (void)setType:(NSString *)str {
[self setStringValue:str forAttribute:kTypeAttr];
}
- (NSString *)stringValue {
if ([self hasDeclaredContentValue]) {
return [self contentStringValue];
}
return nil;
}
- (void)setStringValue:(NSString *)str {
if (![self hasDeclaredContentValue]) {
// if we emit XML later on, we'll want to emit this string
[self addContentValueDeclaration];
}
[self setContentStringValue:str];
}
- (GDataObject *)childObject {
return childObject_;
}
- (void)setChildObject:(GDataObject *)obj {
[childObject_ autorelease];
childObject_ = [obj retain];
}
- (NSArray *)XMLValues {
return [super childXMLElements];
}
- (void)setXMLValues:(NSArray *)arr {
[self setChildXMLElements:arr];
}
- (void)addXMLValue:(NSXMLNode *)node {
[self addChildXMLElement:node];
}
@end