Skip to content

Commit 086d0b0

Browse files
committed
Merge pull request topfunky#7 from zadr/master
Fixes the build, a lot of code cleanup and add some new methods
2 parents d35a6b6 + c1cb1d9 commit 086d0b0

File tree

7 files changed

+135
-33
lines changed

7 files changed

+135
-33
lines changed

Hpple.xcodeproj/project.pbxproj

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
children = (
120120
32CA4F630368D1EE00C91783 /* Hpple_Prefix.pch */,
121121
29B97316FDCFA39411CA2CEA /* main.m */,
122-
75D786350F34E8A600173CB6 /* libxml2.2.dylib */,
123122
);
124123
name = "Other Sources";
125124
sourceTree = "<group>";
@@ -136,6 +135,7 @@
136135
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
137136
isa = PBXGroup;
138137
children = (
138+
75D786350F34E8A600173CB6 /* libxml2.2.dylib */,
139139
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */,
140140
1D30AB110D05D00D00671497 /* Foundation.framework */,
141141
288765FC0DF74451002DB57D /* CoreGraphics.framework */,
@@ -232,7 +232,11 @@
232232
isa = PBXProject;
233233
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "Hpple" */;
234234
compatibilityVersion = "Xcode 3.1";
235+
developmentRegion = English;
235236
hasScannedForEncodings = 1;
237+
knownRegions = (
238+
en,
239+
);
236240
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
237241
projectDirPath = "";
238242
projectRoot = "";
@@ -311,29 +315,60 @@
311315
1D6058940D05DD3E006BFB54 /* Debug */ = {
312316
isa = XCBuildConfiguration;
313317
buildSettings = {
314-
ALWAYS_SEARCH_USER_PATHS = NO;
315-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Geoffrey Grosenbach";
318+
ALWAYS_SEARCH_USER_PATHS = YES;
319+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
320+
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
321+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
316322
COPY_PHASE_STRIP = NO;
317-
GCC_DYNAMIC_NO_PIC = NO;
323+
GCC_DYNAMIC_NO_PIC = YES;
324+
GCC_ENABLE_CPP_EXCEPTIONS = NO;
325+
GCC_ENABLE_CPP_RTTI = NO;
326+
GCC_ENABLE_OBJC_EXCEPTIONS = NO;
327+
GCC_FAST_MATH = YES;
318328
GCC_OPTIMIZATION_LEVEL = 0;
319329
GCC_PRECOMPILE_PREFIX_HEADER = YES;
320330
GCC_PREFIX_HEADER = Hpple_Prefix.pch;
331+
GCC_UNROLL_LOOPS = YES;
332+
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
333+
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
334+
GCC_WARN_PEDANTIC = YES;
335+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
336+
GCC_WARN_UNUSED_FUNCTION = YES;
337+
HEADER_SEARCH_PATHS = /usr/include/libxml2;
321338
INFOPLIST_FILE = Info.plist;
339+
LLVM_LTO = YES;
322340
PRODUCT_NAME = Hpple;
323-
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "CD1F1B03-3E48-4552-ABEF-FDFFFA2F6956";
324-
SDKROOT = iphoneos3.0;
341+
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "";
342+
RUN_CLANG_STATIC_ANALYZER = YES;
343+
SDKROOT = iphoneos;
325344
};
326345
name = Debug;
327346
};
328347
1D6058950D05DD3E006BFB54 /* Release */ = {
329348
isa = XCBuildConfiguration;
330349
buildSettings = {
331-
ALWAYS_SEARCH_USER_PATHS = NO;
350+
ALWAYS_SEARCH_USER_PATHS = YES;
351+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
352+
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
332353
COPY_PHASE_STRIP = YES;
354+
GCC_DYNAMIC_NO_PIC = YES;
355+
GCC_ENABLE_CPP_EXCEPTIONS = NO;
356+
GCC_ENABLE_CPP_RTTI = NO;
357+
GCC_ENABLE_OBJC_EXCEPTIONS = NO;
358+
GCC_FAST_MATH = YES;
333359
GCC_PRECOMPILE_PREFIX_HEADER = YES;
334360
GCC_PREFIX_HEADER = Hpple_Prefix.pch;
361+
GCC_UNROLL_LOOPS = YES;
362+
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
363+
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
364+
GCC_WARN_PEDANTIC = YES;
365+
GCC_WARN_UNINITIALIZED_AUTOS = YES;
366+
GCC_WARN_UNUSED_FUNCTION = YES;
335367
INFOPLIST_FILE = Info.plist;
368+
LLVM_LTO = YES;
336369
PRODUCT_NAME = Hpple;
370+
RUN_CLANG_STATIC_ANALYZER = YES;
371+
SDKROOT = iphoneos;
337372
};
338373
name = Release;
339374
};
@@ -390,14 +425,14 @@
390425
isa = XCBuildConfiguration;
391426
buildSettings = {
392427
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
393-
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Geoffrey Grosenbach";
428+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
394429
GCC_C_LANGUAGE_STANDARD = c99;
395430
GCC_WARN_ABOUT_RETURN_TYPE = YES;
396431
GCC_WARN_UNUSED_VARIABLE = YES;
397432
HEADER_SEARCH_PATHS = /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/include/libxml2;
398433
ONLY_ACTIVE_ARCH = YES;
399434
PREBINDING = NO;
400-
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "CD1F1B03-3E48-4552-ABEF-FDFFFA2F6956";
435+
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "";
401436
SDKROOT = iphoneos2.2.1;
402437
};
403438
name = Debug;

TFHpple.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,21 @@
3333
#import "TFHppleElement.h"
3434

3535
@interface TFHpple : NSObject {
36+
@private
3637
NSData * data;
3738
BOOL isXML;
3839
}
3940

4041
- (id) initWithData:(NSData *)theData isXML:(BOOL)isDataXML;
41-
- (id) initWithHTMLData:(NSData *)theData;
4242
- (id) initWithXMLData:(NSData *)theData;
43-
- (NSArray *) search:(NSString *)xPathOrCSS;
44-
- (TFHppleElement *) at:(NSString *)xPathOrCSS;
43+
- (id) initWithHTMLData:(NSData *)theData;
44+
45+
+ (TFHpple *) hppleWithData:(NSData *)theData isXML:(BOOL)isDataXML;
46+
+ (TFHpple *) hppleWithXMLData:(NSData *)theData;
47+
+ (TFHpple *) hppleWithHTMLData:(NSData *)theData;
48+
49+
- (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS;
50+
- (TFHppleElement *) peekAtSearchWithXPathQuery:(NSString *)xPathOrCSS;
4551

4652
@property (retain) NSData * data;
4753

TFHpple.m

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ - (void) dealloc
4242

4343
- (id) initWithData:(NSData *)theData isXML:(BOOL)isDataXML
4444
{
45-
if (!(self = [super init]))
45+
if (!(self = [super init])) {
4646
return nil;
47+
}
4748

48-
self.data = theData;
49+
[theData retain];
50+
data = theData;
4951
isXML = isDataXML;
5052

5153
return self;
@@ -61,10 +63,24 @@ - (id) initWithHTMLData:(NSData *)theData
6163
return [self initWithData:theData isXML:NO];
6264
}
6365

66+
+ (TFHpple *) hppleWithData:(NSData *)theData isXML:(BOOL)isDataXML {
67+
return [[[[self class] alloc] initWithData:theData isXML:isDataXML] autorelease];
68+
}
69+
70+
+ (TFHpple *) hppleWithHTMLData:(NSData *)theData {
71+
return [[self class] hppleWithData:theData isXML:NO];
72+
}
73+
74+
+ (TFHpple *) hppleWithXMLData:(NSData *)theData {
75+
return [[self class] hppleWithData:theData isXML:YES];
76+
}
77+
78+
#pragma mark -
79+
6480
// Returns all elements at xPath.
65-
- (NSArray *) search:(NSString *)xPathOrCSS
81+
- (NSArray *) searchWithXPathQuery:(NSString *)xPathOrCSS
6682
{
67-
NSArray * detailNodes;
83+
NSArray * detailNodes = nil;
6884
if (isXML) {
6985
detailNodes = PerformXMLXPathQuery(data, xPathOrCSS);
7086
} else {
@@ -73,19 +89,18 @@ - (NSArray *) search:(NSString *)xPathOrCSS
7389

7490
NSMutableArray * hppleElements = [NSMutableArray array];
7591
for (id node in detailNodes) {
76-
TFHppleElement * e = [[TFHppleElement alloc] initWithNode:node];
77-
[hppleElements addObject:e];
78-
[e release];
92+
[hppleElements addObject:[TFHppleElement hppleElementWithNode:node]];
7993
}
8094
return hppleElements;
8195
}
8296

8397
// Returns first element at xPath
84-
- (TFHppleElement *) at:(NSString *)xPathOrCSS
98+
- (TFHppleElement *) peekAtSearchWithXPathQuery:(NSString *)xPathOrCSS
8599
{
86-
NSArray * elements = [self search:xPathOrCSS];
87-
if ([elements count] >= 1)
100+
NSArray * elements = [self searchWithXPathQuery:xPathOrCSS];
101+
if ([elements count] >= 1) {
88102
return [elements objectAtIndex:0];
103+
}
89104

90105
return nil;
91106
}

TFHppleElement.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,33 @@
3131

3232

3333
@interface TFHppleElement : NSObject {
34+
@private
3435
NSDictionary * node;
3536
}
3637

3738
- (id) initWithNode:(NSDictionary *) theNode;
3839

40+
+ (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode;
41+
3942
// Returns this tag's innerHTML content.
40-
- (NSString *) content;
43+
@property (nonatomic, readonly) NSString *content;
4144

4245
// Returns the name of the current tag, such as "h3".
43-
- (NSString *) tagName;
46+
@property (nonatomic, readonly) NSString *tagName;
4447

4548
// Returns tag attributes with name as key and content as value.
4649
// href = 'http://peepcode.com'
4750
// class = 'highlight'
48-
- (NSDictionary *) attributes;
51+
@property (nonatomic, readonly) NSDictionary *attributes;
52+
53+
// Returns the children of a given node
54+
@property (nonatomic, readonly) NSArray *children;
55+
56+
// Returns the first child of a given node
57+
@property (nonatomic, readonly) TFHppleElement *firstChild;
58+
59+
// the parent of a node
60+
@property (nonatomic, retain, readonly) TFHppleElement *parent;
4961

5062
// Provides easy access to the content of a specific attribute,
5163
// such as 'href' or 'class'.

TFHppleElement.m

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@
3030

3131
#import "TFHppleElement.h"
3232

33-
NSString * const TFHppleNodeContentKey = @"nodeContent";
34-
NSString * const TFHppleNodeNameKey = @"nodeName";
35-
NSString * const TFHppleNodeAttributeArrayKey = @"nodeAttributeArray";
36-
NSString * const TFHppleNodeAttributeNameKey = @"attributeName";
33+
static NSString * const TFHppleNodeContentKey = @"nodeContent";
34+
static NSString * const TFHppleNodeNameKey = @"nodeName";
35+
static NSString * const TFHppleNodeChildrenKey = @"nodeChildArray";
36+
static NSString * const TFHppleNodeAttributeArrayKey = @"nodeAttributeArray";
37+
static NSString * const TFHppleNodeAttributeNameKey = @"attributeName";
38+
39+
@interface TFHppleElement ()
40+
@property (nonatomic, retain, readwrite) TFHppleElement *parent;
41+
@end
3742

3843
@implementation TFHppleElement
44+
@synthesize parent;
3945

4046
- (void) dealloc
4147
{
4248
[node release];
43-
49+
[parent release];
4450
[super dealloc];
4551
}
4652

@@ -55,6 +61,11 @@ - (id) initWithNode:(NSDictionary *) theNode
5561
return self;
5662
}
5763

64+
+ (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode {
65+
return [[[[self class] alloc] initWithNode:theNode] autorelease];
66+
}
67+
68+
#pragma mark -
5869

5970
- (NSString *) content
6071
{
@@ -67,6 +78,26 @@ - (NSString *) tagName
6778
return [node objectForKey:TFHppleNodeNameKey];
6879
}
6980

81+
- (NSArray *) children
82+
{
83+
NSMutableArray *children = [NSMutableArray array];
84+
for (NSDictionary *child in [node objectForKey:TFHppleNodeChildrenKey]) {
85+
TFHppleElement *element = [TFHppleElement hppleElementWithNode:child];
86+
element.parent = self;
87+
[children addObject:element];
88+
}
89+
return children;
90+
}
91+
92+
- (TFHppleElement *) firstChild
93+
{
94+
NSArray * children = self.children;
95+
if (children.count)
96+
return [children objectAtIndex:0];
97+
return nil;
98+
}
99+
100+
70101
- (NSDictionary *) attributes
71102
{
72103
NSMutableDictionary * translatedAttributes = [NSMutableDictionary dictionary];

XPathQuery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//
88

99
NSArray *PerformHTMLXPathQuery(NSData *document, NSString *query);
10-
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);
10+
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);

XPathQuery.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#import <libxml/xpath.h>
1515
#import <libxml/xpathInternals.h>
1616

17+
NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult);
18+
NSArray *PerformXPathQuery(xmlDocPtr doc, NSString *query);
19+
1720
NSDictionary *DictionaryForNode(xmlNodePtr currentNode, NSMutableDictionary *parentResult)
1821
{
1922
NSMutableDictionary *resultForNode = [NSMutableDictionary dictionary];
@@ -150,7 +153,7 @@
150153
xmlDocPtr doc;
151154

152155
/* Load XML document */
153-
doc = htmlReadMemory([document bytes], [document length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
156+
doc = htmlReadMemory([document bytes], (int)[document length], "", NULL, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
154157

155158
if (doc == NULL)
156159
{
@@ -169,7 +172,7 @@
169172
xmlDocPtr doc;
170173

171174
/* Load XML document */
172-
doc = xmlReadMemory([document bytes], [document length], "", NULL, XML_PARSE_RECOVER);
175+
doc = xmlReadMemory([document bytes], (int)[document length], "", NULL, XML_PARSE_RECOVER);
173176

174177
if (doc == NULL)
175178
{

0 commit comments

Comments
 (0)