StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNXMLNode.j
Go to the documentation of this file.
00001 /*
00002  * TNXMLNode.j
00003  *
00004  * Copyright (C) 2010  Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 3.0 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 
00023 
00027 @implementation TNXMLNode : CPObject
00028 {
00029     id  _xmlNode;
00030 }
00031 
00032 #pragma mark -
00033 #pragma mark Class methods
00034 
00039 + (TNXMLNode)nodeWithXMLNode:(id)aNode
00040 {
00041     return [[TNXMLNode alloc] initWithNode:aNode];
00042 }
00043 
00048 + (TNXMLNode)nodeWithName:(CPString)aName
00049 {
00050     return [[TNXMLNode alloc] initWithName:aName andAttributes:nil];
00051 }
00052 
00058 + (TNXMLNode)nodeWithName:(CPString)aName andAttributes:(CPDictionary)someAttributes
00059 {
00060     return [[TNXMLNode alloc] initWithName:aName andAttributes:someAttributes];
00061 }
00062 
00063 #pragma mark -
00064 #pragma mark Initialization
00065 
00070 - (TNXMLNode)initWithNode:(id)aNode
00071 {
00072     if (self = [super init])
00073     {
00074         if ((aNode.c) && (aNode.c) != undefined)
00075         {
00076             _xmlNode = aNode;
00077         }
00078         else
00079         {
00080             _xmlNode = new Strophe.Builder('msg');
00081             _xmlNode.nodeTree = aNode;
00082             _xmlNode.node = aNode;
00083         }
00084     }
00085 
00086     return self;
00087 }
00088 
00094 - (TNXMLNode)initWithName:(CPString)aName andAttributes:(CPDictionary)attributes
00095 {
00096     if (self = [super init])
00097     {
00098         _xmlNode = new Strophe.Builder(aName, attributes);
00099     }
00100 
00101     return self;
00102 }
00103 
00104 #pragma mark -
00105 #pragma mark Representation & Navigation
00106 
00110 - (TNXMLNode)copy
00111 {
00112     return [TNXMLNode nodeWithXMLNode:Strophe.copyElement([self tree])];
00113 }
00114 
00118 - (void)addNode:(TNXMLNode)aNode
00119 {
00120     if (!aNode)
00121         return;
00122 
00123     _xmlNode.cnode([aNode tree]);
00124 }
00125 
00129 - (void)addTextNode:(CPString)aText
00130 {
00131     _xmlNode = _xmlNode.t(aText);
00132 }
00133 
00137 - (CPString)text
00138 {
00139     return TNStropheStripHTMLCharCode(Strophe.getText([self tree]));
00140 }
00141 
00145 - (id)tree
00146 {
00147     return _xmlNode.tree();
00148 }
00149 
00152 - (BOOL)up
00153 {
00154     if (_xmlNode.node && _xmlNode.node.parentNode)
00155     {
00156         _xmlNode.up();
00157         return YES;
00158     }
00159     return NO;
00160 }
00161 
00165 - (CPString)stringValue
00166 {
00167     //return Strophe.toString(_xmlNode);
00168     return Strophe.serialize(_xmlNode);
00169 }
00170 
00171 - (CPString)description
00172 {
00173     return [self stringValue];
00174 }
00175 
00176 #pragma mark -
00177 #pragma mark Attributes
00178 
00183 - (CPString)valueForAttribute:(CPString)anAttribute
00184 {
00185     return [self tree].getAttribute(anAttribute);
00186 }
00187 
00192 - (void)setValue:(CPString)aValue forAttribute:(CPString)anAttribute
00193 {
00194     var attr = {};
00195 
00196     attr[anAttribute] = aValue;
00197 
00198     _xmlNode.attrs(attr);
00199 }
00200 
00204 - (CPString)name
00205 {
00206     return [self tree].tagName;
00207 }
00208 
00212 - (CPString)namespace
00213 {
00214     return [self valueForAttribute:@"xmlns"];
00215 }
00216 
00220 - (void)setNamespace:(CPString)aNamespace
00221 {
00222     [self setValue:aNamespace forAttribute:@"xmlns"];
00223 }
00224 
00225 #pragma mark -
00226 #pragma mark Children
00227 
00233 - (void)addChildWithName:(CPString)aTagName andAttributes:(CPDictionary)attributes
00234 {
00235     _xmlNode = _xmlNode.c(aTagName, attributes);
00236 }
00237 
00242 - (void)addChildWithName:(CPString)aTagName
00243 {
00244     [self addChildWithName:aTagName andAttributes:{}];
00245 }
00246 
00251 - (CPArray)childrenWithName:(CPString)aName
00252 {
00253     var nodes       = [CPArray array],
00254         elements    = [self tree].getElementsByTagName(aName);
00255 
00256     for (var i = 0; i < elements.length; i++)
00257         [nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
00258 
00259     return nodes;
00260 }
00261 
00266 - (CPArray)ownChildrenWithName:(CPString)aName
00267 {
00268     var nodes       = [CPArray array],
00269         elements    = [self tree].childNodes;
00270 
00271     for (var i = 0; i < elements.length; i++)
00272         if ((aName === nil) || (aName && elements [i].tagName == aName))
00273             [nodes addObject:[TNXMLNode nodeWithXMLNode:elements[i]]]
00274 
00275     return nodes;
00276 }
00277 
00282 - (TNXMLNode)firstChildWithName:(CPString)aName
00283 {
00284     var elements = [self tree].getElementsByTagName(aName);
00285 
00286     if (elements && (elements.length > 0))
00287         return [TNXMLNode nodeWithXMLNode:elements[0]];
00288     else
00289         return;
00290 }
00291 
00295 - (CPArray)children
00296 {
00297     return [self ownChildrenWithName:nil];
00298 }
00299 
00300 - (BOOL)containsChildrenWithName:(CPString)aName
00301 {
00302     return ([self firstChildWithName:aName]) ? YES : NO;
00303 }
00304 
00305 @end
00306 
00307 @implementation TNXMLNode (CPCoding)
00308 
00309 - (id)initWithCoder:(CPCoder)aCoder
00310 {
00311     if (self = [super init])
00312     {
00313        // _xmlNode = [aCoder decodeObjectForKey:@"_xmlNode"];
00314     }
00315 
00316     return self;
00317 }
00318 
00319 - (void)encodeWithCoder:(CPCoder)aCoder
00320 {
00321     // if ([super respondsToSelector:@selector(encodeWithCoder:)])
00322     //     [super encodeWithCoder:aCoder];
00323 
00324     //[aCoder encodeObject:_xmlNode forKey:@"_xmlNode"];
00325 }
00326 @end
00327 
00328 @implementation TNXMLNode (CPSynthesizedAccessors)
00329 
00333 - (id)xmlNode
00334 {
00335     return _xmlNode;
00336 }
00337 
00338 @end
 All Classes Namespaces Files Functions Variables