use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generateAbstractAccessors.
private void generateAbstractAccessors(Analysis analysis, TypeInfo ti, ElementDefinition e, String indent) throws Exception {
String tn = e.getUserString("java.type");
if (Utilities.noString(tn)) {
throw new Error("??");
}
boolean isReferenceRefField = (ti.getDefn().getName().equals("Reference") && e.getName().equals("reference"));
jdoc(indent, "How many allowed for this property by the implementation");
write(indent + "public int get" + getTitle(getElementName(e.getName(), false)) + "Max() { \r\n");
write(indent + " return " + (e.getMax().equals("*") ? "Integer.MAX_VALUE" : "1") + ";\r\n");
write(indent + "}\r\n");
String simpleType = getSimpleType(tn);
if (e.unbounded()) {
/*
* getXXX()for repeatable type
*/
jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
String listGenericType;
// if (tn == null && e.hasContentReference()) {
// listGenericType = root.getName();
// } else {
listGenericType = tn;
// }
write(indent + "public abstract List<" + listGenericType + "> get" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
/*
* setXXX(List<foo>) for repeating type
*/
jdoc(indent, "@return Returns a reference to <code>this</code> for easy method chaining");
write(indent + "public abstract " + ti.getName() + " set" + getTitle(getElementName(e.getName(), false)) + "(" + "List<" + listGenericType + "> the" + getTitle(getElementName(e.getName(), false)) + "); \r\n");
/*
* hasXXX() for repeatable type
*/
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
write("\r\n");
if (e.getType().size() == 1 && (definitions.hasPrimitiveType(e.typeSummary()) || e.typeSummary().equals("xml:lang") || e.typeSummary().startsWith("canonical("))) {
/*
* addXXXElement() for repeatable primitive
*/
jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
write(indent + "public abstract " + tn + " add" + getTitle(getElementName(e.getName(), false)) + "Element();//2 \r\n");
/*
* addXXX(foo) for repeatable primitive
*/
jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
write(indent + "public abstract " + ti.getName() + " add" + getTitle(getElementName(e.getName(), false)) + "(" + simpleType + " value); //1\r\n");
/*
* hasXXX(foo) for repeatable primitive
*/
jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + "(" + simpleType + " value); \r\n");
} else {
if (!definitions.hasResource(tn)) {
/*
* addXXX() for repeatable composite
*/
write(indent + "public abstract " + tn + " add" + getTitle(getElementName(e.getName(), false)) + "(); //3\r\n");
/*
* addXXX(foo) for repeatable composite
*/
write(indent + "public abstract " + ti.getName() + " add" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " t); //3\r\n");
} else {
/*
* addXXX(foo) for repeatable composite
*/
write(indent + "public abstract " + ti.getName() + " add" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " t); //3\r\n");
}
/*
* getXXXFirstRep() for repeatable element
*/
if (!"DomainResource".equals(ti.getName())) {
jdoc(indent, "@return The first repetition of repeating field {@link #" + getElementName(e.getName(), true) + "}, creating it if it does not already exist {1}");
write(indent + "public abstract " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "FirstRep(); \r\n");
}
}
} else {
if (!"xhtml".equals(e.typeSummary()) && isJavaPrimitive(e) || (e.getType().size() == 1 && e.typeSummary().startsWith("canonical("))) {
jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + "). This is the underlying object with id, value and extensions. The accessor \"get" + getTitle(getElementName(e.getName(), false)) + "\" gives direct access to the value");
if (isReferenceRefField) {
/*
* Reference#getReferenceElement is defined differently in BaseReference.java?
*/
write(indent + "public abstract " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "Element_(); \r\n");
} else {
write(indent + "public abstract " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "Element(); \r\n");
}
write("\r\n");
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + "Element(); \r\n");
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
write("\r\n");
jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + "). This is the underlying object with id, value and extensions. The accessor \"get" + getTitle(getElementName(e.getName(), false)) + "\" gives direct access to the value");
write(indent + "public abstract " + ti.getName() + " set" + getTitle(getElementName(e.getName(), false)) + "Element(" + tn + " value); \r\n");
jdoc(indent, "@return " + replaceTitle(analysis.getName(), e.getDefinition()));
write(indent + "public abstract " + simpleType + " get" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
generateAbstractSetter(e, indent, ti.getName(), tn, simpleType, analysis.getName());
// BigDecimal sugar methods
if (simpleType.equals("BigDecimal")) {
generateAbstractSetter(e, indent, ti.getName(), tn, "long", analysis.getName());
generateAbstractSetter(e, indent, ti.getName(), tn, "double", analysis.getName());
}
} else {
jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
write(indent + "public abstract " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
if (e.getType().size() > 1 && (tn.equals("DataType") || !tn.endsWith(".DataType"))) {
for (TypeRefComponent t : e.getType()) {
jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
String ttn = getTypename(t);
write(indent + "public abstract " + ttn + " get" + getTitle(getElementName(e.getName(), false)) + ttn + "() throws FHIRException; \r\n");
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + ttn + "(); \r\n");
}
}
write(indent + "public abstract boolean has" + getTitle(getElementName(e.getName(), false)) + "(); \r\n");
jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
write(indent + "public abstract " + ti.getName() + " set" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " value); \r\n");
write("\r\n");
}
}
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generatePropertySetterName.
private void generatePropertySetterName(Analysis analysis, TypeInfo ti, String indent) throws Exception {
List<ElementDefinition> children = ti.getChildren();
boolean isInterface = analysis.isInterface();
write(indent + " @Override\r\n");
write(indent + " public Base setProperty(String name, Base value) throws FHIRException {\r\n");
boolean first = true;
for (ElementDefinition e : children) {
if (!isInterface) {
String tn = e.getUserString("java.type");
if (first)
write(indent + " ");
else
write(indent + " } else ");
first = false;
write("if (name.equals(\"" + e.getName() + "\")) {\r\n");
String name = e.getName().replace("[x]", "");
String cn = "(" + tn + ") value";
if (!Utilities.existsInList(e.typeSummary(), "Element", "BackboneElement")) {
if (e.typeSummary().equals("xhtml")) {
cn = "TypeConvertor.castToXhtml(value)";
} else if (tn.contains("Enumeration<")) {
// enumeration
write(indent + " value = new " + tn.substring(tn.indexOf("<") + 1, tn.length() - 1) + "EnumFactory().fromType(TypeConvertor.castToCode(value));\r\n");
cn = "(Enumeration) value";
} else if (e.getType().size() == 1 && !e.typeSummary().equals("*") && !e.getType().get(0).getCode().startsWith("@")) {
cn = "TypeConvertor.castTo" + upFirst(checkConstraint(e.getType().get(0).getWorkingCode())) + "(value)";
} else if (e.getType().size() > 0 && !e.getType().get(0).getCode().startsWith("@")) {
cn = "TypeConvertor.castToType(value)";
}
}
if (e.unbounded()) {
write(indent + " this.get" + upFirst(getElementName(name, false)) + "().add(" + cn + ");\r\n");
} else {
write(indent + " this." + getElementName(name, true) + " = " + cn + "; // " + tn + "\r\n");
}
}
}
if (!first)
write(indent + " } else\r\n");
write(indent + " return super.setProperty(name, value);\r\n");
if (!first)
write(indent + " return value;\r\n");
write(indent + " }\r\n\r\n");
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generatePropertySetterId.
private void generatePropertySetterId(Analysis analysis, TypeInfo ti, String indent) throws Exception {
List<ElementDefinition> children = ti.getChildren();
boolean isInterface = analysis.isInterface();
write(indent + " @Override\r\n");
write(indent + " public Base setProperty(int hash, String name, Base value) throws FHIRException {\r\n");
write(indent + " switch (hash) {\r\n");
for (ElementDefinition e : children) {
if (!isInterface) {
String tn = e.getUserString("java.type");
String name = e.getName().replace("[x]", "");
write(indent + " case " + propId(name) + ": // " + name + "\r\n");
String cn = "(" + tn + ") value";
if (!Utilities.existsInList(e.typeSummary(), "Element", "BackboneElement")) {
if (e.typeSummary().equals("xhtml")) {
cn = "TypeConvertor.castToXhtml(value)";
}
if (tn.contains("Enumeration<")) {
// enumeration
write(indent + " value = new " + tn.substring(tn.indexOf("<") + 1, tn.length() - 1) + "EnumFactory().fromType(TypeConvertor.castToCode(value));\r\n");
cn = "(Enumeration) value";
} else if (e.getType().size() == 1 && !e.typeSummary().equals("*") && !e.getType().get(0).getName().startsWith("@")) {
cn = "TypeConvertor.castTo" + upFirst(checkConstraint(e.getType().get(0).getWorkingCode())) + "(value)";
} else if (e.getType().size() > 0 && !e.getType().get(0).getCode().startsWith("@")) {
cn = "TypeConvertor.castToType(value)";
}
}
if (e.unbounded()) {
write(indent + " this.get" + upFirst(getElementName(name, false)) + "().add(" + cn + "); // " + tn + "\r\n");
} else {
write(indent + " this." + getElementName(name, true) + " = " + cn + "; // " + tn + "\r\n");
}
write(indent + " return value;\r\n");
}
}
write(indent + " default: return super.setProperty(hash, name, value);\r\n");
write(indent + " }\r\n\r\n");
write(indent + " }\r\n\r\n");
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generateChildrenRegister.
private void generateChildrenRegister(Analysis analysis, TypeInfo ti, String indent) throws Exception {
String rn = analysis.getName();
boolean isInterface = analysis.isInterface();
List<ElementDefinition> children = ti.getChildren();
write(indent + " protected void listChildren(List<Property> children) {\r\n");
write(indent + " super.listChildren(children);\r\n");
for (ElementDefinition e : children) {
if (!isInterface && !e.typeSummary().equals("xhtml")) {
write(indent + " children.add(new Property(\"" + e.getName() + "\", \"" + resolvedTypeCode(e) + "\", \"" + Utilities.escapeJava(replaceTitle(rn, e.getDefinition())) + "\", 0, " + (e.unbounded() ? "java.lang.Integer.MAX_VALUE" : e.getMax()) + ", " + getElementName(e.getName(), true) + "));\r\n");
}
}
write(indent + " }\r\n\r\n");
write(indent + " @Override\r\n");
write(indent + " public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException {\r\n");
write(indent + " switch (_hash) {\r\n");
for (ElementDefinition e : children) {
if (!isInterface && !e.typeSummary().equals("xhtml")) {
write(indent + " case " + propId(e.getName()) + ": /*" + e.getName() + "*/ ");
write(" return new Property(\"" + e.getName() + "\", \"" + resolvedTypeCode(e) + "\", \"" + Utilities.escapeJava(replaceTitle(rn, e.getDefinition())) + "\", 0, " + (e.unbounded() ? "java.lang.Integer.MAX_VALUE" : e.getMax()) + ", " + getElementName(e.getName(), true) + ");\r\n");
if (e.getName().endsWith("[x]")) {
String n = e.getName().substring(0, e.getName().length() - 3);
write(indent + " case " + propId(n) + ": /*" + n + "*/ ");
write(" return new Property(\"" + e.getName() + "\", \"" + resolvedTypeCode(e) + "\", \"" + Utilities.escapeJava(replaceTitle(rn, e.getDefinition())) + "\", 0, " + (e.unbounded() ? "java.lang.Integer.MAX_VALUE" : e.getMax()) + ", " + getElementName(e.getName(), true) + ");\r\n");
if (e.typeSummary().equals("*")) {
// master list in datatypes.html
for (String t : new String[] { "base64Binary", "boolean", "canonical", "code", "date", "dateTime", "decimal", "id", "instant", "integer", "integer64", "markdown", "oid", "positiveInt", "string", "time", "unsignedInt", "uri", "url", "uuid", "Address", "Annotation", "Attachment", "CodeableConcept", "Coding", "ContactPoint", "HumanName", "Identifier", "Period", "Quantity", "Range", "Ratio", "Reference", "SampledData", "Signature", "Timing", "Dosage" }) {
String tn = n + Utilities.capitalize(t);
write(indent + " case " + propId(tn) + ": /*" + tn + "*/ ");
write(" return new Property(\"" + e.getName() + "\", \"" + resolvedTypeCode(e, t) + "\", \"" + Utilities.escapeJava(replaceTitle(rn, e.getDefinition())) + "\", 0, " + (e.unbounded() ? "java.lang.Integer.MAX_VALUE" : e.getMax()) + ", " + getElementName(e.getName(), true) + ");\r\n");
}
} else
for (TypeRefComponent tr : e.getType()) {
String tn = n + Utilities.capitalize(checkConstraint(tr.getCode()));
write(indent + " case " + propId(tn) + ": /*" + tn + "*/ ");
write(" return new Property(\"" + e.getName() + "\", \"" + resolvedTypeCode(e, tr.getCode()) + "\", \"" + Utilities.escapeJava(replaceTitle(rn, e.getDefinition())) + "\", 0, " + (e.unbounded() ? "java.lang.Integer.MAX_VALUE" : e.getMax()) + ", " + getElementName(e.getName(), true) + ");\r\n");
}
}
}
}
write(indent + " default: return super.getNamedProperty(_hash, _name, _checkValid);\r\n");
write(indent + " }\r\n\r\n");
write(indent + " }\r\n\r\n");
}
use of org.hl7.fhir.core.generator.analysis.TypeInfo in project org.hl7.fhir.core by hapifhir.
the class JavaResourceGenerator method generateIsEmpty.
private void generateIsEmpty(Analysis analysis, TypeInfo ti, boolean owner) throws Exception {
List<ElementDefinition> children = ti.getChildren();
String tn = ti.getName();
boolean isAbstract = analysis.isAbstract();
boolean isInterface = analysis.isInterface();
write(" public boolean isEmpty() {\r\n");
write(" return super.isEmpty() && ca.uhn.fhir.util.ElementUtil.isEmpty(");
int col = 70;
boolean first = true;
for (ElementDefinition c : children) {
if (!isInterface) {
if (first) {
first = false;
} else {
write(", ");
}
col = col + 2;
String name = getElementName(c.getName(), true);
if (name.endsWith("[x]"))
name = name.substring(0, name.length() - 3);
write(name);
col = col + name.length() + 2;
if (col > 100) {
col = 10;
write("\r\n ");
}
}
}
write(");\r\n");
write(" }\r\n\r\n");
}
Aggregations