Skip to content

Commit

Permalink
Added ability to work with ref attribute in elements (Daij-Djan#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
anivaros committed Dec 6, 2018
1 parent fadd207 commit 0a8e8ca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 52 deletions.
104 changes: 53 additions & 51 deletions framework/XSDschema.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ @implementation XSDschema {
- (id) initWithNode:(NSXMLElement*)node targetNamespacePrefix:(NSString*)prefix error:(NSError**)error {
self = [super initWithNode:node schema:nil];
if(self) {
_knownSimpleTypeDict = [NSMutableDictionary dictionary];
self.simpleTypes = [NSMutableArray array];
_knownComplexTypeDict = [NSMutableDictionary dictionary];
self.complexTypes = [NSMutableArray array];

/* Get namespaces and set derived class prefix */
self.targetNamespace = [[node attributeForName: @"targetNamespace"] stringValue];
self.allNamespaces = [node namespaces];
Expand Down Expand Up @@ -98,15 +103,56 @@ - (id) initWithNode:(NSXMLElement*)node targetNamespacePrefix:(NSString*)prefix
self.xmlSchemaNamespace = @"";
}

//handle includes & imports
NSArray* iNodes = [node nodesForXPath: self.XPathForSchemaIncludes error: error];
NSArray* iNodes2 = [node nodesForXPath: self.XPathForSchemaImports error: error];
if(iNodes2.count) {
NSMutableArray *newNodes = [iNodes2 mutableCopy];
if(iNodes.count) {
[newNodes addObjectsFromArray:iNodes];
}
iNodes = newNodes;
}

/* For the imported schemas, grab their complex and simple types of their elements */
self.includedSchemas = [NSMutableArray array];
for (NSXMLElement* aChild in iNodes) {

id schemaLocation = [aChild attributeForName:@"schemaLocation"].stringValue;
NSURL *url = [NSURL URLWithString:schemaLocation relativeToURL:self.schemaUrl];
if(![[NSFileManager defaultManager] isReadableFileAtPath:url.path]) {
if(error) {
*error = [NSError errorWithDomain:@"XSDschema" code:50 userInfo:@{@"url":url, NSLocalizedRecoverySuggestionErrorKey: [NSString stringWithFormat:@"Cant open included xsd file at %@.", url]}];

}
return nil;
}
XSDschema *xsd = [[self.class alloc] initWithUrl:url targetNamespacePrefix:prefix targetNamespacePostfix:postfix error:error];
if(!xsd) {
return nil;
}

xsd.parentSchema = self;
[((NSMutableArray*)self.includedSchemas) addObject: xsd];

//also add their types to ours, because we fricking know them now :D
for (XSDcomplexType *ct in xsd.complexTypes) {
[(NSMutableDictionary*)_knownComplexTypeDict setObject:ct forKey:ct.name];
[(NSMutableArray*)self.complexTypes addObject:ct];
}
//also add their types to ours, because we fricking know them now :D
for (XSSimpleType *ct in xsd.simpleTypes) {
[(NSMutableDictionary*)_knownSimpleTypeDict setObject:ct forKey:ct.name];
[(NSMutableArray*)self.simpleTypes addObject:ct];
}
}

/* Add basic simple types known in the built-in types */
_knownSimpleTypeDict = [NSMutableDictionary dictionary];
for(XSSimpleType *aSimpleType in [XSSimpleType knownSimpleTypesForSchema:self]) {
[_knownSimpleTypeDict setValue: aSimpleType forKey: aSimpleType.name];
}

/* Add custom simple types */
self.simpleTypes = [NSMutableArray array];

/* Grab all elements that are in the schema base with the simpleType element tag */
NSArray* stNodes = [node nodesForXPath: self.XPathForSchemaSimpleTypes error: error];

Expand All @@ -118,8 +164,6 @@ - (id) initWithNode:(NSXMLElement*)node targetNamespacePrefix:(NSString*)prefix
}

/* Add complex types */
_knownComplexTypeDict = [NSMutableDictionary dictionary];
self.complexTypes = [NSMutableArray array];
NSArray* ctNodes = [node nodesForXPath: self.XPathForSchemaComplexTypes error: error];
/* Iterate through the complex types found and create node elements for them */
for (NSXMLElement* aChild in ctNodes) {
Expand Down Expand Up @@ -165,56 +209,14 @@ - (id) initWithUrl: (NSURL*) schemaUrl targetNamespacePrefix: (NSString*) prefix
return nil;
}

/* The location of where our schema is located */
self.schemaUrl = schemaUrl;

/* From the root element, grab the complex, simple, and elements into their respective arrays */
self = [self initWithNode: [doc rootElement] targetNamespacePrefix: prefix error: error];
/* Continue to setup the schema */
if(self) {
/* The location of where our schema is located */
self.schemaUrl = schemaUrl;
if (self) {

//handle includes & imports
NSArray* iNodes = [[doc rootElement] nodesForXPath: self.XPathForSchemaIncludes error: error];
NSArray* iNodes2 = [[doc rootElement] nodesForXPath: self.XPathForSchemaImports error: error];
if(iNodes2.count) {
NSMutableArray *newNodes = [iNodes2 mutableCopy];
if(iNodes.count) {
[newNodes addObjectsFromArray:iNodes];
}
iNodes = newNodes;
}

/* For the imported schemas, grab their complex and simple types of their elements */
self.includedSchemas = [NSMutableArray array];
for (NSXMLElement* aChild in iNodes) {

id schemaLocation = [aChild attributeForName:@"schemaLocation"].stringValue;
NSURL *url = [NSURL URLWithString:schemaLocation relativeToURL:schemaUrl];
if(![[NSFileManager defaultManager] isReadableFileAtPath:url.path]) {
if(error) {
*error = [NSError errorWithDomain:@"XSDschema" code:50 userInfo:@{@"url":url, NSLocalizedRecoverySuggestionErrorKey: [NSString stringWithFormat:@"Cant open included xsd file at %@.", url]}];

}
return nil;
}
XSDschema *xsd = [[self.class alloc] initWithUrl:url targetNamespacePrefix:prefix error:error];
if(!xsd) {
return nil;
}

xsd.parentSchema = self;
[((NSMutableArray*)self.includedSchemas) addObject: xsd];

//also add their types to ours, because we fricking know them now :D
for (XSDcomplexType *ct in xsd.complexTypes) {
[(NSMutableDictionary*)_knownComplexTypeDict setObject:ct forKey:ct.name];
[(NSMutableArray*)self.complexTypes addObject:ct];
}
//also add their types to ours, because we fricking know them now :D
for (XSSimpleType *ct in xsd.simpleTypes) {
[(NSMutableDictionary*)_knownSimpleTypeDict setObject:ct forKey:ct.name];
[(NSMutableArray*)self.simpleTypes addObject:ct];
}
}
}

return self;
Expand Down
1 change: 1 addition & 0 deletions framework/objects/XSDelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

@property (readonly, nonatomic) id<XSType> localType;
@property (readonly, nonatomic) NSString* name;
@property (readonly, nonatomic) NSString* ref;
@property (readonly, nonatomic) NSString* type;
@property (readonly, nonatomic) NSString* substitutionGroup;
@property (readonly, nonatomic) NSString* defaultValue;
Expand Down
15 changes: 14 additions & 1 deletion framework/objects/XSDelement.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @interface XSDcomplexType (privateAccessors)
@interface XSDelement ()
@property (strong, nonatomic) id<XSType> localType;
@property (strong, nonatomic) NSString* name;
@property (strong, nonatomic) NSString* ref;
@property (strong, nonatomic) NSString* type;
@property (strong, nonatomic) NSString* substitutionGroup;
@property (strong, nonatomic) NSString* defaultValue;
Expand All @@ -40,6 +41,7 @@ - (id) initWithNode:(NSXMLElement*)node schema: (XSDschema*)schema {
self = [super initWithNode:node schema:schema];
if(self) {
self.type = [XMLUtils node: node stringAttribute: @"type"];
self.ref = [XMLUtils node: node stringAttribute: @"ref"];
self.name = [XMLUtils node: node stringAttribute: @"name"];
self.substitutionGroup = [XMLUtils node: node stringAttribute: @"substitutionGroup"];
self.defaultValue = [XMLUtils node: node stringAttribute: @"default"];
Expand All @@ -49,7 +51,18 @@ - (id) initWithNode:(NSXMLElement*)node schema: (XSDschema*)schema {
self.final = [XMLUtils node: node stringAttribute: @"final"];
self.block = [XMLUtils node: node stringAttribute: @"block"];
self.form = [XMLUtils node: node stringAttribute: @"form"];


if (self.ref) {
for (XSDcomplexType *innerCt in schema.complexTypes) {
for (XSDelement *el in innerCt.globalElements) {
if ([el.name isEqualToString:[NSXMLNode localNameForName:self.ref]]) {
self.name = el.name;
self.type = el.type;
}
}
}
}

NSNumberFormatter* numFormatter = [[NSNumberFormatter alloc] init];
numFormatter.numberStyle = NSNumberFormatterDecimalStyle;

Expand Down

0 comments on commit 0a8e8ca

Please sign in to comment.