// // NSString+XMLExtensions.m // WordPress // // Created by Janakiram on 26/08/08. // #import "NSString+XMLExtensions.h" @implementation NSString (XMLExtensions) + (NSString *)encodeXMLCharactersIn : (NSString *)source { if (![source isKindOfClass:[NSString class]] || !source) return @""; NSString *result = [NSString stringWithString:source]; if ([result rangeOfString:@"&"].location != NSNotFound) result = [[result componentsSeparatedByString:@"&"] componentsJoinedByString:@"&"]; if ([result rangeOfString:@"<"].location != NSNotFound) result = [[result componentsSeparatedByString:@"<"] componentsJoinedByString:@"<"]; if ([result rangeOfString:@">"].location != NSNotFound) result = [[result componentsSeparatedByString:@">"] componentsJoinedByString:@">"]; if ([result rangeOfString:@"\""].location != NSNotFound) result = [[result componentsSeparatedByString:@"\""] componentsJoinedByString:@"""]; if ([result rangeOfString:@"'"].location != NSNotFound) result = [[result componentsSeparatedByString:@"'"] componentsJoinedByString:@"'"]; return result; } + (NSString *)decodeXMLCharactersIn:(NSString *)source { if (![source isKindOfClass:[NSString class]] || !source) return @""; NSString *result = [NSString stringWithString:source]; if ([result rangeOfString:@"&"].location != NSNotFound) result = [[result componentsSeparatedByString:@"&"] componentsJoinedByString:@"&"]; if ([result rangeOfString:@"<"].location != NSNotFound) result = [[result componentsSeparatedByString:@"<"] componentsJoinedByString:@"<"]; if ([result rangeOfString:@">"].location != NSNotFound) result = [[result componentsSeparatedByString:@">"] componentsJoinedByString:@">"]; if ([result rangeOfString:@"""].location != NSNotFound) result = [[result componentsSeparatedByString:@"""] componentsJoinedByString:@"\""]; if ([result rangeOfString:@"'"].location != NSNotFound) result = [[result componentsSeparatedByString:@"'"] componentsJoinedByString:@"'"]; if ([result rangeOfString:@" "].location != NSNotFound) result = [[result componentsSeparatedByString:@" "] componentsJoinedByString:@" "]; if ([result rangeOfString:@"“"].location != NSNotFound) result = [[result componentsSeparatedByString:@"“"] componentsJoinedByString:@"\""]; if ([result rangeOfString:@"”"].location != NSNotFound) result = [[result componentsSeparatedByString:@"”"] componentsJoinedByString:@"\""]; if ([result rangeOfString:@"'"].location != NSNotFound) result = [[result componentsSeparatedByString:@"'"] componentsJoinedByString:@"'"]; return result; } @end