Keine Beschreibung

GDataXMLNode.h 7.2KB

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