-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTNXMLNode.j
329 lines (279 loc) · 7.93 KB
/
TNXMLNode.j
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* TNXMLNode.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/Foundation.j>
@import "Resources/Strophe/strophe.js"
@import "TNStropheUtils.j"
/*! @ingroup strophecappuccino
This is an implementation of a really basic XML node in Cappuccino
*/
@implementation TNXMLNode : CPObject
{
id _xmlNode @accessors(readonly, getter=xmlNode);
}
#pragma mark -
#pragma mark Class methods
/*! create an instance of a TNXMLNode from a pure javascript Node
@param aNode a pure Javascript DOM Element
@return an instance of TNXMLNode initialized with aNode
*/
+ (TNXMLNode)nodeWithXMLNode:(id)aNode
{
return [[TNXMLNode alloc] initWithNode:aNode];
}
/*! create an instance of a TNXMLNode from a pure javascript Node
@param aName the name of the node
@return an instance of TNXMLNode initialized with aName
*/
+ (TNXMLNode)nodeWithName:(CPString)aName
{
return [[TNXMLNode alloc] initWithName:aName andAttributes:nil];
}
/*! create an instance of a TNXMLNode from a pure javascript Node
@param aName the name of the node
@param someAttributes CPDictionary containing the attributes
@return an instance of TNXMLNode initialized with aName and attributes
*/
+ (TNXMLNode)nodeWithName:(CPString)aName andAttributes:(CPDictionary)someAttributes
{
return [[TNXMLNode alloc] initWithName:aName andAttributes:someAttributes];
}
#pragma mark -
#pragma mark Initialization
/*! initialize an instance of a TNXMLNode from a pure javascript Node
@param aNode a pure Javascript DOM Element
@return an instance of TNXMLNode initialized with aNode
*/
- (TNXMLNode)initWithNode:(id)aNode
{
if (self = [super init])
{
if ((aNode.c) && (aNode.c) != undefined)
{
_xmlNode = aNode;
}
else
{
_xmlNode = new Strophe.Builder('msg');
_xmlNode.nodeTree = aNode;
_xmlNode.node = aNode;
}
}
return self;
}
/*! initialize an instance of a TNXMLNode with root node and attributes
@param aName name of the root tag
@param attributes CPDictionary contains all attributes
@return an instance of TNXMLNode initialized
*/
- (TNXMLNode)initWithName:(CPString)aName andAttributes:(CPDictionary)attributes
{
if (self = [super init])
{
_xmlNode = new Strophe.Builder(aName, attributes);
}
return self;
}
#pragma mark -
#pragma mark Representation & Navigation
/*! copy the current TNXMLNode
@return a copy of this
*/
- (TNXMLNode)copy
{
return [TNXMLNode nodeWithXMLNode:Strophe.copyElement([self tree])];
}
/*! append a node to the current node
@param aNode the dom element to add.
*/
- (void)addNode:(TNXMLNode)aNode
{
if (!aNode)
return;
_xmlNode.cnode([aNode tree]);
}
/*! Add text value to the current seletected node
@param aText name of the new tag
*/
- (void)addTextNode:(CPString)aText
{
_xmlNode = _xmlNode.t(aText);
}
/*! get the text node value
@return CPString of the content of node
*/
- (CPString)text
{
return TNStropheStripHTMLCharCode(Strophe.getText([self tree]));
}
/*! return a DOM Element of the TNXMLNode
@return an DOM Element
*/
- (id)tree
{
return _xmlNode.tree();
}
/*! Move the pointer to the parent of the current node
*/
- (BOOL)up
{
if (_xmlNode.node && _xmlNode.node.parentNode)
{
_xmlNode.up();
return YES;
}
return NO;
}
/*! convert the TNXMLNode into its string representation
@return string representation of the TNXMLNode
*/
- (CPString)stringValue
{
//return Strophe.toString(_xmlNode);
return Strophe.serialize(_xmlNode);
}
- (CPString)description
{
return [self stringValue];
}
#pragma mark -
#pragma mark Attributes
/*! get value of an attribute of the current node
@param anAttribute the attribute
@return the value of anAttribute
*/
- (CPString)valueForAttribute:(CPString)anAttribute
{
return [self tree].getAttribute(anAttribute);
}
/*! allow to set a value for a given attribute
@param aValue the value
@param anAttribute the attribute name
*/
- (void)setValue:(CPString)aValue forAttribute:(CPString)anAttribute
{
var attr = {};
attr[anAttribute] = aValue;
_xmlNode.attrs(attr);
}
/*! return the name of the current node
@return CPString containing the name of the current node
*/
- (CPString)name
{
return [self tree].tagName;
}
/*! get the xmlns field of the node
@return xmlns field of node
*/
- (CPString)namespace
{
return [self valueForAttribute:@"xmlns"];
}
/*! set the xmlns field of the node
@param the new xmls value
*/
- (void)setNamespace:(CPString)aNamespace
{
[self setValue:aNamespace forAttribute:@"xmlns"];
}
#pragma mark -
#pragma mark Children
/*! Add a child to the current seletected node
This will move the stanza object pointer to the child node
@param aTagName name of the new tag
@param attributes CPDictionary contains all attributes
*/
- (void)addChildWithName:(CPString)aTagName andAttributes:(CPDictionary)attributes
{
_xmlNode = _xmlNode.c(aTagName, attributes);
}
/*! Add a child to the current seletected node
This will move the stanza object pointer to the child node
@param aTagName name of the new tag
*/
- (void)addChildWithName:(CPString)aTagName
{
[self addChildWithName:aTagName andAttributes:{}];
}
/*! get an CPArray of TNXMLNode with matching tag name
@param aName the name tags should match
@return CPArray of TNXMLNode
*/
- (CPArray)childrenWithName:(CPString)aName
{
var nodes = [CPArray array],
elements = [self tree].getElementsByTagName(aName);
for (var i = 0; i < elements.length; i++)
[nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
return nodes;
}
/*! return all direct children wth given name.
@param aName the name tags should match
@return CPArray of TNXMLNode
*/
- (CPArray)ownChildrenWithName:(CPString)aName
{
var nodes = [CPArray array],
elements = [self tree].childNodes;
for (var i = 0; i < elements.length; i++)
if ((aName === nil) || (aName && elements [i].tagName == aName))
[nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
return nodes;
}
/*! get the first TNXMLNode that matching tag name
@param aName the name tags should match
@return the first matching TNXMLNode
*/
- (TNXMLNode)firstChildWithName:(CPString)aName
{
var elements = [self tree].getElementsByTagName(aName);
if (elements && (elements.length > 0))
return [TNXMLNode nodeWithXMLNode:elements[0]];
else
return;
}
/*! get all the children of the current element
@return array of TNXMLNode children
*/
- (CPArray)children
{
return [self ownChildrenWithName:nil];
}
- (BOOL)containsChildrenWithName:(CPString)aName
{
return ([self firstChildWithName:aName]) ? YES : NO;
}
@end
@implementation TNXMLNode (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super init])
{
// _xmlNode = [aCoder decodeObjectForKey:@"_xmlNode"];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
// if ([super respondsToSelector:@selector(encodeWithCoder:)])
// [super encodeWithCoder:aCoder];
//[aCoder encodeObject:_xmlNode forKey:@"_xmlNode"];
}
@end