Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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