1
- #import " DDXMLDocument.h"
2
- #import " NSStringAdditions.h"
3
1
#import " DDXMLPrivate.h"
2
+ #import " NSString+DDXML.h"
4
3
5
4
6
5
@implementation DDXMLDocument
7
6
8
7
/* *
9
8
* Returns a DDXML wrapper object for the given primitive node.
10
9
* The given node MUST be non-NULL and of the proper type.
11
- *
12
- * If the wrapper object already exists, it is retained/autoreleased and returned.
13
- * Otherwise a new wrapper object is alloc/init/autoreleased and returned.
14
10
**/
15
- + (id )nodeWithPrimitive : (xmlKindPtr) kindPtr
11
+ + (id )nodeWithDocPrimitive : (xmlDocPtr) doc freeOnDealloc : ( BOOL ) flag
16
12
{
17
- // If a wrapper object already exists, the _private variable is pointing to it.
18
-
19
- xmlDocPtr doc = (xmlDocPtr)kindPtr;
20
- if (doc->_private == NULL )
21
- return [[[DDXMLDocument alloc ] initWithCheckedPrimitive: kindPtr] autorelease ];
22
- else
23
- return [[((DDXMLDocument *)(doc->_private)) retain ] autorelease ];
13
+ return [[[DDXMLDocument alloc ] initWithDocPrimitive: doc freeOnDealloc: flag] autorelease ];
24
14
}
25
15
26
- /* *
27
- * Returns a DDXML wrapper object for the given primitive node.
28
- * The given node MUST be non-NULL and of the proper type.
29
- *
30
- * The given node is checked, meaning a wrapper object for it does not already exist.
31
- **/
32
- - (id )initWithCheckedPrimitive : (xmlKindPtr)kindPtr
16
+ - (id )initWithDocPrimitive : (xmlDocPtr)doc freeOnDealloc : (BOOL )flag
33
17
{
34
- self = [super initWithCheckedPrimitive: kindPtr ];
18
+ self = [super initWithPrimitive: (xmlKindPtr)doc freeOnDealloc: flag ];
35
19
return self;
36
20
}
37
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
+
38
39
/* *
39
40
* Initializes and returns a DDXMLDocument object created from an NSData object.
40
41
*
@@ -43,7 +44,9 @@ - (id)initWithCheckedPrimitive:(xmlKindPtr)kindPtr
43
44
**/
44
45
- (id )initWithXMLString : (NSString *)string options : (NSUInteger )mask error : (NSError **)error
45
46
{
46
- return [self initWithData: [string dataUsingEncoding: NSUTF8StringEncoding] options: mask error: error];
47
+ return [self initWithData: [string dataUsingEncoding: NSUTF8StringEncoding]
48
+ options: mask
49
+ error: error];
47
50
}
48
51
49
52
/* *
@@ -54,9 +57,9 @@ - (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSErr
54
57
**/
55
58
- (id )initWithData : (NSData *)data options : (NSUInteger )mask error : (NSError **)error
56
59
{
57
- if (data == nil || [data length ] == 0 )
60
+ if (data == nil || [data length ] == 0 )
58
61
{
59
- if (error) *error = [NSError errorWithDomain: @" DDXMLErrorDomain" code: 0 userInfo: nil ];
62
+ if (error) *error = [NSError errorWithDomain: @" DDXMLErrorDomain" code: 0 userInfo: nil ];
60
63
61
64
[self release ];
62
65
return nil ;
@@ -70,15 +73,15 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
70
73
xmlKeepBlanksDefault (0 );
71
74
72
75
xmlDocPtr doc = xmlParseMemory ([data bytes ], [data length ]);
73
- if (doc == NULL )
76
+ if (doc == NULL )
74
77
{
75
- if (error) *error = [NSError errorWithDomain: @" DDXMLErrorDomain" code: 1 userInfo: nil ];
78
+ if (error) *error = [NSError errorWithDomain: @" DDXMLErrorDomain" code: 1 userInfo: nil ];
76
79
77
80
[self release ];
78
81
return nil ;
79
82
}
80
83
81
- return [self initWithCheckedPrimitive: (xmlKindPtr) doc];
84
+ return [self initWithDocPrimitive: doc freeOnDealloc: YES ];
82
85
}
83
86
84
87
/* *
@@ -92,8 +95,8 @@ - (DDXMLElement *)rootElement
92
95
93
96
xmlNodePtr rootNode = xmlDocGetRootElement (doc);
94
97
95
- if (rootNode != NULL )
96
- return [DDXMLElement nodeWithPrimitive: (xmlKindPtr) rootNode];
98
+ if (rootNode != NULL )
99
+ return [DDXMLElement nodeWithElementPrimitive: rootNode freeOnDealloc: NO ];
97
100
else
98
101
return nil ;
99
102
}
0 commit comments