PageRenderTime 29ms CodeModel.GetById 1ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Elements/GDataLink.m

http://macfuse.googlecode.com/
Objective C | 234 lines | 152 code | 55 blank | 27 comment | 17 complexity | 1363bded743f80ca634688115082e27f MD5 | raw file
  1/* Copyright (c) 2007 Google Inc.
  2*
  3* Licensed under the Apache License, Version 2.0 (the "License");
  4* you may not use this file except in compliance with the License.
  5* You may obtain a copy of the License at
  6*
  7*     http://www.apache.org/licenses/LICENSE-2.0
  8*
  9* Unless required by applicable law or agreed to in writing, software
 10* distributed under the License is distributed on an "AS IS" BASIS,
 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12* See the License for the specific language governing permissions and
 13* limitations under the License.
 14*/
 15
 16//
 17//  GDataLink.m
 18//
 19
 20#define GDATALINK_DEFINE_GLOBALS 1
 21
 22#import "GDataLink.h"
 23#import "GDataBaseElements.h"
 24
 25static NSString *const kRelAttr = @"rel";
 26static NSString *const kTypeAttr = @"type";
 27static NSString *const kHrefAttr = @"href";
 28static NSString *const kHrefLangAttr = @"hrefLang";
 29static NSString *const kTitleAttr = @"title";
 30static NSString *const kLangAttr = @"xml:lang";
 31static NSString *const kLengthAttr = @"length";
 32
 33@implementation GDataLink
 34// for links, like <link rel="alternate" type="text/html"
 35//     href="http://www.google.com/calendar/event?eid=b..." title="alternate"/>
 36
 37+ (NSString *)extensionElementURI       { return kGDataNamespaceAtom; }
 38+ (NSString *)extensionElementPrefix    { return kGDataNamespaceAtomPrefix; }
 39+ (NSString *)extensionElementLocalName { return @"link"; }
 40
 41+ (GDataLink *)linkWithRel:(NSString *)rel
 42                      type:(NSString *)type
 43                      href:(NSString *)href {
 44  GDataLink *dataLink = [self object];
 45  [dataLink setRel:rel];
 46  [dataLink setType:type];
 47  [dataLink setHref:href];
 48  return dataLink;
 49}
 50
 51- (void)addParseDeclarations {
 52  NSArray *attrs = [NSArray arrayWithObjects:
 53                    kRelAttr, kTypeAttr, kHrefAttr, kHrefLangAttr,
 54                    kTitleAttr, kLangAttr, kLengthAttr, nil];
 55
 56  [self addLocalAttributeDeclarations:attrs];
 57}
 58
 59- (void)addExtensionDeclarations {
 60
 61  [super addExtensionDeclarations];
 62
 63  Class elementClass = [self class];
 64
 65  [self addExtensionDeclarationForParentClass:elementClass
 66                                   childClass:[GDataAtomContent class]];
 67
 68  [self addAttributeExtensionDeclarationForParentClass:elementClass
 69                                            childClass:[GDataETagAttribute class]];
 70}
 71
 72#if !GDATA_SIMPLE_DESCRIPTIONS
 73- (NSMutableArray *)itemsForDescription {
 74
 75  NSMutableArray *items = [super itemsForDescription];
 76
 77  [self addToArray:items objectDescriptionIfNonNil:[self content] withName:@"content"];
 78  [self addToArray:items objectDescriptionIfNonNil:[self ETag] withName:@"etag"];
 79
 80  return items;
 81}
 82#endif
 83
 84#pragma mark -
 85
 86- (NSString *)rel {
 87  NSString *str = [self stringValueForAttribute:kRelAttr];
 88  return [str length] > 0 ? str : @"alternate"; // per Link.java
 89}
 90
 91- (void)setRel:(NSString *)str {
 92  [self setStringValue:str forAttribute:kRelAttr];
 93}
 94
 95- (NSString *)type {
 96  return [self stringValueForAttribute:kTypeAttr];
 97}
 98
 99- (void)setType:(NSString *)str {
100  [self setStringValue:str forAttribute:kTypeAttr];
101}
102
103- (NSString *)href {
104  return [self stringValueForAttribute:kHrefAttr];
105}
106
107- (void)setHref:(NSString *)str {
108  [self setStringValue:str forAttribute:kHrefAttr];
109}
110
111- (NSString *)hrefLang {
112  return [self stringValueForAttribute:kHrefLangAttr];
113}
114
115- (void)setHrefLang:(NSString *)str {
116  [self setStringValue:str forAttribute:kHrefLangAttr];
117}
118
119- (NSString *)title {
120  return [self stringValueForAttribute:kTitleAttr];
121}
122
123- (void)setTitle:(NSString *)str {
124  [self setStringValue:str forAttribute:kTitleAttr];
125}
126
127- (NSString *)titleLang {
128  return [self stringValueForAttribute:kLangAttr];
129}
130
131- (void)setTitleLang:(NSString *)str {
132  [self setStringValue:str forAttribute:kLangAttr];
133}
134
135- (NSNumber *)resourceLength {
136  return [self intNumberForAttribute:kLengthAttr];
137}
138
139- (void)setResourceLength:(NSNumber *)length {
140  [self setStringValue:[length stringValue] forAttribute:kLengthAttr];
141}
142
143- (NSString *)ETag {
144  NSString *str = [self attributeValueForExtensionClass:[GDataETagAttribute class]];
145  return str;
146}
147
148- (void)setETag:(NSString *)str {
149  [self setAttributeValue:str forExtensionClass:[GDataETagAttribute class]];
150}
151
152- (GDataAtomContent *)content {
153  return [self objectForExtensionClass:[GDataAtomContent class]];
154}
155
156- (void)setContent:(GDataAtomContent *)obj {
157  [self setObject:obj forExtensionClass:[GDataAtomContent class]];
158}
159
160// convenience method
161
162- (NSURL *)URL {
163  NSString *href = [self href];
164  if ([href length] > 0) {
165    return [NSURL URLWithString:href];
166  }
167  return nil;
168}
169
170// utility method
171
172+ (NSArray *)linkNamesFromLinks:(NSArray *)links {
173  // we'll make a list of short, readable link names
174  // by grabbing the rel values, and removing anything before
175  // the last pound sign if there is one
176
177  NSMutableArray *names = nil;
178
179  for (GDataLink *dataLink in links) {
180
181    NSString *rel = [dataLink rel];
182    NSString *displayName;
183
184    NSRange range = [rel rangeOfString:@"#" options:NSBackwardsSearch];
185    if (range.location != NSNotFound) {
186      // the display name is the suffix of the link's rel string
187      displayName = [rel substringFromIndex:(1 + range.location)];
188    } else {
189      displayName = rel;
190    }
191
192    if (names == nil) {
193      names = [NSMutableArray array];
194    }
195    [names addObject:displayName];
196  }
197  return names;
198}
199
200#pragma mark Utilities
201
202// Find the first link with the given rel and type values. Either argument
203// may be nil, which means "match any value".
204+ (GDataLink *)linkWithRel:(NSString *)relValue
205                      type:(NSString *)typeValue
206                 fromLinks:(NSArray *)array {
207
208  for (GDataLink *dataLink in array) {
209
210    NSString *foundRelValue = [dataLink rel];
211    NSString *foundTypeValue = [dataLink type];
212
213    if ((relValue == nil || AreEqualOrBothNil(relValue, foundRelValue))
214        && (typeValue == nil || AreEqualOrBothNil(typeValue, foundTypeValue))) {
215      return dataLink;
216    }
217  }
218  return nil;
219}
220
221+ (GDataLink *)linkWithRelAttributeSuffix:(NSString *)relSuffix
222                                fromLinks:(NSArray *)array {
223
224  for (GDataLink *dataLink in array) {
225
226    NSString *attrValue = [dataLink rel];
227    if (attrValue && [attrValue hasSuffix:relSuffix]) {
228      return dataLink;
229    }
230  }
231  return nil;
232}
233
234@end