@@ -82,14 +82,14 @@ private String getTail(String url) {
8282 }
8383 }
8484
85- private String resolveModelName (String url ) throws Exception {
85+ private String resolveModelName (String url ) {
8686 // Strips off the identifier and type name
8787 String model = getHead (getHead (url ));
8888 if (this .settings .urlToModel .containsKey (model )) {
8989 return this .settings .urlToModel .get (model );
9090 }
9191
92- throw new Exception ("Couldn't resolve model name for url: " + url );
92+ throw new IllegalArgumentException ("Couldn't resolve model name for url: " + url );
9393 }
9494
9595 private String resolveTypeName (String url ) throws Exception {
@@ -144,6 +144,12 @@ protected String getTypeName(StructureDefinition sd) {
144144 if (this .settings .modelPrefix != null && typeName .startsWith (this .settings .modelPrefix )) {
145145 typeName = typeName .substring (this .settings .modelPrefix .length ());
146146 }
147+ else {
148+ String modelName = resolveModelName (sd .getUrl ());
149+ if (modelName != null && typeName .startsWith (modelName )) {
150+ typeName = typeName .substring (modelName .length ());
151+ }
152+ }
147153 typeName = mapTypeName (typeName );
148154 return typeName ;
149155 }
@@ -556,7 +562,7 @@ private TypeSpecifier buildTypeSpecifier(String typeName) {
556562 return this .buildTypeSpecifier (this .getQualifier (typeName ), this .unQualify (typeName ));
557563 }
558564
559- private String resolveMappedTypeName (String url ) throws Exception {
565+ private String resolveMappedTypeName (String url ) {
560566 if (url != null ) {
561567 String modelName = resolveModelName (url );
562568 String typeName = getTypeNameFromUrl (url );
@@ -635,12 +641,22 @@ private String resolveMappedTypeName(String modelName, String typeName) {
635641
636642 private String resolveMappedTypeName (String modelName , TypeRefComponent typeRef ) {
637643 if (typeRef .hasCode () && typeRef .getCode () != null ) {
638- return resolveMappedTypeName (modelName , typeRef .getCode ());
644+ if (this .settings .flatten ) {
645+ return resolveMappedTypeName (modelName , typeRef .getCode ());
646+ }
647+ else {
648+ if (!typeRef .getCode ().startsWith ("http://hl7.org/fhirpath/" )) {
649+ return resolveMappedTypeName ("http://hl7.org/fhir/StructureDefinition/" + typeRef .getCode ());
650+ }
651+ else {
652+ return resolveMappedTypeName (modelName , typeRef .getCode ());
653+ }
654+ }
639655 }
640656
641657 Extension typeExtension = typeRef .getCodeElement ().getExtensionByUrl ("http://hl7.org/fhir/StructureDefinition/structuredefinition-xml-type" );
642658 if (typeExtension != null ) {
643- return resolveMappedTypeName (modelName , typeExtension .getValue ().toString ());
659+ return resolveMappedTypeName (typeExtension .getValue ().toString ());
644660 }
645661
646662 throw new IllegalArgumentException ("Could not determine mapping for null type code" );
@@ -841,6 +857,8 @@ else if (hasRequiredBinding(ed, typeCode)) {
841857 String requiredBindingName = getRequiredBindingName (ed );
842858 if (requiredBindingName != null ) {
843859 typeName = requiredBindingName ;
860+ // NOTE: Always use FHIR as the model name here, since they are base specification defined
861+ modelName = "FHIR" ;
844862 requiredBindingName = getTypeName (modelName , typeName );
845863 if (!requiredBindingTypeNames .contains (requiredBindingName )) {
846864 requiredBindingTypeNames .add (requiredBindingName );
@@ -1151,7 +1169,7 @@ else if (slice.getElementTypeSpecifier() != null) {
11511169 // On return, index will be updated to the index of the next element to be
11521170 // processed
11531171 // This visit should not be used on the root element of a structure definition
1154- private ClassInfoElement visitElementDefinition (String modelName , String pathRoot ,
1172+ private ClassInfoElement visitElementDefinition (String modelName , String baseTypeName , String pathRoot ,
11551173 List <ElementDefinition > eds , AtomicReference <Integer > index , SliceInfo sliceInfo , SliceList slices )
11561174 throws Exception {
11571175 ElementDefinition ed = eds .get (index .get ());
@@ -1200,7 +1218,7 @@ else if (!this.settings.useCQLPrimitives) {
12001218 ElementDefinition e = eds .get (index .get ());
12011219 if (isNextAContinuationOfElement (id , path , e )) {
12021220 SliceList elementSlices = new SliceList ();
1203- ClassInfoElement cie = this .visitElementDefinition (modelName , pathRoot , eds , index , sliceInfo , elementSlices );
1221+ ClassInfoElement cie = this .visitElementDefinition (modelName , baseTypeName , pathRoot , eds , index , sliceInfo , elementSlices );
12041222 if (cie != null && !(cie .getElementType () == null && cie .getElementTypeSpecifier () == null )) {
12051223 elements .add (cie );
12061224 }
@@ -1227,8 +1245,12 @@ else if (!this.settings.useCQLPrimitives) {
12271245 if (typeDefinition != null && (isBackboneElement (typeDefinition ) || isElement (typeDefinition ))) {
12281246 String typeName = this .capitalizePath (path );
12291247
1248+ String baseModelName = getQualifier (baseTypeName );
1249+
12301250 ClassInfo componentClassInfo = new ClassInfo ().withNamespace (modelName ).withName (typeName ).withLabel (null )
1231- .withBaseType (modelName + (isBackboneElement (typeDefinition ) ? ".BackboneElement" : ".Element" ))
1251+ .withBaseType (baseModelName .equals (modelName )
1252+ ? (modelName + (isBackboneElement (typeDefinition ) ? ".BackboneElement" : ".Element" ))
1253+ : this .getTypeName (baseModelName , typeName ))
12321254 .withRetrievable (false ).withElement (elements ).withPrimaryCodePath (null );
12331255
12341256 this .typeInfos .put (this .getTypeName (modelName , typeName ), componentClassInfo );
@@ -1394,7 +1416,7 @@ else if (isSystemTypeName(getTypeName((NamedTypeSpecifier)typeSpecifier)) && sli
13941416 // This approach uses the base type to guide the walk, which requires navigating
13951417 // the derived profiles
13961418 private ClassInfo buildClassInfo (String modelName , StructureDefinition sd ) throws Exception {
1397- if (modelName == null ) {
1419+ if (modelName == null || ! this . settings . flatten ) {
13981420 modelName = this .resolveModelName (sd .getUrl ());
13991421 }
14001422 String typeName = getTypeName (sd );
@@ -1417,15 +1439,18 @@ private ClassInfo buildClassInfo(String modelName, StructureDefinition sd) throw
14171439 List <ClassInfoElement > elements = new ArrayList <>();
14181440 String path = sd .getType (); // Type is used to navigate the elements, regardless of the baseDefinition
14191441 String id = path ; // Id starts with the Type
1420- // TODO: Switch to differential here, but several of the primitive types declare "value" elements of type string when this happens? (positiveInt, markdown, among others)
14211442 List <ElementDefinition > eds = sd .getSnapshot ().getElement ();
14221443 SliceList elementSlices = null ;
14231444
1445+ String baseDefinition = sd .getBaseDefinition ();
1446+ String baseTypeName = (isStructureDefinitionResource (sd ) && this .settings .flatten )
1447+ ? resolveBaseTypeName (sd .getType ()) : resolveTypeName (baseDefinition );
1448+
14241449 while (index .get () < eds .size ()) {
14251450 ElementDefinition e = eds .get (index .get ());
14261451 if (isNextAContinuationOfElement (id , path , e )) {
14271452 elementSlices = new SliceList ();
1428- ClassInfoElement cie = this .visitElementDefinition (modelName , path , eds , index , null , elementSlices );
1453+ ClassInfoElement cie = this .visitElementDefinition (modelName , baseTypeName , path , eds , index , null , elementSlices );
14291454 if (cie != null && !(cie .getElementType () == null && cie .getElementTypeSpecifier () == null )) {
14301455 elements .add (cie );
14311456 }
@@ -1441,12 +1466,8 @@ private ClassInfo buildClassInfo(String modelName, StructureDefinition sd) throw
14411466 }
14421467 }
14431468
1444- String baseDefinition = sd .getBaseDefinition ();
1445- String baseTypeName = isStructureDefinitionResource (sd )
1446- ? resolveBaseTypeName (sd .getType ()) : resolveTypeName (baseDefinition );
1447-
14481469 if (baseTypeName != null && !this .typeInfos .containsKey (baseTypeName )) {
1449- StructureDefinition baseSd = this .structureDefinitions .get (unQualify ( baseTypeName ));
1470+ StructureDefinition baseSd = this .structureDefinitions .get (getTail ( baseDefinition ));
14501471 buildClassInfo (modelName , baseSd );
14511472 }
14521473
0 commit comments