Skip to content

Commit

Permalink
Moved XMLDocument into project directly to avoid submodule confusion.…
Browse files Browse the repository at this point in the history
… Added comments.
  • Loading branch information
nfarina committed Mar 1, 2011
1 parent b32238f commit 106f714
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 8 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

10 changes: 8 additions & 2 deletions SampleProject/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
@implementation AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Demonstrate how we can listen globally to web request error events, to implement a systemwide failure message. Apple likes those!
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webRequestError:) name:kSMWebRequestError object:nil];

// Example 1: Make an autoreleased "set it and forget it" web request to Google.
SMWebRequest *request = [SMWebRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[request addTarget:self action:@selector(googleFinished:) forRequestEvents:SMWebRequestEventComplete];
[request start];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webRequestError:) name:kSMWebRequestError object:nil];

// Example 2: Make a simple but extremely nice+performant RSS reader for Hacker News!
ListController *home = [[ListController alloc] initListController];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:home];
nav.toolbarHidden = NO;
Expand All @@ -22,10 +26,12 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];
}

// Callback from Example 1: we simply get an NSData with the server response.
- (void)googleFinished:(NSData *)responseData {
NSLog(@"Got a response of %i bytes.", responseData.length);
}

// Global error handler displays a simple failure message.
- (void)webRequestError:(NSNotification *)notification {
if (showedOfflineAlert) return;
showedOfflineAlert = YES;
Expand Down
1 change: 1 addition & 0 deletions SampleProject/ItemController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ - (id)initWithItem:(SMXMLElement *)theItem {

- (void)dealloc {
self.item = nil;
[super dealloc];
}

- (void)loadView {
Expand Down
73 changes: 73 additions & 0 deletions SampleProject/SMXMLDocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
The MIT License
Copyright (c) 2011 Spotlight Mobile
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// SMXMLDocument is a very handy lightweight XML parser for iOS.

#ifdef __IPHONE_4_0
@interface SMXMLElement : NSObject<NSXMLParserDelegate> {
#else
@interface SMXMLElement : NSObject {
#endif

@private
SMXMLElement *parent; // nonretained
NSString *name;
NSMutableString *value;
NSMutableArray *children;
NSDictionary *attributes;
}

@property (nonatomic, assign) 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;

- (SMXMLElement *)childNamed:(NSString *)name;
- (NSArray *)childrenNamed:(NSString *)name;
- (SMXMLElement *)childWithAttribute:(NSString *)attributeName value:(NSString *)attributeValue;
- (NSString *)attributeNamed:(NSString *)name;
- (SMXMLElement *)descendantWithPath:(NSString *)path;
- (NSString *)valueWithPath:(NSString *)path;

@end

#ifdef __IPHONE_4_0
@interface SMXMLDocument : NSObject<NSXMLParserDelegate> {
#else
@interface SMXMLDocument : NSObject {
#endif

@private
SMXMLElement *root;
}

@property (nonatomic, retain) SMXMLElement *root;

- (id)initWithData:(NSData *)data;

+ (SMXMLDocument *)documentWithData:(NSData *)data;

@end
171 changes: 171 additions & 0 deletions SampleProject/SMXMLDocument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#import "SMXMLDocument.h"

@implementation SMXMLElement
@synthesize parent, name, value, children, attributes;

- (void)dealloc {
self.parent = nil;
self.name = nil;
self.value = nil;
self.children = nil;
self.attributes = nil;
[super dealloc];
}

- (NSString *)descriptionWithIndent:(NSString *)indent {

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]];

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

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

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

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

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

return s;
}

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

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

if (!string) return;

if (value)
[(NSMutableString *)value appendString:string];
else
self.value = [NSMutableString stringWithString:string];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
SMXMLElement *child = [[[SMXMLElement alloc] init] autorelease];
child.parent = self;
child.name = elementName;
child.attributes = attributeDict;

if (children)
[(NSMutableArray *)children addObject:child];
else
self.children = [NSMutableArray arrayWithObject:child];

[parser setDelegate:child];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
[parser setDelegate:parent];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"ERROR at line %i col %i while parsing <%@>: %@. Value so far: %@", parser.lineNumber, parser.columnNumber, name, parseError, value);
}

- (SMXMLElement *)childNamed:(NSString *)nodeName {
for (SMXMLElement *child in children)
if ([child.name isEqual:nodeName])
return child;
return nil;
}

- (NSArray *)childrenNamed:(NSString *)nodeName {
NSMutableArray *array = [NSMutableArray array];
for (SMXMLElement *child in children)
if ([child.name isEqual:nodeName])
[array addObject:child];
return [[array copy] autorelease];
}

- (SMXMLElement *)childWithAttribute:(NSString *)attributeName value:(NSString *)attributeValue {
for (SMXMLElement *child in children)
if ([[child attributeNamed:attributeName] isEqual:attributeValue])
return child;
return nil;
}

- (NSString *)attributeNamed:(NSString *)attributeName {
return [attributes objectForKey:attributeName];
}

- (SMXMLElement *)descendantWithPath:(NSString *)path {
SMXMLElement *descendant = self;
for (NSString *childName in [path componentsSeparatedByString:@"."])
descendant = [descendant childNamed:childName];
return descendant;
}

- (NSString *)valueWithPath:(NSString *)path {
NSArray *components = [path componentsSeparatedByString:@"@"];
SMXMLElement *descendant = [self descendantWithPath:[components objectAtIndex:0]];
return [components count] > 1 ? [descendant attributeNamed:[components objectAtIndex:1]] : descendant.value;
}


- (SMXMLElement *)firstChild { return [children count] > 0 ? [children objectAtIndex:0] : nil; }
- (SMXMLElement *)lastChild { return [children lastObject]; }

@end

@implementation SMXMLDocument
@synthesize root;

- (id)initWithData:(NSData *)data {
self = [super init];
if (self) {
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldReportNamespacePrefixes:YES];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
return self;
}

- (void)dealloc {
self.root = nil;
[super dealloc];
}

+ (SMXMLDocument *)documentWithData:(NSData *)data {
return [[[SMXMLDocument alloc] initWithData:data] autorelease];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

self.root = [[[SMXMLElement alloc] init] autorelease];
root.name = elementName;
root.attributes = attributeDict;
[parser setDelegate:root];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"ERROR at line %i col %i while parsing root: %@", parser.lineNumber, parser.columnNumber, parseError);
}

- (NSString *)description {
return root.description;
}

@end
4 changes: 2 additions & 2 deletions SampleProject/WebRequestSamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
32CA4F630368D1EE00C91783 /* WebRequestSamples_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebRequestSamples_Prefix.pch; sourceTree = "<group>"; };
4101392E131B6501005D9663 /* ListController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListController.h; sourceTree = "<group>"; };
4101392F131B6501005D9663 /* ListController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ListController.m; sourceTree = "<group>"; };
41013951131B6C6B005D9663 /* SMXMLDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SMXMLDocument.h; path = ../xmldocument/SMXMLDocument.h; sourceTree = SOURCE_ROOT; };
41013952131B6C6B005D9663 /* SMXMLDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SMXMLDocument.m; path = ../xmldocument/SMXMLDocument.m; sourceTree = SOURCE_ROOT; };
41013951131B6C6B005D9663 /* SMXMLDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SMXMLDocument.h; sourceTree = "<group>"; };
41013952131B6C6B005D9663 /* SMXMLDocument.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SMXMLDocument.m; sourceTree = "<group>"; };
41013A50131B9010005D9663 /* ItemController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ItemController.h; sourceTree = "<group>"; };
41013A51131B9010005D9663 /* ItemController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ItemController.m; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* WebRequestSamples-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "WebRequestSamples-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
Expand Down
1 change: 0 additions & 1 deletion xmldocument
Submodule xmldocument deleted from f3e8b8

0 comments on commit 106f714

Please sign in to comment.