3 * Copyright (c) 2007 - 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 <Cocoa/Cocoa.h>
19 #import "AppDelegate.h"
21 @interface NSXMLElement (GDBpAdditions_Private
)
22 - (NSString
*)internalName
;
23 - (NSString
*)internalBase64DecodedValue
;
24 - (void)recursiveBase64DecodedValue
:(NSMutableString
*)stringBuilder
25 depth
:(NSUInteger
)depth
;
28 @implementation NSXMLElement (GDBpAdditions
)
31 * Returns whether or not this node has any children
35 return ([[[self attributeForName
:@
"children"] stringValue
] intValue
] == 0);
39 * Returns the "name" attribute.
41 - (NSString
*)internalName
43 return [[self attributeForName
:@
"name"] stringValue
];
47 * Does the actual work of decoding base64.
49 - (NSString
*)internalBase64DecodedValue
51 // The value of the node is base64 encoded.
52 if ([[[self attributeForName
:@
"encoding"] stringValue
] isEqualToString
:@
"base64"]) {
53 NSData
* base64Data
= [[[NSData alloc
] initWithBase64Encoding
:[self stringValue
]] autorelease
];
55 return [[[NSString alloc
] initWithData
:base64Data encoding
:NSUTF8StringEncoding
] autorelease
];
57 return @
"<< Failed to base64-decode data >>";
61 // The value is just a normal string.
62 return [self stringValue
];
66 * Returns the value of the property
68 - (NSString
*)base64DecodedValue
71 // For non-leaf nodes, display the object structure by recursively printing
72 // the base64-decoded values.
73 NSMutableString
* mutableString
= [[NSMutableString alloc
] initWithString
:@
"(\n"];
74 [self recursiveBase64DecodedValue
:mutableString depth
:1];
75 [mutableString appendString
:@
")"];
76 return [mutableString autorelease
];
79 return [self internalBase64DecodedValue
];
83 * Recursively builds a print_r()-style output by attaching the data to
84 * |stringBuilder| with indent level specified by |depth|.
86 - (void)recursiveBase64DecodedValue
:(NSMutableString
*)stringBuilder
87 depth
:(NSUInteger
)depth
89 // Create the indention string for this level.
90 NSString
* indent
= [@
"" stringByPaddingToLength
:depth withString
:@
"\t" startingAtIndex
:0];
93 // If this is a leaf node, simply append the key=>value pair.
94 [stringBuilder appendFormat
:@
"%@%@\t=>\t%@\n", indent
, [self internalName
], [self internalBase64DecodedValue
]];
96 // If this node has children, increase the depth and recurse.
97 [stringBuilder appendFormat
:@
"%@%@\t=>\t(\n", indent
, [self internalName
]];
98 for (NSXMLElement
* elm
in [self children
]) {
99 [elm recursiveBase64DecodedValue
:stringBuilder depth
:depth
+ 1];
101 [stringBuilder appendFormat
:@
"%@)\n", indent
];