-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDataLink.m
234 lines (179 loc) · 6.15 KB
/
GDataLink.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
/* Copyright (c) 2007 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.
*/
//
// GDataLink.m
//
#define GDATALINK_DEFINE_GLOBALS 1
#import "GDataLink.h"
#import "GDataBaseElements.h"
static NSString *const kRelAttr = @"rel";
static NSString *const kTypeAttr = @"type";
static NSString *const kHrefAttr = @"href";
static NSString *const kHrefLangAttr = @"hrefLang";
static NSString *const kTitleAttr = @"title";
static NSString *const kLangAttr = @"xml:lang";
static NSString *const kLengthAttr = @"length";
@implementation GDataLink
// for links, like <link rel="alternate" type="text/html"
// href="http://www.google.com/calendar/event?eid=b..." title="alternate"/>
+ (NSString *)extensionElementURI { return kGDataNamespaceAtom; }
+ (NSString *)extensionElementPrefix { return kGDataNamespaceAtomPrefix; }
+ (NSString *)extensionElementLocalName { return @"link"; }
+ (GDataLink *)linkWithRel:(NSString *)rel
type:(NSString *)type
href:(NSString *)href {
GDataLink *dataLink = [self object];
[dataLink setRel:rel];
[dataLink setType:type];
[dataLink setHref:href];
return dataLink;
}
- (void)addParseDeclarations {
NSArray *attrs = [NSArray arrayWithObjects:
kRelAttr, kTypeAttr, kHrefAttr, kHrefLangAttr,
kTitleAttr, kLangAttr, kLengthAttr, nil];
[self addLocalAttributeDeclarations:attrs];
}
- (void)addExtensionDeclarations {
[super addExtensionDeclarations];
Class elementClass = [self class];
[self addExtensionDeclarationForParentClass:elementClass
childClass:[GDataAtomContent class]];
[self addAttributeExtensionDeclarationForParentClass:elementClass
childClass:[GDataETagAttribute class]];
}
#if !GDATA_SIMPLE_DESCRIPTIONS
- (NSMutableArray *)itemsForDescription {
NSMutableArray *items = [super itemsForDescription];
[self addToArray:items objectDescriptionIfNonNil:[self content] withName:@"content"];
[self addToArray:items objectDescriptionIfNonNil:[self ETag] withName:@"etag"];
return items;
}
#endif
#pragma mark -
- (NSString *)rel {
NSString *str = [self stringValueForAttribute:kRelAttr];
return [str length] > 0 ? str : @"alternate"; // per Link.java
}
- (void)setRel:(NSString *)str {
[self setStringValue:str forAttribute:kRelAttr];
}
- (NSString *)type {
return [self stringValueForAttribute:kTypeAttr];
}
- (void)setType:(NSString *)str {
[self setStringValue:str forAttribute:kTypeAttr];
}
- (NSString *)href {
return [self stringValueForAttribute:kHrefAttr];
}
- (void)setHref:(NSString *)str {
[self setStringValue:str forAttribute:kHrefAttr];
}
- (NSString *)hrefLang {
return [self stringValueForAttribute:kHrefLangAttr];
}
- (void)setHrefLang:(NSString *)str {
[self setStringValue:str forAttribute:kHrefLangAttr];
}
- (NSString *)title {
return [self stringValueForAttribute:kTitleAttr];
}
- (void)setTitle:(NSString *)str {
[self setStringValue:str forAttribute:kTitleAttr];
}
- (NSString *)titleLang {
return [self stringValueForAttribute:kLangAttr];
}
- (void)setTitleLang:(NSString *)str {
[self setStringValue:str forAttribute:kLangAttr];
}
- (NSNumber *)resourceLength {
return [self intNumberForAttribute:kLengthAttr];
}
- (void)setResourceLength:(NSNumber *)length {
[self setStringValue:[length stringValue] forAttribute:kLengthAttr];
}
- (NSString *)ETag {
NSString *str = [self attributeValueForExtensionClass:[GDataETagAttribute class]];
return str;
}
- (void)setETag:(NSString *)str {
[self setAttributeValue:str forExtensionClass:[GDataETagAttribute class]];
}
- (GDataAtomContent *)content {
return [self objectForExtensionClass:[GDataAtomContent class]];
}
- (void)setContent:(GDataAtomContent *)obj {
[self setObject:obj forExtensionClass:[GDataAtomContent class]];
}
// convenience method
- (NSURL *)URL {
NSString *href = [self href];
if ([href length] > 0) {
return [NSURL URLWithString:href];
}
return nil;
}
// utility method
+ (NSArray *)linkNamesFromLinks:(NSArray *)links {
// we'll make a list of short, readable link names
// by grabbing the rel values, and removing anything before
// the last pound sign if there is one
NSMutableArray *names = nil;
for (GDataLink *dataLink in links) {
NSString *rel = [dataLink rel];
NSString *displayName;
NSRange range = [rel rangeOfString:@"#" options:NSBackwardsSearch];
if (range.location != NSNotFound) {
// the display name is the suffix of the link's rel string
displayName = [rel substringFromIndex:(1 + range.location)];
} else {
displayName = rel;
}
if (names == nil) {
names = [NSMutableArray array];
}
[names addObject:displayName];
}
return names;
}
#pragma mark Utilities
// Find the first link with the given rel and type values. Either argument
// may be nil, which means "match any value".
+ (GDataLink *)linkWithRel:(NSString *)relValue
type:(NSString *)typeValue
fromLinks:(NSArray *)array {
for (GDataLink *dataLink in array) {
NSString *foundRelValue = [dataLink rel];
NSString *foundTypeValue = [dataLink type];
if ((relValue == nil || AreEqualOrBothNil(relValue, foundRelValue))
&& (typeValue == nil || AreEqualOrBothNil(typeValue, foundTypeValue))) {
return dataLink;
}
}
return nil;
}
+ (GDataLink *)linkWithRelAttributeSuffix:(NSString *)relSuffix
fromLinks:(NSArray *)array {
for (GDataLink *dataLink in array) {
NSString *attrValue = [dataLink rel];
if (attrValue && [attrValue hasSuffix:relSuffix]) {
return dataLink;
}
}
return nil;
}
@end