Refactored OPML
This commit is contained in:
@@ -1,22 +1,48 @@
|
||||
//
|
||||
// RSXMLError.m
|
||||
// RSXML
|
||||
//
|
||||
// Created by Brent Simmons on 2/28/16.
|
||||
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RSXMLError.h"
|
||||
|
||||
NSString *RSXMLErrorDomain = @"com.ranchero.RSXML";
|
||||
NSErrorDomain kLIBXMLParserErrorDomain = @"LIBXMLParserErrorDomain";
|
||||
NSErrorDomain kRSXMLParserErrorDomain = @"RSXMLParserErrorDomain";
|
||||
|
||||
NSError *RSOPMLWrongFormatError(NSString *fileName) {
|
||||
|
||||
NSString *localizedDescriptionFormatString = NSLocalizedString(@"The file ‘%@’ can’t be parsed because it’s not an OPML file.", @"OPML wrong format");
|
||||
NSString *localizedDescription = [NSString stringWithFormat:localizedDescriptionFormatString, fileName];
|
||||
|
||||
NSString *localizedFailureString = NSLocalizedString(@"The file is not an OPML file.", @"OPML wrong format");
|
||||
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: localizedDescription, NSLocalizedFailureReasonErrorKey: localizedFailureString};
|
||||
|
||||
return [[NSError alloc] initWithDomain:RSXMLErrorDomain code:RSXMLErrorCodeDataIsWrongFormat userInfo:userInfo];
|
||||
NSString * getErrorMessageForRSXMLError(RSXMLError code, id paramA);
|
||||
NSString * getErrorMessageForRSXMLError(RSXMLError code, id paramA) {
|
||||
switch (code) { // switch statement will warn if an enum value is missing
|
||||
case RSXMLErrorNoData:
|
||||
return @"Couldn't parse feed. No data available.";
|
||||
case RSXMLErrorMissingLeftCaret:
|
||||
return @"Couldn't parse feed. Missing left caret character ('<').";
|
||||
case RSXMLErrorProbablyHTML:
|
||||
return @"Couldn't parse feed. Expecting XML data but found html data.";
|
||||
case RSXMLErrorContainsXMLErrorsTag:
|
||||
return @"Couldn't parse feed. XML contains 'errors' tag.";
|
||||
case RSXMLErrorNoSuitableParser:
|
||||
return @"Couldn't parse feed. No suitable parser found. XML document not well-formed.";
|
||||
case RSXMLErrorFileNotOPML:
|
||||
if (paramA) {
|
||||
return [NSString stringWithFormat:@"The file ‘%@’ can't be parsed because it's not an OPML file.", paramA];
|
||||
}
|
||||
return @"The file can't be parsed because it's not an OPML file.";
|
||||
}
|
||||
}
|
||||
|
||||
void RSXMLSetError(NSError **error, RSXMLError code, NSString *filename) {
|
||||
if (error) {
|
||||
*error = RSXMLMakeError(code, filename);
|
||||
}
|
||||
}
|
||||
|
||||
NSError * RSXMLMakeError(RSXMLError code, NSString *filename) {
|
||||
return [NSError errorWithDomain:kRSXMLParserErrorDomain code:code
|
||||
userInfo:@{NSLocalizedDescriptionKey: getErrorMessageForRSXMLError(code, nil)}];
|
||||
}
|
||||
|
||||
NSError * RSXMLMakeErrorFromLIBXMLError(xmlErrorPtr err) {
|
||||
if (err) {
|
||||
int errCode = err->code;
|
||||
char * msg = err->message;
|
||||
//if (err->level == XML_ERR_FATAL)
|
||||
NSString *errMsg = [[NSString stringWithFormat:@"%s", msg] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
return [NSError errorWithDomain:kLIBXMLParserErrorDomain code:errCode userInfo:@{NSLocalizedDescriptionKey: errMsg}];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user