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