use of org.hl7.v3.CD in project kindling by HL7.
the class ResourceValidator method check.
// private List<ValidationMessage> check(String n, BindingSpecification cd) throws Exception {
// List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
// check(errors, n, cd);
// return errors;
// }
private void check(List<ValidationMessage> errors, String path, BindingSpecification cd, String sd, ElementDefn e) throws Exception {
// basic integrity checks
List<DefinedCode> ac = cd.getAllCodes(definitions.getCodeSystems(), definitions.getValuesets(), false);
for (DefinedCode c : ac) {
String d = c.getCode();
if (Utilities.noString(d))
d = c.getId();
if (Utilities.noString(d))
d = c.getDisplay();
if (Utilities.noString(d))
d = c.getDisplay();
if (Utilities.noString(c.getSystem()))
warning(errors, IssueType.STRUCTURE, "Binding @ " + path, !Utilities.noString(c.getDefinition()), "Code " + d + " must have a definition");
rule(errors, IssueType.STRUCTURE, "Binding @ " + path, !(Utilities.noString(c.getId()) && Utilities.noString(c.getSystem())), "Code " + d + " must have a id or a system");
}
// trigger processing into a Heirachical set if necessary
// rule(errors, IssueType.STRUCTURE, "Binding @ "+path, !cd.isHeirachical() || (cd.getChildCodes().size() < cd.getCodes().size()), "Logic error processing Hirachical code set");
// now, rules for the source
hint(errors, IssueType.STRUCTURE, "Binding @ " + path, cd.getBinding() != BindingMethod.Unbound, "Need to provide a binding");
rule(errors, IssueType.STRUCTURE, "Binding @ " + path, Utilities.noString(cd.getDefinition()) || (cd.getDefinition().charAt(0) == cd.getDefinition().toUpperCase().charAt(0)), "Definition cannot start with a lowercase letter");
if (cd.getBinding() == BindingMethod.CodeList || (cd.getBinding() == BindingMethod.ValueSet && cd.getStrength() == BindingStrength.REQUIRED && ac.size() > 0 && "code".equals(e.typeCode()))) {
if (path.toLowerCase().endsWith("status")) {
if (rule(errors, IssueType.STRUCTURE, path, definitions.getStatusCodes().containsKey(path), "Status element not registered in status-codes.xml")) {
// rule(errors, IssueType.STRUCTURE, path, e.isModifier(), "Status elements that map to status-codes should be labelled as a modifier");
ArrayList<String> list = definitions.getStatusCodes().get(path);
for (DefinedCode c : ac) {
boolean ok = false;
for (String s : list) {
String[] parts = s.split("\\,");
for (String p : parts) if (p.trim().equals(c.getCode()))
ok = true;
}
rule(errors, IssueType.STRUCTURE, path, ok, "Status element code \"" + c.getCode() + "\" not found in status-codes.xml");
}
for (String s : list) {
String[] parts = s.split("\\,");
for (String p : parts) {
List<String> cl = new ArrayList<>();
if (!Utilities.noString(p)) {
boolean ok = false;
for (DefinedCode c : ac) {
cl.add(c.getCode());
if (p.trim().equals(c.getCode()))
ok = true;
}
if (!ok)
rule(errors, IssueType.STRUCTURE, path, ok, "Status element code \"" + p + "\" found for " + path + " in status-codes.xml but has no matching code in the resource (codes = " + cl + ")");
}
}
}
}
}
StringBuilder b = new StringBuilder();
for (DefinedCode c : ac) {
if (!c.getAbstract())
b.append(" | ").append(c.getCode());
}
if (sd.equals("*")) {
e.setShortDefn(b.toString().substring(3));
sd = b.toString().substring(3);
}
if (sd.contains("|")) {
if (b.length() < 3)
throw new Error("surprise");
String esd = b.substring(3);
rule(errors, IssueType.STRUCTURE, path, sd.startsWith(esd) || (sd.endsWith("+") && b.substring(3).startsWith(sd.substring(0, sd.length() - 1))), "The short description \"" + sd + "\" does not match the expected (\"" + b.substring(3) + "\")");
} else {
rule(errors, IssueType.STRUCTURE, path, cd.getStrength() != BindingStrength.REQUIRED || ac.size() > 12 || ac.size() <= 1 || !hasGoodCode(ac) || isExemptFromCodeList(path), "The short description of an element with a code list should have the format code | code | etc (is " + sd.toString() + ") (" + ac.size() + " codes = \"" + b.toString() + "\")");
}
}
boolean isComplex = !e.typeCode().equals("code");
if (isComplex && cd.getValueSet() != null && hasInternalReference(cd.getValueSet()) && cd.getStrength() != BindingStrength.EXAMPLE) {
hint(errors, IssueType.BUSINESSRULE, path, false, "The value " + cd.getValueSet().getUrl() + " defines codes, but is used by a Coding/CodeableConcept @ " + path + ", so it should not use FHIR defined codes");
cd.getValueSet().setUserData("vs-val-warned", true);
}
if (cd.getElementType() == ElementType.Unknown) {
if (isComplex)
cd.setElementType(ElementType.Complex);
else
cd.setElementType(ElementType.Simple);
} else if (isComplex && !cd.hasMax())
rule(errors, IssueType.STRUCTURE, path, cd.getElementType() == ElementType.Complex, "Cannot use a binding from both code and Coding/CodeableConcept elements");
else
rule(errors, IssueType.STRUCTURE, path, cd.getElementType() == ElementType.Simple, "Cannot use a binding from both code and Coding/CodeableConcept elements");
if (isComplex && cd.getValueSet() != null) {
for (ConceptSetComponent inc : cd.getValueSet().getCompose().getInclude()) if (inc.hasSystem())
txurls.add(inc.getSystem());
}
rule(errors, IssueType.STRUCTURE, "Binding @ " + path, (cd.getElementType() != ElementType.Simple || cd.getStrength() == BindingStrength.REQUIRED || cd.hasMax()) || isExemptFromProperBindingRules(path), "Must be a required binding if bound to a code instead of a Coding/CodeableConcept");
rule(errors, IssueType.STRUCTURE, "Binding @ " + path, cd.getElementType() != ElementType.Simple || cd.getBinding() != BindingMethod.Unbound, "Need to provide a binding for code elements");
if (!isComplex && !externalException(path)) {
ValueSet vs = cd.getValueSet();
if (warning(errors, IssueType.REQUIRED, path, vs != null || cd.hasReference(), "Unable to resolve value set on 'code' Binding")) {
hint(errors, IssueType.REQUIRED, path, noExternals(vs), "Bindings for code data types should only use internally defined codes (" + vs.getUrl() + ")");
// don't disable this without discussion on Zulip
}
}
}
use of org.hl7.v3.CD in project kindling by HL7.
the class CDAGenerator method processAttribute.
private void processAttribute(List<Element> classes, List<ElementDefinition> diff, List<ElementDefinition> snapshot, Element cclss, String path, Element attr) throws FHIRFormatError {
String n = attr.getAttribute("name");
ElementDefinition ed = new ElementDefinition();
ed.setPath(path + "." + n);
ed.setMin(Integer.parseInt(attr.getAttribute("minimumMultiplicity")));
ed.setMax(attr.getAttribute("maximumMultiplicity"));
if (ed.getMax().equals("0"))
return;
seePath(ed);
if (!Utilities.noString(attr.getAttribute("namespace")))
ed.addExtension().setUrl("http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace").setValue(new UriType(attr.getAttribute("namespace")));
String type = getType(attr);
if ("true".equals(attr.getAttribute("isImmutable"))) {
if (primitiveTypes.containsKey(type))
type = primitiveTypes.get(type);
else
throw new Error("Immutable attribute that is not a primitive type");
ed.addRepresentation(PropertyRepresentation.XMLATTR);
}
if ("GTS".equals(type)) {
ed.setMax("*");
ed.addRepresentation(PropertyRepresentation.TYPEATTR);
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/IVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/EIVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/PIVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/SXPR_TS");
ed.addExtension().setUrl("http://hl7.org/fhir/StructureDefinition/elementdefinition-defaulttype").setValue(new StringType("SXPR_TS"));
} else if ("ANY".equals(type)) {
ed.addRepresentation(PropertyRepresentation.TYPEATTR);
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/BL");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/ED");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/ST");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/CD");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/CV");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/CE");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/SC");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/II");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/TEL");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/AD");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/EN");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/INT");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/REAL");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/PQ");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/MO");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/IVL_PQ");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/IVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/PIVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/EIVL_TS");
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/SXPR_TS");
} else if (isPrimitive(type))
ed.addType().setCode(type);
else if (Utilities.existsInList(type, "PN"))
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/EN");
else if (Utilities.existsInList(type, "ON"))
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/ST");
else if (Utilities.existsInList(type, "NARRATIVE")) {
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/StrucDoc.Text");
ed.addRepresentation(PropertyRepresentation.CDATEXT);
} else
ed.addType().setCode("http://hl7.org/fhir/cda/StructureDefinition/" + type);
if ("R".equals(attr.getAttribute("conformance")))
ed.setMustSupport(true);
if (attr.hasAttribute("defaultValue"))
ed.setDefaultValue(buildValue(attr.getAttribute("defaultValue"), type, ed.getPath()));
List<Element> enums = new ArrayList<Element>();
XMLUtil.getNamedChildren(attr, "enumerationValue", enums);
if (enums.size() == 1)
ed.setFixed(buildValue(enums.get(0).getTextContent(), type, ed.getPath()));
if (enums.size() > 1) {
// throw new Error("todo: enums on "+ed.getPath());
} else if (XMLUtil.getNamedChild(attr, "vocabulary") != null) {
// <vocabulary codingStrength="CWE"><conceptDomain name="ActClass"/></vocabulary>
Element vocab = XMLUtil.getNamedChild(attr, "vocabulary");
String cs = vocab.getAttribute("codingStrength");
String cd = XMLUtil.getNamedChildAttribute(vocab, "conceptDomain", "name");
ElementDefinitionBindingComponent bd = ed.getBinding();
bd.setStrength(cs.equals("CNE") ? BindingStrength.REQUIRED : BindingStrength.EXTENSIBLE);
bd.setValueSet("http://terminology.hl7.org/ValueSet/v3-" + cd);
v3vs.add(cd);
}
diff.add(ed);
snapshot.add(popBase(ed));
}
use of org.hl7.v3.CD in project kindling by HL7.
the class JsonLDGenerator method generateElement.
private void generateElement(ElementDefn root, String name, ElementDefn e, JsonObject base, Set<String> types) throws Exception {
if ((e.getTypes().size() == 1 && e.getTypes().get(0).isWildcardType())) {
if (!e.getName().contains("[x]"))
throw new Exception("Element " + e.getName() + " in " + root.getName() + " has multiple types as a choice doesn't have a [x] in the element name");
for (TypeRef tr : datatypes) {
String tn = tr.getName();
if (tn.equals("SimpleQuantity"))
tn = "Quantity";
else
tn = Utilities.capitalize(tn);
String en = e.getName().substring(0, e.getName().length() - 3) + tn;
JsonObject property = new JsonObject();
base.add(name + "." + en, property);
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + en);
}
// if (e.getTypes().size() == 1)
// generateAny(root, e, e.getName().replace("[x]", ""), props, relative);
// else {
// for (TypeRef t : e.getTypes()) {
// JsonObject property = new JsonObject();
// JsonObject property_ = null;
// String en = e.getName().replace("[x]", "");
// props.add(en+upFirst(t.getName()), property);
// property.addProperty("description", e.getDefinition());
// String tref = null;
// String type = null;
// String pattern = null;
// if (definitions.getPrimitives().containsKey(t.getName())) {
// DefinedCode def = definitions.getPrimitives().get(t.getName());
// type = def.getJsonType();
// pattern = def.getRegex();
// if (!Utilities.noString(pattern))
// property.addProperty("pattern", pattern);
//
// property.addProperty("type", type);
// property_ = new JsonObject();
// props.add("_"+en+upFirst(t.getName()), property_);
// property_.addProperty("description", "Extensions for "+en+upFirst(t.getName()));
// tref = (relative ? "#" : "Element.schema.json#") +"/definitions/Element";
// property_.addProperty("$ref", tref);
// } else {
// String tn = encodeType(e, t, true);
// tref = (relative ? "#" : tn.replace(".", "_")+".schema.json#") +"/definitions/"+tn.replace(".", "_");
// property.addProperty("$ref", tref);
// }
// }
// }
} else if (e.getName().endsWith("[x]")) {
for (TypeRef tr : e.getTypes()) {
String tn = tr.getName();
if (tn.equals("SimpleQuantity"))
tn = "Quantity";
else
tn = Utilities.capitalize(tn);
String en = e.getName().substring(0, e.getName().length() - 3) + tn;
JsonObject property = new JsonObject();
base.add(name + "." + en, property);
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + en);
}
} else {
JsonObject property = new JsonObject();
base.add(name + "." + e.getName(), property);
if (e.getPath() == null)
property.addProperty("@id", "http://hl7.org/fhir/" + name + "." + e.getName());
else
property.addProperty("@id", "http://hl7.org/fhir/" + e.getPath());
// if we're using lists:
// if (e.unbounded())
// property.addProperty("@container", "@list");
// property.addProperty("fhir-@type", "http://hl7.org/fhir/"+e.typeCode());
// String tref = null;
// String type = null;
// String pattern = null;
//
// if (e.usesCompositeType()/* && types.containsKey(root.getElementByName(e.typeCode().substring(1)))*/) {
// ElementDefn ref = root.getElementByName(definitions, e.typeCode().substring(1), true, false);
// String rtn = types.get(ref);
// if (rtn == null)
// throw new Exception("logic error in schema generator (null composite reference in "+types.toString()+")");
//
// if(rtn == "Type")
// rtn = "Element";
// type=rtn;
// tref = "#/definitions/"+rtn.replace(".", "_");
// } else if (e.getTypes().size() == 0 && e.getElements().size() > 0){
// tref = "#/definitions/"+types.get(e).replace(".", "_");
// type=types.get(e).replace(".", "_");
// } else if (e.getTypes().size() == 1) {
// String tn = encodeType(e, e.getTypes().get(0), true);
// type=tn;
// if (definitions.getPrimitives().containsKey(e.typeCode())) {
// DefinedCode def = definitions.getPrimitives().get(e.typeCode());
// type = def.getJsonType();
// pattern = def.getRegex();
// property_ = new JsonObject();
// props.add("_"+e.getName(), property_);
// property_.addProperty("description", "Extensions for "+e.getName());
// tref = (relative ? "#" : "Element.schema.json#") +"/definitions/Element";
// BindingSpecification cd = e.getBinding();
//
// if (cd != null && (cd.getBinding() == BindingSpecification.BindingMethod.CodeList)) {
// ValueSet vs = cd.getValueSet();
// if (vs!= null) {
// ValueSet ex = workerContext.expandVS(vs, true, false).getValueset();
// JsonArray enums = new JsonArray();
// for (ValueSetExpansionContainsComponent cc : ex.getExpansion().getContains()) {
// enums.add(new JsonPrimitive(cc.getCode()));
// }
// property.add("enum", enums);
// pattern = null;
// }
// }
// } else {
// tref = (relative ? "#" : tn.replace(".", "_")+".schema.json#") +"/definitions/"+tn.replace(".", "_");
// }
// } else
// throw new Exception("how do we get here? "+e.getName()+" in "+root.getName()+" "+Integer.toString(e.getTypes().size()));
//
// if (e.unbounded()) {
// property.addProperty("type", "array");
// if (property_ != null) {
// property_.addProperty("type", "array");
// JsonObject items = new JsonObject();
// property.add("items", items);
// items.addProperty("type", type);
// if (!Utilities.noString(pattern))
// items.addProperty("pattern", pattern);
//
// items = new JsonObject();
// property_.add("items", items);
// items.addProperty("$ref", tref);
// } else {
// JsonObject items = new JsonObject();
// property.add("items", items);
// items.addProperty("$ref", tref);
// }
// } else {
// if (property_ != null) {
// property.addProperty("type", type);
// if (!Utilities.noString(pattern))
// property.addProperty("pattern", pattern);
//
// property_.addProperty("$ref", tref);
// } else if("div".equals(e.getName()) && "xhtml".equals(type)) {
// // Is there a better type, or ref for html?
// property.addProperty("type", "string");
// } else {
// property.addProperty("$ref", tref);
// }
// }
// if (e.getMinCardinality() > 0 && property_ == null)
// required.add(e.getName());
}
}
use of org.hl7.v3.CD in project kindling by HL7.
the class XSDBaseGenerator method genPrimitives.
private void genPrimitives() throws Exception {
for (DefinedCode cd : definitions.getPrimitives().values()) {
if (cd instanceof PrimitiveType) {
PrimitiveType pt = (PrimitiveType) cd;
// two very special cases due to schema weirdness
if (cd.getCode().equals("date")) {
write(" <xs:simpleType name=\"date-primitive\">\r\n");
write(" <xs:restriction>\r\n");
write(" <xs:simpleType>\r\n");
write(" <xs:union memberTypes=\"xs:gYear xs:gYearMonth xs:date\"/>\r\n");
write(" </xs:simpleType>\r\n");
write(" <xs:pattern value=\"([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1]))?)?\"/>\r\n");
write(" </xs:restriction>\r\n");
write(" </xs:simpleType>\r\n");
} else if (cd.getCode().equals("dateTime")) {
write(" <xs:simpleType name=\"dateTime-primitive\">\r\n");
write(" <xs:restriction>\r\n");
write(" <xs:simpleType>\r\n");
write(" <xs:union memberTypes=\"xs:gYear xs:gYearMonth xs:date xs:dateTime\"/>\r\n");
write(" </xs:simpleType>\r\n");
write(" <xs:pattern value=\"([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|(\\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?\"/>\r\n");
write(" </xs:restriction>\r\n");
write(" </xs:simpleType>\r\n");
} else if (cd.getCode().equals("time")) {
write(" <xs:simpleType name=\"time-primitive\">\r\n");
write(" <xs:restriction base=\"xs:time\">\r\n");
write(" <xs:pattern value=\"([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?\"/>\r\n");
write(" </xs:restriction>\r\n");
write(" </xs:simpleType>\r\n");
} else {
write(" <xs:simpleType name=\"" + pt.getCode() + "-primitive\">\r\n");
if (pt.getSchemaType().contains(",")) {
write(" <xs:union memberTypes=\"" + pt.getSchemaType().replace(",", "") + "\"/>\r\n");
} else if (pt.getSchemaType().equals("string")) {
write(" <xs:restriction base=\"xs:" + pt.getSchemaType() + "\">\r\n");
write(" <xs:minLength value=\"1\"/>\r\n");
write(" </xs:restriction>\r\n");
} else if (!Utilities.noString(pt.getRegex())) {
write(" <xs:restriction base=\"xs:" + pt.getSchemaType() + "\">\r\n");
write(" <xs:pattern value=\"" + pt.getRegex() + "\"/>\r\n");
write(" </xs:restriction>\r\n");
} else {
write(" <xs:restriction base=\"xs:" + pt.getSchemaType() + "\"/>\r\n");
}
write(" </xs:simpleType>\r\n");
}
write(" <xs:complexType name=\"" + pt.getCode() + "\">\r\n");
write(" <xs:annotation>\r\n");
write(" <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(pt.getDefinition()) + "</xs:documentation>\r\n");
if (!Utilities.noString(pt.getComment()))
write(" <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(pt.getComment()) + "</xs:documentation>\r\n");
write(" <xs:documentation xml:lang=\"en\">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>\r\n");
write(" </xs:annotation>\r\n");
write(" <xs:complexContent>\r\n");
write(" <xs:extension base=\"Element\">\r\n");
write(" <xs:attribute name=\"value\" type=\"" + pt.getCode() + "-primitive\" use=\"optional\"/>\r\n");
write(" </xs:extension>\r\n");
write(" </xs:complexContent>\r\n");
write(" </xs:complexType>\r\n");
} else {
DefinedStringPattern sp = (DefinedStringPattern) cd;
write(" <xs:simpleType name=\"" + sp.getCode() + "-primitive\">\r\n");
if (sp.getSchema().endsWith("+")) {
write(" <xs:restriction base=\"" + sp.getSchema().substring(0, sp.getSchema().length() - 1) + "\">\r\n");
write(" <xs:pattern value=\"" + sp.getRegex() + "\"/>\r\n");
write(" <xs:minLength value=\"1\"/>\r\n");
if (sp.getCode().equals("id"))
write(" <xs:maxLength value=\"64\"/>\r\n");
write(" </xs:restriction>\r\n");
write(" </xs:simpleType>\r\n");
} else {
write(" <xs:restriction base=\"" + sp.getSchema() + "\">\r\n");
if (!sp.getSchema().contains("Integer")) {
write(" <xs:minLength value=\"1\"/>\r\n");
}
if (!Utilities.noString(sp.getRegex())) {
write(" <xs:pattern value=\"" + sp.getRegex() + "\"/>\r\n");
}
write(" </xs:restriction>\r\n");
write(" </xs:simpleType>\r\n");
}
write(" <xs:complexType name=\"" + sp.getCode() + "\">\r\n");
write(" <xs:annotation>\r\n");
write(" <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(sp.getDefinition()) + "</xs:documentation>\r\n");
if (!Utilities.noString(sp.getComment()))
write(" <xs:documentation xml:lang=\"en\">" + Utilities.escapeXml(sp.getComment()) + "</xs:documentation>\r\n");
write(" <xs:documentation xml:lang=\"en\">If the element is present, it must have either a @value, an @id referenced from the Narrative, or extensions</xs:documentation>\r\n");
write(" </xs:annotation>\r\n");
write(" <xs:complexContent>\r\n");
write(" <xs:extension base=\"Element\">\r\n");
write(" <xs:attribute name=\"value\" type=\"" + sp.getCode() + "-primitive\"/>\r\n");
write(" </xs:extension>\r\n");
write(" </xs:complexContent>\r\n");
write(" </xs:complexType>\r\n");
}
}
}
use of org.hl7.v3.CD in project kindling by HL7.
the class JsonGenerator method generateElement.
private void generateElement(ElementDefn root, ElementDefn e, Set<String> required, JsonObject props, boolean relative) throws Exception {
if (e.getTypes().size() > 1 || (e.getTypes().size() == 1 && e.getTypes().get(0).isWildcardType())) {
if (!e.getName().contains("[x]"))
throw new Exception("Element " + e.getName() + " in " + root.getName() + " has multiple types as a choice doesn't have a [x] in the element name");
if (e.getTypes().size() == 1)
generateAny(root, e, e.getName().replace("[x]", ""), props, relative);
else {
for (TypeRef t : e.getTypes()) {
JsonObject property = new JsonObject();
JsonObject property_ = null;
TypeDefn td = null;
if (definitions.getConstraints().containsKey(t.getName()))
td = definitions.getElementDefn(definitions.getConstraints().get(t.getName()).getBaseType());
else if (definitions.hasElementDefn(t.getName()))
td = definitions.getElementDefn(t.getName());
String en = e.getName().replace("[x]", "");
props.add(en + upFirst(td == null ? t.getName() : td.getName()), property);
property.addProperty("description", e.getDefinition());
String tref = null;
String type = null;
String pattern = null;
if (definitions.getPrimitives().containsKey(t.getName())) {
DefinedCode def = definitions.getPrimitives().get(t.getName());
type = def.getJsonType();
pattern = def.getRegex();
if (!Utilities.noString(pattern))
property.addProperty("pattern", "^" + pattern + "$");
property.addProperty("type", type);
property_ = new JsonObject();
props.add("_" + en + upFirst(t.getName()), property_);
property_.addProperty("description", "Extensions for " + en + upFirst(t.getName()));
tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
property_.addProperty("$ref", tref);
} else {
String tn = encodeType(e, t, true);
tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
property.addProperty("$ref", tref);
}
}
}
} else {
JsonObject property = new JsonObject();
JsonObject property_ = null;
props.add(e.getName(), property);
property.addProperty("description", e.getDefinition());
String tref = null;
String type = null;
String pattern = null;
if (e.usesCompositeType()) /* && types.containsKey(root.getElementByName(e.typeCode().substring(1)))*/
{
ElementDefn ref = root.getElementByName(definitions, e.typeCode().substring(1), true, false, null);
String rtn = types.get(ref);
if (rtn == null)
throw new Exception("logic error in schema generator (null composite reference in " + types.toString() + ")");
if (rtn == "Type")
rtn = "Element";
type = rtn;
tref = "#/definitions/" + rtn.replace(".", "_");
} else if (e.getTypes().size() == 0 && e.getElements().size() > 0) {
tref = "#/definitions/" + types.get(e).replace(".", "_");
type = types.get(e).replace(".", "_");
} else if (e.getTypes().size() == 1) {
String tn = encodeType(e, e.getTypes().get(0), true);
tref = "#/definitions/" + tn;
if (definitions.getPrimitives().containsKey(e.typeCode())) {
DefinedCode def = definitions.getPrimitives().get(e.typeCode());
if (e.getName().equals("id")) {
tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
property.addProperty("$ref", tref);
} else {
property_ = new JsonObject();
props.add("_" + e.getName(), property_);
property_.addProperty("description", "Extensions for " + e.getName());
BindingSpecification cd = e.getBinding();
if (cd != null && (cd.getBinding() == BindingSpecification.BindingMethod.CodeList)) {
ValueSet vs = cd.getValueSet();
if (vs != null) {
ValueSet ex = workerContext.expandVS(vs, true, false).getValueset();
JsonArray enums = new JsonArray();
for (ValueSetExpansionContainsComponent cc : ex.getExpansion().getContains()) {
enums.add(new JsonPrimitive(cc.getCode()));
}
property.add("enum", enums);
pattern = null;
}
} else {
property.addProperty("$ref", tref);
}
tref = (relative ? "#" : "Element.schema.json#") + "/definitions/Element";
}
} else {
tref = (relative ? "#" : tn.replace(".", "_") + ".schema.json#") + "/definitions/" + tn.replace(".", "_");
}
} else
throw new Exception("how do we get here? " + e.getName() + " in " + root.getName() + " " + Integer.toString(e.getTypes().size()));
if (property_ != null) {
if (!Utilities.noString(type))
property.addProperty("type", type);
if (!Utilities.noString(pattern))
property.addProperty("pattern", "^" + pattern + "$");
if (!Utilities.noString(tref))
property_.addProperty("$ref", tref);
} else if ("div".equals(e.getName()) && "xhtml".equals(type)) {
// Is there a better type, or ref for html?
property.addProperty("type", "string");
} else {
property.addProperty("$ref", tref);
}
if (e.unbounded()) {
makeArray(property);
if (property_ != null)
makeArray(property_);
}
if (e.getMinCardinality() > 0 && property_ == null)
required.add(e.getName());
}
}
Aggregations