Skip to content

Commit

Permalink
Simplification of header; assuming LLVM 3.0 due to ARC requirement. N…
Browse files Browse the repository at this point in the history
…ew -fullDescription doesn't truncate values.
  • Loading branch information
nfarina committed Oct 19, 2011
1 parent 2748bb2 commit b7e8da4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
24 changes: 7 additions & 17 deletions SMXMLDocument.h
Expand Up @@ -28,23 +28,15 @@ extern NSString *const SMXMLDocumentErrorDomain;

@class SMXMLDocument;

@interface SMXMLElement : NSObject<NSXMLParserDelegate> {
@private
__unsafe_unretained SMXMLDocument *document; // nonretained
__unsafe_unretained SMXMLElement *parent; // nonretained
NSString *name;
NSMutableString *value;
NSMutableArray *children;
NSDictionary *attributes;
}
@interface SMXMLElement : NSObject<NSXMLParserDelegate>

@property (nonatomic, assign) SMXMLDocument *document;
@property (nonatomic, assign) SMXMLElement *parent;
@property (nonatomic, weak) SMXMLDocument *document;
@property (nonatomic, weak) SMXMLElement *parent;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSString *value;
@property (nonatomic, retain) NSArray *children;
@property (nonatomic, readonly) SMXMLElement *firstChild, *lastChild;
@property (nonatomic, retain) NSDictionary *attributes;
@property (nonatomic, readonly) SMXMLElement *firstChild, *lastChild;

- (id)initWithDocument:(SMXMLDocument *)document;
- (SMXMLElement *)childNamed:(NSString *)name;
Expand All @@ -56,17 +48,15 @@ extern NSString *const SMXMLDocumentErrorDomain;

@end

@interface SMXMLDocument : NSObject<NSXMLParserDelegate> {
@private
SMXMLElement *root;
NSError *error;
}
@interface SMXMLDocument : NSObject<NSXMLParserDelegate>

@property (nonatomic, retain) SMXMLElement *root;
@property (nonatomic, retain) NSError *error;

- (id)initWithData:(NSData *)data error:(NSError **)outError;

- (NSString *)fullDescription; // like -description, this writes the document out to an XML string, but doesn't truncate the node values.

+ (SMXMLDocument *)documentWithData:(NSData *)data error:(NSError **)outError;

@end
26 changes: 15 additions & 11 deletions SMXMLDocument.m
Expand Up @@ -22,42 +22,42 @@ - (id)initWithDocument:(SMXMLDocument *)aDocument {
return self;
}

- (NSString *)descriptionWithIndent:(NSString *)indent {
- (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)truncated {

NSMutableString *s = [NSMutableString string];
[s appendFormat:@"%@<%@", indent, name];

for (NSString *attribute in attributes)
[s appendFormat:@" %@=\"%@\"", attribute, [attributes objectForKey:attribute]];

NSString *trimVal = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if (trimVal.length > 25)
trimVal = [NSString stringWithFormat:@"%@", [trimVal substringToIndex:25]];
NSString *valueOrTrimmed = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (truncated && valueOrTrimmed.length > 25)
valueOrTrimmed = [NSString stringWithFormat:@"%@", [valueOrTrimmed substringToIndex:25]];

if (children.count) {
[s appendString:@">\n"];

NSString *childIndent = [indent stringByAppendingString:@" "];

if (trimVal.length)
[s appendFormat:@"%@%@\n", childIndent, trimVal];
if (valueOrTrimmed.length)
[s appendFormat:@"%@%@\n", childIndent, valueOrTrimmed];

for (SMXMLElement *child in children)
[s appendFormat:@"%@\n", [child descriptionWithIndent:childIndent]];
[s appendFormat:@"%@\n", [child descriptionWithIndent:childIndent truncatedValues:truncated]];

[s appendFormat:@"%@</%@>", indent, name];
}
else if (trimVal.length) {
[s appendFormat:@">%@</%@>", trimVal, name];
else if (valueOrTrimmed.length) {
[s appendFormat:@">%@</%@>", valueOrTrimmed, name];
}
else [s appendString:@"/>"];

return s;
}

- (NSString *)description {
return [self descriptionWithIndent:@""];
return [self descriptionWithIndent:@"" truncatedValues:YES];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
Expand Down Expand Up @@ -179,4 +179,8 @@ - (NSString *)description {
return root.description;
}

- (NSString *)fullDescription {
return [root descriptionWithIndent:@"" truncatedValues:NO];
}

@end
Expand Up @@ -8,8 +8,8 @@

/* Begin PBXBuildFile section */
0173BC9C12E765430037CA90 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BC9B12E765430037CA90 /* AppDelegate.m */; };
0173BCAA12E7656F0037CA90 /* SMXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BCA812E7656F0037CA90 /* SMXMLDocument.m */; };
0173BCC312E766160037CA90 /* sample.xml in Resources */ = {isa = PBXBuildFile; fileRef = 0173BCC212E766160037CA90 /* sample.xml */; };
01B0AE9D144DE6A00020AF67 /* SMXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BCA812E7656F0037CA90 /* SMXMLDocument.m */; };
1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
Expand Down Expand Up @@ -158,7 +158,7 @@
files = (
1D60589B0D05DD56006BFB54 /* main.m in Sources */,
0173BC9C12E765430037CA90 /* AppDelegate.m in Sources */,
0173BCAA12E7656F0037CA90 /* SMXMLDocument.m in Sources */,
01B0AE9D144DE6A00020AF67 /* SMXMLDocument.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -169,6 +169,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand All @@ -183,6 +184,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = XMLDocumentSamples_Prefix.pch;
Expand Down
8 changes: 3 additions & 5 deletions XMLDocumentSamples/main.m
@@ -1,8 +1,6 @@

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
[pool release];
return retVal;
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"AppDelegate");
}
}

0 comments on commit b7e8da4

Please sign in to comment.