3 * Copyright (c) 2011, Blue Static <http://www.bluestatic.org>
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 #import "VariableNode.h"
19 #include "NSXMLElementAdditions.h"
21 @implementation VariableNode
{
22 NSMutableArray
* _children
;
26 - (id)initWithXMLNode
:(NSXMLElement
*)node
{
27 if (self = [super init
]) {
28 _name
= [[[node attributeForName
:@
"name"] stringValue
] copy
];
29 _fullName
= [[[node attributeForName
:@
"fullname"] stringValue
] copy
];
30 _className
= [[[node attributeForName
:@
"classname"] stringValue
] copy
];
31 _type
= [[[node attributeForName
:@
"type"] stringValue
] copy
];
32 _nodeValue
= [[node base64DecodedValue
] copy
];
33 _children
= [[NSMutableArray alloc
] init
];
34 if ([node children
]) {
35 [self setChildrenFromXMLChildren
:[node children
]];
37 _childCount
= [[[node attributeForName
:@
"numchildren"] stringValue
] integerValue
];
38 _address
= [[[node attributeForName
:@
"address"] stringValue
] copy
];
43 - (void)setChildrenFromXMLChildren
:(NSArray
*)children
{
44 [self willChangeValueForKey
:@
"children"];
46 [_children removeAllObjects
];
48 for (NSXMLNode
* child
in children
) {
49 // Other child nodes may be the string value.
50 if ([child isKindOfClass
:[NSXMLElement
class]]) {
51 VariableNode
* node
= [[VariableNode alloc
] initWithXMLNode
:(NSXMLElement
*)child
];
52 // Don't include the CLASSNAME property as that information is retrieved
54 if (![node.name isEqualToString
:@
"CLASSNAME"])
55 [_children addObject
:node
];
59 [self didChangeValueForKey
:@
"children"];
63 return self.childCount
== 0;
66 - (NSString
*)displayType
{
67 if (self.className
!= nil) {
68 return [NSString stringWithFormat
:@
"%@ (%@)", self.className
, self.type
];
73 - (NSString
*)description
{
74 return [NSString stringWithFormat
:@
"<VariableNode %p : %@>", self, self.fullName
];
79 if (self.childCount
!= self.children.count
) {
82 // For non-leaf nodes, display the object structure by recursively printing
83 // the base64-decoded values.
84 NSMutableString
* mutableString
= [[NSMutableString alloc
] initWithString
:@
"(\n"];
85 for (VariableNode
* child
in self.children
) {
86 [self recusivelyFormatNode
:child appendTo
:mutableString depth
:1];
88 [mutableString appendString
:@
")"];
96 ////////////////////////////////////////////////////////////////////////////////
100 * Recursively builds a print_r()-style output by attaching the data to
101 * |stringBuilder| with indent level specified by |depth|.
103 - (void)recusivelyFormatNode
:(VariableNode
*)node
104 appendTo
:(NSMutableString
*)stringBuilder
105 depth
:(NSUInteger
)depth
107 // Create the indention string for this level.
108 NSString
* indent
= [@
"" stringByPaddingToLength
:depth withString
:@
"\t" startingAtIndex
:0];
111 // If this is a leaf node, simply append the key=>value pair.
112 [stringBuilder appendFormat
:@
"%@%@\t=>\t%@\n", indent
, node.name
, node
->_nodeValue
];
114 // If this node has children, increase the depth and recurse.
115 [stringBuilder appendFormat
:@
"%@%@\t=>\t(\n", indent
, node.name
];
116 for (VariableNode
* child
in node.children
) {
117 [self recusivelyFormatNode
:child appendTo
:stringBuilder depth
:depth
+ 1];
119 [stringBuilder appendFormat
:@
"%@)\n", indent
];