Skip to content

Commit

Permalink
Adopt modern Obj-C nullability, generics matching NSXML API #66
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Jul 6, 2016
1 parent d203084 commit 77c749d
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 109 deletions.
12 changes: 7 additions & 5 deletions KissXML/Additions/DDXMLElementAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
// These methods are not part of the standard NSXML API.
// But any developer working extensively with XML will likely appreciate them.

NS_ASSUME_NONNULL_BEGIN
@interface DDXMLElement (DDAdditions)

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

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

- (NSString *)xmlns;
- (nullable NSString *)xmlns;
- (void)setXmlns:(NSString *)ns;

- (NSString *)prettyXMLString;
- (NSString *)compactXMLString;

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

- (NSDictionary *)attributesAsDictionary;
- (NSDictionary<NSString*,NSString*> *)attributesAsDictionary;

@end
NS_ASSUME_NONNULL_END
2 changes: 2 additions & 0 deletions KissXML/Categories/NSString+DDXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// We redefine xmlChar to avoid a non-modular include
typedef unsigned char xmlChar;

NS_ASSUME_NONNULL_BEGIN
@interface NSString (DDXML)

/**
Expand All @@ -13,3 +14,4 @@ typedef unsigned char xmlChar;
- (NSString *)stringByTrimming;

@end
NS_ASSUME_NONNULL_END
18 changes: 10 additions & 8 deletions KissXML/DDXMLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ enum {
};
typedef NSUInteger DDXMLDocumentContentKind;

NS_ASSUME_NONNULL_BEGIN
@interface DDXMLDocument : DDXMLNode
{
}

- (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error;
//- (id)initWithContentsOfURL:(NSURL *)url options:(NSUInteger)mask error:(NSError **)error;
- (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error;
//- (id)initWithRootElement:(DDXMLElement *)element;
- (nullable instancetype)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error;
//- (instancetype)initWithContentsOfURL:(NSURL *)url options:(NSUInteger)mask error:(NSError **)error;
- (nullable instancetype)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error;
//- (instancetype)initWithRootElement:(DDXMLElement *)element;

//+ (Class)replacementClassForClass:(Class)cls;

Expand All @@ -58,7 +59,7 @@ typedef NSUInteger DDXMLDocumentContentKind;
//- (DDXMLDTD *)DTD;

//- (void)setRootElement:(DDXMLNode *)root;
- (DDXMLElement *)rootElement;
- (nullable DDXMLElement *)rootElement;

//- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index;

Expand All @@ -75,10 +76,11 @@ typedef NSUInteger DDXMLDocumentContentKind;
- (NSData *)XMLData;
- (NSData *)XMLDataWithOptions:(NSUInteger)options;

//- (id)objectByApplyingXSLT:(NSData *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (id)objectByApplyingXSLTString:(NSString *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (id)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)argument error:(NSError **)error;
//- (instancetype)objectByApplyingXSLT:(NSData *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (instancetype)objectByApplyingXSLTString:(NSString *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (instancetype)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)argument error:(NSError **)error;

//- (BOOL)validateAndReturnError:(NSError **)error;

@end
NS_ASSUME_NONNULL_END
12 changes: 6 additions & 6 deletions KissXML/DDXMLDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ @implementation DDXMLDocument
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
**/
+ (id)nodeWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)owner
+ (instancetype)nodeWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)owner
{
return [[DDXMLDocument alloc] initWithDocPrimitive:doc owner:owner];
}

- (id)initWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)inOwner
- (instancetype)initWithDocPrimitive:(xmlDocPtr)doc owner:(DDXMLNode *)inOwner
{
self = [super initWithPrimitive:(xmlKindPtr)doc owner:inOwner];
return self;
}

+ (id)nodeWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)owner
+ (instancetype)nodeWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)owner
{
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes
NSAssert(NO, @"Use nodeWithDocPrimitive:owner:");

return nil;
}

- (id)initWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)inOwner
- (instancetype)initWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)inOwner
{
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes.
NSAssert(NO, @"Use initWithDocPrimitive:owner:");
Expand All @@ -62,7 +62,7 @@ - (id)initWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)inOwner
* Returns an initialized DDXMLDocument object, or nil if initialization fails
* because of parsing errors or other reasons.
**/
- (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error
- (instancetype)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error
{
return [self initWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
options:mask
Expand All @@ -75,7 +75,7 @@ - (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSErr
* Returns an initialized DDXMLDocument object, or nil if initialization fails
* because of parsing errors or other reasons.
**/
- (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error
- (instancetype)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error
{
if (data == nil || [data length] == 0)
{
Expand Down
30 changes: 15 additions & 15 deletions KissXML/DDXMLElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@
* https://github.com/robbiehanson/KissXML/wiki/Reference
**/

NS_ASSUME_NONNULL_BEGIN
@interface DDXMLElement : DDXMLNode
{
}

- (id)initWithName:(NSString *)name;
- (id)initWithName:(NSString *)name URI:(NSString *)URI;
- (id)initWithName:(NSString *)name stringValue:(NSString *)string;
- (id)initWithXMLString:(NSString *)string error:(NSError **)error;
- (instancetype)initWithName:(NSString *)name;
- (instancetype)initWithName:(NSString *)name URI:(nullable NSString *)URI;
- (instancetype)initWithName:(NSString *)name stringValue:(nullable NSString *)string;
- (nullable instancetype)initWithXMLString:(NSString *)string error:(NSError **)error NS_DESIGNATED_INITIALIZER;

#pragma mark --- Elements by name ---

- (NSArray *)elementsForName:(NSString *)name;
- (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI;
- (NSArray<DDXMLElement *> *)elementsForName:(NSString *)name;
- (NSArray<DDXMLElement *> *)elementsForLocalName:(NSString *)localName URI:(nullable NSString *)URI;

#pragma mark --- Attributes ---

- (void)addAttribute:(DDXMLNode *)attribute;
- (void)removeAttributeForName:(NSString *)name;
- (void)setAttributes:(NSArray *)attributes;
@property (nullable, copy) NSArray<DDXMLNode *> *attributes;
//- (void)setAttributesAsDictionary:(NSDictionary *)attributes;
- (NSArray *)attributes;
- (DDXMLNode *)attributeForName:(NSString *)name;
- (nullable DDXMLNode *)attributeForName:(NSString *)name;
//- (DDXMLNode *)attributeForLocalName:(NSString *)localName URI:(NSString *)URI;

#pragma mark --- Namespaces ---

- (void)addNamespace:(DDXMLNode *)aNamespace;
- (void)removeNamespaceForPrefix:(NSString *)name;
- (void)setNamespaces:(NSArray *)namespaces;
- (NSArray *)namespaces;
- (DDXMLNode *)namespaceForPrefix:(NSString *)prefix;
- (DDXMLNode *)resolveNamespaceForName:(NSString *)name;
- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
@property (nullable, copy) NSArray<DDXMLNode *> *namespaces; //primitive
- (nullable DDXMLNode *)namespaceForPrefix:(NSString *)prefix;
- (nullable DDXMLNode *)resolveNamespaceForName:(NSString *)name;
- (nullable NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;

#pragma mark --- Children ---

- (void)insertChild:(DDXMLNode *)child atIndex:(NSUInteger)index;
//- (void)insertChildren:(NSArray *)children atIndex:(NSUInteger)index;
- (void)removeChildAtIndex:(NSUInteger)index;
- (void)setChildren:(NSArray *)children;
- (void)setChildren:(nullable NSArray<DDXMLNode *> *)children;
- (void)addChild:(DDXMLNode *)child;
//- (void)replaceChildAtIndex:(NSUInteger)index withNode:(DDXMLNode *)node;
//- (void)normalizeAdjacentTextNodesPreservingCDATA:(BOOL)preserve;

@end
NS_ASSUME_NONNULL_END
16 changes: 8 additions & 8 deletions KissXML/DDXMLElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ @implementation DDXMLElement
* Returns a DDXML wrapper object for the given primitive node.
* The given node MUST be non-NULL and of the proper type.
**/
+ (id)nodeWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)owner
+ (instancetype)nodeWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)owner
{
return [[DDXMLElement alloc] initWithElementPrimitive:node owner:owner];
}

- (id)initWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)inOwner
- (instancetype)initWithElementPrimitive:(xmlNodePtr)node owner:(DDXMLNode *)inOwner
{
self = [super initWithPrimitive:(xmlKindPtr)node owner:inOwner];
return self;
}

+ (id)nodeWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)owner
+ (instancetype)nodeWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)owner
{
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes
NSAssert(NO, @"Use nodeWithElementPrimitive:owner:");

return nil;
}

- (id)initWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)inOwner
- (instancetype)initWithPrimitive:(xmlKindPtr)kindPtr owner:(DDXMLNode *)inOwner
{
// Promote initializers which use proper parameter types to enable compiler to catch more mistakes.
NSAssert(NO, @"Use initWithElementPrimitive:owner:");

return nil;
}

- (id)initWithName:(NSString *)name
- (instancetype)initWithName:(NSString *)name
{
// Note: Make every guarantee that genericPtr is not null

Expand All @@ -69,7 +69,7 @@ - (id)initWithName:(NSString *)name
return [self initWithElementPrimitive:node owner:nil];
}

- (id)initWithName:(NSString *)name URI:(NSString *)URI
- (instancetype)initWithName:(NSString *)name URI:(NSString *)URI
{
// Note: Make every guarantee that genericPtr is not null

Expand All @@ -85,7 +85,7 @@ - (id)initWithName:(NSString *)name URI:(NSString *)URI
return result;
}

- (id)initWithName:(NSString *)name stringValue:(NSString *)string
- (instancetype)initWithName:(NSString *)name stringValue:(NSString *)string
{
// Note: Make every guarantee that genericPtr is not null

Expand All @@ -101,7 +101,7 @@ - (id)initWithName:(NSString *)name stringValue:(NSString *)string
return result;
}

- (id)initWithXMLString:(NSString *)string error:(NSError **)error
- (instancetype)initWithXMLString:(NSString *)string error:(NSError **)error
{
DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:string options:0 error:error];
if (doc == nil)
Expand Down
58 changes: 28 additions & 30 deletions KissXML/DDXMLNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ enum {
};



NS_ASSUME_NONNULL_BEGIN
@interface DDXMLNode : NSObject <NSCopying>

//- (id)initWithKind:(DDXMLNodeKind)kind;
//- (instancetype)initWithKind:(DDXMLNodeKind)kind;

//- (id)initWithKind:(DDXMLNodeKind)kind options:(NSUInteger)options;
//- (instancetype)initWithKind:(DDXMLNodeKind)kind options:(NSUInteger)options;

//+ (id)document;
//+ (instancetype)document;

//+ (id)documentWithRootElement:(DDXMLElement *)element;
//+ (instancetype)documentWithRootElement:(DDXMLElement *)element;

+ (id)elementWithName:(NSString *)name;

+ (id)elementWithName:(NSString *)name URI:(NSString *)URI;

+ (id)elementWithName:(NSString *)name stringValue:(NSString *)string;

+ (id)elementWithName:(NSString *)name children:(NSArray *)children attributes:(NSArray *)attributes;
+ (id)elementWithName:(NSString *)name children:(nullable NSArray<DDXMLNode *> *)children attributes:(nullable NSArray<DDXMLNode *> *)attributes;

+ (id)attributeWithName:(NSString *)name stringValue:(NSString *)stringValue;

Expand All @@ -76,68 +76,66 @@ enum {

+ (id)textWithStringValue:(NSString *)stringValue;

//+ (id)DTDNodeWithXMLString:(NSString *)string;
//+ (instancetype)DTDNodeWithXMLString:(NSString *)string;

#pragma mark --- Properties ---

- (DDXMLNodeKind)kind;

- (void)setName:(NSString *)name;
- (NSString *)name;
@property (nullable, copy) NSString *name; //primitive

//- (void)setObjectValue:(id)value;
//- (id)objectValue;
//- (instancetype)objectValue;

- (void)setStringValue:(NSString *)string;
@property (nullable, copy) NSString *stringValue; //primitive
//- (void)setStringValue:(NSString *)string resolvingEntities:(BOOL)resolve;
- (NSString *)stringValue;

#pragma mark --- Tree Navigation ---

- (NSUInteger)index;

- (NSUInteger)level;

- (DDXMLDocument *)rootDocument;
- (nullable DDXMLDocument *)rootDocument;

- (DDXMLNode *)parent;
- (nullable DDXMLNode *)parent;
- (NSUInteger)childCount;
- (NSArray *)children;
- (DDXMLNode *)childAtIndex:(NSUInteger)index;
- (nullable NSArray<DDXMLNode *> *)children;
- (nullable DDXMLNode *)childAtIndex:(NSUInteger)index;

- (DDXMLNode *)previousSibling;
- (DDXMLNode *)nextSibling;
- (nullable DDXMLNode *)previousSibling;
- (nullable DDXMLNode *)nextSibling;

- (DDXMLNode *)previousNode;
- (DDXMLNode *)nextNode;
- (nullable DDXMLNode *)previousNode;
- (nullable DDXMLNode *)nextNode;

- (void)detach;

- (NSString *)XPath;
- (nullable NSString *)XPath;

#pragma mark --- QNames ---

- (NSString *)localName;
- (NSString *)prefix;
- (nullable NSString *)localName;
- (nullable NSString *)prefix;

- (void)setURI:(NSString *)URI;
- (NSString *)URI;
@property (nullable, copy) NSString *URI; //primitive

+ (NSString *)localNameForName:(NSString *)name;
+ (NSString *)prefixForName:(NSString *)name;
+ (nullable NSString *)prefixForName:(NSString *)name;
//+ (DDXMLNode *)predefinedNamespaceForPrefix:(NSString *)name;

#pragma mark --- Output ---

- (NSString *)description;
- (NSString *)XMLString;
- (NSString *)XMLStringWithOptions:(NSUInteger)options;
- (nonnull NSString *)description;
- (nonnull NSString *)XMLString;
- (nonnull NSString *)XMLStringWithOptions:(NSUInteger)options;
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;

#pragma mark --- XPath/XQuery ---

- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
- (nullable NSArray<__kindof DDXMLNode *> *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
//- (NSArray *)objectsForXQuery:(NSString *)xquery constants:(NSDictionary *)constants error:(NSError **)error;
//- (NSArray *)objectsForXQuery:(NSString *)xquery error:(NSError **)error;

@end
NS_ASSUME_NONNULL_END
Loading

0 comments on commit 77c749d

Please sign in to comment.