Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ZaBlanc/RaptureXML
base: master
...
head fork: myell0w/RaptureXML
compare: code-cleanup
Checking mergeability… Don't worry, you can still create the pull request.
  • 13 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
View
2  EncodingTests.m
@@ -23,7 +23,7 @@ - (void)setUp {
}
- (void)testChinese {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:chineseXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:chineseXML_ encoding:NSUTF8StringEncoding];
STAssertEqualObjects([rxml attribute:@"data"], @"以晴为主", nil);
}
View
18 RaptureXML.xcodeproj/project.pbxproj
@@ -37,8 +37,8 @@
0252B2DD142ADFC60018B75D /* RaptureXMLTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RaptureXMLTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
0252B2DE142ADFC60018B75D /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
0252B305142AE3FF0018B75D /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
- 027DAC2E14FBF443001BA563 /* RXMLElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RXMLElement.h; sourceTree = "<group>"; };
- 027DAC2F14FBF443001BA563 /* RXMLElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RXMLElement.m; sourceTree = "<group>"; };
+ 027DAC2E14FBF443001BA563 /* RXMLElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = RXMLElement.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
+ 027DAC2F14FBF443001BA563 /* RXMLElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = RXMLElement.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
027DAC3814FBF4B5001BA563 /* Library-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Library-Prefix.pch"; sourceTree = "<group>"; };
02F3A4041526D7BC00E8C822 /* EncodingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EncodingTests.m; sourceTree = SOURCE_ROOT; };
0DEB8EB41467EC9B00024989 /* libRaptureXML.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRaptureXML.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -82,7 +82,6 @@
0252B2B4142ADFC60018B75D = {
isa = PBXGroup;
children = (
- 02041DC21526D03A00D1F36A /* MediaPlayer.framework */,
027DAC3814FBF4B5001BA563 /* Library-Prefix.pch */,
027DAC2B14FBF443001BA563 /* RaptureXML */,
0252B2E5142ADFC60018B75D /* Tests */,
@@ -105,6 +104,7 @@
children = (
02041DB71526A71200D1F36A /* libxml2.dylib */,
0252B305142AE3FF0018B75D /* libz.dylib */,
+ 02041DC21526D03A00D1F36A /* MediaPlayer.framework */,
0252B2C3142ADFC60018B75D /* UIKit.framework */,
0252B2C5142ADFC60018B75D /* Foundation.framework */,
0252B2C7142ADFC60018B75D /* CoreGraphics.framework */,
@@ -352,10 +352,16 @@
0DEB8EBD1467EC9B00024989 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
DSTROOT = /tmp/RaptureXML.dst;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Library-Prefix.pch";
GCC_VERSION = "";
+ GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+ GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
+ GCC_WARN_SHADOW = YES;
+ GCC_WARN_UNDECLARED_SELECTOR = YES;
+ GCC_WARN_UNUSED_FUNCTION = YES;
HEADER_SEARCH_PATHS = "\"$(SDK_DIR)\"/usr/include/libxml2/**";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -366,10 +372,16 @@
0DEB8EBE1467EC9B00024989 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
DSTROOT = /tmp/RaptureXML.dst;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Library-Prefix.pch";
GCC_VERSION = "";
+ GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+ GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
+ GCC_WARN_SHADOW = YES;
+ GCC_WARN_UNDECLARED_SELECTOR = YES;
+ GCC_WARN_UNUSED_FUNCTION = YES;
HEADER_SEARCH_PATHS = "\"$(SDK_DIR)\"/usr/include/libxml2/**";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = "$(TARGET_NAME)";
View
64 RaptureXML/RXMLElement.h
@@ -32,50 +32,48 @@
#import <libxml2/libxml/xmlreader.h>
#import <libxml2/libxml/xmlmemory.h>
-@interface RXMLElement : NSObject {
- xmlDocPtr doc_;
- xmlNodePtr node_;
-}
+@class RXMLElement;
-- (id)initFromXMLString:(NSString *)xmlString withEncoding:(NSStringEncoding)encoding;
-- (id)initFromXMLFile:(NSString *)filename;
-- (id)initFromXMLFile:(NSString *)filename fileExtension:(NSString*)extension;
-- (id)initFromURL:(NSURL *)url;
-- (id)initFromXMLData:(NSData *)data;
-- (id)initFromXMLNode:(xmlNodePtr)node;
+typedef void (^RXMLBlock)(RXMLElement *element);
-+ (id)elementFromXMLString:(NSString *)xmlString withEncoding:(NSStringEncoding)encoding;
-+ (id)elementFromXMLFile:(NSString *)filename;
-+ (id)elementFromXMLFilename:(NSString *)filename fileExtension:(NSString *)extension;
-+ (id)elementFromURL:(NSURL *)url;
-+ (id)elementFromXMLData:(NSData *)data;
-+ (id)elementFromXMLNode:(xmlNodePtr)node;
+@interface RXMLElement : NSObject
-- (NSString *)attribute:(NSString *)attName;
-- (NSString *)attribute:(NSString *)attName inNamespace:(NSString *)namespace;
+- (id)initWithString:(NSString *)xmlString encoding:(NSStringEncoding)encoding;
+- (id)initWithFilepath:(NSString *)filename;
+- (id)initWithFilename:(NSString *)filename extension:(NSString *)extension;
+- (id)initWithURL:(NSURL *)url;
+- (id)initWithData:(NSData *)data;
+- (id)initWithNode:(xmlNodePtr)node;
-- (NSInteger)attributeAsInt:(NSString *)attName;
-- (NSInteger)attributeAsInt:(NSString *)attName inNamespace:(NSString *)namespace;
++ (id)elementWithString:(NSString *)xmlString encoding:(NSStringEncoding)encoding;
++ (id)elementWithFilepath:(NSString *)filename;
++ (id)elementWithFilename:(NSString *)filename extension:(NSString *)extension;
++ (id)elementWithURL:(NSURL *)url;
++ (id)elementWithData:(NSData *)data;
++ (id)elementWithNode:(xmlNodePtr)node;
-- (double)attributeAsDouble:(NSString *)attName;
-- (double)attributeAsDouble:(NSString *)attName inNamespace:(NSString *)namespace;
+- (NSString *)attribute:(NSString *)attributeName;
+- (NSString *)attribute:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace;
-- (RXMLElement *)child:(NSString *)tagName;
-- (RXMLElement *)child:(NSString *)tagName inNamespace:(NSString *)namespace;
+- (NSInteger)attributeAsInteger:(NSString *)attributeName;
+- (NSInteger)attributeAsInteger:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace;
-- (NSArray *)children:(NSString *)tagName;
-- (NSArray *)children:(NSString *)tagName inNamespace:(NSString *)namespace;
+- (double)attributeAsDouble:(NSString *)attributeName;
+- (double)attributeAsDouble:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace;
-- (void)iterate:(NSString *)query with:(void (^)(RXMLElement *))blk;
-- (void)iterateElements:(NSArray *)elements with:(void (^)(RXMLElement *))blk;
+- (RXMLElement *)childWithTagName:(NSString *)tagName;
+- (RXMLElement *)childWithTagName:(NSString *)tagName inNamespace:(NSString *)xmlNamespace;
+
+- (NSArray *)childrenWithTagName:(NSString *)tagName;
+- (NSArray *)childrenWithTagName:(NSString *)tagName inNamespace:(NSString *)xmlNamespace;
+
+- (void)iteratePath:(NSString *)path usingBlock:(RXMLBlock)block;
+- (void)iterateElements:(NSArray *)elements usingBlock:(RXMLBlock)block;
@property (nonatomic, readonly) NSString *tag;
@property (nonatomic, readonly) NSString *text;
-@property (nonatomic, readonly) NSInteger textAsInt;
+@property (nonatomic, readonly) NSInteger textAsInteger;
@property (nonatomic, readonly) double textAsDouble;
-@property (nonatomic, readonly) BOOL isValid;
+@property (nonatomic, readonly, getter = isValid) BOOL valid;
@end
-
-typedef void (^RXMLBlock)(RXMLElement *);
-
View
361 RaptureXML/RXMLElement.m
@@ -30,142 +30,123 @@
#import "RXMLElement.h"
-@implementation RXMLElement
+@interface RXMLElement () {
+ xmlDocPtr document_;
+ xmlNodePtr node_;
+}
+
+- (void)setupWithData:(NSData *)data;
+
+@end
+
+@implementation RXMLElement
-- (id)initFromXMLString:(NSString *)xmlString withEncoding:(NSStringEncoding)encoding {
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Lifecycle
+////////////////////////////////////////////////////////////////////////
+
+- (id)initWithString:(NSString *)xmlString encoding:(NSStringEncoding)encoding {
if ((self = [super init])) {
NSData *data = [xmlString dataUsingEncoding:encoding];
-
- doc_ = xmlReadMemory([data bytes], [data length], "", nil, XML_PARSE_RECOVER);
- if ([self isValid]) {
- node_ = xmlDocGetRootElement(doc_);
-
- if (!node_) {
- xmlFreeDoc(doc_); doc_ = nil;
- }
- }
+ [self setupWithData:data];
}
return self;
}
-- (id)initFromXMLFile:(NSString *)filename {
+- (id)initWithFilepath:(NSString *)filename {
if ((self = [super init])) {
NSString *fullPath = [[[NSBundle bundleForClass:self.class] bundlePath] stringByAppendingPathComponent:filename];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
-
- doc_ = xmlReadMemory([data bytes], [data length], "", nil, XML_PARSE_RECOVER);
- if ([self isValid]) {
- node_ = xmlDocGetRootElement(doc_);
-
- if (!node_) {
- xmlFreeDoc(doc_); doc_ = nil;
- }
- }
+ [self setupWithData:data];
}
return self;
}
-- (id)initFromXMLFile:(NSString *)filename fileExtension:(NSString *)extension {
+- (id)initWithFilename:(NSString *)filename extension:(NSString *)extension {
if ((self = [super init])) {
NSString *fullPath = [[NSBundle bundleForClass:[self class]] pathForResource:filename ofType:extension];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
- doc_ = xmlReadMemory([data bytes], [data length], "", nil, XML_PARSE_RECOVER);
-
- if ([self isValid]) {
- node_ = xmlDocGetRootElement(doc_);
-
- if (!node_) {
- xmlFreeDoc(doc_); doc_ = nil;
- }
- }
+ [self setupWithData:data];
}
return self;
}
-- (id)initFromURL:(NSURL *)url {
+- (id)initWithURL:(NSURL *)url {
if ((self = [super init])) {
NSData *data = [NSData dataWithContentsOfURL:url];
- doc_ = xmlReadMemory([data bytes], [data length], "", nil, XML_PARSE_RECOVER);
-
- if ([self isValid]) {
- node_ = xmlDocGetRootElement(doc_);
-
- if (!node_) {
- xmlFreeDoc(doc_); doc_ = nil;
- }
- }
+ [self setupWithData:data];
}
return self;
}
-- (id)initFromXMLData:(NSData *)data {
+- (id)initWithData:(NSData *)data {
if ((self = [super init])) {
- doc_ = xmlReadMemory([data bytes], [data length], "", nil, XML_PARSE_RECOVER);
-
- if ([self isValid]) {
- node_ = xmlDocGetRootElement(doc_);
-
- if (!node_) {
- xmlFreeDoc(doc_); doc_ = nil;
- }
- }
+ [self setupWithData:data];
}
return self;
}
-- (id)initFromXMLNode:(xmlNodePtr)node {
+- (id)initWithNode:(xmlNodePtr)node {
if ((self = [super init])) {
- doc_ = nil;
+ document_ = nil;
node_ = node;
}
return self;
}
-+ (id)elementFromXMLString:(NSString *)attributeXML_ withEncoding:(NSStringEncoding)encoding {
- return [[[RXMLElement alloc] initFromXMLString:attributeXML_ withEncoding:encoding] autorelease];
++ (id)elementWithString:(NSString *)attributeXML_ encoding:(NSStringEncoding)encoding {
+ return [[[RXMLElement alloc] initWithString:attributeXML_ encoding:encoding] autorelease];
}
-+ (id)elementFromXMLFile:(NSString *)filename {
- return [[[RXMLElement alloc] initFromXMLFile:filename] autorelease];
++ (id)elementWithFilepath:(NSString *)filename {
+ return [[[RXMLElement alloc] initWithFilepath:filename] autorelease];
}
-+ (id)elementFromXMLFilename:(NSString *)filename fileExtension:(NSString *)extension {
- return [[[RXMLElement alloc] initFromXMLFile:filename fileExtension:extension] autorelease];
++ (id)elementWithFilename:(NSString *)filename extension:(NSString *)extension {
+ return [[[RXMLElement alloc] initWithFilename:filename extension:extension] autorelease];
}
-+ (id)elementFromURL:(NSURL *)url {
- return [[[RXMLElement alloc] initFromURL:url] autorelease];
++ (id)elementWithURL:(NSURL *)url {
+ return [[[RXMLElement alloc] initWithURL:url] autorelease];
}
-+ (id)elementFromXMLData:(NSData *)data {
- return [[[RXMLElement alloc] initFromXMLData:data] autorelease];
++ (id)elementWithData:(NSData *)data {
+ return [[[RXMLElement alloc] initWithData:data] autorelease];
}
-+ (id)elementFromXMLNode:(xmlNodePtr)node {
- return [[[RXMLElement alloc] initFromXMLNode:node] autorelease];
-}
-
-- (NSString *)description {
- return [self text];
++ (id)elementWithNode:(xmlNodePtr)node {
+ return [[[RXMLElement alloc] initWithNode:node] autorelease];
}
- (void)dealloc {
- if (doc_ != nil) xmlFreeDoc(doc_);
+ if (document_ != nil) {
+ xmlFreeDoc(document_);
+ }
[super dealloc];
}
-#pragma mark -
+////////////////////////////////////////////////////////////////////////
+#pragma mark - NSObject
+////////////////////////////////////////////////////////////////////////
+
+- (NSString *)description {
+ return [self text];
+}
+
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Getter
+////////////////////////////////////////////////////////////////////////
- (NSString *)tag {
return [NSString stringWithUTF8String:(const char *)node_->name];
@@ -175,243 +156,263 @@ - (NSString *)text {
xmlChar *key = xmlNodeGetContent(node_);
NSString *text = (key ? [NSString stringWithUTF8String:(const char *)key] : @"");
xmlFree(key);
-
+
return text;
}
-- (NSInteger)textAsInt {
- return [self.text intValue];
+- (NSInteger)textAsInteger {
+ return [self.text integerValue];
}
- (double)textAsDouble {
return [self.text doubleValue];
}
-- (NSString *)attribute:(NSString *)attName {
- const unsigned char *attCStr = xmlGetProp(node_, (const xmlChar *)[attName cStringUsingEncoding:NSUTF8StringEncoding]);
-
- if (attCStr) {
- return [NSString stringWithUTF8String:(const char *)attCStr];
+- (NSString *)attribute:(NSString *)attributeName {
+ const unsigned char *attributeValueC = xmlGetProp(node_, (const xmlChar *)[attributeName cStringUsingEncoding:NSUTF8StringEncoding]);
+
+ if (attributeValueC != NULL) {
+ return [NSString stringWithUTF8String:(const char *)attributeValueC];
}
return nil;
}
-- (NSString *)attribute:(NSString *)attName inNamespace:(NSString *)namespace {
- const unsigned char *attCStr = xmlGetNsProp(node_, (const xmlChar *)[attName cStringUsingEncoding:NSUTF8StringEncoding], (const xmlChar *)[namespace cStringUsingEncoding:NSUTF8StringEncoding]);
-
- if (attCStr) {
- return [NSString stringWithUTF8String:(const char *)attCStr];
+- (NSString *)attribute:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace {
+ const unsigned char *attributeValueC = xmlGetNsProp(node_, (const xmlChar *)[attributeName cStringUsingEncoding:NSUTF8StringEncoding], (const xmlChar *)[xmlNamespace cStringUsingEncoding:NSUTF8StringEncoding]);
+
+ if (attributeValueC != NULL) {
+ return [NSString stringWithUTF8String:(const char *)attributeValueC];
}
return nil;
}
-- (NSInteger)attributeAsInt:(NSString *)attName {
- return [[self attribute:attName] intValue];
+- (NSInteger)attributeAsInteger:(NSString *)attributeName {
+ return [[self attribute:attributeName] integerValue];
}
-- (NSInteger)attributeAsInt:(NSString *)attName inNamespace:(NSString *)namespace {
- return [[self attribute:attName inNamespace:namespace] intValue];
+- (NSInteger)attributeAsInteger:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace {
+ return [[self attribute:attributeName inNamespace:xmlNamespace] integerValue];
}
-- (double)attributeAsDouble:(NSString *)attName {
- return [[self attribute:attName] doubleValue];
+- (double)attributeAsDouble:(NSString *)attributeName {
+ return [[self attribute:attributeName] doubleValue];
}
-- (double)attributeAsDouble:(NSString *)attName inNamespace:(NSString *)namespace {
- return [[self attribute:attName inNamespace:namespace] doubleValue];
+- (double)attributeAsDouble:(NSString *)attributeName inNamespace:(NSString *)xmlNamespace {
+ return [[self attribute:attributeName inNamespace:xmlNamespace] doubleValue];
}
- (BOOL)isValid {
- return (doc_ != nil);
+ return document_ != nil;
}
-#pragma mark -
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Child Nodes
+////////////////////////////////////////////////////////////////////////
-- (RXMLElement *)child:(NSString *)tagName {
+- (RXMLElement *)childWithTagName:(NSString *)tagName {
NSArray *components = [tagName componentsSeparatedByString:@"."];
- xmlNodePtr cur = node_;
+ xmlNodePtr currentNode = node_;
// navigate down
- for (NSInteger i=0; i < components.count; ++i) {
- NSString *iTagName = [components objectAtIndex:i];
- const xmlChar *tagNameC = (const xmlChar *)[iTagName cStringUsingEncoding:NSUTF8StringEncoding];
-
- if ([iTagName isEqualToString:@"*"]) {
- cur = cur->children;
+ for (NSString *currentTagName in components) {
+ const xmlChar *tagNameC = (const xmlChar *)[currentTagName cStringUsingEncoding:NSUTF8StringEncoding];
+
+ if ([currentTagName isEqualToString:@"*"]) {
+ currentNode = currentNode->children;
- while (cur != nil && cur->type != XML_ELEMENT_NODE) {
- cur = cur->next;
+ while (currentNode != NULL && currentNode->type != XML_ELEMENT_NODE) {
+ currentNode = currentNode->next;
}
} else {
- cur = cur->children;
- while (cur != nil) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC)) {
+ currentNode = currentNode->children;
+ while (currentNode != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0) {
break;
}
- cur = cur->next;
+ currentNode = currentNode->next;
}
}
- if (!cur) {
+ if (currentNode == NULL) {
break;
}
}
- if (cur) {
- return [RXMLElement elementFromXMLNode:cur];
+ if (currentNode != NULL) {
+ return [RXMLElement elementWithNode:currentNode];
}
-
+
return nil;
}
-- (RXMLElement *)child:(NSString *)tagName inNamespace:(NSString *)namespace {
+- (RXMLElement *)childWithTagName:(NSString *)tagName inNamespace:(NSString *)xmlNamespace {
NSArray *components = [tagName componentsSeparatedByString:@"."];
- xmlNodePtr cur = node_;
- const xmlChar *namespaceC = (const xmlChar *)[namespace cStringUsingEncoding:NSUTF8StringEncoding];
+ xmlNodePtr currentNode = node_;
+ const xmlChar *namespaceC = (const xmlChar *)[xmlNamespace cStringUsingEncoding:NSUTF8StringEncoding];
// navigate down
- for (NSInteger i=0; i < components.count; ++i) {
- NSString *iTagName = [components objectAtIndex:i];
- const xmlChar *tagNameC = (const xmlChar *)[iTagName cStringUsingEncoding:NSUTF8StringEncoding];
+ for (NSString *currentTagName in components) {
+ const xmlChar *tagNameC = (const xmlChar *)[currentTagName cStringUsingEncoding:NSUTF8StringEncoding];
- if ([iTagName isEqualToString:@"*"]) {
- cur = cur->children;
+ if ([currentTagName isEqualToString:@"*"]) {
+ currentNode = currentNode->children;
- while (cur != nil && cur->type != XML_ELEMENT_NODE && !xmlStrcmp(cur->ns->href, namespaceC)) {
- cur = cur->next;
+ while (currentNode != NULL && currentNode->type != XML_ELEMENT_NODE && xmlStrcmp(currentNode->ns->href, namespaceC) == 0) {
+ currentNode = currentNode->next;
}
} else {
- cur = cur->children;
- while (cur != nil) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC) && !xmlStrcmp(cur->ns->href, namespaceC)) {
+ currentNode = currentNode->children;
+ while (currentNode != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0 && xmlStrcmp(currentNode->ns->href, namespaceC) == 0) {
break;
}
- cur = cur->next;
+ currentNode = currentNode->next;
}
}
- if (!cur) {
+ if (currentNode == NULL) {
break;
}
}
- if (cur) {
- return [RXMLElement elementFromXMLNode:cur];
+ if (currentNode != NULL) {
+ return [RXMLElement elementWithNode:currentNode];
}
return nil;
}
-- (NSArray *)children:(NSString *)tagName {
+- (NSArray *)childrenWithTagName:(NSString *)tagName {
const xmlChar *tagNameC = (const xmlChar *)[tagName cStringUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *children = [NSMutableArray array];
- xmlNodePtr cur = node_->children;
-
- while (cur != nil) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC)) {
- [children addObject:[RXMLElement elementFromXMLNode:cur]];
+ xmlNodePtr currentNode = node_->children;
+
+ while (currentNode != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0) {
+ [children addObject:[RXMLElement elementWithNode:currentNode]];
}
- cur = cur->next;
+ currentNode = currentNode->next;
}
return [[children copy] autorelease];
}
-- (NSArray *)children:(NSString *)tagName inNamespace:(NSString *)namespace {
+- (NSArray *)childrenWithTagName:(NSString *)tagName inNamespace:(NSString *)xmlNamespace {
const xmlChar *tagNameC = (const xmlChar *)[tagName cStringUsingEncoding:NSUTF8StringEncoding];
- const xmlChar *namespaceC = (const xmlChar *)[namespace cStringUsingEncoding:NSUTF8StringEncoding];
+ const xmlChar *namespaceC = (const xmlChar *)[xmlNamespace cStringUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *children = [NSMutableArray array];
- xmlNodePtr cur = node_->children;
+ xmlNodePtr currentNode = node_->children;
- while (cur != nil) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC) && !xmlStrcmp(cur->ns->href, namespaceC)) {
- [children addObject:[RXMLElement elementFromXMLNode:cur]];
+ while (currentNode != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0 && xmlStrcmp(currentNode->ns->href, namespaceC) == 0) {
+ [children addObject:[RXMLElement elementWithNode:currentNode]];
}
- cur = cur->next;
+ currentNode = currentNode->next;
}
return [[children copy] autorelease];
}
-#pragma mark -
-
-- (void)iterate:(NSString *)query with:(void (^)(RXMLElement *))blk {
- NSArray *components = [query componentsSeparatedByString:@"."];
- xmlNodePtr cur = node_;
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Block Iterations
+////////////////////////////////////////////////////////////////////////
+- (void)iteratePath:(NSString *)path usingBlock:(RXMLBlock)block {
+ NSArray *components = [path componentsSeparatedByString:@"."];
+ xmlNodePtr currentNode = node_;
+
// navigate down
- for (NSInteger i=0; i < components.count; ++i) {
- NSString *iTagName = [components objectAtIndex:i];
+ for (NSUInteger i=0; i < components.count; ++i) {
+ NSString *currentTagName = [components objectAtIndex:i];
- if ([iTagName isEqualToString:@"*"]) {
- cur = cur->children;
-
+ if ([currentTagName isEqualToString:@"*"]) {
+ currentNode = currentNode->children;
+
// different behavior depending on if this is the end of the query or midstream
if (i < (components.count - 1)) {
// midstream
do {
- if (cur->type == XML_ELEMENT_NODE) {
- RXMLElement *element = [RXMLElement elementFromXMLNode:cur];
+ if (currentNode->type == XML_ELEMENT_NODE) {
+ RXMLElement *element = [RXMLElement elementWithNode:currentNode];
NSString *restOfQuery = [[components subarrayWithRange:NSMakeRange(i + 1, components.count - i - 1)] componentsJoinedByString:@"."];
- [element iterate:restOfQuery with:blk];
+
+ [element iteratePath:restOfQuery usingBlock:block];
}
- cur = cur->next;
- } while (cur != nil);
-
+ currentNode = currentNode->next;
+ } while (currentNode != nil);
+
}
} else {
- const xmlChar *tagNameC = (const xmlChar *)[iTagName cStringUsingEncoding:NSUTF8StringEncoding];
-
- cur = cur->children;
- while (cur != nil) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC)) {
+ const xmlChar *tagNameC = (const xmlChar *)[currentTagName cStringUsingEncoding:NSUTF8StringEncoding];
+
+ currentNode = currentNode->children;
+ while (currentNode != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0) {
break;
}
- cur = cur->next;
+ currentNode = currentNode->next;
}
}
-
- if (!cur) {
+
+ if (currentNode == NULL) {
break;
}
}
-
- if (cur) {
+
+ if (currentNode != NULL) {
// enumerate
NSString *childTagName = [components lastObject];
do {
- if (cur->type == XML_ELEMENT_NODE) {
- RXMLElement *element = [RXMLElement elementFromXMLNode:cur];
- blk(element);
+ if (currentNode->type == XML_ELEMENT_NODE) {
+ RXMLElement *element = [RXMLElement elementWithNode:currentNode];
+ block(element);
}
if ([childTagName isEqualToString:@"*"]) {
- cur = cur->next;
+ currentNode = currentNode->next;
} else {
const xmlChar *tagNameC = (const xmlChar *)[childTagName cStringUsingEncoding:NSUTF8StringEncoding];
-
- while ((cur = cur->next)) {
- if (cur->type == XML_ELEMENT_NODE && !xmlStrcmp(cur->name, tagNameC)) {
+
+ while ((currentNode = currentNode->next) != NULL) {
+ if (currentNode->type == XML_ELEMENT_NODE && xmlStrcmp(currentNode->name, tagNameC) == 0) {
break;
}
}
}
- } while (cur);
+ } while (currentNode != NULL);
+ }
+}
+
+- (void)iterateElements:(NSArray *)elements usingBlock:(RXMLBlock)block {
+ for (RXMLElement *element in elements) {
+ block(element);
}
}
-- (void)iterateElements:(NSArray *)elements with:(void (^)(RXMLElement *))blk {
- for (RXMLElement *iElement in elements) {
- blk(iElement);
+////////////////////////////////////////////////////////////////////////
+#pragma mark - Private
+////////////////////////////////////////////////////////////////////////
+
+- (void)setupWithData:(NSData *)data {
+ document_ = xmlReadMemory([data bytes], (int)[data length], "", nil, XML_PARSE_RECOVER);
+
+ if ([self isValid]) {
+ node_ = xmlDocGetRootElement(document_);
+
+ if (node_ == NULL) {
+ xmlFreeDoc(document_);
+ document_ = nil;
+ }
}
}
View
30 Tests/BoundaryTests.m
@@ -33,53 +33,53 @@ - (void)setUp {
}
- (void)testEmptyXML {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:emptyXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:emptyXML_ encoding:NSUTF8StringEncoding];
STAssertFalse(rxml.isValid, nil);
}
- (void)testEmptyTopTagXML {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:emptyTopTagXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:emptyTopTagXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
STAssertEqualObjects(rxml.text, @"", nil);
- STAssertEqualObjects([rxml children:@"*"], [NSArray array], nil);
+ STAssertEqualObjects([rxml childrenWithTagName:@"*"], [NSArray array], nil);
}
- (void)testAttribute {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:attributeXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:attributeXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
STAssertEqualObjects([rxml attribute:@"foo"], @"bar", nil);
}
- (void)testNamespaceAttribute {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:namespaceXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:namespaceXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
STAssertEqualObjects([rxml attribute:@"foo" inNamespace:@"*"], @"bar", nil);
- STAssertEquals([rxml attributeAsInt:@"one" inNamespace:@"*"], 1, nil);
+ STAssertEquals([rxml attributeAsInteger:@"one" inNamespace:@"*"], 1, nil);
}
- (void)testChild {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:childXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:childXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
- STAssertEqualObjects([rxml child:@"empty_child"].text, @"", nil);
- STAssertEqualObjects([rxml child:@"text_child"].text, @"foo", nil);
+ STAssertEqualObjects([rxml childWithTagName:@"empty_child"].text, @"", nil);
+ STAssertEqualObjects([rxml childWithTagName:@"text_child"].text, @"foo", nil);
}
- (void)testNamespaceChild {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:namespaceXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:namespaceXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
- STAssertEqualObjects([rxml child:@"text" inNamespace:@"*"].text, @"something", nil);
+ STAssertEqualObjects([rxml childWithTagName:@"text" inNamespace:@"*"].text, @"something", nil);
}
- (void)testChildren {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:childrenXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:childrenXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
- STAssertEquals([rxml children:@"child"].count, 3U, nil);
+ STAssertEquals([rxml childrenWithTagName:@"child"].count, 3U, nil);
}
- (void)testNamespaceChildren {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:namespaceXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:namespaceXML_ encoding:NSUTF8StringEncoding];
STAssertTrue(rxml.isValid, nil);
- STAssertEquals([rxml children:@"text" inNamespace:@"*"].count, 1U, nil);
+ STAssertEquals([rxml childrenWithTagName:@"text" inNamespace:@"*"].count, 1U, nil);
}
@end
View
20 Tests/DeepChildrenTests.m
@@ -18,14 +18,14 @@ @interface DeepChildrenTests : SenTestCase {
@implementation DeepChildrenTests
- (void)testQuery {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
__block NSInteger i = 0;
// count the players
- RXMLElement *players = [rxml child:@"players"];
- NSArray *children = [players children:@"player"];
+ RXMLElement *players = [rxml childWithTagName:@"players"];
+ NSArray *children = [players childrenWithTagName:@"player"];
- [rxml iterateElements:children with: ^(RXMLElement *e) {
+ [rxml iterateElements:children usingBlock: ^(RXMLElement *e) {
i++;
}];
@@ -33,22 +33,22 @@ - (void)testQuery {
}
- (void)testDeepChildQuery {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
// count the players
- RXMLElement *coachingYears = [rxml child:@"players.coach.experience.years"];
+ RXMLElement *coachingYears = [rxml childWithTagName:@"players.coach.experience.years"];
- STAssertEquals(coachingYears.textAsInt, 1, nil);
+ STAssertEquals(coachingYears.textAsInteger, 1, nil);
}
- (void)testDeepChildQueryWithWildcard {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
// count the players
- RXMLElement *coachingYears = [rxml child:@"players.coach.experience.teams.*"];
+ RXMLElement *coachingYears = [rxml childWithTagName:@"players.coach.experience.teams.*"];
// first team returned
- STAssertEquals(coachingYears.textAsInt, 53, nil);
+ STAssertEquals(coachingYears.textAsInteger, 53, nil);
}
@end
View
8 Tests/DeepTests.m
@@ -18,13 +18,13 @@ @interface DeepTests : SenTestCase {
@implementation DeepTests
- (void)testQuery {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
__block NSInteger i;
// count the players
i = 0;
- [rxml iterate:@"players.player" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.player" usingBlock: ^(RXMLElement *e) {
i++;
}];
@@ -33,7 +33,7 @@ - (void)testQuery {
// count the first players' names
i = 0;
- [rxml iterate:@"players.player.name" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.player.name" usingBlock: ^(RXMLElement *e) {
i++;
}];
@@ -42,7 +42,7 @@ - (void)testQuery {
// count the coaches
i = 0;
- [rxml iterate:@"players.coach" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.coach" usingBlock: ^(RXMLElement *e) {
i++;
}];
View
12 Tests/ErrorTests.m
@@ -30,22 +30,22 @@ - (void)setUp {
}
- (void)testBadXML {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:badXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:badXML_ encoding:NSUTF8StringEncoding];
STAssertFalse([rxml isValid], nil);
}
- (void)testMissingTag {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:simplifiedXML_ withEncoding:NSUTF8StringEncoding];
- RXMLElement *hexagon = [rxml child:@"hexagon"];
+ RXMLElement *rxml = [RXMLElement elementWithString:simplifiedXML_ encoding:NSUTF8StringEncoding];
+ RXMLElement *hexagon = [rxml childWithTagName:@"hexagon"];
STAssertNil(hexagon, nil);
}
- (void)testMissingTagIteration {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:simplifiedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:simplifiedXML_ encoding:NSUTF8StringEncoding];
__block NSInteger i = 0;
- [rxml iterate:@"hexagon" with:^(RXMLElement *e) {
+ [rxml iteratePath:@"hexagon" usingBlock:^(RXMLElement *e) {
i++;
}];
@@ -53,7 +53,7 @@ - (void)testMissingTagIteration {
}
- (void)testMissingAttribute {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:simplifiedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:simplifiedXML_ encoding:NSUTF8StringEncoding];
NSString *missingName = [rxml attribute:@"name"];
STAssertNil(missingName, nil);
View
12 Tests/SimpleTests.m
@@ -40,20 +40,20 @@ - (void)setUp {
}
- (void)testInterruptedText {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:interruptedTextXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:interruptedTextXML_ encoding:NSUTF8StringEncoding];
STAssertEqualObjects(rxml.text, @"thisisinterruptedtext", nil);
}
- (void)testCDataText {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:cdataXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:cdataXML_ encoding:NSUTF8StringEncoding];
STAssertEqualObjects(rxml.text, @"thisiscdata", nil);
}
- (void)testTags {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:simplifiedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:simplifiedXML_ encoding:NSUTF8StringEncoding];
__block NSInteger i = 0;
- [rxml iterate:@"*" with:^(RXMLElement *e) {
+ [rxml iteratePath:@"*" usingBlock:^(RXMLElement *e) {
if (i == 0) {
STAssertEqualObjects(e.tag, @"square", nil);
STAssertEqualObjects(e.text, @"Square", nil);
@@ -72,10 +72,10 @@ - (void)testTags {
}
- (void)testAttributes {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:attributedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:attributedXML_ encoding:NSUTF8StringEncoding];
__block NSInteger i = 0;
- [rxml iterate:@"*" with:^(RXMLElement *e) {
+ [rxml iteratePath:@"*" usingBlock:^(RXMLElement *e) {
if (i == 0) {
STAssertEqualObjects([e attribute:@"name"], @"Square", nil);
} else if (i == 1) {
View
16 Tests/TextConversionTests.m
@@ -44,14 +44,14 @@ - (void)setUp {
}
- (void)testIntTags {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:simplifiedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:simplifiedXML_ encoding:NSUTF8StringEncoding];
__block NSInteger i = 0;
- [rxml iterate:@"*" with:^(RXMLElement *e) {
+ [rxml iteratePath:@"*" usingBlock:^(RXMLElement *e) {
if (i == 0) {
- STAssertEquals([e child:@"id"].textAsInt, 1, nil);
+ STAssertEquals([e childWithTagName:@"id"].textAsInteger, 1, nil);
} else if (i == 1) {
- STAssertEqualsWithAccuracy([e child:@"id"].textAsDouble, 2.5, 0.01, nil);
+ STAssertEqualsWithAccuracy([e childWithTagName:@"id"].textAsDouble, 2.5, 0.01, nil);
}
i++;
@@ -59,16 +59,16 @@ - (void)testIntTags {
}
- (void)testIntAttributes {
- RXMLElement *rxml = [RXMLElement elementFromXMLString:attributedXML_ withEncoding:NSUTF8StringEncoding];
+ RXMLElement *rxml = [RXMLElement elementWithString:attributedXML_ encoding:NSUTF8StringEncoding];
__block NSInteger i = 0;
- [rxml iterate:@"*" with:^(RXMLElement *e) {
+ [rxml iteratePath:@"*" usingBlock:^(RXMLElement *e) {
if (i == 0) {
- STAssertEquals([e attributeAsInt:@"id"], 1, nil);
+ STAssertEquals([e attributeAsInteger:@"id"], 1, nil);
} else if (i == 1) {
STAssertEqualsWithAccuracy([e attributeAsDouble:@"id"], 2.5, 0.01, nil);
} else if (i == 2) {
- STAssertEquals([e attributeAsInt:@"id"], 3, nil);
+ STAssertEquals([e attributeAsInteger:@"id"], 3, nil);
}
i++;
View
10 Tests/WildcardTests.m
@@ -18,13 +18,13 @@ @interface WildcardTests : SenTestCase {
@implementation WildcardTests
- (void)testEndingWildcard {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
__block NSInteger i;
// count the players and coaches
i = 0;
- [rxml iterate:@"players.*" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.*" usingBlock: ^(RXMLElement *e) {
i++;
}];
@@ -32,13 +32,13 @@ - (void)testEndingWildcard {
}
- (void)testMidstreamWildcard {
- RXMLElement *rxml = [RXMLElement elementFromXMLFile:@"players.xml"];
+ RXMLElement *rxml = [RXMLElement elementWithFilepath:@"players.xml"];
__block NSInteger i;
// count the tags that have a name
i = 0;
- [rxml iterate:@"players.*.name" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.*.name" usingBlock: ^(RXMLElement *e) {
i++;
}];
@@ -47,7 +47,7 @@ - (void)testMidstreamWildcard {
// count the tags that have a position
i = 0;
- [rxml iterate:@"players.*.position" with: ^(RXMLElement *e) {
+ [rxml iteratePath:@"players.*.position" usingBlock: ^(RXMLElement *e) {
i++;
}];

No commit comments for this range

Something went wrong with that request. Please try again.