PageRenderTime 54ms CodeModel.GetById 18ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 266 lines | 158 code | 68 blank | 40 comment | 23 complexity | d7f4ce8a76d89fe43b785643c3469b66 MD5 | raw file
  1/* Copyright (c) 2007-2008 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//  GDataEntryContent.m
 18//
 19
 20#define GDATAENTRYCONTENT_DEFINE_GLOBALS 1
 21#import "GDataEntryContent.h"
 22
 23static NSString* const kLangAttr = @"xml:lang";
 24static NSString* const kTypeAttr = @"type";
 25static NSString* const kSourceAttr = @"src";
 26
 27static BOOL IsTypeEqualToText(NSString *str) {
 28  // internal utility routine
 29  return (str == nil)
 30  || [str isEqual:@"text"]
 31  || [str hasPrefix:@"text/"]
 32  || [str isEqual:@"html"]
 33  || [str isEqual:@"xhtml"];
 34}
 35
 36
 37@implementation GDataEntryContent
 38
 39+ (NSString *)extensionElementURI       { return kGDataNamespaceAtom; }
 40+ (NSString *)extensionElementPrefix    { return kGDataNamespaceAtomPrefix; }
 41+ (NSString *)extensionElementLocalName { return @"content"; }
 42
 43+ (id)contentWithString:(NSString *)str {
 44
 45  // RFC4287 Sec 4.1.3.1. says that omitted type attributes are assumed to be
 46  // "text", so we don't need to explicitly set it to text
 47
 48  GDataEntryContent *obj = [self object];
 49  [obj setStringValue:str];
 50  return obj;
 51}
 52
 53+ (id)contentWithSourceURI:(NSString *)str type:(NSString *)type {
 54
 55  GDataEntryContent *obj = [self object];
 56  [obj setSourceURI:str];
 57  [obj setType:type];
 58  return obj;
 59}
 60
 61+ (id)contentWithXMLValue:(NSXMLNode *)node type:(NSString *)type {
 62
 63  GDataEntryContent *obj = [self object];
 64
 65  // declare that we'll be using child elements as XML values
 66  [obj addChildXMLElementsDeclaration];
 67
 68  [obj setType:type];
 69
 70  if (node != nil) {
 71    [obj addXMLValue:node];
 72  }
 73  return obj;
 74}
 75
 76+ (id)textConstructWithString:(NSString *)str {
 77
 78  // deprecated; kept for compatibility with the previous
 79  // implementation of GDataEntryContent
 80  GDATA_DEBUG_LOG(@"GDataEntryContent: +textConstructWithString deprecated, use +contentWithString");
 81
 82  return [self contentWithString:str];
 83}
 84
 85- (void)addParseDeclarations {
 86
 87  NSArray *attrs = [NSArray arrayWithObjects:
 88                    kTypeAttr, kLangAttr, kSourceAttr, nil];
 89
 90  [self addLocalAttributeDeclarations:attrs];
 91
 92  // we're not calling -addContentValueDeclaration since the content may not
 93  // be plain text but rather XML for an entry or feed that we will parse
 94}
 95
 96- (NSArray *)attributesIgnoredForEquality {
 97
 98  // ignore the "type" attribute since we test for it uniquely below
 99  return [NSArray arrayWithObject:kTypeAttr];
100}
101
102- (void)parseAttributesForElement:(NSXMLElement *)element {
103
104  // override the attribute parsing method
105  //
106  // once we have parsed the attributes, we can decide
107  // how to parse the contents or children
108  [super parseAttributesForElement:element];
109
110  NSString *type = [self type];
111  if (IsTypeEqualToText(type)) {
112
113    // content is plain text
114    [self addContentValueDeclaration];
115
116  } else if ([type hasPrefix:@"application/atom+xml;"]) {
117
118    // content is a feed or entry stored in unknownChildren
119    GDataObject *obj = [self objectForChildOfElement:element
120                                       qualifiedName:@"*"
121                                        namespaceURI:@"*"
122                                         objectClass:nil];
123    [self setChildObject:obj];
124
125  } else if ([type hasPrefix:kGDataContentTypeKML]) {
126
127    // content is KML
128    [self addChildXMLElementsDeclaration];
129  }
130}
131
132- (void)dealloc {
133  [childObject_ release];
134  [super dealloc];
135}
136
137- (NSXMLElement *)XMLElement {
138
139  NSXMLElement *element = [self XMLElementWithExtensionsAndDefaultName:nil];
140
141  GDataObject *obj = [self childObject];
142  if (obj) {
143    NSXMLElement *elem = [obj XMLElement];
144    [element addChild:elem];
145  }
146
147  return element;
148}
149
150- (id)copyWithZone:(NSZone *)zone {
151  GDataEntryContent* newObj = [super copyWithZone:zone];
152
153  [newObj setChildObject:[[[self childObject] copy] autorelease]];
154
155  return newObj;
156}
157
158- (BOOL)isEqual:(GDataEntryContent *)other {
159
160  // override isEqual: to allow nil types to be considered equal to "text"
161  return [super isEqual:other]
162
163    // a missing type attribute is equal to "text" per RFC 4287 3.1.1
164    //
165    // consider them equal if both are some flavor of "text"
166    && (AreEqualOrBothNil([self type], [other type])
167        || (IsTypeEqualToText([self type]) && IsTypeEqualToText([other type])))
168    && AreEqualOrBothNil([self childObject], [other childObject]);
169}
170
171#if !GDATA_SIMPLE_DESCRIPTIONS
172- (NSMutableArray *)itemsForDescription {
173
174  NSMutableArray *items = [super itemsForDescription];
175
176  // if the base class is not storing the content value, we must provide the
177  // description item here
178  if (![self hasDeclaredContentValue]) {
179    [self addToArray:items objectDescriptionIfNonNil:[self stringValue] withName:@"content"];
180  }
181
182  [self addToArray:items objectDescriptionIfNonNil:[self XMLValues] withName:@"xml"];
183
184  GDataObject *obj = [self childObject];
185  if (obj != nil) {
186    NSString *className = NSStringFromClass([obj class]);
187    [self addToArray:items objectDescriptionIfNonNil:className withName:@"childObject"];
188  }
189  return items;
190}
191#endif
192
193#pragma mark -
194
195- (NSString *)lang {
196  return [self stringValueForAttribute:kLangAttr];
197}
198
199- (void)setLang:(NSString *)str {
200  [self setStringValue:str forAttribute:kLangAttr];
201}
202
203- (NSString *)type {
204  return [self stringValueForAttribute:kTypeAttr];
205}
206
207- (NSString *)sourceURI {
208  return [self stringValueForAttribute:kSourceAttr];
209}
210
211- (void)setSourceURI:(NSString *)str {
212  [self setStringValue:str forAttribute:kSourceAttr];
213}
214
215- (NSURL *)sourceURL {
216  NSString *sourceURI = [self sourceURI];
217  if ([sourceURI length] > 0) {
218
219    NSURL *url = [NSURL URLWithString:sourceURI];
220    return url;
221  }
222  return nil;
223}
224
225- (void)setType:(NSString *)str {
226  [self setStringValue:str forAttribute:kTypeAttr];
227}
228
229- (NSString *)stringValue {
230  if ([self hasDeclaredContentValue]) {
231    return [self contentStringValue];
232  }
233  return nil;
234}
235
236- (void)setStringValue:(NSString *)str {
237  if (![self hasDeclaredContentValue]) {
238    // if we emit XML later on, we'll want to emit this string
239    [self addContentValueDeclaration];
240  }
241
242  [self setContentStringValue:str];
243}
244
245- (GDataObject *)childObject {
246  return childObject_;
247}
248
249- (void)setChildObject:(GDataObject *)obj {
250  [childObject_ autorelease];
251  childObject_ = [obj retain];
252}
253
254- (NSArray *)XMLValues {
255  return [super childXMLElements];
256}
257
258- (void)setXMLValues:(NSArray *)arr {
259  [self setChildXMLElements:arr];
260}
261
262- (void)addXMLValue:(NSXMLNode *)node {
263  [self addChildXMLElement:node];
264}
265
266@end