/Source/externals/GData/Source/Clients/Photos/GDataEXIFTags.m

http://google-email-uploader-mac.googlecode.com/ · Objective C · 193 lines · 116 code · 47 blank · 30 comment · 10 complexity · dfb85081707bfd52b693c5a6b8e3e777 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. // GDataEXIFTag.m
  17. //
  18. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_PHOTOS_SERVICE
  19. #import "GDataEXIFTags.h"
  20. #import "GDataPhotoConstants.h"
  21. @implementation GDataEXIFTag
  22. + (NSString *)extensionElementPrefix { return kGDataNamespacePhotosEXIFPrefix; }
  23. + (NSString *)extensionElementURI { return kGDataNamespacePhotosEXIF; }
  24. + (NSString *)extensionElementLocalName {
  25. // wildcard * matches all elements with the proper namespace URI
  26. return @"*";
  27. }
  28. #pragma mark -
  29. + (GDataEXIFTag *)tagWithName:(NSString *)name
  30. value:(NSString *)value {
  31. GDataEXIFTag *obj = [GDataEXIFTag valueWithString:value];
  32. NSString *qualifiedName = [NSString stringWithFormat:@"%@:%@",
  33. kGDataNamespacePhotosEXIFPrefix, name];
  34. [obj setElementName:qualifiedName];
  35. return obj;
  36. }
  37. - (NSString *)name {
  38. NSString *qualifiedName = [self elementName];
  39. NSString *localName = [NSXMLNode localNameForName:qualifiedName];
  40. return localName;
  41. }
  42. @end
  43. @implementation GDataEXIFTags
  44. // for exif:tags, like
  45. // <exif:tags>
  46. // <exif:fstop>0.0</exif:fstop>
  47. // <exif:make>Nokia</exif:make>
  48. // </exif:tags>
  49. + (NSString *)extensionElementURI { return kGDataNamespacePhotosEXIF; }
  50. + (NSString *)extensionElementPrefix { return kGDataNamespacePhotosEXIFPrefix; }
  51. + (NSString *)extensionElementLocalName { return @"tags"; }
  52. + (GDataEXIFTags *)EXIFTags {
  53. GDataEXIFTags *obj = [self object];
  54. return obj;
  55. }
  56. - (void)addExtensionDeclarations {
  57. [super addExtensionDeclarations];
  58. // media:group may contain media:content
  59. [self addExtensionDeclarationForParentClass:[self class]
  60. childClass:[GDataEXIFTag class]];
  61. }
  62. - (BOOL)isEqual:(GDataEXIFTags *)other {
  63. if (self == other) return YES;
  64. if (![other isKindOfClass:[GDataEXIFTags class]]) return NO;
  65. return [super isEqual:other];
  66. }
  67. - (id)copyWithZone:(NSZone *)zone {
  68. GDataEXIFTags* newObj = [super copyWithZone:zone];
  69. return newObj;
  70. }
  71. #if !GDATA_SIMPLE_DESCRIPTIONS
  72. - (NSMutableArray *)itemsForDescription {
  73. NSMutableArray *items = [NSMutableArray array];
  74. // make an array of "name:value" items for each tag
  75. NSArray *tags = [self tags];
  76. NSEnumerator *tagsEnum = [tags objectEnumerator];
  77. NSMutableArray *tagsArray = [NSMutableArray array];
  78. GDataEXIFTag *tag;
  79. while ((tag = [tagsEnum nextObject]) != nil) {
  80. NSString *string = [NSString stringWithFormat:@"%@:%@",
  81. [tag name], [tag stringValue]];
  82. [tagsArray addObject:string];
  83. }
  84. [self addToArray:items
  85. objectDescriptionIfNonNil:[tagsArray componentsJoinedByString:@" "]
  86. withName:@"tags"];
  87. return items;
  88. }
  89. #endif
  90. - (NSXMLElement *)XMLElement {
  91. NSXMLElement *element = [self XMLElementWithExtensionsAndDefaultName:@"exif:tags"];
  92. return element;
  93. }
  94. #pragma mark -
  95. - (NSArray *)tags {
  96. NSArray *tags = [self objectsForExtensionClass:[GDataEXIFTag class]];
  97. return tags;
  98. }
  99. - (void)setTags:(NSArray *)tags {
  100. [self setObjects:tags forExtensionClass:[GDataEXIFTag class]];
  101. }
  102. - (void)addTag:(GDataEXIFTag *)tag {
  103. [self addObject:tag forExtensionClass:[GDataEXIFTag class]];
  104. }
  105. #pragma mark -
  106. - (GDataEXIFTag *)tagWithName:(NSString *)name {
  107. NSArray *tags = [self tags];
  108. GDataEXIFTag *tag = nil;
  109. for (tag in tags) {
  110. if (AreEqualOrBothNil([tag name], name)) {
  111. break;
  112. }
  113. }
  114. return tag;
  115. }
  116. - (NSString *)valueForTagName:(NSString *)name {
  117. return [[self tagWithName:name] stringValue];
  118. }
  119. - (void)removeTagWithName:(NSString *)name {
  120. GDataEXIFTag *tag = [self tagWithName:name];
  121. if (tag) {
  122. [self removeObject:tag forExtensionClass:[GDataEXIFTag class]];
  123. }
  124. }
  125. - (void)setTagWithName:(NSString *)name
  126. textValue:(NSString *)value {
  127. [self removeTagWithName:name];
  128. GDataEXIFTag *newTag = [GDataEXIFTag tagWithName:name value:value];
  129. [self addObject:newTag forExtensionClass:[GDataEXIFTag class]];
  130. }
  131. // EXIFTagDictionary returns a dictionary of exif tags, with
  132. // xml element names as keys, and tag values as values.
  133. // This is to facilitate key-value coding access to the attributes
  134. - (NSDictionary *)tagDictionary {
  135. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  136. NSArray *tags = [self tags];
  137. // Add in reverse order so the first tag in the array wins in the case of
  138. // duplicates.
  139. for (NSInteger idx = [tags count] - 1; idx >= 0; idx--) {
  140. GDataEXIFTag *tag = [tags objectAtIndex:idx];
  141. [dict setObject:[tag stringValue] forKey:[tag name]];
  142. }
  143. return dict;
  144. }
  145. @end
  146. #endif // !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_PHOTOS_SERVICE