/core/externals/update-engine/externals/gdata-objectivec-client/Source/XMLSupport/GDataXMLNode.h

http://macfuse.googlecode.com/ · C++ Header · 221 lines · 104 code · 48 blank · 69 comment · 0 complexity · b96c7a4944b6f88176a442793494a28e MD5 · raw file

  1. /* Copyright (c) 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. // These node, element, and document classes implement a subset of the methods
  16. // provided by NSXML. While NSXML behavior is mimicked as much as possible,
  17. // there are important differences.
  18. //
  19. // The biggest difference is that, since this is based on libxml2, there
  20. // is no retain model for the underlying node data. Rather than copy every
  21. // node obtained from a parse tree (which would have a substantial memory
  22. // impact), we rely on weak references, and it is up to the code that
  23. // created a document to retain it for as long as any
  24. // references rely on nodes inside that document tree.
  25. #import <Foundation/Foundation.h>
  26. // libxml includes require that the target Header Search Paths contain
  27. //
  28. // /usr/include/libxml2
  29. //
  30. // and Other Linker Flags contain
  31. //
  32. // -lxml2
  33. #import <libxml/tree.h>
  34. #import <libxml/parser.h>
  35. #import <libxml/xmlstring.h>
  36. #import <libxml/xpath.h>
  37. #import <libxml/xpathInternals.h>
  38. #ifdef GDATA_TARGET_NAMESPACE
  39. // we're using target namespace macros
  40. #import "GDataDefines.h"
  41. #endif
  42. #undef _EXTERN
  43. #undef _INITIALIZE_AS
  44. #ifdef GDATAXMLNODE_DEFINE_GLOBALS
  45. #define _EXTERN
  46. #define _INITIALIZE_AS(x) =x
  47. #else
  48. #if defined(__cplusplus)
  49. #define _EXTERN extern "C"
  50. #else
  51. #define _EXTERN extern
  52. #endif
  53. #define _INITIALIZE_AS(x)
  54. #endif
  55. // when no namespace dictionary is supplied for XPath, the default namespace
  56. // for the evaluated tree is registered with the prefix _def_ns
  57. _EXTERN const char* kGDataXMLXPathDefaultNamespacePrefix _INITIALIZE_AS("_def_ns");
  58. // Nomenclature for method names:
  59. //
  60. // Node = GData node
  61. // XMLNode = xmlNodePtr
  62. //
  63. // So, for example:
  64. // + (id)nodeConsumingXMLNode:(xmlNodePtr)theXMLNode;
  65. @class NSArray, NSDictionary, NSError, NSString, NSURL;
  66. @class GDataXMLElement, GDataXMLDocument;
  67. enum {
  68. GDataXMLInvalidKind = 0,
  69. GDataXMLDocumentKind,
  70. GDataXMLElementKind,
  71. GDataXMLAttributeKind,
  72. GDataXMLNamespaceKind,
  73. GDataXMLProcessingInstructionKind,
  74. GDataXMLCommentKind,
  75. GDataXMLTextKind,
  76. GDataXMLDTDKind,
  77. GDataXMLEntityDeclarationKind,
  78. GDataXMLAttributeDeclarationKind,
  79. GDataXMLElementDeclarationKind,
  80. GDataXMLNotationDeclarationKind
  81. };
  82. typedef NSUInteger GDataXMLNodeKind;
  83. @interface GDataXMLNode : NSObject <NSCopying> {
  84. @protected
  85. // NSXMLNodes can have a namespace URI or prefix even if not part
  86. // of a tree; xmlNodes cannot. When we create nodes apart from
  87. // a tree, we'll store the dangling prefix or URI in the xmlNode's name,
  88. // like
  89. // "prefix:name"
  90. // or
  91. // "{http://uri}:name"
  92. //
  93. // We will fix up the node's namespace and name (and those of any children)
  94. // later when adding the node to a tree with addChild: or addAttribute:.
  95. // See fixUpNamespacesForNode:.
  96. xmlNodePtr xmlNode_; // may also be an xmlAttrPtr or xmlNsPtr
  97. BOOL shouldFreeXMLNode_; // if yes, xmlNode_ will be free'd in dealloc
  98. // cached values
  99. NSString *cachedName_;
  100. NSArray *cachedChildren_;
  101. NSArray *cachedAttributes_;
  102. }
  103. + (GDataXMLElement *)elementWithName:(NSString *)name;
  104. + (GDataXMLElement *)elementWithName:(NSString *)name stringValue:(NSString *)value;
  105. + (GDataXMLElement *)elementWithName:(NSString *)name URI:(NSString *)value;
  106. + (id)attributeWithName:(NSString *)name stringValue:(NSString *)value;
  107. + (id)attributeWithName:(NSString *)name URI:(NSString *)attributeURI stringValue:(NSString *)value;
  108. + (id)namespaceWithName:(NSString *)name stringValue:(NSString *)value;
  109. + (id)textWithStringValue:(NSString *)value;
  110. - (NSString *)stringValue;
  111. - (void)setStringValue:(NSString *)str;
  112. - (NSUInteger)childCount;
  113. - (NSArray *)children;
  114. - (GDataXMLNode *)childAtIndex:(unsigned)index;
  115. - (NSString *)localName;
  116. - (NSString *)name;
  117. - (NSString *)prefix;
  118. - (NSString *)URI;
  119. - (GDataXMLNodeKind)kind;
  120. - (NSString *)XMLString;
  121. + (NSString *)localNameForName:(NSString *)name;
  122. + (NSString *)prefixForName:(NSString *)name;
  123. // This is the preferred entry point for nodesForXPath. This takes an explicit
  124. // namespace dictionary (keys are prefixes, values are URIs).
  125. - (NSArray *)nodesForXPath:(NSString *)xpath namespaces:(NSDictionary *)namespaces error:(NSError **)error;
  126. // This implementation of nodesForXPath registers namespaces only from the
  127. // document's root node. _def_ns may be used as a prefix for the default
  128. // namespace, though there's no guarantee that the default namespace will
  129. // be consistenly the same namespace in server responses.
  130. - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
  131. // access to the underlying libxml node; be sure to release the cached values
  132. // if you change the underlying tree at all
  133. - (xmlNodePtr)XMLNode;
  134. - (void)releaseCachedValues;
  135. @end
  136. @interface GDataXMLElement : GDataXMLNode
  137. - (id)initWithXMLString:(NSString *)str error:(NSError **)error;
  138. - (NSArray *)namespaces;
  139. - (void)setNamespaces:(NSArray *)namespaces;
  140. - (void)addNamespace:(GDataXMLNode *)aNamespace;
  141. // addChild adds a copy of the child node to the element
  142. - (void)addChild:(GDataXMLNode *)child;
  143. - (void)removeChild:(GDataXMLNode *)child;
  144. - (NSArray *)elementsForName:(NSString *)name;
  145. - (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI;
  146. - (NSArray *)attributes;
  147. - (GDataXMLNode *)attributeForName:(NSString *)name;
  148. - (GDataXMLNode *)attributeForLocalName:(NSString *)name URI:(NSString *)attributeURI;
  149. - (void)addAttribute:(GDataXMLNode *)attribute;
  150. - (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
  151. @end
  152. @interface GDataXMLDocument : NSObject {
  153. @protected
  154. xmlDoc* xmlDoc_; // strong; always free'd in dealloc
  155. }
  156. - (id)initWithXMLString:(NSString *)str options:(unsigned int)mask error:(NSError **)error;
  157. - (id)initWithData:(NSData *)data options:(unsigned int)mask error:(NSError **)error;
  158. // initWithRootElement uses a copy of the argument as the new document's root
  159. - (id)initWithRootElement:(GDataXMLElement *)element;
  160. - (GDataXMLElement *)rootElement;
  161. - (NSData *)XMLData;
  162. - (void)setVersion:(NSString *)version;
  163. - (void)setCharacterEncoding:(NSString *)encoding;
  164. // This is the preferred entry point for nodesForXPath. This takes an explicit
  165. // namespace dictionary (keys are prefixes, values are URIs).
  166. - (NSArray *)nodesForXPath:(NSString *)xpath namespaces:(NSDictionary *)namespaces error:(NSError **)error;
  167. // This implementation of nodesForXPath registers namespaces only from the
  168. // document's root node. _def_ns may be used as a prefix for the default
  169. // namespace, though there's no guarantee that the default namespace will
  170. // be consistenly the same namespace in server responses.
  171. - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
  172. - (NSString *)description;
  173. @end