use of org.hl7.fhir.r5.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class ObjectConverter method convertElement.
private Element convertElement(Property property, Base base) throws FHIRException {
if (base == null)
return null;
String tn = base.fhirType();
StructureDefinition sd = context.fetchTypeDefinition(tn);
if (sd == null)
throw new FHIRException("Unable to find definition for type " + tn);
Element res = new Element(property.getName(), property);
if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
res.setValue(((PrimitiveType) base).asStringValue());
List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
for (ElementDefinition child : children) {
String n = tail(child.getPath());
if (sd.getKind() != StructureDefinitionKind.PRIMITIVETYPE || !"value".equals(n)) {
Base[] values = base.getProperty(n.hashCode(), n, false);
if (values != null)
for (Base value : values) {
res.getChildren().add(convertElement(new Property(context, child, sd), value));
}
}
}
return res;
}
use of org.hl7.fhir.r5.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generateEnum.
private void generateEnum(EnumInfo e) throws Exception {
String tn = e.getName();
String tns = tn;
if (tn.startsWith("Enumeration<")) {
tns = tn.substring(tn.indexOf("<") + 1);
tns = tns.substring(0, tns.length() - 1);
} else {
tn = "Enumeration<" + tn + ">";
}
ValueSet vs = e.getValueSet();
ValueSet vse = (ValueSet) vs.getUserData("expansion");
if (vs.hasUserData("shared")) {
return;
}
if (vse == null) {
return;
}
List<ValueSetExpansionContainsComponent> codes = vse.getExpansion().getContains();
String url = vs.getUrl();
CommaSeparatedStringBuilder el = new CommaSeparatedStringBuilder();
write(" public enum " + tns + " {\r\n");
int l = codes.size();
int i = 0;
for (ValueSetExpansionContainsComponent c : codes) {
i++;
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
el.append(cc);
String definition = definitions.getCodeDefinition(c.getSystem(), c.getCode());
write(" /**\r\n");
write(" * " + Utilities.escapeJava(definition) + "\r\n");
write(" */\r\n");
write(" " + cc.toUpperCase() + ", \r\n");
}
write(" /**\r\n");
write(" * added to help the parsers with the generic types\r\n");
write(" */\r\n");
write(" NULL;\r\n");
el.append("NULL");
write(" public static " + tns + " fromCode(String codeString) throws FHIRException {\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return " + cc + ";\r\n");
}
write(" if (Configuration.isAcceptInvalidEnums())\r\n");
write(" return null;\r\n");
write(" else\r\n");
write(" throw new FHIRException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public String toCode() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + c.getCode() + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getSystem() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + c.getSystem() + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getDefinition() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
String definition = definitions.getCodeDefinition(c.getSystem(), c.getCode());
write(" case " + cc + ": return \"" + Utilities.escapeJava(definition) + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getDisplay() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + Utilities.escapeJava(Utilities.noString(c.getDisplay()) ? c.getCode() : c.getDisplay()) + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" }\r\n");
write("\r\n");
write(" public static class " + tns + "EnumFactory implements EnumFactory<" + tns + "> {\r\n");
write(" public " + tns + " fromCode(String codeString) throws IllegalArgumentException {\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return " + tns + "." + cc + ";\r\n");
}
write(" throw new IllegalArgumentException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public Enumeration<" + tns + "> fromType(Base code) throws FHIRException {\r\n");
write(" if (code == null)\r\n");
write(" return null;\r\n");
write(" if (code.isEmpty())\r\n");
write(" return new Enumeration<" + tns + ">(this);\r\n");
write(" String codeString = ((PrimitiveType) code).asStringValue();\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return new Enumeration<" + tns + ">(this, " + tns + "." + cc + ");\r\n");
}
write(" throw new FHIRException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public String toCode(" + tns + " code) {\r\n");
for (ValueSetExpansionContainsComponent c : codes) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (code == " + tns + "." + cc + ")\r\n return \"" + c.getCode() + "\";\r\n");
}
write(" return \"?\";\r\n");
write(" }\r\n");
write(" public String toSystem(" + tns + " code) {\r\n");
write(" return code.getSystem();\r\n");
write(" }\r\n");
write(" }\r\n");
write("\r\n");
// enumInfo.put("org.hl7.fhir."+jid+".model."+name+"."+tns, url+"|"+el.toString());
}
use of org.hl7.fhir.r5.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class VersionConvertorPrimitiveType30_50Test method testNullValueDstu2Primitive.
@ParameterizedTest(name = "Testing dstu3 -> r5 conversion of null value {0}.")
@MethodSource("dstu3PrimitiveTypes")
public <T extends PrimitiveType> void testNullValueDstu2Primitive(String classname, T obj) {
obj.addExtension().setUrl("http://example.com/AnyValue").setValue(new StringType("A value"));
Assertions.assertNull(((org.hl7.fhir.r5.model.PrimitiveType) VersionConvertorFactory_30_50.convertType(obj)).getValue());
}
use of org.hl7.fhir.r5.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class JavaEnumerationsGenerator method generateEnum.
private void generateEnum(String name, ValueSet vs) throws Exception {
String url = vs.getUrl();
CommaSeparatedStringBuilder el = new CommaSeparatedStringBuilder();
write(" public enum " + name + " {\r\n");
int l = vs.getExpansion().getContains().size();
int i = 0;
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
i++;
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
el.append(cc);
write(" /**\r\n");
write(" * " + definitions.getCodeDefinition(c.getSystem(), c.getCode()) + "\r\n");
write(" */\r\n");
write(" " + cc.toUpperCase() + ", \r\n");
}
write(" /**\r\n");
write(" * added to help the parsers\r\n");
write(" */\r\n");
write(" NULL;\r\n");
el.append("NULL");
write(" public static " + name + " fromCode(String codeString) throws FHIRException {\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return " + cc + ";\r\n");
}
write(" throw new FHIRException(\"Unknown " + name + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public String toCode() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + c.getCode() + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getSystem() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + c.getSystem() + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getDefinition() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + Utilities.escapeJava(definitions.getCodeDefinition(c.getSystem(), c.getCode())) + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
write(" public String getDisplay() {\r\n");
write(" switch (this) {\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" case " + cc + ": return \"" + Utilities.escapeJava(Utilities.noString(c.getDisplay()) ? c.getCode() : c.getDisplay()) + "\";\r\n");
}
write(" case NULL: return null;\r\n");
write(" default: return \"?\";\r\n");
write(" }\r\n");
write(" }\r\n");
if (config.getAdornments().containsKey(name)) {
write("// manual code from configuration.txt:\r\n");
write(config.getAdornments().get(name) + "\r\n");
write("// end addition\r\n");
}
write(" }\r\n");
write("\r\n");
write(" public static class " + name + "EnumFactory implements EnumFactory<" + name + "> {\r\n");
write(" public " + name + " fromCode(String codeString) throws IllegalArgumentException {\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return " + name + "." + cc + ";\r\n");
}
write(" throw new IllegalArgumentException(\"Unknown " + name + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public Enumeration<" + name + "> fromType(Base code) throws FHIRException {\r\n");
write(" if (code == null)\r\n");
write(" return null;\r\n");
write(" if (code.isEmpty())\r\n");
write(" return new Enumeration<" + name + ">(this);\r\n");
write(" String codeString = ((PrimitiveType) code).asStringValue();\r\n");
write(" if (codeString == null || \"\".equals(codeString))\r\n");
write(" return null;\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (\"" + c.getCode() + "\".equals(codeString))\r\n");
write(" return new Enumeration<" + name + ">(this, " + name + "." + cc + ");\r\n");
}
write(" throw new FHIRException(\"Unknown " + name + " code '\"+codeString+\"'\");\r\n");
write(" }\r\n");
write(" public String toCode(" + name + " code) {\r\n");
for (ValueSetExpansionContainsComponent c : vs.getExpansion().getContains()) {
String cc = Utilities.camelCase(c.getCode());
cc = makeConst(cc);
write(" if (code == " + name + "." + cc + ")\r\n return \"" + c.getCode() + "\";\r\n");
}
write(" return \"?\";\r\n");
write(" }\r\n");
write(" public String toSystem(" + name + " code) {\r\n");
write(" return code.getSystem();\r\n");
write(" }\r\n");
write(" }\r\n");
write("\r\n");
}
use of org.hl7.fhir.r5.model.PrimitiveType in project org.hl7.fhir.core by hapifhir.
the class JavaCoreGenerator method generate.
private void generate(String version, String src, String dest) throws Exception {
long start = System.currentTimeMillis();
Date date = new Date();
String ap = Utilities.path(src);
System.out.println("Load Configuration from " + ap);
Configuration config = new Configuration(ap);
String pid = VersionUtilities.isR4BVer(version) ? "r4b" : "r5";
String jid = VersionUtilities.isR4BVer(version) ? "r4b" : "r5";
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
System.out.println("Cache: " + pcm.getFolder());
System.out.println("Load hl7.fhir." + pid + ".core");
NpmPackage npm = pcm.loadPackage("hl7.fhir." + pid + ".core", version);
Definitions master = VersionUtilities.isR4BVer(version) ? DefinitionsLoaderR4B.load(npm) : DefinitionsLoaderR5.load(npm);
master.fix();
markValueSets(master, config);
System.out.println("Load hl7.fhir." + pid + ".expansions");
Definitions expansions = DefinitionsLoaderR5.load(pcm.loadPackage("hl7.fhir." + pid + ".expansions", version));
System.out.println("Process Expansions");
updateExpansions(master, expansions);
System.out.println("Generate Model");
System.out.println(" .. Constants");
JavaConstantsGenerator cgen = new JavaConstantsGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "Constants.java")), master, config, date, npm.version(), jid);
cgen.generate();
cgen.close();
System.out.println(" .. Enumerations");
JavaEnumerationsGenerator egen = new JavaEnumerationsGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "Enumerations.java")), master, config, date, npm.version(), jid);
egen.generate();
egen.close();
JavaFactoryGenerator fgen = new JavaFactoryGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "ResourceFactory.java")), master, config, date, npm.version(), jid);
JavaTypeGenerator tgen = new JavaTypeGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "model", "ResourceType.java")), master, config, date, npm.version(), jid);
JavaParserJsonGenerator jgen = new JavaParserJsonGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "JsonParser.java")), master, config, date, npm.version(), jid);
JavaParserXmlGenerator xgen = new JavaParserXmlGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "XmlParser.java")), master, config, date, npm.version(), jid);
JavaParserRdfGenerator rgen = new JavaParserRdfGenerator(new FileOutputStream(Utilities.path(dest, "src", "main", "java", "org", "hl7", "fhir", jid, "formats", "RdfParser.java")), master, config, date, npm.version(), jid);
if (VersionUtilities.isR4BVer(version)) {
StructureDefinition sd = master.getStructures().get("http://hl7.org/fhir/StructureDefinition/Element");
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
for (StructureDefinition sd : master.getStructures().getList()) {
if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.COMPLEXTYPE) {
if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && sd.getAbstract()) {
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
}
}
for (StructureDefinition sd : master.getStructures().getList()) {
if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.COMPLEXTYPE) {
if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && !sd.getAbstract()) {
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
}
}
if (VersionUtilities.isR4BVer(version)) {
StructureDefinition sd = master.getStructures().get("http://hl7.org/fhir/StructureDefinition/Resource");
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
for (StructureDefinition sd : master.getStructures().getList()) {
if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.RESOURCE) {
if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && sd.getAbstract()) {
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
}
}
for (StructureDefinition sd : master.getStructures().getList()) {
if (sd.getDerivation() == TypeDerivationRule.SPECIALIZATION && sd.getKind() == StructureDefinitionKind.RESOURCE) {
if (!Utilities.existsInList(sd.getName(), "Base", "PrimitiveType") && !sd.getName().contains(".") && !sd.getAbstract()) {
genClass(version, dest, date, config, jid, npm, master, jgen, xgen, rgen, sd);
}
}
}
System.out.println(" .. Factory");
fgen.generate();
fgen.close();
System.out.println(" .. Types");
tgen.generate();
tgen.close();
System.out.println(" .. JsonParser");
jgen.generate();
jgen.close();
System.out.println(" .. XmlParser");
xgen.generate();
xgen.close();
System.out.println(" .. RdfParser");
rgen.generate();
rgen.close();
System.out.println("Done (" + Long.toString(System.currentTimeMillis() - start) + "ms)");
}
Aggregations