Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion KissXML/DDXMLDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ - (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)err
// Therefore, we call it again here just to be safe.
xmlKeepBlanksDefault(0);

[DDXMLNode installErrorHandlersInThread];
xmlDocPtr doc = xmlParseMemory([data bytes], [data length]);
if (doc == NULL)
{
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:nil];
NSError *lastError = [DDXMLNode lastError];
NSDictionary *userInfo = lastError ? [NSDictionary dictionaryWithObjectsAndKeys:lastError, NSUnderlyingErrorKey, nil] : nil;
if (error) *error = [NSError errorWithDomain:@"DDXMLErrorDomain" code:1 userInfo:userInfo];

return nil;
}
Expand Down
2 changes: 2 additions & 0 deletions KissXML/DDXMLNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,6 @@ enum {
//- (NSArray *)objectsForXQuery:(NSString *)xquery constants:(NSDictionary *)constants error:(NSError **)error;
//- (NSArray *)objectsForXQuery:(NSString *)xquery error:(NSError **)error;

+ (void)installErrorHandlersInThread;

@end
41 changes: 17 additions & 24 deletions KissXML/DDXMLNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ @implementation DDXMLNode

#endif

+ (void)installErrorHandlersInThread
{
// Redirect error output to our own function (don't clog up the console)
initGenericErrorDefaultFunc(NULL);
xmlSetStructuredErrorFunc(NULL, MyErrorHandler);
}


/**
* From Apple's Documentation:
*
Expand All @@ -59,10 +67,6 @@ + (void)initialize
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

// Redirect error output to our own function (don't clog up the console)
initGenericErrorDefaultFunc(NULL);
xmlSetStructuredErrorFunc(NULL, MyErrorHandler);

// Tell libxml not to keep ignorable whitespace (such as node indentation, formatting, etc).
// NSXML ignores such whitespace.
// This also has the added benefit of taking up less RAM when parsing formatted XML documents.
Expand Down Expand Up @@ -1200,6 +1204,8 @@ - (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
DDXMLNotZombieAssert();
#endif

[[self class] installErrorHandlersInThread];

xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;

Expand Down Expand Up @@ -1944,29 +1950,12 @@ + (void)removeAllChildrenFromNode:(xmlNodePtr)node
**/
+ (NSError *)lastError
{
NSValue *lastErrorValue = [[[NSThread currentThread] threadDictionary] objectForKey:DDLastErrorKey];
if(lastErrorValue)
{
xmlError lastError;
[lastErrorValue getValue:&lastError];

int errCode = lastError.code;
NSString *errMsg = [[NSString stringWithFormat:@"%s", lastError.message] stringByTrimming];

NSDictionary *info = [NSDictionary dictionaryWithObject:errMsg forKey:NSLocalizedDescriptionKey];

return [NSError errorWithDomain:@"DDXMLErrorDomain" code:errCode userInfo:info];
}
else
{
return nil;
}
return [[[NSThread currentThread] threadDictionary] objectForKey:DDLastErrorKey];
}

static void MyErrorHandler(void * userData, xmlErrorPtr error)
{
// This method is called by libxml when an error occurs.
// We register for this error in the initialize method below.

// Extract error message and store in the current thread's dictionary.
// This ensure's thread safey, and easy access for all other DDXML classes.
Expand All @@ -1977,9 +1966,13 @@ static void MyErrorHandler(void * userData, xmlErrorPtr error)
}
else
{
NSValue *errorValue = [NSValue valueWithBytes:error objCType:@encode(xmlError)];
int errCode = error->code;
NSString *errMsg = [[NSString stringWithFormat:@"%s", error->message] stringByTrimming];

NSDictionary *info = [NSDictionary dictionaryWithObject:errMsg forKey:NSLocalizedDescriptionKey];
NSError *errorObject = [NSError errorWithDomain:@"libxml2" code:errCode userInfo:info];

[[[NSThread currentThread] threadDictionary] setObject:errorValue forKey:DDLastErrorKey];
[[[NSThread currentThread] threadDictionary] setObject:errorObject forKey:DDLastErrorKey];
}
}

Expand Down
1 change: 1 addition & 0 deletions KissXML/Private/DDXMLPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ NS_INLINE BOOL IsXmlNsPtr(void *kindPtr)
BOOL DDXMLIsZombie(void *xmlPtr, DDXMLNode *wrapper);

+ (NSError *)lastError;
+ (void)installErrorHandlersInThread;

@end

Expand Down