PageRenderTime 65ms CodeModel.GetById 11ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 0ms

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