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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
* file.
*/
#include "config.h"
#include <string.h>
#import "OFXMLProcessingInstructions.h"
#import "OFXMLNode+Private.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFInvalidArgumentException.h"
@implementation OFXMLProcessingInstructions
+ (instancetype)processingInstructionsWithString: (OFString *)string
{
return [[[self alloc] initWithString: string] autorelease];
}
- (instancetype)initWithString: (OFString *)string
{
self = [super of_init];
@try {
_processingInstructions = [string copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
self = [super of_init];
@try {
void *pool = objc_autoreleasePoolPush();
if (![element.name isEqual: self.className] ||
![element.namespace isEqual: OF_SERIALIZATION_NS])
@throw [OFInvalidArgumentException exception];
_processingInstructions = [element.stringValue copy];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_processingInstructions release];
[super dealloc];
}
- (bool)isEqual: (id)object
{
OFXMLProcessingInstructions *processingInstructions;
if (object == self)
return true;
if (![object isKindOfClass: [OFXMLProcessingInstructions class]])
return false;
processingInstructions = object;
return [processingInstructions->_processingInstructions
isEqual: _processingInstructions];
}
- (unsigned long)hash
{
return _processingInstructions.hash;
}
- (OFString *)stringValue
{
return @"";
}
- (OFString *)XMLString
{
return [OFString stringWithFormat: @"<?%@?>", _processingInstructions];
}
- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
{
return [OFString stringWithFormat: @"<?%@?>", _processingInstructions];
}
- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
level: (unsigned int)level
{
OFString *ret;
if (indentation > 0 && level > 0) {
char *whitespaces = of_alloc((level * indentation) + 1, 1);
memset(whitespaces, ' ', level * indentation);
whitespaces[level * indentation] = 0;
@try {
ret = [OFString stringWithFormat:
@"%s<?%@?>", whitespaces, _processingInstructions];
} @finally {
free(whitespaces);
}
} else
ret = [OFString stringWithFormat: @"<?%@?>",
_processingInstructions];
return ret;
}
- (OFString *)description
{
return [OFString stringWithFormat: @"<?%@?>", _processingInstructions];
}
- (OFXMLElement *)XMLElementBySerializing
{
return [OFXMLElement elementWithName: self.className
namespace: OF_SERIALIZATION_NS
stringValue: _processingInstructions];
}
@end
|
|
|
|
>
|
>
>
|
>
|
>
|
>
>
|
<
<
>
|
>
>
>
>
>
>
|
>
|
|
|
|
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
|
<
<
>
|
>
|
>
>
>
>
|
|
<
<
<
|
>
>
|
|
|
|
>
|
>
>
>
>
>
>
>
|
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
* file.
*/
#include "config.h"
#include <string.h>
#import "OFXMLProcessingInstruction.h"
#import "OFString.h"
#import "OFXMLAttribute.h"
#import "OFXMLElement.h"
#import "OFXMLNode+Private.h"
#import "OFInvalidArgumentException.h"
@implementation OFXMLProcessingInstruction
@synthesize target = _target, data = _data;
+ (instancetype)processingInstructionWithTarget: (OFString *)target
data: (OFString *)data
{
return [[[self alloc] initWithTarget: target
data: data] autorelease];
}
- (instancetype)initWithTarget: (OFString *)target
data: (OFString *)data
{
self = [super of_init];
@try {
_target = [target copy];
_data = [data copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
@try {
void *pool = objc_autoreleasePoolPush();
OFXMLAttribute *targetAttr;
if (![element.name isEqual: self.className] ||
![element.namespace isEqual: OFSerializationNS])
@throw [OFInvalidArgumentException exception];
targetAttr = [element attributeForName: @"target"
namespace: OFSerializationNS];
if (targetAttr.stringValue.length == 0)
@throw [OFInvalidArgumentException exception];
self = [self initWithTarget: targetAttr.stringValue
data: element.stringValue];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_target release];
[_data release];
[super dealloc];
}
- (bool)isEqual: (id)object
{
OFXMLProcessingInstruction *processingInstruction;
if (object == self)
return true;
if (![object isKindOfClass: [OFXMLProcessingInstruction class]])
return false;
processingInstruction = object;
if (![processingInstruction->_target isEqual: _target])
return false;
if (processingInstruction->_data != _data &&
![processingInstruction->_data isEqual: _data])
return false;
return true;
}
- (unsigned long)hash
{
unsigned long hash;
OFHashInit(&hash);
OFHashAddHash(&hash, _target.hash);
OFHashAddHash(&hash, _data.hash);
OFHashFinalize(&hash);
return hash;
}
- (OFString *)stringValue
{
return @"";
}
- (OFString *)XMLString
{
if (_data.length > 0)
return [OFString stringWithFormat: @"<?%@ %@?>",
_target, _data];
else
return [OFString stringWithFormat: @"<?%@?>", _target];
}
- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
{
return self.XMLString;
}
- (OFString *)XMLStringWithIndentation: (unsigned int)indentation
level: (unsigned int)level
{
if (indentation > 0 && level > 0) {
OFString *ret;
char *whitespaces = OFAllocMemory((level * indentation) + 1, 1);
memset(whitespaces, ' ', level * indentation);
whitespaces[level * indentation] = 0;
@try {
if (_data.length > 0)
ret = [OFString stringWithFormat:
@"%s<?%@ %@?>", whitespaces,
_target, _data];
else
ret = [OFString stringWithFormat:
@"%s<?%@?>", whitespaces, _target];
} @finally {
OFFreeMemory(whitespaces);
}
return ret;
} else
return self.XMLString;
}
- (OFString *)description
{
return self.XMLString;
}
- (OFXMLElement *)XMLElementBySerializing
{
OFXMLElement *ret = [OFXMLElement elementWithName: self.className
namespace: OFSerializationNS
stringValue: _data];
void *pool = objc_autoreleasePoolPush();
[ret addAttribute: [OFXMLAttribute attributeWithName: @"target"
stringValue: _target]];
objc_autoreleasePoolPop(pool);
return ret;
}
@end
|