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
* data
= [[self stringValue
] dataUsingEncoding
:NSASCIIStringEncoding
];
54 NSData
* base64Data
= [[NSData alloc
] initWithBase64EncodedData
:data options
:0];
56 return [[NSString alloc
] initWithData
:base64Data encoding
:NSUTF8StringEncoding
];
58 return @
"<< Failed to base64-decode data >>";
62 // The value is just a normal string.
63 return [self stringValue
];
67 * Returns the value of the property
69 - (NSString
*)base64DecodedValue
72 // For non-leaf nodes, display the object structure by recursively printing
73 // the base64-decoded values.
74 NSMutableString
* mutableString
= [[NSMutableString alloc
] initWithString
:@
"(\n"];
75 [self recursiveBase64DecodedValue
:mutableString depth
:1];
76 [mutableString appendString
:@
")"];
80 return [self internalBase64DecodedValue
];
84 * Recursively builds a print_r()-style output by attaching the data to
85 * |stringBuilder| with indent level specified by |depth|.
87 - (void)recursiveBase64DecodedValue
:(NSMutableString
*)stringBuilder
88 depth
:(NSUInteger
)depth
90 // Create the indention string for this level.
91 NSString
* indent
= [@
"" stringByPaddingToLength
:depth withString
:@
"\t" startingAtIndex
:0];
94 // If this is a leaf node, simply append the key=>value pair.
95 [stringBuilder appendFormat
:@
"%@%@\t=>\t%@\n", indent
, [self internalName
], [self internalBase64DecodedValue
]];
97 // If this node has children, increase the depth and recurse.
98 [stringBuilder appendFormat
:@
"%@%@\t=>\t(\n", indent
, [self internalName
]];
99 for (NSXMLElement
* elm
in [self children
]) {
100 [elm recursiveBase64DecodedValue
:stringBuilder depth
:depth
+ 1];
102 [stringBuilder appendFormat
:@
"%@)\n", indent
];