Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalduggal committed Dec 8, 2011
1 parent 1d2c2b7 commit bc6b5fc
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 112 deletions.
86 changes: 15 additions & 71 deletions iphone/Classes/TiDOMDocumentProxy.m
Expand Up @@ -101,52 +101,20 @@ -(id)createAttributeNS:(id)args

ENSURE_ARG_OR_NIL_AT_INDEX(theURI, args, 0, NSString);
ENSURE_ARG_AT_INDEX(tagName, args, 1, NSString);

NSString *error = nil;
NSString *suberror = nil;

[self validateAttributeParameters:tagName withUri:theURI reason:&error subreason:&suberror];
if (error != nil) {
[self throwException:error subreason:suberror location:CODELOCATION];
}

//THIS WILL NOT WORK UNTIL ADD CHILD IS CALLED SO CREATE A NAMESPACE POINTER AND SET IT EXPLICITLY
//GDataXMLNode* resultNode = (GDataXMLNode*)[GDataXMLElement attributeWithName:tagName URI:theURI stringValue:@""];
NSString* prefix = [GDataXMLNode prefixForName:tagName];
NSString* localName = [GDataXMLNode localNameForName:tagName];

if (![[tagName lowercaseString] isEqualToString:@"xmlns"]) {
//Check name validity
if (![TiDOMValidator checkAttributeName:localName]) {
[self throwException:@"Invalid attribute name" subreason:[NSString stringWithFormat:@"Offending localName %@",localName] location:CODELOCATION];
return [NSNull null];
}

if (prefix != nil && ([theURI length]==0) ) {
[self throwException:@"Can not have a prefix with a nil or empty URI" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}

if ( [prefix isEqualToString:@"xml"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/XML/1998/namespace"]) {
[self throwException:@"Invalid URI for prefix" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
[self throwException:@"Invalid prefix" subreason:[NSString stringWithFormat:@"Offending prefix %@",prefix] location:CODELOCATION];
return [NSNull null];
}
//Check URI validity
if (![TiDOMValidator checkNamespaceURI:theURI]) {
[self throwException:@"Invalid URI" subreason:[NSString stringWithFormat:@"Offending URI %@",theURI] location:CODELOCATION];
return [NSNull null];
}
}

}
else {
if (![theURI isEqualToString:@"http://www.w3.org/2000/xmlns/"]) {
[self throwException:@"Invalid URI for qualified name xmlns" subreason:[NSString stringWithFormat:@"%@:%@",tagName,theURI] location:CODELOCATION];
return [NSNull null];
}
}


id context = ([self executionContext]==nil)?[self pageContext]:[self executionContext];

TiDOMAttrProxy *result = [[[TiDOMAttrProxy alloc] _initWithPageContext:context] autorelease];
Expand Down Expand Up @@ -245,42 +213,18 @@ -(id)createElementNS:(id)args
ENSURE_ARG_OR_NIL_AT_INDEX(theURI, args, 0, NSString);
ENSURE_ARG_AT_INDEX(tagName, args, 1, NSString);

NSString *error = nil;
NSString *suberror = nil;
[self validateElementParameters:tagName withUri:theURI reason:&error subreason:&suberror];
if (error != nil) {
[self throwException:error subreason:suberror location:CODELOCATION];
}

//THIS WILL NOT WORK UNTIL ADD CHILD IS CALLED SO CREATE A NAMESPACE POINTER AND SET IT EXPLICITLY
//GDataXMLElement * resultElement = [GDataXMLElement elementWithName:tagName URI:theURI];
NSString* prefix = [GDataXMLNode prefixForName:tagName];
NSString* localName = [GDataXMLNode localNameForName:tagName];

//Check name validity
if (![TiDOMValidator checkElementName:localName]) {
[self throwException:@"Invalid element name" subreason:[NSString stringWithFormat:@"Offending localName %@",localName] location:CODELOCATION];
return [NSNull null];
}

if (prefix != nil && ([theURI length]==0) ) {
[self throwException:@"Can not have a prefix with a nil or empty URI" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}

if ( [prefix isEqualToString:@"xml"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/XML/1998/namespace"]) {
[self throwException:@"Invalid URI for prefix xml" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
[self throwException:@"Invalid prefix" subreason:[NSString stringWithFormat:@"Offending prefix %@",prefix] location:CODELOCATION];
return [NSNull null];
}
//Check URI validity
if (![TiDOMValidator checkNamespaceURI:theURI]) {
[self throwException:@"Invalid URI" subreason:[NSString stringWithFormat:@"Offending URI %@",theURI] location:CODELOCATION];
return [NSNull null];
}
}


id context = ([self executionContext]==nil)?[self pageContext]:[self executionContext];
TiDOMElementProxy * result = [[[TiDOMElementProxy alloc] _initWithPageContext:context] autorelease];
GDataXMLElement * resultElement = [GDataXMLElement elementWithName:localName];
Expand Down
47 changes: 7 additions & 40 deletions iphone/Classes/TiDOMElementProxy.m
Expand Up @@ -178,50 +178,17 @@ -(void)setAttributeNS:(id)args
ENSURE_ARG_AT_INDEX(name, args, 1, NSString);
ENSURE_ARG_OR_NIL_AT_INDEX(val, args, 2, NSString);

NSString *error = nil;
NSString *suberror = nil;

[self validateAttributeParameters:name withUri:theURI reason:&error subreason:&suberror];
if (error != nil) {
[self throwException:error subreason:suberror location:CODELOCATION];
}

NSString* prefix = [GDataXMLNode prefixForName:name];
NSString* localName = [GDataXMLNode localNameForName:name];

if (![[name lowercaseString] isEqualToString:@"xmlns"]) {
//Check name validity
if (![TiDOMValidator checkAttributeName:localName]) {
[self throwException:@"Invalid attribute name" subreason:[NSString stringWithFormat:@"Offending localName %@",localName] location:CODELOCATION];
return [NSNull null];
}

if (prefix != nil && ([theURI length]==0) ) {
[self throwException:@"Can not have a prefix with a nil or empty URI" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}

if ( [prefix isEqualToString:@"xml"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/XML/1998/namespace"]) {
[self throwException:@"Invalid URI for prefix" subreason:[NSString stringWithFormat:@"%@:%@",prefix,theURI] location:CODELOCATION];
return [NSNull null];
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
[self throwException:@"Invalid prefix" subreason:[NSString stringWithFormat:@"Offending prefix %@",prefix] location:CODELOCATION];
return [NSNull null];
}
//Check URI validity
if (![TiDOMValidator checkNamespaceURI:theURI]) {
[self throwException:@"Invalid URI" subreason:[NSString stringWithFormat:@"Offending URI %@",theURI] location:CODELOCATION];
return [NSNull null];
}
}

}
else {
if (![theURI isEqualToString:@"http://www.w3.org/2000/xmlns/"]) {
[self throwException:@"Invalid URI for qualified name xmlns" subreason:[NSString stringWithFormat:@"%@:%@",name,theURI] location:CODELOCATION];
return [NSNull null];
}
}


if (val == nil) {
val = @"";
}
Expand Down
5 changes: 4 additions & 1 deletion iphone/Classes/TiDOMNodeProxy.h
Expand Up @@ -21,11 +21,14 @@
-(NSString *)XMLString;
-(id)makeNode:(id)child context:(id<TiEvaluator>)context;
+(id)makeNode:(id)child context:(id<TiEvaluator>)context;
-(id)makeNodeListProxyFromArray:(NSArray*)nodes context:(id<TiEvaluator>)context;
+(id)nodeForXMLNode:(xmlNodePtr) nodePtr;
+(void)setNode:(id)node forXMLNode:(xmlNodePtr) nodePtr;
+(void)removeNodeForXMLNode:(xmlNodePtr)nodePtr;

-(id)makeNodeListProxyFromArray:(NSArray*)nodes context:(id<TiEvaluator>)context;
-(void)validateAttributeParameters:(NSString*)tagName withUri:(NSString*)theURI reason:(NSString**)error subreason:(NSString**)suberror;
-(void)validateElementParameters:(NSString*)tagName withUri:(NSString*)theURI reason:(NSString**)error subreason:(NSString**)suberror;

@property(nonatomic,readonly) id nodeName;
@property(nonatomic,copy,readwrite) id nodeValue;
@property(nonatomic,readonly) id nodeType;
Expand Down
90 changes: 90 additions & 0 deletions iphone/Classes/TiDOMNodeProxy.m
Expand Up @@ -22,6 +22,7 @@
#import "TiDOMEntityRefProxy.h"
#import "TiDOMNotationProxy.h"
#import "TiUtils.h"
#import "TiDOMValidator.h"
#include <libkern/OSAtomic.h>

/*
Expand Down Expand Up @@ -122,6 +123,95 @@ -(id)makeNodeListProxyFromArray:(NSArray*)nodes context:(id<TiEvaluator>)context

}

-(void)validateAttributeParameters:(NSString*)tagName withUri:(NSString*)theURI reason:(NSString**)error subreason:(NSString**)suberror
{
NSString* prefix = [GDataXMLNode prefixForName:tagName];
NSString* localName = [GDataXMLNode localNameForName:tagName];

if (![[tagName lowercaseString] isEqualToString:@"xmlns"]) {
//Check name validity
if (![TiDOMValidator checkAttributeName:localName]) {
*error = @"Invalid attribute name" ;
*suberror = [NSString stringWithFormat:@"Offending localName %@",localName];
return;
}

if (prefix != nil && ([theURI length]==0) ) {
*error = @"Can not have a prefix with a nil or empty URI" ;
return;
}

if ( [prefix isEqualToString:@"xml"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/XML/1998/namespace"]) {
*error = @"Invalid URI for prefix";
*suberror = [NSString stringWithFormat:@"%@:%@",prefix,theURI];
return;
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
*error = @"Invalid prefix" ;
*suberror = [NSString stringWithFormat:@"Offending prefix %@",prefix];
return;
}
//Check URI validity
if (![TiDOMValidator checkNamespaceURI:theURI]) {
*error = @"Invalid URI" ;
*suberror = [NSString stringWithFormat:@"Offending URI %@",theURI];
return;
}
}

}
else {
if (![theURI isEqualToString:@"http://www.w3.org/2000/xmlns/"]) {
*error = @"Invalid URI for qualified name xmlns" ;
*suberror = [NSString stringWithFormat:@"%@:%@",tagName,theURI];
}
}
}

-(void)validateElementParameters:(NSString*)tagName withUri:(NSString*)theURI reason:(NSString**)error subreason:(NSString**)suberror
{
NSString* prefix = [GDataXMLNode prefixForName:tagName];
NSString* localName = [GDataXMLNode localNameForName:tagName];

//Check name validity
if (![TiDOMValidator checkElementName:localName]) {
*error = @"Invalid element name" ;
*suberror = [NSString stringWithFormat:@"Offending localName %@",localName];
return;
}

if (prefix != nil && ([theURI length]==0) ) {
*error = @"Can not have a prefix with a nil or empty URI" ;
return;
}

if ( [prefix isEqualToString:@"xml"] ) {
if (![theURI isEqualToString:@"http://www.w3.org/XML/1998/namespace"]) {
*error = @"Invalid URI for prefix xml" ;
*suberror = [NSString stringWithFormat:@"%@:%@",prefix,theURI];
return;
}
}
else {
//Check prefix validity
if (![TiDOMValidator checkNamespacePrefix:prefix]) {
*error = @"Invalid prefix" ;
*suberror = [NSString stringWithFormat:@"Offending prefix %@",prefix];
return;
}
//Check URI validity
if (![TiDOMValidator checkNamespaceURI:theURI]) {
*error = @"Invalid URI" ;
*suberror = [NSString stringWithFormat:@"Offending URI %@",theURI];
return;
}
}
}

-(id)makeNode:(id)child context:(id<TiEvaluator>)context
{
// if already a proxy, just return it.
Expand Down

0 comments on commit bc6b5fc

Please sign in to comment.