use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class Analyser method analyse.
public Analysis analyse(StructureDefinition sd) throws Exception {
Analysis res = new Analysis(definitions, sd);
if (VersionUtilities.isR4BVer(version)) {
res.setAncestor(definitions.getStructures().get(getR4bAncestor(sd)));
} else {
res.setAncestor(definitions.getStructures().get(sd.getBaseDefinition()));
}
res.setAbstract(sd.getAbstract());
res.setInterface(sd.hasExtension("http://hl7.org/fhir/StructureDefinition/structuredefinition-interface"));
res.setClassName(sd.getName().equals("List") ? "ListResource" : sd.getName());
TypeInfo type = new TypeInfo();
type.setName(res.getClassName());
if (res.getAncestor() != null) {
type.setAncestorName(res.getAncestor().getName());
}
res.getTypes().put(type.getName(), type);
res.setRootType(type);
sd.setUserData("java.type.info", type);
type.setDefn(sd.getSnapshot().getElementFirstRep());
type.setChildren(filterChildren(new ProfileUtilities(null, null, null).getChildList(sd, type.getDefn())));
if (res.getAncestor() != null) {
type.setInheritedChildren(getAbstractChildren(res.getAncestor()));
}
for (ElementDefinition e : type.getChildren()) {
scanNestedTypes(res, type, type.getName(), e);
}
if (sd.getKind() == StructureDefinitionKind.RESOURCE) {
res.setSearchParams(getSearchParams(sd.getName()));
}
for (ElementDefinition e : type.getChildren()) {
String nn = e.getUserString("java.type");
if (nn.startsWith("@")) {
ElementDefinition er = getElementForPath(sd, nn.substring(1));
if (!er.hasUserData("java.type")) {
throw new Exception("not found: " + er);
}
String nnn = er.getUserString("java.type");
e.setUserData("java.type", nnn);
e.setUserData("java.type.info", er.getUserData("java.type.info"));
}
}
return res;
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class Analyser method scanNestedTypes.
private void scanNestedTypes(Analysis analysis, TypeInfo type, String path, ElementDefinition e) throws Exception {
String tn = null;
if (e.typeSummary().equals("code") && e.hasBinding()) {
ElementDefinitionBindingComponent cd = e.getBinding();
if (isEnum(cd)) {
ValueSet vs = definitions.getValuesets().get(cd.getValueSet());
if (vs != null) {
tn = getCodeListType(vs.getName());
EnumInfo ei = analysis.getEnums().get(tn);
if (ei == null) {
ei = new EnumInfo(tn);
analysis.getEnums().put(tn, ei);
ei.setValueSet(vs);
}
if (tn.equals("SubscriptionStatus")) {
// work around cause there's a Resource with the same name
tn = "org.hl7.fhir.r4b.model.Enumerations." + tn;
}
e.setUserData("java.type", "Enumeration<" + tn + ">");
e.setUserData("java.enum", ei);
}
}
}
if (tn == null) {
if (e.getType().size() > 0 && !e.hasContentReference() && (!Utilities.existsInList(e.getType().get(0).getCode(), "Element", "BackboneElement"))) {
tn = getTypeName(e);
if (e.typeSummary().equals("xml:lang"))
tn = "CodeType";
if (e.typeSummary().equals("xhtml"))
tn = "XhtmlNode";
else if (e.getType().size() > 1)
tn = "DataType";
else if (definitions.hasPrimitiveType(tn))
tn = upFirst(tn) + "Type";
e.setUserData("java.type", tn);
} else {
if (e.hasContentReference()) {
ElementDefinition er = getElementForPath(analysis.getStructure(), e.getContentReference().substring(1));
tn = er.getUserString("java.type");
if (Utilities.noString(tn)) {
// have to resolve this later
e.setUserData("java.type", "@" + er.getPath());
} else {
e.setUserData("java.type", tn);
}
} else {
String cpath;
if (e.hasExtension("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name")) {
tn = upFirst(e.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name")) + "Component";
cpath = e.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name");
} else if (config.getIni().hasProperty("typenames", e.getPath())) {
tn = upFirst(config.getIni().getStringProperty("typenames", e.getPath())) + "Component";
cpath = config.getIni().getStringProperty("typenames", e.getPath());
} else {
tn = path + upFirst(getTitle(e.getName())) + "Component";
cpath = path + getTitle(e.getName());
}
if (tn.equals("Element"))
tn = "Element_";
if (analysis.getTypes().containsKey(tn)) {
char i = 'A';
while (analysis.getTypes().containsKey(tn + i)) {
i++;
}
tn = tn + i;
}
e.setUserData("java.type", tn);
tn = upFirst(tn);
TypeInfo ctype = new TypeInfo();
ctype.setName(tn);
analysis.getTypes().put(ctype.getName(), ctype);
analysis.getTypeList().add(ctype);
ctype.setDefn(e);
ctype.setAncestorName(e.typeSummary());
ctype.setChildren(filterChildren(new ProfileUtilities(null, null, null).getChildList(analysis.getStructure(), ctype.getDefn())));
for (ElementDefinition c : ctype.getChildren()) {
scanNestedTypes(analysis, ctype, cpath, c);
}
}
}
}
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testGetChoiceTypesValid.
@Test
public void testGetChoiceTypesValid() {
ModelInfo modelInfo = JAXB.unmarshal(new File("src/test/resources/abstract-context/modelinfo/abstract-modelinfo-1.0.0.xml"), ModelInfo.class);
TypeInfo typeInfo = modelInfo.getTypeInfo().stream().map(ClassInfo.class::cast).filter(classInfo -> classInfo.getName().equals("AlphaNumeric")).findFirst().orElse(null);
Collection<String> choiceTypes = ModelUtils.getChoiceTypeNames(typeInfo);
assertThat(choiceTypes, Matchers.containsInAnyOrder("Alpha", "Numeric"));
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testGetChoiceTypesNone.
@Test
public void testGetChoiceTypesNone() {
ModelInfo modelInfo = JAXB.unmarshal(new File("src/test/resources/abstract-context/modelinfo/abstract-modelinfo-1.0.0.xml"), ModelInfo.class);
TypeInfo typeInfo = modelInfo.getTypeInfo().stream().map(ClassInfo.class::cast).filter(classInfo -> classInfo.getName().equals("Alpha")).findFirst().orElse(null);
Collection<String> choiceTypes = ModelUtils.getChoiceTypeNames(typeInfo);
assertThat(choiceTypes, Matchers.empty());
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testGetBaseTypeNameInvalidNamespacePrefix.
@Test
public void testGetBaseTypeNameInvalidNamespacePrefix() {
ModelInfo modelInfo = new ModelInfo();
modelInfo.setName("Dummy");
modelInfo.setVersion("2.1.3");
modelInfo.setUrl("urn:oid:Dummy");
ClassInfo typeInfo = new ClassInfo();
typeInfo.setName("MyType");
typeInfo.setBaseType("System.Child.BaseType");
IllegalArgumentException iex = assertThrows(IllegalArgumentException.class, () -> ModelUtils.getBaseTypeName(modelInfo, typeInfo));
assertTrue(iex.getMessage().contains("System.Child.BaseType"));
assertTrue(iex.getMessage().contains(modelInfo.getName()));
assertTrue(iex.getMessage(), iex.getMessage().contains(modelInfo.getVersion()));
}
Aggregations