diff --git a/framework/XSDschema.m b/framework/XSDschema.m index 6edba36..8704c7c 100755 --- a/framework/XSDschema.m +++ b/framework/XSDschema.m @@ -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]; @@ -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]; @@ -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) { @@ -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; diff --git a/framework/objects/XSDelement.h b/framework/objects/XSDelement.h index 61ef52b..279da36 100755 --- a/framework/objects/XSDelement.h +++ b/framework/objects/XSDelement.h @@ -14,6 +14,7 @@ @property (readonly, nonatomic) id 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; diff --git a/framework/objects/XSDelement.m b/framework/objects/XSDelement.m index b5d178b..cce8938 100755 --- a/framework/objects/XSDelement.m +++ b/framework/objects/XSDelement.m @@ -21,6 +21,7 @@ @interface XSDcomplexType (privateAccessors) @interface XSDelement () @property (strong, nonatomic) id 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; @@ -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"]; @@ -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;