Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 330 lines (279 sloc) 8.12 kB
eb88555 @benlangfeld Whitespace
benlangfeld authored
1 /*
a05ba16 add the missing files
Antoine Mercadal authored
2 * TNXMLNode.j
eb88555 @benlangfeld Whitespace
benlangfeld authored
3 *
3692668 use of LGPL instead of AGPL. La liberte n'admet par definition aucun …
Antoine Mercadal authored
4 * Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
eb88555 @benlangfeld Whitespace
benlangfeld authored
5 *
3692668 use of LGPL instead of AGPL. La liberte n'admet par definition aucun …
Antoine Mercadal authored
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3.0 of the License, or (at your option) any later version.
eb88555 @benlangfeld Whitespace
benlangfeld authored
10 *
3692668 use of LGPL instead of AGPL. La liberte n'admet par definition aucun …
Antoine Mercadal authored
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
1c50a12 add TNStropheJID
Antoine Mercadal authored
15 *
3692668 use of LGPL instead of AGPL. La liberte n'admet par definition aucun …
Antoine Mercadal authored
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a05ba16 add the missing files
Antoine Mercadal authored
19 */
20
21
22 @import <Foundation/Foundation.j>
23
7d51b13 @primalmotion Fix missing imports
primalmotion authored
24 @import "Resources/Strophe/strophe.js"
2ca313b @primalmotion Fix missing imports and bugs found thanks to new objj compiler
primalmotion authored
25 @import "TNStropheUtils.j"
7d51b13 @primalmotion Fix missing imports
primalmotion authored
26
a05ba16 add the missing files
Antoine Mercadal authored
27 /*! @ingroup strophecappuccino
28 This is an implementation of a really basic XML node in Cappuccino
29 */
30 @implementation TNXMLNode : CPObject
31 {
acae6d5 @primalmotion fix warnings
primalmotion authored
32 id _xmlNode @accessors(readonly, getter=xmlNode);
a05ba16 add the missing files
Antoine Mercadal authored
33 }
34
a7fb546 @benlangfeld Some more pragma marks
benlangfeld authored
35 #pragma mark -
36 #pragma mark Class methods
37
a05ba16 add the missing files
Antoine Mercadal authored
38 /*! create an instance of a TNXMLNode from a pure javascript Node
39 @param aNode a pure Javascript DOM Element
40 @return an instance of TNXMLNode initialized with aNode
41 */
42 + (TNXMLNode)nodeWithXMLNode:(id)aNode
43 {
44 return [[TNXMLNode alloc] initWithNode:aNode];
45 }
46
fe9afb1 add some smart initializers and fix a bug with addNode:
Antoine Mercadal authored
47 /*! create an instance of a TNXMLNode from a pure javascript Node
48 @param aName the name of the node
49 @return an instance of TNXMLNode initialized with aName
50 */
51 + (TNXMLNode)nodeWithName:(CPString)aName
52 {
53 return [[TNXMLNode alloc] initWithName:aName andAttributes:nil];
54 }
55
56 /*! create an instance of a TNXMLNode from a pure javascript Node
57 @param aName the name of the node
58 @param someAttributes CPDictionary containing the attributes
59 @return an instance of TNXMLNode initialized with aName and attributes
60 */
61 + (TNXMLNode)nodeWithName:(CPString)aName andAttributes:(CPDictionary)someAttributes
62 {
63 return [[TNXMLNode alloc] initWithName:aName andAttributes:someAttributes];
64 }
65
a7fb546 @benlangfeld Some more pragma marks
benlangfeld authored
66 #pragma mark -
67 #pragma mark Initialization
68
a05ba16 add the missing files
Antoine Mercadal authored
69 /*! initialize an instance of a TNXMLNode from a pure javascript Node
70 @param aNode a pure Javascript DOM Element
71 @return an instance of TNXMLNode initialized with aNode
72 */
73 - (TNXMLNode)initWithNode:(id)aNode
74 {
75 if (self = [super init])
76 {
77 if ((aNode.c) && (aNode.c) != undefined)
78 {
79 _xmlNode = aNode;
80 }
81 else
82 {
83 _xmlNode = new Strophe.Builder('msg');
84 _xmlNode.nodeTree = aNode;
85 _xmlNode.node = aNode;
86 }
87 }
88
89 return self;
90 }
91
92 /*! initialize an instance of a TNXMLNode with root node and attributes
93 @param aName name of the root tag
94 @param attributes CPDictionary contains all attributes
95 @return an instance of TNXMLNode initialized
96 */
97 - (TNXMLNode)initWithName:(CPString)aName andAttributes:(CPDictionary)attributes
98 {
99 if (self = [super init])
100 {
101 _xmlNode = new Strophe.Builder(aName, attributes);
102 }
103
104 return self;
105 }
106
a7fb546 @benlangfeld Some more pragma marks
benlangfeld authored
107 #pragma mark -
108 #pragma mark Representation & Navigation
109
a05ba16 add the missing files
Antoine Mercadal authored
110 /*! copy the current TNXMLNode
111 @return a copy of this
112 */
113 - (TNXMLNode)copy
114 {
115 return [TNXMLNode nodeWithXMLNode:Strophe.copyElement([self tree])];
116 }
117
118 /*! append a node to the current node
119 @param aNode the dom element to add.
120 */
fe9afb1 add some smart initializers and fix a bug with addNode:
Antoine Mercadal authored
121 - (void)addNode:(TNXMLNode)aNode
a05ba16 add the missing files
Antoine Mercadal authored
122 {
e452c88 don't crash if adding nil as textnode
Antoine Mercadal authored
123 if (!aNode)
124 return;
125
fe9afb1 add some smart initializers and fix a bug with addNode:
Antoine Mercadal authored
126 _xmlNode.cnode([aNode tree]);
a05ba16 add the missing files
Antoine Mercadal authored
127 }
128
129 /*! Add text value to the current seletected node
130 @param aText name of the new tag
131 */
132 - (void)addTextNode:(CPString)aText
133 {
134 _xmlNode = _xmlNode.t(aText);
135 }
136
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
137 /*! get the text node value
138 @return CPString of the content of node
a05ba16 add the missing files
Antoine Mercadal authored
139 */
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
140 - (CPString)text
a05ba16 add the missing files
Antoine Mercadal authored
141 {
5b6fdfb @primalmotion strip HTML special char when asking for TNXMLNode text
primalmotion authored
142 return TNStropheStripHTMLCharCode(Strophe.getText([self tree]));
a05ba16 add the missing files
Antoine Mercadal authored
143 }
144
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
145 /*! return a DOM Element of the TNXMLNode
146 @return an DOM Element
a05ba16 add the missing files
Antoine Mercadal authored
147 */
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
148 - (id)tree
a05ba16 add the missing files
Antoine Mercadal authored
149 {
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
150 return _xmlNode.tree();
a05ba16 add the missing files
Antoine Mercadal authored
151 }
152
153 /*! Move the pointer to the parent of the current node
154 */
155 - (BOOL)up
156 {
157 if (_xmlNode.node && _xmlNode.node.parentNode)
158 {
2ca313b @primalmotion Fix missing imports and bugs found thanks to new objj compiler
primalmotion authored
159 _xmlNode.up();
a05ba16 add the missing files
Antoine Mercadal authored
160 return YES;
161 }
162 return NO;
163 }
164
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
165 /*! convert the TNXMLNode into its string representation
166 @return string representation of the TNXMLNode
167 */
168 - (CPString)stringValue
169 {
170 //return Strophe.toString(_xmlNode);
171 return Strophe.serialize(_xmlNode);
172 }
173
174 - (CPString)description
175 {
176 return [self stringValue];
177 }
178
179 #pragma mark -
180 #pragma mark Attributes
181
a05ba16 add the missing files
Antoine Mercadal authored
182 /*! get value of an attribute of the current node
183 @param anAttribute the attribute
184 @return the value of anAttribute
185 */
186 - (CPString)valueForAttribute:(CPString)anAttribute
187 {
188 return [self tree].getAttribute(anAttribute);
189 }
190
191 /*! allow to set a value for a given attribute
192 @param aValue the value
193 @param anAttribute the attribute name
194 */
195 - (void)setValue:(CPString)aValue forAttribute:(CPString)anAttribute
196 {
197 var attr = {};
198
199 attr[anAttribute] = aValue;
200
201 _xmlNode.attrs(attr);
202 }
203
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
204 /*! return the name of the current node
205 @return CPString containing the name of the current node
206 */
207 - (CPString)name
208 {
209 return [self tree].tagName;
210 }
211
212 /*! get the xmlns field of the node
213 @return xmlns field of node
214 */
215 - (CPString)namespace
216 {
217 return [self valueForAttribute:@"xmlns"];
218 }
219
220 /*! set the xmlns field of the node
221 @param the new xmls value
222 */
223 - (void)setNamespace:(CPString)aNamespace
224 {
225 [self setValue:aNamespace forAttribute:@"xmlns"];
226 }
227
228 #pragma mark -
229 #pragma mark Children
230
231 /*! Add a child to the current seletected node
232 This will move the stanza object pointer to the child node
233 @param aTagName name of the new tag
234 @param attributes CPDictionary contains all attributes
235 */
236 - (void)addChildWithName:(CPString)aTagName andAttributes:(CPDictionary)attributes
237 {
238 _xmlNode = _xmlNode.c(aTagName, attributes);
239 }
240
241 /*! Add a child to the current seletected node
242 This will move the stanza object pointer to the child node
243 @param aTagName name of the new tag
244 */
245 - (void)addChildWithName:(CPString)aTagName
246 {
247 [self addChildWithName:aTagName andAttributes:{}];
248 }
249
a05ba16 add the missing files
Antoine Mercadal authored
250 /*! get an CPArray of TNXMLNode with matching tag name
251 @param aName the name tags should match
252 @return CPArray of TNXMLNode
253 */
254 - (CPArray)childrenWithName:(CPString)aName
255 {
256 var nodes = [CPArray array],
257 elements = [self tree].getElementsByTagName(aName);
258
259 for (var i = 0; i < elements.length; i++)
2c2f8d0 @benlangfeld Couple of minor issues
benlangfeld authored
260 [nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
a05ba16 add the missing files
Antoine Mercadal authored
261
262 return nodes;
263 }
264
265 /*! return all direct children wth given name.
266 @param aName the name tags should match
267 @return CPArray of TNXMLNode
268 */
269 - (CPArray)ownChildrenWithName:(CPString)aName
270 {
271 var nodes = [CPArray array],
272 elements = [self tree].childNodes;
273
274 for (var i = 0; i < elements.length; i++)
2c2f8d0 @benlangfeld Couple of minor issues
benlangfeld authored
275 if ((aName === nil) || (aName && elements [i].tagName == aName))
276 [nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
a05ba16 add the missing files
Antoine Mercadal authored
277
278 return nodes;
279 }
280
281 /*! get the first TNXMLNode that matching tag name
282 @param aName the name tags should match
283 @return the first matching TNXMLNode
284 */
285 - (TNXMLNode)firstChildWithName:(CPString)aName
286 {
287 var elements = [self tree].getElementsByTagName(aName);
288
289 if (elements && (elements.length > 0))
290 return [TNXMLNode nodeWithXMLNode:elements[0]];
291 else
b1c04c1 @benlangfeld return; is the same as return nil; and looks nicer
benlangfeld authored
292 return;
a05ba16 add the missing files
Antoine Mercadal authored
293 }
294
295 /*! get all the children of the current element
296 @return array of TNXMLNode children
297 */
298 - (CPArray)children
299 {
300 return [self ownChildrenWithName:nil];
301 }
302
303 - (BOOL)containsChildrenWithName:(CPString)aName
304 {
305 return ([self firstChildWithName:aName]) ? YES : NO;
306 }
307
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
308 @end
a05ba16 add the missing files
Antoine Mercadal authored
309
886bce6 @benlangfeld Tidy up class method grouping using pragma marks rather than categories
benlangfeld authored
310 @implementation TNXMLNode (CPCoding)
a05ba16 add the missing files
Antoine Mercadal authored
311
312 - (id)initWithCoder:(CPCoder)aCoder
313 {
3007cac fix CPCoding problems
Antoine Mercadal authored
314 if (self = [super init])
a05ba16 add the missing files
Antoine Mercadal authored
315 {
316 // _xmlNode = [aCoder decodeObjectForKey:@"_xmlNode"];
317 }
318
319 return self;
320 }
321
322 - (void)encodeWithCoder:(CPCoder)aCoder
323 {
324 // if ([super respondsToSelector:@selector(encodeWithCoder:)])
325 // [super encodeWithCoder:aCoder];
326
327 //[aCoder encodeObject:_xmlNode forKey:@"_xmlNode"];
328 }
5b6fdfb @primalmotion strip HTML special char when asking for TNXMLNode text
primalmotion authored
329 @end
Something went wrong with that request. Please try again.