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"
22 @interface NSXMLElement (GDBpAdditions_Private
)
23 - (NSString
*)internalName
;
24 - (NSString
*)internalBase64DecodedValue
;
25 - (void)recursiveBase64DecodedValue
:(NSMutableString
*)stringBuilder
26 depth
:(NSUInteger
)depth
;
29 @implementation NSXMLElement (GDBpAdditions
)
32 * Returns whether or not this node has any children
36 return ([[[self attributeForName
:@
"children"] stringValue
] intValue
] == 0);
40 * Returns the "name" attribute.
42 - (NSString
*)internalName
44 return [[self attributeForName
:@
"name"] stringValue
];
48 * Does the actual work of decoding base64.
50 - (NSString
*)internalBase64DecodedValue
52 // The value of the node is base64 encoded.
53 if ([[[self attributeForName
:@
"encoding"] stringValue
] isEqualToString
:@
"base64"]) {
54 const char* src
= [[self stringValue
] UTF8String
];
55 NSUInteger srclen
= [[self stringValue
] length
];
57 int destlen
= modp_b64_decode_len(srclen
);
58 char* dest
= malloc(destlen
);
59 memset(dest
, 0, destlen
);
61 modp_b64_decode(dest
, src
, srclen
);
65 ret
= [NSString stringWithUTF8String
:dest
];
71 // The value is just a normal string.
72 return [self stringValue
];
76 * Returns the value of the property
78 - (NSString
*)base64DecodedValue
81 // For non-leaf nodes, display the object structure by recursively printing
82 // the base64-decoded values.
83 NSMutableString
* mutableString
= [[NSMutableString alloc
] initWithString
:@
"(\n"];
84 [self recursiveBase64DecodedValue
:mutableString depth
:1];
85 [mutableString appendString
:@
")"];
86 return [mutableString autorelease
];
89 return [self internalBase64DecodedValue
];
93 * Recursively builds a print_r()-style output by attaching the data to
94 * |stringBuilder| with indent level specified by |depth|.
96 - (void)recursiveBase64DecodedValue
:(NSMutableString
*)stringBuilder
97 depth
:(NSUInteger
)depth
99 // Create the indention string for this level.
100 NSString
* indent
= [@
"" stringByPaddingToLength
:depth withString
:@
"\t" startingAtIndex
:0];
103 // If this is a leaf node, simply append the key=>value pair.
104 [stringBuilder appendFormat
:@
"%@%@\t=>\t%@\n", indent
, [self internalName
], [self internalBase64DecodedValue
]];
106 // If this node has children, increase the depth and recurse.
107 [stringBuilder appendFormat
:@
"%@%@\t=>\t(\n", indent
, [self internalName
]];
108 for (NSXMLElement
* elm
in [self children
]) {
109 [elm recursiveBase64DecodedValue
:stringBuilder depth
:depth
+ 1];
111 [stringBuilder appendFormat
:@
"%@)\n", indent
];