This commit is contained in:
giuseppenuc committed 2016-02-24 16:56:39 +01:00
1 parent d281d765ea
commit 5b074a5176
1261 files changed
+199158 -7303

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
#import <Foundation/Foundation.h>
@interface NSData (XMPP)
- (NSData *)xmpp_md5Digest;
- (NSData *)xmpp_sha1Digest;
- (NSString *)xmpp_hexStringValue;
- (NSString *)xmpp_base64Encoded;
- (NSData *)xmpp_base64Decoded;
- (BOOL)xmpp_isJPEG;
- (BOOL)xmpp_isPNG;
- (NSString *)xmpp_imageType;
@end
#ifndef XMPP_EXCLUDE_DEPRECATED
#define XMPP_DEPRECATED($message) __attribute__((deprecated($message)))
@interface NSData (XMPPDeprecated)
- (NSData *)md5Digest XMPP_DEPRECATED("Use -xmpp_md5Digest");
- (NSData *)sha1Digest XMPP_DEPRECATED("Use -xmpp_sha1Digest");
- (NSString *)hexStringValue XMPP_DEPRECATED("Use -xmpp_hexStringValue");
- (NSString *)base64Encoded XMPP_DEPRECATED("Use -xmpp_base64Encoded");
- (NSData *)base64Decoded XMPP_DEPRECATED("Use -xmpp_base64Decoded");
@end
#undef XMPP_DEPRECATED
#endif
+240
View File
@@ -0,0 +1,240 @@
#import "NSData+XMPP.h"
#import <CommonCrypto/CommonDigest.h>
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
@implementation NSData (XMPP)
static char encodingTable[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
- (NSData *)xmpp_md5Digest
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5([self bytes], (CC_LONG)[self length], result);
return [NSData dataWithBytes:result length:CC_MD5_DIGEST_LENGTH];
}
- (NSData *)xmpp_sha1Digest
{
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CC_SHA1([self bytes], (CC_LONG)[self length], result);
return [NSData dataWithBytes:result length:CC_SHA1_DIGEST_LENGTH];
}
- (NSString *)xmpp_hexStringValue
{
NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
const unsigned char *dataBuffer = [self bytes];
int i;
for (i = 0; i < [self length]; ++i)
{
[stringBuffer appendFormat:@"%02x", (unsigned int)dataBuffer[i]];
}
return [stringBuffer copy];
}
- (NSString *)xmpp_base64Encoded
{
const unsigned char *bytes = [self bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
unsigned long ixtext = 0;
unsigned long lentext = [self length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
unsigned short i = 0;
unsigned short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;
while( YES )
{
ctremaining = lentext - ixtext;
if( ctremaining <= 0 ) break;
for( i = 0; i < 3; i++ ) {
ix = ixtext + i;
if( ix < lentext ) inbuf[i] = bytes[ix];
else inbuf [i] = 0;
}
outbuf [0] = (inbuf [0] & 0xFC) >> 2;
outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
outbuf [3] = inbuf [2] & 0x3F;
ctcopy = 4;
switch( ctremaining )
{
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for( i = 0; i < ctcopy; i++ )
[result appendFormat:@"%c", encodingTable[outbuf[i]]];
for( i = ctcopy; i < 4; i++ )
[result appendString:@"="];
ixtext += 3;
charsonline += 4;
}
return [NSString stringWithString:result];
}
- (NSData *)xmpp_base64Decoded
{
const unsigned char *bytes = [self bytes];
NSMutableData *result = [NSMutableData dataWithCapacity:[self length]];
unsigned long ixtext = 0;
unsigned long lentext = [self length];
unsigned char ch = 0;
unsigned char inbuf[4] = {0, 0, 0, 0};
unsigned char outbuf[3] = {0, 0, 0};
short i = 0, ixinbuf = 0;
BOOL flignore = NO;
BOOL flendtext = NO;
while( YES )
{
if( ixtext >= lentext ) break;
ch = bytes[ixtext++];
flignore = NO;
if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
else if( ch == '+' ) ch = 62;
else if( ch == '=' ) flendtext = YES;
else if( ch == '/' ) ch = 63;
else flignore = YES;
if( ! flignore )
{
short ctcharsinbuf = 3;
BOOL flbreak = NO;
if( flendtext )
{
if( ! ixinbuf ) break;
if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
else ctcharsinbuf = 2;
ixinbuf = 3;
flbreak = YES;
}
inbuf [ixinbuf++] = ch;
if( ixinbuf == 4 )
{
ixinbuf = 0;
outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
for( i = 0; i < ctcharsinbuf; i++ )
[result appendBytes:&outbuf[i] length:1];
}
if( flbreak ) break;
}
}
return [NSData dataWithData:result];
}
- (BOOL)xmpp_isJPEG
{
if (self.length > 4)
{
unsigned char buffer[4];
[self getBytes:&buffer length:4];
return buffer[0]==0xff &&
buffer[1]==0xd8 &&
buffer[2]==0xff &&
buffer[3]==0xe0;
}
return NO;
}
- (BOOL)xmpp_isPNG
{
if (self.length > 4)
{
unsigned char buffer[4];
[self getBytes:&buffer length:4];
return buffer[0]==0x89 &&
buffer[1]==0x50 &&
buffer[2]==0x4e &&
buffer[3]==0x47;
}
return NO;
}
- (NSString *)xmpp_imageType
{
NSString *result = nil;
if([self xmpp_isPNG])
{
result = @"image/png";
}
else if([self xmpp_isJPEG])
{
result = @"image/jpeg";
}
return result;
}
@end
#ifndef XMPP_EXCLUDE_DEPRECATED
@implementation NSData (XMPPDeprecated)
- (NSData *)md5Digest {
return [self xmpp_md5Digest];
}
- (NSData *)sha1Digest {
return [self xmpp_sha1Digest];
}
- (NSString *)hexStringValue {
return [self xmpp_hexStringValue];
}
- (NSString *)base64Encoded {
return [self xmpp_base64Encoded];
}
- (NSData *)base64Decoded {
return [self xmpp_base64Decoded];
}
@end
#endif
+46
View File
@@ -0,0 +1,46 @@
#import <Foundation/Foundation.h>
@interface NSNumber (XMPP)
+ (NSNumber *)xmpp_numberWithPtr:(const void *)ptr;
- (id)xmpp_initWithPtr:(const void *)ptr __attribute__((objc_method_family(init)));
+ (BOOL)xmpp_parseString:(NSString *)str intoInt32:(int32_t *)pNum;
+ (BOOL)xmpp_parseString:(NSString *)str intoUInt32:(uint32_t *)pNum;
+ (BOOL)xmpp_parseString:(NSString *)str intoInt64:(int64_t *)pNum;
+ (BOOL)xmpp_parseString:(NSString *)str intoUInt64:(uint64_t *)pNum;
+ (BOOL)xmpp_parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum;
+ (BOOL)xmpp_parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum;
+ (UInt8)xmpp_extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset;
+ (UInt16)xmpp_extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag;
+ (UInt32)xmpp_extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag;
@end
#ifndef XMPP_EXCLUDE_DEPRECATED
#define XMPP_DEPRECATED($message) __attribute__((deprecated($message)))
@interface NSNumber (XMPPDeprecated)
+ (NSNumber *)numberWithPtr:(const void *)ptr XMPP_DEPRECATED("Use +xmpp_numberWithPtr:");
- (id)initWithPtr:(const void *)ptr XMPP_DEPRECATED("Use -xmpp_initWithPtr:");
+ (BOOL)parseString:(NSString *)str intoInt32:(int32_t *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoInt32:");
+ (BOOL)parseString:(NSString *)str intoUInt32:(uint32_t *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoUInt32:");
+ (BOOL)parseString:(NSString *)str intoInt64:(int64_t *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoInt64:");
+ (BOOL)parseString:(NSString *)str intoUInt64:(uint64_t *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoUInt64:");
+ (BOOL)parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoNSInteger:");
+ (BOOL)parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum XMPP_DEPRECATED("Use +xmpp_parseString:intoNSUInteger:");
+ (UInt8)extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset XMPP_DEPRECATED("Use +xmpp_extractUInt8FromData:atOffset:");
+ (UInt16)extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag XMPP_DEPRECATED("Use +xmpp_extractUInt16FromData:atOffset:andConvertFromNetworkOrder:");
+ (UInt32)extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag XMPP_DEPRECATED("Use +xmpp_extractUInt32FromData:atOffset:andConvertFromNetworkOrder:");
@end
#undef XMPP_DEPRECATED
#endif
+265
View File
@@ -0,0 +1,265 @@
#import "NSNumber+XMPP.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
@implementation NSNumber (XMPP)
+ (NSNumber *)xmpp_numberWithPtr:(const void *)ptr
{
return [[NSNumber alloc] xmpp_initWithPtr:ptr];
}
- (id)xmpp_initWithPtr:(const void *)ptr
{
return [self initWithLong:(long)ptr];
}
+ (BOOL)xmpp_parseString:(NSString *)str intoInt32:(int32_t *)pNum
{
if (str == nil)
{
*pNum = (int32_t)0;
return NO;
}
errno = 0;
long result = strtol([str UTF8String], NULL, 10);
if (LONG_BIT != 32)
{
if (result > INT32_MAX)
{
*pNum = INT32_MAX;
return NO;
}
if (result < INT32_MIN)
{
*pNum = INT32_MIN;
return NO;
}
}
// From the manpage:
//
// If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
// If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
//
// Clamped means it will be TYPE_MAX or TYPE_MIN.
// If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
*pNum = (int32_t)result;
if (errno != 0)
return NO;
else
return YES;
}
+ (BOOL)xmpp_parseString:(NSString *)str intoUInt32:(uint32_t *)pNum
{
if (str == nil)
{
*pNum = (uint32_t)0;
return NO;
}
errno = 0;
unsigned long result = strtoul([str UTF8String], NULL, 10);
if (LONG_BIT != 32)
{
if (result > UINT32_MAX)
{
*pNum = UINT32_MAX;
return NO;
}
}
// From the manpage:
//
// If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
// If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
//
// Clamped means it will be TYPE_MAX or TYPE_MIN.
// If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
*pNum = (uint32_t)result;
if (errno != 0)
return NO;
else
return YES;
}
+ (BOOL)xmpp_parseString:(NSString *)str intoInt64:(int64_t *)pNum
{
if (str == nil)
{
*pNum = (int64_t)0;
return NO;
}
errno = 0;
// On both 32-bit and 64-bit machines, long long = 64 bit
*pNum = strtoll([str UTF8String], NULL, 10);
// From the manpage:
//
// If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
// If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
//
// Clamped means it will be TYPE_MAX or TYPE_MIN.
// If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
if (errno != 0)
return NO;
else
return YES;
}
+ (BOOL)xmpp_parseString:(NSString *)str intoUInt64:(uint64_t *)pNum
{
if (str == nil)
{
*pNum = (uint64_t)0;
return NO;
}
errno = 0;
// On both 32-bit and 64-bit machines, unsigned long long = 64 bit
*pNum = strtoull([str UTF8String], NULL, 10);
// From the manpage:
//
// If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
// If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
//
// Clamped means it will be TYPE_MAX or TYPE_MIN.
// If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
if (errno != 0)
return NO;
else
return YES;
}
+ (BOOL)xmpp_parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum
{
if (NSIntegerMax == INT32_MAX)
return [self xmpp_parseString:str intoInt32:(int32_t *)pNum];
else
return [self xmpp_parseString:str intoInt64:(int64_t *)pNum];
}
+ (BOOL)xmpp_parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum
{
if (NSUIntegerMax == UINT32_MAX)
return [self xmpp_parseString:str intoUInt32:(uint32_t *)pNum];
else
return [self xmpp_parseString:str intoUInt64:(uint64_t *)pNum];
}
+ (UInt8)xmpp_extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset
{
// 8 bits = 1 byte
if([data length] < offset + 1) return 0;
UInt8 *pResult = (UInt8 *)([data bytes] + offset);
UInt8 result = *pResult;
return result;
}
+ (UInt16)xmpp_extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag
{
// 16 bits = 2 bytes
if([data length] < offset + 2) return 0;
UInt16 *pResult = (UInt16 *)([data bytes] + offset);
UInt16 result = *pResult;
if(flag)
return ntohs(result);
else
return result;
}
+ (UInt32)xmpp_extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag
{
// 32 bits = 4 bytes
if([data length] < offset + 4) return 0;
UInt32 *pResult = (UInt32 *)([data bytes] + offset);
UInt32 result = *pResult;
if(flag)
return ntohl(result);
else
return result;
}
@end
#ifndef XMPP_EXCLUDE_DEPRECATED
@implementation NSNumber (XMPPDeprecated)
+ (NSNumber *)numberWithPtr:(const void *)ptr {
return [self xmpp_numberWithPtr:ptr];
}
- (id)initWithPtr:(const void *)ptr {
return [self xmpp_initWithPtr:ptr];
}
+ (BOOL)parseString:(NSString *)str intoInt32:(int32_t *)pNum {
return [self xmpp_parseString:str intoInt32:pNum];
}
+ (BOOL)parseString:(NSString *)str intoUInt32:(uint32_t *)pNum {
return [self xmpp_parseString:str intoUInt32:pNum];
}
+ (BOOL)parseString:(NSString *)str intoInt64:(int64_t *)pNum {
return [self xmpp_parseString:str intoInt64:pNum];
}
+ (BOOL)parseString:(NSString *)str intoUInt64:(uint64_t *)pNum {
return [self xmpp_parseString:str intoUInt64:pNum];
}
+ (BOOL)parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum {
return [self xmpp_parseString:str intoNSInteger:pNum];
}
+ (BOOL)parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum {
return [self xmpp_parseString:str intoNSUInteger:pNum];
}
+ (UInt8)extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset {
return [self xmpp_extractUInt8FromData:data atOffset:offset];
}
+ (UInt16)extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag {
return [self xmpp_extractUInt16FromData:data atOffset:offset andConvertFromNetworkOrder:flag];
}
+ (UInt32)extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag {
return [self xmpp_extractUInt32FromData:data atOffset:offset andConvertFromNetworkOrder:flag];
}
@end
#endif
+157
View File
@@ -0,0 +1,157 @@
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import "DDXML.h"
#endif
@interface NSXMLElement (XMPP)
/**
* Convenience methods for Creating elements.
**/
+ (NSXMLElement *)elementWithName:(NSString *)name numberValue:(NSNumber *)number;
- (id)initWithName:(NSString *)name numberValue:(NSNumber *)number;
+ (NSXMLElement *)elementWithName:(NSString *)name objectValue:(id)objectValue;
- (id)initWithName:(NSString *)name objectValue:(id)objectValue;
/**
* Creating elements with explicit xmlns values.
*
* Use these instead of [NSXMLElement initWithName:URI:].
* The category methods below are more readable, and they actually work.
**/
+ (NSXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns;
- (id)initWithName:(NSString *)name xmlns:(NSString *)ns;
/**
* Extracting multiple elements.
**/
- (NSArray *)elementsForXmlns:(NSString *)ns;
- (NSArray *)elementsForXmlnsPrefix:(NSString *)nsPrefix;
/**
* Extracting a single element.
**/
- (NSXMLElement *)elementForName:(NSString *)name;
- (NSXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns;
- (NSXMLElement *)elementForName:(NSString *)name xmlnsPrefix:(NSString *)xmlnsPrefix;
/**
* Convenience methods for removing child elements.
*
* If the element doesn't exist, these methods do nothing.
**/
- (void)removeElementForName:(NSString *)name;
- (void)removeElementsForName:(NSString *)name;
- (void)removeElementForName:(NSString *)name xmlns:(NSString *)xmlns;
- (void)removeElementForName:(NSString *)name xmlnsPrefix:(NSString *)xmlnsPrefix;
/**
* Working with the common xmpp xmlns value.
*
* Use these instead of getting/setting the URI.
* The category methods below are more readable, and they actually work.
**/
- (NSString *)xmlns;
- (void)setXmlns:(NSString *)ns;
/**
* Convenience methods for printing xml elements with different styles.
**/
- (NSString *)prettyXMLString;
- (NSString *)compactXMLString;
/**
* Convenience methods for adding attributes.
**/
- (void)addAttributeWithName:(NSString *)name intValue:(int)intValue;
- (void)addAttributeWithName:(NSString *)name boolValue:(BOOL)boolValue;
- (void)addAttributeWithName:(NSString *)name floatValue:(float)floatValue;
- (void)addAttributeWithName:(NSString *)name doubleValue:(double)doubleValue;
- (void)addAttributeWithName:(NSString *)name integerValue:(NSInteger)integerValue;
- (void)addAttributeWithName:(NSString *)name unsignedIntegerValue:(NSUInteger)unsignedIntegerValue;
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string;
- (void)addAttributeWithName:(NSString *)name numberValue:(NSNumber *)number;
- (void)addAttributeWithName:(NSString *)name objectValue:(id)objectValue;
/**
* Convenience methods for extracting attribute values in different formats.
*
* E.g. <beer name="guinness" price="4.50"/> // float price = [beer attributeFloatValueForName:@"price"];
**/
- (int)attributeIntValueForName:(NSString *)name;
- (BOOL)attributeBoolValueForName:(NSString *)name;
- (float)attributeFloatValueForName:(NSString *)name;
- (double)attributeDoubleValueForName:(NSString *)name;
- (int32_t)attributeInt32ValueForName:(NSString *)name;
- (uint32_t)attributeUInt32ValueForName:(NSString *)name;
- (int64_t)attributeInt64ValueForName:(NSString *)name;
- (uint64_t)attributeUInt64ValueForName:(NSString *)name;
- (NSInteger)attributeIntegerValueForName:(NSString *)name;
- (NSUInteger)attributeUnsignedIntegerValueForName:(NSString *)name;
- (NSString *)attributeStringValueForName:(NSString *)name;
- (NSNumber *)attributeNumberIntValueForName:(NSString *)name;
- (NSNumber *)attributeNumberBoolValueForName:(NSString *)name;
- (NSNumber *)attributeNumberFloatValueForName:(NSString *)name;
- (NSNumber *)attributeNumberDoubleValueForName:(NSString *)name;
- (NSNumber *)attributeNumberInt32ValueForName:(NSString *)name;
- (NSNumber *)attributeNumberUInt32ValueForName:(NSString *)name;
- (NSNumber *)attributeNumberInt64ValueForName:(NSString *)name;
- (NSNumber *)attributeNumberUInt64ValueForName:(NSString *)name;
- (NSNumber *)attributeNumberIntegerValueForName:(NSString *)name;
- (NSNumber *)attributeNumberUnsignedIntegerValueForName:(NSString *)name;
- (int)attributeIntValueForName:(NSString *)name withDefaultValue:(int)defaultValue;
- (BOOL)attributeBoolValueForName:(NSString *)name withDefaultValue:(BOOL)defaultValue;
- (float)attributeFloatValueForName:(NSString *)name withDefaultValue:(float)defaultValue;
- (double)attributeDoubleValueForName:(NSString *)name withDefaultValue:(double)defaultValue;
- (int32_t)attributeInt32ValueForName:(NSString *)name withDefaultValue:(int32_t)defaultValue;
- (uint32_t)attributeUInt32ValueForName:(NSString *)name withDefaultValue:(uint32_t)defaultValue;
- (int64_t)attributeInt64ValueForName:(NSString *)name withDefaultValue:(int64_t)defaultValue;
- (uint64_t)attributeUInt64ValueForName:(NSString *)name withDefaultValue:(uint64_t)defaultValue;
- (NSInteger)attributeIntegerValueForName:(NSString *)name withDefaultValue:(NSInteger)defaultValue;
- (NSUInteger)attributeUnsignedIntegerValueForName:(NSString *)name withDefaultValue:(NSUInteger)defaultValue;
- (NSString *)attributeStringValueForName:(NSString *)name withDefaultValue:(NSString *)defaultValue;
- (NSNumber *)attributeNumberIntValueForName:(NSString *)name withDefaultValue:(int)defaultValue;
- (NSNumber *)attributeNumberBoolValueForName:(NSString *)name withDefaultValue:(BOOL)defaultValue;
- (NSMutableDictionary *)attributesAsDictionary;
/**
* Convenience methods for extracting element values in different formats.
*
* E.g. <price>9.99</price> // float price = [priceElement stringValueAsFloat];
**/
- (int)stringValueAsInt;
- (BOOL)stringValueAsBool;
- (float)stringValueAsFloat;
- (double)stringValueAsDouble;
- (int32_t)stringValueAsInt32;
- (uint32_t)stringValueAsUInt32;
- (int64_t)stringValueAsInt64;
- (uint64_t)stringValueAsUInt64;
- (NSInteger)stringValueAsNSInteger;
- (NSUInteger)stringValueAsNSUInteger;
/**
* Working with namespaces.
**/
- (void)addNamespaceWithPrefix:(NSString *)prefix stringValue:(NSString *)string;
- (NSString *)namespaceStringValueForPrefix:(NSString *)prefix;
- (NSString *)namespaceStringValueForPrefix:(NSString *)prefix withDefaultValue:(NSString *)defaultValue;
@end
+663
View File
@@ -0,0 +1,663 @@
#import "NSXMLElement+XMPP.h"
#import "NSNumber+XMPP.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
@implementation NSXMLElement (XMPP)
/**
* Convenience methods for Creating elements.
**/
+ (NSXMLElement *)elementWithName:(NSString *)name numberValue:(NSNumber *)number
{
return [self elementWithName:name stringValue:[number stringValue]];
}
- (id)initWithName:(NSString *)name numberValue:(NSNumber *)number
{
return [self initWithName:name stringValue:[number stringValue]];
}
+ (NSXMLElement *)elementWithName:(NSString *)name objectValue:(id)objectValue
{
if([objectValue isKindOfClass:[NSString class]])
{
return [self elementWithName:name stringValue:objectValue];
}
else if([objectValue isKindOfClass:[NSNumber class]])
{
return [self elementWithName:name numberValue:objectValue];
}
else if([objectValue respondsToSelector:@selector(stringValue)])
{
return [self elementWithName:name stringValue:[objectValue stringValue]];
}
else
{
return [self elementWithName:name];
}
}
- (id)initWithName:(NSString *)name objectValue:(id)objectValue
{
if([objectValue isKindOfClass:[NSString class]])
{
return [self initWithName:name stringValue:objectValue];
}
else if([objectValue isKindOfClass:[NSNumber class]])
{
return [self initWithName:name numberValue:objectValue];
}
else if([objectValue respondsToSelector:@selector(stringValue)])
{
return [self initWithName:name stringValue:[objectValue stringValue]];
}
else
{
return [self initWithName:name];
}
}
/**
* Quick method to create an element
**/
+ (NSXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns
{
NSXMLElement *element = [NSXMLElement elementWithName:name];
[element setXmlns:ns];
return element;
}
- (id)initWithName:(NSString *)name xmlns:(NSString *)ns
{
if ((self = [self initWithName:name]))
{
[self setXmlns:ns];
}
return self;
}
- (NSArray *)elementsForXmlns:(NSString *)ns
{
NSMutableArray *elements = [NSMutableArray array];
for (NSXMLNode *node in [self children])
{
if ([node isKindOfClass:[NSXMLElement class]])
{
NSXMLElement *element = (NSXMLElement *)node;
if ([[element xmlns] isEqual:ns])
{
[elements addObject:element];
}
}
}
return elements;
}
- (NSArray *)elementsForXmlnsPrefix:(NSString *)nsPrefix
{
NSMutableArray *elements = [NSMutableArray array];
for (NSXMLNode *node in [self children])
{
if ([node isKindOfClass:[NSXMLElement class]])
{
NSXMLElement *element = (NSXMLElement *)node;
if ([[element xmlns] hasPrefix:nsPrefix])
{
[elements addObject:element];
}
}
}
return elements;
}
/**
* This method returns the first child element for the given name (as an NSXMLElement).
* If no child elements exist for the given name, nil is returned.
**/
- (NSXMLElement *)elementForName:(NSString *)name
{
NSArray *elements = [self elementsForName:name];
if ([elements count] > 0)
{
return elements[0];
}
else
{
// There is a bug in the NSXMLElement elementsForName: method.
// Consider the following XML fragment:
//
// <query xmlns="jabber:iq:private">
// <x xmlns="some:other:namespace"></x>
// </query>
//
// Calling [query elementsForName:@"x"] results in an empty array!
//
// However, it will work properly if you use the following:
// [query elementsForLocalName:@"x" URI:@"some:other:namespace"]
//
// The trouble with this is that we may not always know the xmlns in advance,
// so in this particular case there is no way to access the element without looping through the children.
//
// This bug was submitted to apple on June 1st, 2007 and was classified as "serious".
//
// --!!-- This bug does NOT exist in DDXML --!!--
return nil;
}
}
/**
* This method returns the first child element for the given name and given xmlns (as an NSXMLElement).
* If no child elements exist for the given name and given xmlns, nil is returned.
**/
- (NSXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns
{
NSArray *elements = [self elementsForLocalName:name URI:xmlns];
if ([elements count] > 0)
{
return elements[0];
}
else
{
return nil;
}
}
- (NSXMLElement *)elementForName:(NSString *)name xmlnsPrefix:(NSString *)xmlnsPrefix{
NSXMLElement *result = nil;
for (NSXMLNode *node in [self children])
{
if ([node isKindOfClass:[NSXMLElement class]])
{
NSXMLElement *element = (NSXMLElement *)node;
if ([[element name] isEqualToString:name] && [[element xmlns] hasPrefix:xmlnsPrefix])
{
result = element;
break;
}
}
}
return result;
}
/**
* This method removes the first child element for the given name.
* If no child elements exist for the given name, this method does nothing.
**/
- (void)removeElementForName:(NSString *)name
{
NSXMLElement *element = [self elementForName:name];
if(element)
{
[self removeChildAtIndex:[[self children] indexOfObject:element]];
}
}
/**
* This method removes the all child elements for the given name.
* If no child elements exist for the given name, this method does nothing.
**/
- (void)removeElementsForName:(NSString *)name
{
NSArray *elements = [self elementsForName:name];
for(NSXMLElement *element in elements)
{
[self removeChildAtIndex:[[self children] indexOfObject:element]];
}
}
/**
* This method removes the first child element for the given name and given xmlns.
* If no child elements exist for the given name and given xmlns, this method does nothing.
**/
- (void)removeElementForName:(NSString *)name xmlns:(NSString *)xmlns
{
NSXMLElement *element = [self elementForName:name xmlns:xmlns];
if(element)
{
[self removeChildAtIndex:[[self children] indexOfObject:element]];
}
}
/**
* This method removes the first child element for the given name and given xmlns prefix.
* If no child elements exist for the given name and given xmlns prefix, this method does nothing.
**/
- (void)removeElementForName:(NSString *)name xmlnsPrefix:(NSString *)xmlnsPrefix
{
NSXMLElement *element = [self elementForName:name xmlnsPrefix:xmlnsPrefix];
if(element)
{
[self removeChildAtIndex:[[self children] indexOfObject:element]];
}
}
/**
* Returns the common xmlns "attribute", which is only accessible via the namespace methods.
* The xmlns value is often used in jabber elements.
**/
- (NSString *)xmlns
{
return [[self namespaceForPrefix:@""] stringValue];
}
- (void)setXmlns:(NSString *)ns
{
// If we use setURI: then the xmlns won't be displayed in the XMLString.
// Adding the namespace this way works properly.
[self addNamespace:[NSXMLNode namespaceWithName:@"" stringValue:ns]];
}
/**
* Shortcut to get a pretty (formatted) string representation of the element.
**/
- (NSString *)prettyXMLString
{
return [self XMLStringWithOptions:(NSXMLNodePrettyPrint | NSXMLNodeCompactEmptyElement)];
}
/**
* Shortcut to get a compact string representation of the element.
**/
- (NSString *)compactXMLString
{
return [self XMLStringWithOptions:NSXMLNodeCompactEmptyElement];
}
/**
* Shortcut to avoid having to use NSXMLNode everytime
**/
- (void)addAttributeWithName:(NSString *)name intValue:(int)intValue
{
[self addAttributeWithName:name numberValue:@(intValue)];
}
- (void)addAttributeWithName:(NSString *)name boolValue:(BOOL)boolValue
{
[self addAttributeWithName:name numberValue:@(boolValue)];
}
- (void)addAttributeWithName:(NSString *)name floatValue:(float)floatValue
{
[self addAttributeWithName:name numberValue:@(floatValue)];
}
- (void)addAttributeWithName:(NSString *)name doubleValue:(double)doubleValue
{
[self addAttributeWithName:name numberValue:@(doubleValue)];
}
- (void)addAttributeWithName:(NSString *)name integerValue:(NSInteger)integerValue
{
[self addAttributeWithName:name numberValue:@(integerValue)];
}
- (void)addAttributeWithName:(NSString *)name unsignedIntegerValue:(NSUInteger)unsignedIntegerValue
{
[self addAttributeWithName:name numberValue:@(unsignedIntegerValue)];
}
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string
{
[self addAttribute:[NSXMLNode attributeWithName:name stringValue:string]];
}
- (void)addAttributeWithName:(NSString *)name numberValue:(NSNumber *)number
{
[self addAttributeWithName:name stringValue:[number stringValue]];
}
- (void)addAttributeWithName:(NSString *)name objectValue:(id)objectValue
{
if([objectValue isKindOfClass:[NSString class]])
{
[self addAttributeWithName:name stringValue:objectValue];
}
else if([objectValue isKindOfClass:[NSNumber class]])
{
[self addAttributeWithName:name numberValue:objectValue];
}
else if([objectValue respondsToSelector:@selector(stringValue)])
{
[self addAttributeWithName:name stringValue:[objectValue stringValue]];
}
}
/**
* The following methods return the corresponding value of the attribute with the given name.
**/
- (int)attributeIntValueForName:(NSString *)name
{
return [[self attributeStringValueForName:name] intValue];
}
- (BOOL)attributeBoolValueForName:(NSString *)name
{
NSString *attributeStringValueForName = [self attributeStringValueForName:name];
BOOL result = NO;
// An XML boolean datatype can have the following legal literals: true, false, 1, 0
if ([attributeStringValueForName isEqualToString:@"true"] || [attributeStringValueForName isEqualToString:@"1"])
{
result = YES;
}
else if([attributeStringValueForName isEqualToString:@"false"] || [attributeStringValueForName isEqualToString:@"0"])
{
result = NO;
}
else
{
result = [attributeStringValueForName boolValue];
}
return result;
}
- (float)attributeFloatValueForName:(NSString *)name
{
return [[self attributeStringValueForName:name] floatValue];
}
- (double)attributeDoubleValueForName:(NSString *)name
{
return [[self attributeStringValueForName:name] doubleValue];
}
- (int32_t)attributeInt32ValueForName:(NSString *)name
{
int32_t result = 0;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoInt32:&result];
return result;
}
- (uint32_t)attributeUInt32ValueForName:(NSString *)name
{
uint32_t result = 0;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoUInt32:&result];
return result;
}
- (int64_t)attributeInt64ValueForName:(NSString *)name
{
int64_t result;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoInt64:&result];
return result;
}
- (uint64_t)attributeUInt64ValueForName:(NSString *)name
{
uint64_t result;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoUInt64:&result];
return result;
}
- (NSInteger)attributeIntegerValueForName:(NSString *)name
{
NSInteger result;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoNSInteger:&result];
return result;
}
- (NSUInteger)attributeUnsignedIntegerValueForName:(NSString *)name
{
NSUInteger result = 0;
[NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoNSUInteger:&result];
return result;
}
- (NSString *)attributeStringValueForName:(NSString *)name
{
return [[self attributeForName:name] stringValue];
}
- (NSNumber *)attributeNumberIntValueForName:(NSString *)name
{
return @([self attributeIntValueForName:name]);
}
- (NSNumber *)attributeNumberBoolValueForName:(NSString *)name
{
return @([self attributeBoolValueForName:name]);
}
- (NSNumber *)attributeNumberFloatValueForName:(NSString *)name
{
return @([self attributeFloatValueForName:name]);
}
- (NSNumber *)attributeNumberDoubleValueForName:(NSString *)name
{
return @([self attributeDoubleValueForName:name]);
}
- (NSNumber *)attributeNumberInt32ValueForName:(NSString *)name
{
return @([self attributeInt32ValueForName:name]);
}
- (NSNumber *)attributeNumberUInt32ValueForName:(NSString *)name
{
return @([self attributeUInt32ValueForName:name]);
}
- (NSNumber *)attributeNumberInt64ValueForName:(NSString *)name
{
return @([self attributeInt64ValueForName:name]);
}
- (NSNumber *)attributeNumberUInt64ValueForName:(NSString *)name
{
return @([self attributeUInt64ValueForName:name]);
}
- (NSNumber *)attributeNumberIntegerValueForName:(NSString *)name
{
return @([self attributeIntegerValueForName:name]);
}
- (NSNumber *)attributeNumberUnsignedIntegerValueForName:(NSString *)name
{
return @([self attributeUnsignedIntegerValueForName:name]);
}
/**
* The following methods return the corresponding value of the attribute with the given name.
* If the attribute does not exist, the given defaultValue is returned.
**/
- (int)attributeIntValueForName:(NSString *)name withDefaultValue:(int)defaultValue
{
NSXMLNode *attr = [self attributeForName:name];
return (attr) ? [[attr stringValue] intValue] : defaultValue;
}
- (BOOL)attributeBoolValueForName:(NSString *)name withDefaultValue:(BOOL)defaultValue
{
NSXMLNode *attr = [self attributeForName:name];
return (attr) ? [[attr stringValue] boolValue] : defaultValue;
}
- (float)attributeFloatValueForName:(NSString *)name withDefaultValue:(float)defaultValue
{
NSXMLNode *attr = [self attributeForName:name];
return (attr) ? [[attr stringValue] floatValue] : defaultValue;
}
- (double)attributeDoubleValueForName:(NSString *)name withDefaultValue:(double)defaultValue
{
NSXMLNode *attr = [self attributeForName:name];
return (attr) ? [[attr stringValue] doubleValue] : defaultValue;
}
- (int32_t)attributeInt32ValueForName:(NSString *)name withDefaultValue:(int32_t)defaultValue
{
int32_t result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoInt32:&result])
return result;
else
return defaultValue;
}
- (uint32_t)attributeUInt32ValueForName:(NSString *)name withDefaultValue:(uint32_t)defaultValue
{
uint32_t result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoUInt32:&result])
return result;
else
return defaultValue;
}
- (int64_t)attributeInt64ValueForName:(NSString *)name withDefaultValue:(int64_t)defaultValue
{
int64_t result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoInt64:&result])
return result;
else
return defaultValue;
}
- (uint64_t)attributeUInt64ValueForName:(NSString *)name withDefaultValue:(uint64_t)defaultValue
{
uint64_t result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoUInt64:&result])
return result;
else
return defaultValue;
}
- (NSInteger)attributeIntegerValueForName:(NSString *)name withDefaultValue:(NSInteger)defaultValue
{
NSInteger result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoNSInteger:&result])
return result;
else
return defaultValue;
}
- (NSUInteger)attributeUnsignedIntegerValueForName:(NSString *)name withDefaultValue:(NSUInteger)defaultValue
{
NSUInteger result = 0;
if ([NSNumber xmpp_parseString:[self attributeStringValueForName:name] intoNSUInteger:&result])
return result;
else
return defaultValue;
}
- (NSString *)attributeStringValueForName:(NSString *)name withDefaultValue:(NSString *)defaultValue
{
NSXMLNode *attr = [self attributeForName:name];
return (attr) ? [attr stringValue] : defaultValue;
}
- (NSNumber *)attributeNumberIntValueForName:(NSString *)name withDefaultValue:(int)defaultValue
{
return @([self attributeIntValueForName:name withDefaultValue:defaultValue]);
}
- (NSNumber *)attributeNumberBoolValueForName:(NSString *)name withDefaultValue:(BOOL)defaultValue
{
return @([self attributeBoolValueForName:name withDefaultValue:defaultValue]);
}
/**
* Returns all the attributes in a dictionary.
**/
- (NSMutableDictionary *)attributesAsDictionary
{
NSArray *attributes = [self attributes];
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[attributes count]];
NSUInteger i;
for(i = 0; i < [attributes count]; i++)
{
NSXMLNode *node = attributes[i];
result[[node name]] = [node stringValue];
}
return result;
}
/**
* The following methods return the corresponding value of the node.
**/
- (int)stringValueAsInt
{
return [[self stringValue] intValue];
}
- (BOOL)stringValueAsBool
{
return [[self stringValue] boolValue];
}
- (float)stringValueAsFloat
{
return [[self stringValue] floatValue];
}
- (double)stringValueAsDouble
{
return [[self stringValue] doubleValue];
}
- (int32_t)stringValueAsInt32
{
int32_t result;
if ([NSNumber xmpp_parseString:[self stringValue] intoInt32:&result])
return result;
else
return 0;
}
- (uint32_t)stringValueAsUInt32
{
uint32_t result;
if ([NSNumber xmpp_parseString:[self stringValue] intoUInt32:&result])
return result;
else
return 0;
}
- (int64_t)stringValueAsInt64
{
int64_t result = 0;
if ([NSNumber xmpp_parseString:[self stringValue] intoInt64:&result])
return result;
else
return 0;
}
- (uint64_t)stringValueAsUInt64
{
uint64_t result = 0;
if ([NSNumber xmpp_parseString:[self stringValue] intoUInt64:&result])
return result;
else
return 0;
}
- (NSInteger)stringValueAsNSInteger
{
NSInteger result = 0;
if ([NSNumber xmpp_parseString:[self stringValue] intoNSInteger:&result])
return result;
else
return 0;
}
- (NSUInteger)stringValueAsNSUInteger
{
NSUInteger result = 0;
if ([NSNumber xmpp_parseString:[self stringValue] intoNSUInteger:&result])
return result;
else
return 0;
}
/**
* Shortcut to avoid having to use NSXMLNode everytime
**/
- (void)addNamespaceWithPrefix:(NSString *)prefix stringValue:(NSString *)string
{
[self addNamespace:[NSXMLNode namespaceWithName:prefix stringValue:string]];
}
/**
* Just to make your code look a little bit cleaner.
**/
- (NSString *)namespaceStringValueForPrefix:(NSString *)prefix
{
return [[self namespaceForPrefix:prefix] stringValue];
}
- (NSString *)namespaceStringValueForPrefix:(NSString *)prefix withDefaultValue:(NSString *)defaultValue
{
NSXMLNode *namespace = [self namespaceForPrefix:prefix];
return (namespace) ? [namespace stringValue] : defaultValue;
}
@end