use of org.hl7.fhir.r4b.model.Element 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.r4b.model.Element 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.r4b.model.Element in project kindling by HL7.
the class SourceParser method divideContainedResources.
private Map<String, Document> divideContainedResources(String rootId, Document doc) throws Exception {
Map<String, Document> res = new HashMap<String, Document>();
List<Element> list = new ArrayList<Element>();
XMLUtil.getNamedChildren(doc.getDocumentElement(), "contained", list);
for (Element e : list) {
Element r = XMLUtil.getFirstChild(e);
String id = XMLUtil.getNamedChildValue(r, "id");
if (Utilities.noString(id))
throw new Exception("Contained Resource has no id");
String nid = rootId + "-" + id;
if (!nid.matches(FormatUtilities.ID_REGEX))
throw new Exception("Contained Resource combination is illegal");
replaceReferences(doc.getDocumentElement(), id, r.getNodeName() + "/" + nid);
XMLUtil.setNamedChildValue(r, "id", nid);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document ndoc = docBuilder.newDocument();
Node newNode = ndoc.importNode(r, true);
ndoc.appendChild(newNode);
res.put(id, ndoc);
doc.getDocumentElement().removeChild(e);
}
return res;
}
use of org.hl7.fhir.r4b.model.Element in project kindling by HL7.
the class SourceParser method loadIgs.
private void loadIgs() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document xdoc = builder.parse(new CSFileInputStream(srcDir + "igs.xml"));
Element root = xdoc.getDocumentElement();
if (root.getNodeName().equals("igs")) {
Element ig = XMLUtil.getFirstChild(root);
while (ig != null) {
if (ig.getNodeName().equals("ig") && (!ig.hasAttribute("local") || isOkLocally(ig.getAttribute("code"))) && !isRuledOutLocally(ig.getAttribute("code"))) {
ImplementationGuideDefn igg = new ImplementationGuideDefn(ig.getAttribute("committee"), ig.getAttribute("code"), ig.getAttribute("name"), ig.getAttribute("brief"), ig.getAttribute("source").replace('\\', File.separatorChar), "1".equals(ig.getAttribute("review")), ig.getAttribute("ballot"), ig.getAttribute("fmm"), ig.getAttribute("section"), "yes".equals(ig.getAttribute("core")), page.getValidationErrors());
definitions.getIgs().put(igg.getCode(), igg);
definitions.getSortedIgs().add(igg);
}
ig = XMLUtil.getNextSibling(ig);
}
}
}
use of org.hl7.fhir.r4b.model.Element in project kindling by HL7.
the class ADLImporter method execute.
private void execute() throws Exception {
// load config
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
adlConfig = builder.parse(new FileInputStream(config)).getDocumentElement();
// load ADL
builder = factory.newDocumentBuilder();
adl = builder.parse(new FileInputStream(source)).getDocumentElement();
check("root", adl.getNamespaceURI(), "http://schemas.openehr.org/v1", "Wrong namespace for ADL XML");
check("root", adl.getNodeName(), "archetype", "Wrong XML for ADL XML");
check("root", XMLUtil.getNamedChild(adl, "adl_version").getTextContent(), "1.4", "unsupported ADL version");
String id = XMLUtil.getFirstChild(XMLUtil.getNamedChild(adl, "archetype_id")).getTextContent().split("\\.")[1];
String baseType = XMLUtil.getNamedChild(XMLUtil.getNamedChild(adl, "definition"), "rm_type_name").getTextContent();
if (!baseType.equals("OBSERVATION"))
return;
// load texts from ontology
List<Element> set = new ArrayList<Element>();
Element ontology = XMLUtil.getNamedChild(adl, "ontology");
Element term_definitions = XMLUtil.getNamedChild(ontology, "term_definitions");
set.clear();
XMLUtil.getNamedChildren(term_definitions, "items", set);
for (Element item : set) {
processTextItem(item);
}
// create structure definition
StructureDefinition sd = new StructureDefinition();
sd.setId("netha-" + id);
sd.setUrl("http://hl7.org/fhir/StructureDefinition/" + sd.getId());
sd.setKind(StructureDefinitionKind.LOGICAL);
populateMetadata(set, sd);
// load data and protocol
Element definition = XMLUtil.getNamedChild(adl, "definition");
NodeTreeEntry root = new NodeTreeEntry();
root.typeName = XMLUtil.getNamedChild(definition, "rm_type_name").getTextContent();
root.atCode = XMLUtil.getNamedChild(definition, "node_id").getTextContent();
root.name = generateToken(root.atCode, true);
sd.setName(root.name);
root.cardinality = readCardinality("root", XMLUtil.getNamedChild(definition, "occurrences"));
set.clear();
XMLUtil.getNamedChildren(definition, "attributes", set);
for (Element item : set) {
// we're actually skipping this level - we don't care about data protocol etc.
// XMLUtil.getNamedChild(XMLUtil.getNamedChild(item, "children"), "attributes");
Element attributes = item;
loadChildren(root.atCode, root, attributes);
}
dumpChildren("", root);
genElements(sd, root.name, root);
// save
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), sd);
System.out.println("done. saved as " + dest);
}
Aggregations