-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved XMLDocument into project directly to avoid submodule confusion.…
… Added comments.
- Loading branch information
Showing
7 changed files
with
255 additions
and
8 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule xmldocument
deleted from
f3e8b8