@@ -10,17 +10,27 @@ - (id)initWithName:(NSString *)name
10
10
// Note: Make every guarantee that genericPtr is not null
11
11
12
12
xmlNodePtr node = xmlNewNode (NULL , [name xmlChar ]);
13
+ if (node == NULL )
14
+ {
15
+ [self release ];
16
+ return nil ;
17
+ }
13
18
14
- return [self initWithPrimitive : (xmlKindPtr)node];
19
+ return [self initWithCheckedPrimitive : (xmlKindPtr)node];
15
20
}
16
21
17
22
- (id )initWithName : (NSString *)name URI : (NSString *)URI
18
23
{
19
24
// Note: Make every guarantee that genericPtr is not null
20
25
21
26
xmlNodePtr node = xmlNewNode (NULL , [name xmlChar ]);
27
+ if (node == NULL )
28
+ {
29
+ [self release ];
30
+ return nil ;
31
+ }
22
32
23
- id result = [self initWithPrimitive : (xmlKindPtr)node];
33
+ DDXMLElement * result = [self initWithCheckedPrimitive : (xmlKindPtr)node];
24
34
[result setURI: URI];
25
35
26
36
return result;
@@ -31,8 +41,13 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string
31
41
// Note: Make every guarantee that genericPtr is not null
32
42
33
43
xmlNodePtr node = xmlNewNode (NULL , [name xmlChar ]);
44
+ if (node == NULL )
45
+ {
46
+ [self release ];
47
+ return nil ;
48
+ }
34
49
35
- id result = [self initWithPrimitive : (xmlKindPtr)node];
50
+ DDXMLElement * result = [self initWithCheckedPrimitive : (xmlKindPtr)node];
36
51
[result setStringValue: string];
37
52
38
53
return result;
@@ -41,33 +56,57 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string
41
56
- (id )initWithXMLString : (NSString *)string error : (NSError **)error
42
57
{
43
58
DDXMLDocument *doc = [[DDXMLDocument alloc ] initWithXMLString: string options: 0 error: error];
44
-
45
59
if (doc == nil )
46
60
{
61
+ [self release ];
47
62
return nil ;
48
63
}
49
64
50
- DDXMLNode *result = [doc rootElement ];
65
+ DDXMLElement *result = [doc rootElement ];
51
66
[result detach ];
52
67
[doc release ];
53
68
69
+ [self release ];
54
70
return [result retain ];
55
71
}
56
72
57
73
+ (id )nodeWithPrimitive : (xmlKindPtr)nodePtr
58
74
{
59
- return [[[DDXMLElement alloc ] initWithPrimitive: nodePtr] autorelease ];
75
+ if (nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
76
+ {
77
+ return nil ;
78
+ }
79
+
80
+ xmlNodePtr node = (xmlNodePtr)nodePtr;
81
+ if (node->_private == NULL )
82
+ return [[[DDXMLElement alloc ] initWithCheckedPrimitive: nodePtr] autorelease ];
83
+ else
84
+ return [[((DDXMLElement *)(node->_private)) retain ] autorelease ];
60
85
}
61
86
62
- - (id )initWithPrimitive : (xmlKindPtr)nodePtr
87
+ - (id )initWithUncheckedPrimitive : (xmlKindPtr)nodePtr
63
88
{
64
89
if (nodePtr == NULL || nodePtr->type != XML_ELEMENT_NODE)
65
90
{
66
91
[self release ];
67
92
return nil ;
68
93
}
69
94
70
- self = [super initWithPrimitive: nodePtr];
95
+ xmlNodePtr node = (xmlNodePtr)nodePtr;
96
+ if (node->_private == NULL )
97
+ {
98
+ return [self initWithCheckedPrimitive: nodePtr];
99
+ }
100
+ else
101
+ {
102
+ [self release ];
103
+ return [((DDXMLElement *)(node->_private)) retain ];
104
+ }
105
+ }
106
+
107
+ - (id )initWithCheckedPrimitive : (xmlKindPtr)nodePtr
108
+ {
109
+ self = [super initWithCheckedPrimitive: nodePtr];
71
110
return self;
72
111
}
73
112
@@ -174,10 +213,7 @@ - (NSArray *)elementsWithName:(NSString *)name uri:(NSString *)uri
174
213
175
214
if (match)
176
215
{
177
- DDXMLElement *childElement = [[DDXMLElement alloc ] initWithPrimitive: (xmlKindPtr)child];
178
-
179
- [result addObject: childElement];
180
- [childElement release ];
216
+ [result addObject: [DDXMLElement nodeWithPrimitive: (xmlKindPtr)child]];
181
217
}
182
218
}
183
219
@@ -241,10 +277,7 @@ - (NSArray *)attributes
241
277
xmlAttrPtr attr = ((xmlNodePtr)genericPtr)->properties ;
242
278
while (attr != NULL )
243
279
{
244
- DDXMLNode *attrNode = [[DDXMLNode alloc ] initWithPrimitive: (xmlKindPtr)attr];
245
-
246
- [result addObject: attrNode];
247
- [attrNode release ];
280
+ [result addObject: [DDXMLNode nodeWithPrimitive: (xmlKindPtr)attr]];
248
281
249
282
attr = attr->next ;
250
283
}
@@ -362,10 +395,7 @@ - (NSArray *)namespaces
362
395
xmlNsPtr ns = ((xmlNodePtr)genericPtr)->nsDef ;
363
396
while (ns != NULL )
364
397
{
365
- DDXMLNode *nsNode = [[DDXMLNode alloc ] initWithPrimitive: (xmlKindPtr)ns nsParent: (xmlNodePtr)genericPtr];
366
-
367
- [result addObject: nsNode];
368
- [nsNode release ];
398
+ [result addObject: [DDXMLNode nodeWithPrimitive: (xmlKindPtr)ns nsParent: (xmlNodePtr)genericPtr]];
369
399
370
400
ns = ns->next ;
371
401
}
0 commit comments