use of org.hl7.fhir.dstu3.model.StructureDefinition in project kindling by HL7.
the class OldSpreadsheetParser method parseConformancePackage.
public void parseConformancePackage(Profile ap, Definitions definitions, String folder, String usage, List<ValidationMessage> issues, WorkGroup wg) throws Exception {
try {
isProfile = true;
this.folder = folder;
checkMappings(ap);
Sheet sheet = loadSheet("Bindings");
if (sheet != null)
readBindings(sheet);
sheet = loadSheet("Metadata");
for (int row = 0; row < sheet.rows.size(); row++) {
String n = sheet.getColumn(row, "Name");
String v = sheet.getColumn(row, "Value");
if (n != null && v != null) {
if (ap.getMetadata().containsKey(n))
ap.getMetadata().get(n).add(v);
else {
ArrayList<String> vl = new ArrayList<String>();
vl.add(v);
ap.getMetadata().put(n, vl);
}
}
}
if (!Utilities.noString(ap.metadata("category")))
usage = ap.metadata("category");
if (ap.hasMetadata("name"))
ap.setTitle(ap.metadata("name"));
if (ap.hasMetadata("introduction"))
ap.setIntroduction(Utilities.path(folder, ap.metadata("introduction")));
if (ap.hasMetadata("notes"))
ap.setNotes(Utilities.path(folder, ap.metadata("notes")));
if (!ap.hasMetadata("id"))
throw new Exception("Error parsing " + ap.getId() + "/" + ap.getTitle() + " no 'id' found in metadata");
if (!ap.metadata("id").matches(FormatUtilities.ID_REGEX))
throw new Exception("Error parsing " + ap.getId() + "/" + ap.getTitle() + " 'id' is not a valid id");
if (wg == null)
wg = workgroups.get(ap.metadata("workgroup"));
if (wg == null)
wg = committee;
if (wg == null)
throw new Exception("Error parsing " + ap.getId() + "/" + ap.getTitle() + " : no workgroup value in the metadata");
if (!ap.metadata("id").equals(ap.metadata("id").toLowerCase()))
throw new Exception("Error parsing " + ap.getId() + "/" + ap.getTitle() + " 'id' must be all lowercase");
this.profileExtensionBase = ap.metadata("extension.uri");
if (ig == null || ig.isCore()) {
if (!profileExtensionBase.startsWith("http://hl7.org/fhir/StructureDefinition/") && !profileExtensionBase.startsWith("http://fhir-registry.smarthealthit.org/StructureDefinition/"))
throw new Exception("Core extensions must have a url starting with http://hl7.org/fhir/StructureDefinition/ for " + ap.getId());
} else {
if (!profileExtensionBase.startsWith("http://hl7.org/fhir/StructureDefinition/" + ig.getCode() + "-"))
throw new Exception("Core extensions must have a url starting with http://hl7.org/fhir/StructureDefinition/" + ig.getCode() + "- for " + ap.getId());
}
Map<String, Invariant> invariants = null;
sheet = loadSheet("Extensions-Inv");
if (sheet != null) {
invariants = readInvariants(sheet, "", "Extensions-Inv");
}
sheet = loadSheet("Extensions");
if (sheet != null) {
int row = 0;
while (row < sheet.rows.size()) {
if (sheet.getColumn(row, "Code").startsWith("!"))
row++;
else
row = processExtension(null, sheet, row, definitions, ap.metadata("extension.uri"), ap, issues, invariants, wg);
}
}
List<String> namedSheets = new ArrayList<String>();
if (ap.getMetadata().containsKey("published.structure")) {
for (String n : ap.getMetadata().get("published.structure")) {
if (!Utilities.noString(n)) {
if (ig != null && !ig.isCore() && !n.toLowerCase().startsWith(ig.getCode() + "-"))
throw new Exception("Error: published structure names must start with the implementation guide code (" + ig.getCode() + "-)");
String fmm = ap.metadata("fmm-" + n);
if (Utilities.noString(fmm))
fmm = ap.metadata("fmm");
if (Utilities.noString(fmm))
// default fmm value
fmm = "1";
ap.getProfiles().add(parseProfileSheet(definitions, ap, n, namedSheets, true, usage, issues, wg, fmm));
}
}
}
int i = 0;
while (i < namedSheets.size()) {
String fmm = ap.metadata("fmm-" + namedSheets.get(i));
if (Utilities.noString(fmm))
fmm = ap.metadata("fmm");
if (Utilities.noString(fmm))
// default fmm value
fmm = "1";
ap.getProfiles().add(parseProfileSheet(definitions, ap, namedSheets.get(i), namedSheets, false, usage, issues, wg, fmm));
i++;
}
if (namedSheets.isEmpty() && xls.getSheets().containsKey("Search"))
readSearchParams(ap, xls.getSheets().get("Search"), this.profileExtensionBase);
if (xls.getSheets().containsKey("Operations"))
readOperations(ap.getOperations(), loadSheet("Operations"));
} catch (Exception e) {
throw new Exception("exception parsing pack " + ap.getSource() + ": " + e.getMessage(), e);
}
}
use of org.hl7.fhir.dstu3.model.StructureDefinition in project kindling by HL7.
the class OldSpreadsheetParser method readSearchParams.
private void readSearchParams(ResourceDefn root2, Sheet sheet, boolean forProfile) throws Exception {
if (sheet != null) {
for (int row = 0; row < sheet.rows.size(); row++) {
if (!sheet.hasColumn(row, "Name"))
throw new Exception("Search Param has no name " + getLocation(row));
String n = sheet.getColumn(row, "Name");
if (!n.startsWith("!")) {
if (!sheet.hasColumn(row, "Type"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " has no type " + getLocation(row));
if (n.endsWith("-before") || n.endsWith("-after"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " includes relative time " + getLocation(row));
if (root2.getSearchParams().containsKey(n))
throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
String d = sheet.getColumn(row, "Description");
SearchType t = readSearchType(sheet.getColumn(row, "Type"), row);
SearchParameter.XPathUsageType pu = readSearchXPathUsage(sheet.getColumn(row, "Path Usage"), row);
if (Utilities.noString(sheet.getColumn(row, "Path")) && !root2.getName().equals("Resource") && !root2.getName().equals("DomainResource"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " has no path at " + getLocation(row));
SearchParameterDefn sp = null;
if (t == SearchType.composite) {
List<CompositeDefinition> pn = new ArrayList<CompositeDefinition>();
if (Utilities.noString(d))
throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
String[] pl = sheet.getColumn(row, "Path").split("\\&");
String[] pe = sheet.getColumn(row, "Expression").split("\\;");
if (pe.length != pl.length + 1)
throw new Exception("Composite Search Param " + root2.getName() + "/" + n + " needs expressions " + getLocation(row));
int i = 0;
for (String pi : pl) {
String p = pi.trim();
i++;
String e = pe[i].trim();
if (!root2.getSearchParams().containsKey(p)) {
boolean found = false;
if (p.endsWith("[x]"))
for (String pan : root2.getSearchParams().keySet()) {
if (pan.startsWith(p.substring(0, p.length() - 3)))
found = true;
}
if (!found)
throw new Exception("Composite Search Param " + root2.getName() + "/" + n + " refers to an unknown component " + p + " at " + getLocation(row));
}
pn.add(new CompositeDefinition(p, e));
}
StandardsStatus ss = root2.getStatus();
if (!Utilities.noString(sheet.getColumn(row, "Standards-Status")))
ss = StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status"));
sp = new SearchParameterDefn(n, d, t, pu, ss);
sp.setExpression(pe[0].trim());
sp.getComposites().addAll(pn);
} else {
List<String> pn = new ArrayList<String>();
String xp = sheet.getColumn(row, "XPath");
String[] pl = sheet.getColumn(row, "Path").split("\\|");
boolean hierarchy = false;
for (String pi : pl) {
String p = pi.trim();
ElementDefn e = null;
if (!Utilities.noString(p) && !p.startsWith("!") && !p.startsWith("Extension{") && definitions != null) {
e = root2.getRoot().getElementForPath(trimIndexes(p), definitions, "search param", true, true);
}
if (e != null && e.hasHierarchy() && e.getHierarchy())
hierarchy = true;
if (Utilities.noString(d) && e != null)
d = e.getShortDefn();
if (p.startsWith("Extension(")) {
String url = extractExtensionUrl(p);
StructureDefinition ex = context.fetchResource(StructureDefinition.class, url);
if (ex == null)
throw new Exception("Search Param " + root2.getName() + "/" + n + " refers to unknown extension '" + url + "' " + getLocation(row));
if (Utilities.noString(d))
d = ex.getDescription();
pn.add(p);
}
if (d == null)
throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
if (e != null)
pn.add(p);
if (t == SearchType.reference) {
if (e == null && !forProfile && !sheet.hasColumn(row, "Target Types"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " of type reference has wrong path '" + p + "' at " + getLocation(row));
if (!forProfile && e != null && (!e.hasType("Reference")) && (!e.hasType("canonical")) && (!e.hasType("Resource")))
throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is reference, but the element type is " + e.typeCode());
} else {
if (e != null && e.hasOnlyType("Reference"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
if (t == SearchType.uri) {
if (e != null && !(e.typeCode().equals("uri") || e.typeCode().equals("url") || e.typeCode().equals("oid") || e.typeCode().startsWith("canonical(")))
throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
} else {
if (e != null && e.typeCode().equals("uri"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + t.toString() + ", but the element type is " + e.typeCode());
}
}
}
if (!forProfile && t == SearchType.reference && pn.size() == 0 && !sheet.hasColumn(row, "Target Types"))
throw new Exception("Search Param " + root2.getName() + "/" + n + " of type reference has no path(s) " + getLocation(row));
StandardsStatus ss = root2.getStatus();
if (!Utilities.noString(sheet.getColumn(row, "Standards-Status")))
ss = StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status"));
sp = new SearchParameterDefn(n, d, t, pu, ss);
sp.getPaths().addAll(pn);
if (!Utilities.noString(xp))
sp.setXPath(xp);
if (!Utilities.noString(sheet.getColumn(row, "Expression")))
sp.setExpression(sheet.getColumn(row, "Expression"));
if (!Utilities.noString(sheet.getColumn(row, "Target Types"))) {
sp.setManualTypes(sheet.getColumn(row, "Target Types").split("\\,"));
}
sp.setHierarchy(hierarchy);
CommonSearchParameter csp = definitions.getCommonSearchParameters().get(root2.getName() + "::" + n);
if (csp != null)
for (String s : csp.getResources()) {
if (!root2.getName().equals(s))
sp.getOtherResources().add(s);
}
}
root2.getSearchParams().put(n, sp);
}
}
}
}
use of org.hl7.fhir.dstu3.model.StructureDefinition in project kindling by HL7.
the class OldSpreadsheetParser method readSearchParams.
/* for profiles that have a "search" tab not tied to a structure */
private void readSearchParams(Profile pack, Sheet sheet, String prefix) throws Exception {
for (int row = 0; row < sheet.rows.size(); row++) {
if (!sheet.hasColumn(row, "Name"))
throw new Exception("Search Param has no name " + getLocation(row));
String n = sheet.getColumn(row, "Name");
if (!n.startsWith("!")) {
SearchParameter sp = new SearchParameter();
if (!sheet.hasColumn(row, "Type"))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no type " + getLocation(row));
if (n.endsWith("-before") || n.endsWith("-after"))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " includes relative time " + getLocation(row));
// if (!n.toLowerCase().equals(n))
// throw new Exception("Search Param "+pack.getTitle()+"/"+n+" must be all lowercase "+ getLocation(row));
sp.setVersion(version.toCode());
sp.setName(n);
sp.setCode(n);
if (pack.getProfiles().size() > 0 && pack.getProfiles().get(0).getResource() != null) {
sp.setStatus(pack.getProfiles().get(0).getResource().getStatus());
sp.setExperimental(pack.getProfiles().get(0).getResource().getExperimental());
} else {
// we just guess
sp.setStatus(PublicationStatus.DRAFT);
sp.setExperimental(true);
}
String d = sheet.getColumn(row, "Description");
sp.setType(SearchParamType.fromCode(sheet.getColumn(row, "Type")));
List<String> pn = new ArrayList<String>();
String path = sheet.getColumn(row, "Path");
if (Utilities.noString(path))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no path");
if (!path.contains(".") && !path.startsWith("#"))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path: " + path);
ResourceDefn root2 = null;
if (!path.startsWith("#")) {
path = path.substring(0, path.indexOf('.'));
if (!pkp.isResource(path))
throw new Exception("Ilegal Search Parameter path " + sheet.getColumn(row, "Path"));
sp.addBase(path);
sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
if (definitions != null) {
// igtodo (and below)
root2 = definitions.getResourceByName(path);
if (root2 == null)
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path (resource not found)");
if (!pkp.isResource(root2.getName()))
throw new Exception("Ilegal Search Parameter path " + sheet.getColumn(row, "Path"));
sp.getBase().clear();
sp.addBase(root2.getName());
sp.setId(pack.getId() + "-" + (root2 == null ? "all" : root2.getName()) + "-" + sp.getName());
}
}
if (!Utilities.noString(sheet.getColumn(row, "Target Types")))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has manually specified targets (not allowed)");
if (root2 != null && root2.getSearchParams().containsKey(n))
throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
if (sp.getType() == SearchParamType.COMPOSITE) {
throw new Exception("not supported");
} else {
String[] pl = sheet.getColumn(row, "Path").split("\\|");
String xp = sheet.getColumn(row, "XPath");
for (String pi : pl) {
String p = pi.trim();
ElementDefn e = null;
if (Utilities.noString(p))
throw new Exception("Search Param " + root2.getName() + "/" + n + ": empty path " + getLocation(row));
if (p.startsWith("#")) {
// root less extension search parameter
StructureDefinition ex = pack.getExtension(prefix + p.substring(1));
if (ex == null)
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to unknown extension '" + p + "' " + getLocation(row));
e = definitions.getElementDefn("Extension");
if (ex.getContext().size() != 1 || ex.getContext().get(0).getType() != ExtensionContextType.ELEMENT)
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to an extension with multiple contexts, not not an element context - not supported '" + p + "' " + getLocation(row));
path = ex.getContext().get(0).getExpression();
if (Utilities.noString(path))
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has no path");
if (path.contains("."))
path = path.substring(0, path.indexOf('.'));
sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
root2 = definitions.getResourceByName(path);
if (root2 == null)
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " has an invalid path (resource not found)");
if (root2 != null && root2.getSearchParams().containsKey(n))
throw new Exception("Search Param " + root2.getName() + "/" + n + ": duplicate name " + getLocation(row));
sp.setId(pack.getId() + "-" + path + "-" + sp.getName());
pn.add(ex.getContext().get(0).getExpression() + ".extension{" + ex.getUrl() + "}");
} else if (p.contains(".extension{")) {
String url = extractExtensionUrl(p);
// not created yet?
StructureDefinition ex = context.fetchResource(StructureDefinition.class, url);
if (ex == null)
ex = context.getExtensionStructure(null, url);
if (ex == null)
throw new Exception("Search Param " + pack.getTitle() + "/" + n + " refers to unknown extension '" + url + "' " + getLocation(row));
if (Utilities.noString(d))
d = ex.getDescription();
if (definitions != null)
e = definitions.getElementDefn("Extension");
pn.add(p);
} else if (!p.startsWith("!") && !p.startsWith("Extension{") && root2 != null) {
e = root2.getRoot().getElementForPath(p, definitions, "search param", true, true);
}
if (e == null && Utilities.noString(d))
throw new Exception("unable to resolve sarch param " + p);
if (e == null)
sp.setExpression(p);
if (Utilities.noString(d) && e != null)
d = e.getShortDefn();
if (d == null)
throw new Exception("Search Param " + root2.getName() + "/" + n + " has no description " + getLocation(row));
if (e != null)
pn.add(p);
if (sp.getType() == SearchParamType.REFERENCE) {
// no check?
} else if (e != null && e.typeCode().startsWith("Reference("))
throw new Exception("Search Param " + root2.getName() + "/" + n + " wrong type. The search type is " + sp.getType().toCode() + ", but the element type is " + e.typeCode());
sp.setDescription(d);
}
sp.setXpath(Utilities.noString(xp) ? new XPathQueryGenerator(definitions, log, null).generateXpath(pn, null) : xp);
sp.setXpathUsage(readSearchXPathUsage(sheet.getColumn(row, "Path Usage"), row));
}
sp.setUrl("http://hl7.org/fhir/SearchParameter/" + sp.getId());
if (definitions != null)
definitions.addNs(sp.getUrl(), "Search Parameter " + sp.getName(), pack.getId() + ".html#search");
if (context.getSearchParameter(sp.getUrl()) != null)
throw new Exception("Duplicated Search Parameter " + sp.getUrl());
context.cacheResource(sp);
pack.getSearchParameters().add(sp);
}
}
}
use of org.hl7.fhir.dstu3.model.StructureDefinition in project kindling by HL7.
the class SourceParser method genTypeProfile.
private void genTypeProfile(org.hl7.fhir.definitions.model.TypeDefn t) throws Exception {
StructureDefinition profile;
try {
profile = new ProfileGenerator(definitions, context, page, genDate, version, null, fpUsages, page.getFolders().rootDir, page.getUml(), page.getRc()).generate(t);
t.setProfile(profile);
DataTypeTableGenerator dtg = new DataTypeTableGenerator(dstDir, page, t.getName(), true, version);
t.getProfile().getText().setDiv(new XhtmlNode(NodeType.Element, "div"));
t.getProfile().getText().getDiv().getChildNodes().add(dtg.generate(t, null, false));
if (context.hasResource(StructureDefinition.class, t.getProfile().getUrl()))
throw new Exception("Duplicate Profile " + t.getProfile().getUrl());
context.cacheResource(t.getProfile());
} catch (Exception e) {
throw new Exception("Error generating profile for '" + t.getName() + "': " + e.getMessage(), e);
}
}
use of org.hl7.fhir.dstu3.model.StructureDefinition in project kindling by HL7.
the class SourceParser method loadConformancePackage.
private void loadConformancePackage(Profile ap, List<ValidationMessage> issues, WorkGroup wg) throws FileNotFoundException, IOException, Exception {
if (ap.getSourceType() == ConformancePackageSourceType.Spreadsheet) {
OldSpreadsheetParser sparser = new OldSpreadsheetParser(ap.getCategory(), new CSFileInputStream(ap.getSource()), Utilities.noString(ap.getId()) ? ap.getSource() : ap.getId(), ap.getSource(), definitions, srcDir, logger, registry, version, context, genDate, false, page, false, ini, wg, definitions.getProfileIds(), fpUsages, page.getConceptMaps(), exceptionIfExcelNotNormalised, page.packageInfo(), page.getRc());
sparser.setFolder(Utilities.getDirectoryForFile(ap.getSource()));
sparser.parseConformancePackage(ap, definitions, Utilities.getDirectoryForFile(ap.getSource()), ap.getCategory(), issues, wg);
errors.addAll(sparser.getErrors());
} else if (ap.getSourceType() == ConformancePackageSourceType.StructureDefinition) {
Resource rf;
try {
rf = new XmlParser().parse(new CSFileInputStream(ap.getSource()));
} catch (Exception e) {
throw new Exception("Error parsing " + ap.getSource() + ": " + e.getMessage(), e);
}
if (!(rf instanceof StructureDefinition))
throw new Exception("Error parsing Profile: not a structure definition");
StructureDefinition sd = (StructureDefinition) rf;
sd.setVersion(version.toCode());
ap.putMetadata("id", sd.getId() + "-pack");
ap.putMetadata("date", sd.getDateElement().asStringValue());
ap.putMetadata("title", sd.getTitle());
ap.putMetadata("status", sd.getStatus().toCode());
ap.putMetadata("description", new XhtmlComposer(XhtmlComposer.HTML).compose(sd.getText().getDiv()));
if (ToolingExtensions.hasExtension(sd, "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg")) {
wg = definitions.getWorkgroups().get(ToolingExtensions.readStringExtension(sd, "http://hl7.org/fhir/StructureDefinition/structuredefinition-wg"));
ap.putMetadata("workgroup", wg.getCode());
}
ap.setTitle(sd.getTitle());
new ProfileUtilities(page.getWorkerContext(), null, null).setIds(sd, false);
ap.getProfiles().add(new ConstraintStructure(sd, definitions.getUsageIG(ap.getCategory(), "Parsing " + ap.getSource()), wg == null ? wg(sd) : wg, fmm(sd), sd.getExperimental()));
} else if (ap.getSource() != null) {
parseConformanceDocument(ap, ap.getId(), new CSFile(ap.getSource()), ap.getCategory(), wg);
}
}
Aggregations