Skip to content

Commit

Permalink
Revert dd_ prefix for additions
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Oct 27, 2019
1 parent 806ee2a commit dd16c72
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 139 deletions.
36 changes: 9 additions & 27 deletions KissXML/Additions/DDXMLElementAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,20 @@
NS_ASSUME_NONNULL_BEGIN
@interface DDXMLElement (DDAdditions)

+ (nullable DDXMLElement *)dd_elementWithName:(NSString *)name xmlns:(NSString *)ns;
+ (nullable DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns;

- (nullable DDXMLElement *)dd_elementForName:(NSString *)name;
- (nullable DDXMLElement *)dd_elementForName:(NSString *)name xmlns:(NSString *)xmlns;
- (nullable DDXMLElement *)elementForName:(NSString *)name;
- (nullable DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns;

@property (nonatomic, readwrite, nullable) NSString *dd_xmlns;
- (nullable NSString *)xmlns;
- (void)setXmlns:(NSString *)ns;

@property (nonatomic, readonly) NSString *dd_prettyXMLString;
@property (nonatomic, readonly) NSString *dd_compactXMLString;
- (NSString *)prettyXMLString;
- (NSString *)compactXMLString;

- (void)dd_addAttributeWithName:(NSString *)name stringValue:(NSString *)string;
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string;

@property (nonatomic, readonly) NSDictionary<NSString*,NSString*> *dd_attributesAsDictionary;

@end

@interface DDXMLElement (DDAdditionsDeprecated)

+ (nullable DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns DEPRECATED_MSG_ATTRIBUTE("use dd_elementWithName:xmlns: instead.");

- (nullable DDXMLElement *)elementForName:(NSString *)name DEPRECATED_MSG_ATTRIBUTE("use dd_elementForName: instead.");
- (nullable DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns DEPRECATED_MSG_ATTRIBUTE("use dd_elementForName:xmlns: instead.");

- (nullable NSString *)xmlns DEPRECATED_MSG_ATTRIBUTE("use dd_xmlns instead.");
- (void)setXmlns:(NSString *)ns DEPRECATED_MSG_ATTRIBUTE("use setDd_xmlns: instead.");

- (NSString *)prettyXMLString DEPRECATED_MSG_ATTRIBUTE("use dd_prettyXMLString instead.");
- (NSString *)compactXMLString DEPRECATED_MSG_ATTRIBUTE("use dd_compactXMLString instead.");

- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string DEPRECATED_MSG_ATTRIBUTE("use dd_addAttributeWithName:stringValue: instead.");

- (NSDictionary<NSString*,NSString*> *)attributesAsDictionary DEPRECATED_MSG_ATTRIBUTE("use dd_attributesAsDictionary instead.");
- (NSDictionary<NSString*,NSString*> *)attributesAsDictionary;

@end
NS_ASSUME_NONNULL_END
184 changes: 72 additions & 112 deletions KissXML/Additions/DDXMLElementAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,167 +5,127 @@ @implementation DDXMLElement (DDAdditions)
/**
* Quick method to create an element
**/
+ (DDXMLElement *)dd_elementWithName:(NSString *)name xmlns:(NSString *)ns
+ (DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns
{
DDXMLElement *element = [DDXMLElement elementWithName:name];
element.dd_xmlns = ns;
return element;
DDXMLElement *element = [DDXMLElement elementWithName:name];
[element setXmlns:ns];
return element;
}

/**
* This method returns the first child element for the given name.
* If no child element exists for the given name, returns nil.
**/
- (DDXMLElement *)dd_elementForName:(NSString *)name
- (DDXMLElement *)elementForName:(NSString *)name
{
NSArray *elements = [self elementsForName:name];
if([elements count] > 0)
{
return [elements objectAtIndex:0];
}
else
{
// Note: If you port this code to work with Apple's NSXML, beware of the following:
//
// 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;
}
NSArray *elements = [self elementsForName:name];
if([elements count] > 0)
{
return [elements objectAtIndex:0];
}
else
{
// Note: If you port this code to work with Apple's NSXML, beware of the following:
//
// 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.
* If no child elements exist for the given name and given xmlns, returns nil.
**/
- (DDXMLElement *)dd_elementForName:(NSString *)name xmlns:(NSString *)xmlns
- (DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns
{
NSArray *elements = [self elementsForLocalName:name URI:xmlns];
if([elements count] > 0)
{
return [elements objectAtIndex:0];
}
else
{
return nil;
}
NSArray *elements = [self elementsForLocalName:name URI:xmlns];
if([elements count] > 0)
{
return [elements objectAtIndex:0];
}
else
{
return nil;
}
}

/**
* Returns the common xmlns "attribute", which is only accessible via the namespace methods.
* The xmlns value is often used in jabber elements.
**/
- (NSString *)dd_xmlns
- (NSString *)xmlns
{
return [[self namespaceForPrefix:@""] stringValue];
return [[self namespaceForPrefix:@""] stringValue];
}

- (void)setDd_xmlns:(NSString *)ns
- (void)setXmlns:(NSString *)ns
{
// If you use setURI: then the xmlns won't be displayed in the XMLString.
// Adding the namespace this way works properly.
//
// This applies to both Apple's NSXML and DDXML.
[self addNamespace:[DDXMLNode namespaceWithName:@"" stringValue:ns]];
// If you use setURI: then the xmlns won't be displayed in the XMLString.
// Adding the namespace this way works properly.
//
// This applies to both Apple's NSXML and DDXML.
[self addNamespace:[DDXMLNode namespaceWithName:@"" stringValue:ns]];
}

/**
* Shortcut to get a pretty (formatted) string representation of the element.
**/
- (NSString *)dd_prettyXMLString
- (NSString *)prettyXMLString
{
return [self XMLStringWithOptions:(DDXMLNodePrettyPrint | DDXMLNodeCompactEmptyElement)];
return [self XMLStringWithOptions:(DDXMLNodePrettyPrint | DDXMLNodeCompactEmptyElement)];
}

/**
* Shortcut to get a compact string representation of the element.
**/
- (NSString *)dd_compactXMLString
- (NSString *)compactXMLString
{
return [self XMLStringWithOptions:DDXMLNodeCompactEmptyElement];
}

/**
* Shortcut to avoid having to manually create a DDXMLNode everytime.
* Shortcut to avoid having to manually create a DDXMLNode everytime.
**/
- (void)dd_addAttributeWithName:(NSString *)name stringValue:(NSString *)string
- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string
{
[self addAttribute:[DDXMLNode attributeWithName:name stringValue:string]];
[self addAttribute:[DDXMLNode attributeWithName:name stringValue:string]];
}

/**
* Returns all the attributes as a dictionary.
**/
- (NSDictionary *)dd_attributesAsDictionary
- (NSDictionary *)attributesAsDictionary
{
NSArray *attributes = [self attributes];
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[attributes count]];

uint i;
for(i = 0; i < [attributes count]; i++)
{
DDXMLNode *node = [attributes objectAtIndex:i];

[result setObject:[node stringValue] forKey:[node name]];
}
return result;
}

@end

@implementation DDXMLElement (DDAdditionsDeprecated)

+ (nullable DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns {
return [self dd_elementWithName:name xmlns:ns];
}

- (nullable DDXMLElement *)elementForName:(NSString *)name {
return [self dd_elementForName:name];
}

- (nullable DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns {
return [self dd_elementForName:name xmlns:xmlns];
}

- (nullable NSString *)xmlns {
return [self dd_xmlns];
}

- (void)setXmlns:(NSString *)ns {
[self setDd_xmlns:ns];
}

- (NSString *)prettyXMLString {
return [self dd_prettyXMLString];
}

- (NSString *)compactXMLString {
return [self dd_compactXMLString];
}

- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string {
return [self dd_addAttributeWithName:name stringValue:string];
}

- (NSDictionary<NSString*,NSString*> *)attributesAsDictionary {
return [self dd_attributesAsDictionary];
NSArray *attributes = [self attributes];
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[attributes count]];

uint i;
for(i = 0; i < [attributes count]; i++)
{
DDXMLNode *node = [attributes objectAtIndex:i];

[result setObject:[node stringValue] forKey:[node name]];
}
return result;
}

@end

0 comments on commit dd16c72

Please sign in to comment.