Use NSData's base64 encoding/decoding and delete modp_b64.
[macgdbp.git] / Source / NSXMLElementAdditions.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import <Cocoa/Cocoa.h>
18
19 #import "AppDelegate.h"
20
21 @interface NSXMLElement (GDBpAdditions_Private)
22 - (NSString*)internalName;
23 - (NSString*)internalBase64DecodedValue;
24 - (void)recursiveBase64DecodedValue:(NSMutableString*)stringBuilder
25 depth:(NSUInteger)depth;
26 @end
27
28 @implementation NSXMLElement (GDBpAdditions)
29
30 /**
31 * Returns whether or not this node has any children
32 */
33 - (BOOL)isLeaf
34 {
35 return ([[[self attributeForName:@"children"] stringValue] intValue] == 0);
36 }
37
38 /**
39 * Returns the "name" attribute.
40 */
41 - (NSString*)internalName
42 {
43 return [[self attributeForName:@"name"] stringValue];
44 }
45
46 /**
47 * Does the actual work of decoding base64.
48 */
49 - (NSString*)internalBase64DecodedValue
50 {
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];
54 if (base64Data) {
55 return [[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding] autorelease];
56 } else {
57 return @"<< Failed to base64-decode data >>";
58 }
59 }
60
61 // The value is just a normal string.
62 return [self stringValue];
63 }
64
65 /**
66 * Returns the value of the property
67 */
68 - (NSString*)base64DecodedValue
69 {
70 if (![self isLeaf]) {
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];
77 }
78
79 return [self internalBase64DecodedValue];
80 }
81
82 /**
83 * Recursively builds a print_r()-style output by attaching the data to
84 * |stringBuilder| with indent level specified by |depth|.
85 */
86 - (void)recursiveBase64DecodedValue:(NSMutableString*)stringBuilder
87 depth:(NSUInteger)depth
88 {
89 // Create the indention string for this level.
90 NSString* indent = [@"" stringByPaddingToLength:depth withString:@"\t" startingAtIndex:0];
91
92 if ([self isLeaf]) {
93 // If this is a leaf node, simply append the key=>value pair.
94 [stringBuilder appendFormat:@"%@%@\t=>\t%@\n", indent, [self internalName], [self internalBase64DecodedValue]];
95 } else {
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];
100 }
101 [stringBuilder appendFormat:@"%@)\n", indent];
102 }
103 }
104
105 @end