PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 197 lines | 118 code | 45 blank | 34 comment | 18 complexity | 0ff9cb6b5ef42f47675b0ebb5cd264cb 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//  GDataExtendedProperty.m
 18//
 19
 20#if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_CALENDAR_SERVICE \
 21  || GDATA_INCLUDE_CONTACTS_SERVICE
 22
 23#define GDATAEXTENDEDPROPERTY_DEFINE_GLOBALS 1
 24
 25#import "GDataExtendedProperty.h"
 26
 27static NSString* const kNameAttr = @"name";
 28static NSString* const kValueAttr = @"value";
 29static NSString* const kRealmAttr = @"realm";
 30
 31@implementation GDataExtendedProperty
 32// an element with a name="" and a value="" attribute, as in
 33//  <gd:extendedProperty name='X-MOZ-ALARM-LAST-ACK' value='2006-10-03T19:01:14Z'/>
 34//
 35// or an arbitrary XML blob, as in
 36//  <gd:extendedProperty name='com.myCompany.myProperties'> <myXMLBlob /> </gd:extendedProperty>
 37//
 38// Servers may impose additional restrictions on names or on the size
 39// or composition of the values.
 40
 41+ (NSString *)extensionElementURI       { return kGDataNamespaceGData; }
 42+ (NSString *)extensionElementPrefix    { return kGDataNamespaceGDataPrefix; }
 43+ (NSString *)extensionElementLocalName { return @"extendedProperty"; }
 44
 45- (void)addEmptyDefaultNamespace {
 46
 47  // We don't want child XML lacking a prefix to be intepreted as being in the
 48  // atom namespace, so we'll specify that no default namespace applies.
 49  // This will add the attribute xmlns="" to the extendedProperty element.
 50
 51  NSDictionary *defaultNS = [NSDictionary dictionaryWithObject:@""
 52                                                        forKey:@""];
 53  [self addNamespaces:defaultNS];
 54}
 55
 56+ (id)propertyWithName:(NSString *)name
 57                 value:(NSString *)value {
 58
 59  GDataExtendedProperty* obj = [self object];
 60  [obj setName:name];
 61  [obj setValue:value];
 62  [obj addEmptyDefaultNamespace];
 63  return obj;
 64}
 65
 66- (id)init {
 67  self = [super init];
 68  if (self) {
 69    if ([[self namespaces] objectForKey:@""] == nil) {
 70      [self addEmptyDefaultNamespace];
 71    }
 72  }
 73  return self;
 74}
 75
 76- (id)initWithXMLElement:(NSXMLElement *)element
 77                  parent:(GDataObject *)parent {
 78  self = [super initWithXMLElement:element
 79                            parent:parent];
 80  if (self) {
 81    if ([[self namespaces] objectForKey:@""] == nil) {
 82      [self addEmptyDefaultNamespace];
 83    }
 84  }
 85  return self;
 86}
 87
 88- (void)addParseDeclarations {
 89
 90  NSArray *attrs = [NSArray arrayWithObjects:
 91                    kNameAttr, kValueAttr, kRealmAttr, nil];
 92
 93  [self addLocalAttributeDeclarations:attrs];
 94
 95  [self addChildXMLElementsDeclaration];
 96}
 97
 98- (NSString *)value {
 99  return [self stringValueForAttribute:kValueAttr];
100}
101
102- (void)setValue:(NSString *)str {
103  [self setStringValue:str forAttribute:kValueAttr];
104}
105
106- (NSString *)name {
107  return [self stringValueForAttribute:kNameAttr];
108}
109
110- (void)setName:(NSString *)str {
111  [self setStringValue:str forAttribute:kNameAttr];
112}
113
114- (NSString *)realm {
115  return [self stringValueForAttribute:kRealmAttr];
116}
117
118- (void)setRealm:(NSString *)str {
119  [self setStringValue:str forAttribute:kRealmAttr];
120}
121
122- (NSArray *)XMLValues {
123  return [super childXMLElements];
124}
125
126- (void)setXMLValues:(NSArray *)arr {
127  [self setChildXMLElements:arr];
128}
129
130- (void)addXMLValue:(NSXMLNode *)node {
131  [self addChildXMLElement:node];
132}
133
134#pragma mark -
135
136- (void)setXMLValue:(NSString *)value forKey:(NSString *)key {
137
138  // change or remove an entry in the values dictionary
139  //
140  // dict may be nil
141  NSMutableDictionary *dict = [[[self XMLValuesDictionary] mutableCopy] autorelease];
142
143  if (dict == nil && value != nil) {
144    dict = [NSMutableDictionary dictionary];
145  }
146  [dict setValue:value forKey:key];
147
148  [self setXMLValuesDictionary:dict];
149}
150
151- (NSString *)XMLValueForKey:(NSString *)key {
152
153  NSDictionary *dict = [self XMLValuesDictionary];
154  NSString *value = [dict valueForKey:key];
155  return value;
156}
157
158- (NSDictionary *)XMLValuesDictionary {
159
160  NSArray *xmlNodes = [self XMLValues];
161  if (xmlNodes == nil) return nil;
162
163  // step through all elements in the XML children and make a dictionary
164  // entry for each
165  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
166  for (id xmlNode in xmlNodes) {
167
168    NSString *qualifiedName = [xmlNode name];
169    NSString *value = [xmlNode stringValue];
170
171    [dict setValue:value forKey:qualifiedName];
172  }
173  return dict;
174}
175
176- (void)setXMLValuesDictionary:(NSDictionary *)dict {
177
178  NSMutableArray *nodes = [NSMutableArray array];
179
180  // replace the XML child elements with elements from the dictionary
181  for (NSString *key in dict) {
182    NSString *value = [dict objectForKey:key];
183    NSXMLNode *node = [NSXMLNode elementWithName:key
184                                     stringValue:value];
185    [nodes addObject:node];
186  }
187
188  if ([nodes count] > 0) {
189    [self setXMLValues:nodes];
190  } else {
191    [self setXMLValues:nil];
192  }
193}
194
195@end
196
197#endif // #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_*_SERVICE