| 1 |
#import "DDXMLPrivate.h" |
| 2 |
#import "NSString+DDXML.h" |
| 3 |
|
| 4 |
|
| 5 |
@implementation DDXMLDocument |
| 6 |
|
| 7 |
/** |
| 8 |
* Returns a DDXML wrapper object for the given primitive node. |
| 9 |
* The given node MUST be non-NULL and of the proper type. |
| 10 |
**/ |
| 11 |
+ (id)nodeWithDocPrimitive:(xmlDocPtr)doc freeOnDealloc:(BOOL)flag |
| 12 |
{ |
| 13 |
return [[[DDXMLDocument alloc] initWithDocPrimitive:doc freeOnDealloc:flag] autorelease]; |
| 14 |
} |
| 15 |
|
| 16 |
- (id)initWithDocPrimitive:(xmlDocPtr)doc freeOnDealloc:(BOOL)flag |
| 17 |
{ |
| 18 |
self = [super initWithPrimitive:(xmlKindPtr)doc freeOnDealloc:flag]; |
| 19 |
return self; |
| 20 |
} |
| 21 |
|
| 22 |
+ (id)nodeWithPrimitive:(xmlKindPtr)kindPtr freeOnDealloc:(BOOL)flag |
| 23 |
{ |
| 24 |
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes |
| 25 |
NSAssert(NO, @"Use nodeWithDocPrimitive:freeOnDealloc:"); |
| 26 |
|
| 27 |
return nil; |
| 28 |
} |
| 29 |
|
| 30 |
- (id)initWithPrimitive:(xmlKindPtr)kindPtr freeOnDealloc:(BOOL)flag |
| 31 |
{ |
| 32 |
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes. |
| 33 |
NSAssert(NO, @"Use initWithDocPrimitive:freeOnDealloc:"); |
| 34 |
|
| 35 |
[self release]; |
| 36 |
return nil; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Initializes and returns a DDXMLDocument object created from an NSData object. |
| 41 |
* |
| 42 |
* Returns an initialized DDXMLDocument object, or nil if initialization fails |
| 43 |
* because of parsing errors or other reasons. |
| 44 |
**/ |
| 45 |
- (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error |
| 46 |
{ |
| 47 |
return [self initWithData:[string dataUsingEncoding:NSUTF8StringEncoding] |
| 48 |
options:mask |
| 49 |
error:error]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Initializes and returns a DDXMLDocument object created from an NSData object. |
| 54 |
* |
| 55 |
* Returns an initialized DDXMLDocument object, or nil if initialization fails |
| 56 |
* because of parsing errors or other reasons. |
| 57 |
**/ |
| 58 |
- (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error |
| 59 |
{ |
| 60 |
if (data == nil || [data length] == 0) |
| 61 |
{ |
| 62 |
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:0 userInfo:nil]; |
| 63 |
|
| 64 |
[self release]; |
| 65 |
return nil; |
| 66 |
} |
| 67 |
|
| 68 |
// Even though xmlKeepBlanksDefault(0) is called in DDXMLNode's initialize method, |
| 69 |
// it has been documented that this call seems to get reset on the iPhone: |
| 70 |
// http://code.google.com/p/kissxml/issues/detail?id=8 |
| 71 |
// |
| 72 |
// Therefore, we call it again here just to be safe. |
| 73 |
xmlKeepBlanksDefault(0); |
| 74 |
|
| 75 |
xmlDocPtr doc = xmlParseMemory([data bytes], [data length]); |
| 76 |
if (doc == NULL) |
| 77 |
{ |
| 78 |
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:nil]; |
| 79 |
|
| 80 |
[self release]; |
| 81 |
return nil; |
| 82 |
} |
| 83 |
|
| 84 |
return [self initWithDocPrimitive:doc freeOnDealloc:YES]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the root element of the receiver. |
| 89 |
**/ |
| 90 |
- (DDXMLElement *)rootElement |
| 91 |
{ |
| 92 |
xmlDocPtr doc = (xmlDocPtr)genericPtr; |
| 93 |
|
| 94 |
// doc->children is a list containing possibly comments, DTDs, etc... |
| 95 |
|
| 96 |
xmlNodePtr rootNode = xmlDocGetRootElement(doc); |
| 97 |
|
| 98 |
if (rootNode != NULL) |
| 99 |
return [DDXMLElement nodeWithElementPrimitive:rootNode freeOnDealloc:NO]; |
| 100 |
else |
| 101 |
return nil; |
| 102 |
} |
| 103 |
|
| 104 |
- (NSData *)XMLData |
| 105 |
{ |
| 106 |
return [[self XMLString] dataUsingEncoding:NSUTF8StringEncoding]; |
| 107 |
} |
| 108 |
|
| 109 |
- (NSData *)XMLDataWithOptions:(NSUInteger)options |
| 110 |
{ |
| 111 |
return [[self XMLStringWithOptions:options] dataUsingEncoding:NSUTF8StringEncoding]; |
| 112 |
} |
| 113 |
|
| 114 |
@end |