/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. // GDataLink.m
  17. //
  18. #define GDATALINK_DEFINE_GLOBALS 1
  19. #import "GDataLink.h"
  20. #import "GDataBaseElements.h"
  21. static NSString *const kRelAttr = @"rel";
  22. static NSString *const kTypeAttr = @"type";
  23. static NSString *const kHrefAttr = @"href";
  24. static NSString *const kHrefLangAttr = @"hrefLang";
  25. static NSString *const kTitleAttr = @"title";
  26. static NSString *const kLangAttr = @"xml:lang";
  27. static NSString *const kLengthAttr = @"length";
  28. @implementation GDataLink
  29. // for links, like <link rel="alternate" type="text/html"
  30. // href="http://www.google.com/calendar/event?eid=b..." title="alternate"/>
  31. + (NSString *)extensionElementURI { return kGDataNamespaceAtom; }
  32. + (NSString *)extensionElementPrefix { return kGDataNamespaceAtomPrefix; }
  33. + (NSString *)extensionElementLocalName { return @"link"; }
  34. + (GDataLink *)linkWithRel:(NSString *)rel
  35. type:(NSString *)type
  36. href:(NSString *)href {
  37. GDataLink *dataLink = [self object];
  38. [dataLink setRel:rel];
  39. [dataLink setType:type];
  40. [dataLink setHref:href];
  41. return dataLink;
  42. }
  43. - (void)addParseDeclarations {
  44. NSArray *attrs = [NSArray arrayWithObjects:
  45. kRelAttr, kTypeAttr, kHrefAttr, kHrefLangAttr,
  46. kTitleAttr, kLangAttr, kLengthAttr, nil];
  47. [self addLocalAttributeDeclarations:attrs];
  48. }
  49. - (void)addExtensionDeclarations {
  50. [super addExtensionDeclarations];
  51. Class elementClass = [self class];
  52. [self addExtensionDeclarationForParentClass:elementClass
  53. childClass:[GDataAtomContent class]];
  54. [self addAttributeExtensionDeclarationForParentClass:elementClass
  55. childClass:[GDataETagAttribute class]];
  56. }
  57. #if !GDATA_SIMPLE_DESCRIPTIONS
  58. - (NSMutableArray *)itemsForDescription {
  59. NSMutableArray *items = [super itemsForDescription];
  60. [self addToArray:items objectDescriptionIfNonNil:[self content] withName:@"content"];
  61. [self addToArray:items objectDescriptionIfNonNil:[self ETag] withName:@"etag"];
  62. return items;
  63. }
  64. #endif
  65. #pragma mark -
  66. - (NSString *)rel {
  67. NSString *str = [self stringValueForAttribute:kRelAttr];
  68. return [str length] > 0 ? str : @"alternate"; // per Link.java
  69. }
  70. - (void)setRel:(NSString *)str {
  71. [self setStringValue:str forAttribute:kRelAttr];
  72. }
  73. - (NSString *)type {
  74. return [self stringValueForAttribute:kTypeAttr];
  75. }
  76. - (void)setType:(NSString *)str {
  77. [self setStringValue:str forAttribute:kTypeAttr];
  78. }
  79. - (NSString *)href {
  80. return [self stringValueForAttribute:kHrefAttr];
  81. }
  82. - (void)setHref:(NSString *)str {
  83. [self setStringValue:str forAttribute:kHrefAttr];
  84. }
  85. - (NSString *)hrefLang {
  86. return [self stringValueForAttribute:kHrefLangAttr];
  87. }
  88. - (void)setHrefLang:(NSString *)str {
  89. [self setStringValue:str forAttribute:kHrefLangAttr];
  90. }
  91. - (NSString *)title {
  92. return [self stringValueForAttribute:kTitleAttr];
  93. }
  94. - (void)setTitle:(NSString *)str {
  95. [self setStringValue:str forAttribute:kTitleAttr];
  96. }
  97. - (NSString *)titleLang {
  98. return [self stringValueForAttribute:kLangAttr];
  99. }
  100. - (void)setTitleLang:(NSString *)str {
  101. [self setStringValue:str forAttribute:kLangAttr];
  102. }
  103. - (NSNumber *)resourceLength {
  104. return [self intNumberForAttribute:kLengthAttr];
  105. }
  106. - (void)setResourceLength:(NSNumber *)length {
  107. [self setStringValue:[length stringValue] forAttribute:kLengthAttr];
  108. }
  109. - (NSString *)ETag {
  110. NSString *str = [self attributeValueForExtensionClass:[GDataETagAttribute class]];
  111. return str;
  112. }
  113. - (void)setETag:(NSString *)str {
  114. [self setAttributeValue:str forExtensionClass:[GDataETagAttribute class]];
  115. }
  116. - (GDataAtomContent *)content {
  117. return [self objectForExtensionClass:[GDataAtomContent class]];
  118. }
  119. - (void)setContent:(GDataAtomContent *)obj {
  120. [self setObject:obj forExtensionClass:[GDataAtomContent class]];
  121. }
  122. // convenience method
  123. - (NSURL *)URL {
  124. NSString *href = [self href];
  125. if ([href length] > 0) {
  126. return [NSURL URLWithString:href];
  127. }
  128. return nil;
  129. }
  130. // utility method
  131. + (NSArray *)linkNamesFromLinks:(NSArray *)links {
  132. // we'll make a list of short, readable link names
  133. // by grabbing the rel values, and removing anything before
  134. // the last pound sign if there is one
  135. NSMutableArray *names = nil;
  136. for (GDataLink *dataLink in links) {
  137. NSString *rel = [dataLink rel];
  138. NSString *displayName;
  139. NSRange range = [rel rangeOfString:@"#" options:NSBackwardsSearch];
  140. if (range.location != NSNotFound) {
  141. // the display name is the suffix of the link's rel string
  142. displayName = [rel substringFromIndex:(1 + range.location)];
  143. } else {
  144. displayName = rel;
  145. }
  146. if (names == nil) {
  147. names = [NSMutableArray array];
  148. }
  149. [names addObject:displayName];
  150. }
  151. return names;
  152. }
  153. #pragma mark Utilities
  154. // Find the first link with the given rel and type values. Either argument
  155. // may be nil, which means "match any value".
  156. + (GDataLink *)linkWithRel:(NSString *)relValue
  157. type:(NSString *)typeValue
  158. fromLinks:(NSArray *)array {
  159. for (GDataLink *dataLink in array) {
  160. NSString *foundRelValue = [dataLink rel];
  161. NSString *foundTypeValue = [dataLink type];
  162. if ((relValue == nil || AreEqualOrBothNil(relValue, foundRelValue))
  163. && (typeValue == nil || AreEqualOrBothNil(typeValue, foundTypeValue))) {
  164. return dataLink;
  165. }
  166. }
  167. return nil;
  168. }
  169. + (GDataLink *)linkWithRelAttributeSuffix:(NSString *)relSuffix
  170. fromLinks:(NSArray *)array {
  171. for (GDataLink *dataLink in array) {
  172. NSString *attrValue = [dataLink rel];
  173. if (attrValue && [attrValue hasSuffix:relSuffix]) {
  174. return dataLink;
  175. }
  176. }
  177. return nil;
  178. }
  179. @end