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