/core/externals/update-engine/externals/gdata-objectivec-client/Source/Media/GDataMediaKeywords.m

http://macfuse.googlecode.com/ · Objective C · 140 lines · 85 code · 27 blank · 28 comment · 12 complexity · b036671a239fe03a3c1d1578650f0583 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. // GDataMediaKeywords.m
  17. //
  18. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_PHOTOS_SERVICE \
  19. || GDATA_INCLUDE_YOUTUBE_SERVICE
  20. #import "GDataMediaKeywords.h"
  21. #import "GDataMediaGroup.h"
  22. @interface GDataMediaKeywords (PrivateMethods)
  23. + (NSString *)trimString:(NSString *)str;
  24. @end
  25. @implementation GDataMediaKeywords
  26. // like <media:keywords>kitty, cat, big dog, yarn, fluffy</media:keywords>
  27. // http://search.yahoo.com/mrss
  28. + (NSString *)extensionElementURI { return kGDataNamespaceMedia; }
  29. + (NSString *)extensionElementPrefix { return kGDataNamespaceMediaPrefix; }
  30. + (NSString *)extensionElementLocalName { return @"keywords"; }
  31. + (GDataMediaKeywords *)keywordsWithStrings:(NSArray *)array {
  32. GDataMediaKeywords* obj = [self object];
  33. [obj setKeywords:array];
  34. return obj;
  35. }
  36. + (GDataMediaKeywords *)keywordsWithString:(NSString *)str {
  37. // takes a string with a comma-separated list of keywords
  38. GDataMediaKeywords* obj = [self object];
  39. NSArray *array = [GDataMediaKeywords keywordsFromString:str];
  40. [obj setKeywords:array];
  41. return obj;
  42. }
  43. #if !GDATA_SIMPLE_DESCRIPTIONS
  44. - (NSMutableArray *)itemsForDescription {
  45. static struct GDataDescriptionRecord descRecs[] = {
  46. { @"keywords", @"stringValue", kGDataDescValueLabeled },
  47. { nil, nil, (GDataDescRecTypes)0 }
  48. };
  49. NSMutableArray *items = [super itemsForDescription];
  50. [self addDescriptionRecords:descRecs toItems:items];
  51. return items;
  52. }
  53. #endif
  54. #pragma mark -
  55. - (NSArray *)keywords {
  56. NSString *str = [self stringValue];
  57. NSArray *array = [GDataMediaKeywords keywordsFromString:str];
  58. return array;
  59. }
  60. - (void)setKeywords:(NSArray *)array {
  61. NSString *str = [GDataMediaKeywords stringFromKeywords:array];
  62. [self setStringValue:str];
  63. }
  64. - (void)addKeyword:(NSString *)str {
  65. str = [GDataMediaKeywords trimString:str];
  66. if ([str length] > 0) {
  67. NSArray *array = [self keywords];
  68. if ([array count] == 0) {
  69. // this is the first keyword
  70. [self setStringValue:str];
  71. } else {
  72. // check that this is not already in the array
  73. if (! [array containsObject:str]) {
  74. NSMutableArray *mutableArray = [[array mutableCopy] autorelease];
  75. [mutableArray addObject:str];
  76. [self setKeywords:mutableArray];
  77. }
  78. }
  79. }
  80. }
  81. #pragma mark Utilities
  82. + (NSString *)trimString:(NSString *)str {
  83. // remove leading and trailing whitespace from the string
  84. NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  85. return [str stringByTrimmingCharactersInSet:whitespace];
  86. }
  87. + (NSArray *)keywordsFromString:(NSString *)commaSeparatedString {
  88. // split the words into strings at the commas
  89. NSArray *rawWordArray = [commaSeparatedString componentsSeparatedByString:@","];
  90. NSMutableArray *keywordArray = nil;
  91. for (NSString *word in rawWordArray) {
  92. // trim each word in the array, and if a trimmed word is non-empty,
  93. // add it to the array
  94. NSString *trimmedWord = [GDataMediaKeywords trimString:word];
  95. if ([trimmedWord length] > 0) {
  96. if (keywordArray == nil) {
  97. keywordArray = [NSMutableArray array];
  98. }
  99. [keywordArray addObject:trimmedWord];
  100. }
  101. }
  102. return keywordArray;
  103. }
  104. + (NSString *)stringFromKeywords:(NSArray *)keywords {
  105. // join keywords with commas; return the string if it's non-empty,
  106. // or nil otherwise
  107. if ([keywords count] > 0) {
  108. return [keywords componentsJoinedByString:@", "];
  109. }
  110. return nil;
  111. }
  112. @end
  113. #endif // #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_*_SERVICE