Switch away from deprecated base64 methods.
[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* data = [[self stringValue] dataUsingEncoding:NSASCIIStringEncoding];
54 NSData* base64Data = [[[NSData alloc] initWithBase64EncodedData:data options:0] autorelease];
55 if (base64Data) {
56 return [[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding] autorelease];
57 } else {
58 return @"<< Failed to base64-decode data >>";
59 }
60 }
61
62 // The value is just a normal string.
63 return [self stringValue];
64 }
65
66 /**
67 * Returns the value of the property
68 */
69 - (NSString*)base64DecodedValue
70 {
71 if (![self isLeaf]) {
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:@")"];
77 return [mutableString autorelease];
78 }
79
80 return [self internalBase64DecodedValue];
81 }
82
83 /**
84 * Recursively builds a print_r()-style output by attaching the data to
85 * |stringBuilder| with indent level specified by |depth|.
86 */
87 - (void)recursiveBase64DecodedValue:(NSMutableString*)stringBuilder
88 depth:(NSUInteger)depth
89 {
90 // Create the indention string for this level.
91 NSString* indent = [@"" stringByPaddingToLength:depth withString:@"\t" startingAtIndex:0];
92
93 if ([self isLeaf]) {
94 // If this is a leaf node, simply append the key=>value pair.
95 [stringBuilder appendFormat:@"%@%@\t=>\t%@\n", indent, [self internalName], [self internalBase64DecodedValue]];
96 } else {
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];
101 }
102 [stringBuilder appendFormat:@"%@)\n", indent];
103 }
104 }
105
106 @end