Search in sources :

Example 6 with TypeParser

use of org.hl7.fhir.definitions.parsers.TypeParser in project kindling by HL7.

the class PageProcessor method tsForDt.

private String tsForDt(String dt) throws Exception {
    File tmp = Utilities.createTempFile("tmp", ".tmp");
    tmp.deleteOnExit();
    TerminologyNotesGenerator gen = new TerminologyNotesGenerator(new FileOutputStream(tmp), this);
    TypeParser tp = new TypeParser(version.toCode());
    TypeRef t = tp.parse(dt, false, null, workerContext, true).get(0);
    ElementDefn e = definitions.getElementDefn(t.getName());
    if (e == null) {
        gen.close();
        throw new Exception("unable to find definition for " + dt);
    } else {
        gen.generate("", e);
        gen.close();
    }
    String val = TextFile.fileToString(tmp.getAbsolutePath()) + "\r\n";
    tmp.delete();
    return val;
}
Also used : ITypeParser(org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) TerminologyNotesGenerator(org.hl7.fhir.definitions.generators.specification.TerminologyNotesGenerator) TypeRef(org.hl7.fhir.definitions.model.TypeRef) FileOutputStream(java.io.FileOutputStream) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 7 with TypeParser

use of org.hl7.fhir.definitions.parsers.TypeParser in project kindling by HL7.

the class PageProcessor method xmlForDt.

private String xmlForDt(String dt, String pn) throws Exception {
    File tmp = Utilities.createTempFile("tmp", ".tmp");
    XmlSpecGenerator gen = new XmlSpecGenerator(new FileOutputStream(tmp), pn == null ? null : pn.substring(0, pn.indexOf(".")) + "-definitions.html", null, this, "");
    TypeParser tp = new TypeParser(version.toCode());
    TypeRef t = tp.parse(dt, false, null, workerContext, true).get(0);
    ElementDefn e = definitions.getElementDefn(t.getName());
    if (e == null) {
        gen.close();
        throw new Exception("unable to find definition for " + dt);
    } else {
        gen.generate(e, e.getName().equals("Element") || e.getName().equals("BackboneElement"), false);
        gen.close();
    }
    String val = TextFile.fileToString(tmp.getAbsolutePath()) + "\r\n";
    tmp.delete();
    return val;
}
Also used : XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) ITypeParser(org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) TypeRef(org.hl7.fhir.definitions.model.TypeRef) FileOutputStream(java.io.FileOutputStream) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 8 with TypeParser

use of org.hl7.fhir.definitions.parsers.TypeParser in project kindling by HL7.

the class PageProcessor method ttlForDt.

private String ttlForDt(String dt, String pn) throws Exception {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    TurtleSpecGenerator gen = new TurtleSpecGenerator(b, pn == null ? null : pn.substring(0, pn.indexOf(".")) + "-definitions.html", null, this, "", version.toCode());
    TypeParser tp = new TypeParser(version.toCode());
    TypeRef t = tp.parse(dt, false, null, workerContext, true).get(0);
    ElementDefn e = definitions.getElementDefn(t.getName());
    if (e == null) {
        gen.close();
        throw new Exception("unable to find definition for " + dt);
    } else {
        gen.generate(e, false);
        gen.close();
    }
    String val = new String(b.toByteArray()) + "\r\n";
    return val;
}
Also used : ITypeParser(org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) TypeRef(org.hl7.fhir.definitions.model.TypeRef) TurtleSpecGenerator(org.hl7.fhir.definitions.generators.specification.TurtleSpecGenerator) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 9 with TypeParser

use of org.hl7.fhir.definitions.parsers.TypeParser in project kindling by HL7.

the class PageProcessor method jsonForDt.

private String jsonForDt(String dt, String pn) throws Exception {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    JsonSpecGenerator gen = new JsonSpecGenerator(b, pn == null ? null : pn.substring(0, pn.indexOf(".")) + "-definitions.html", null, this, "", version.toCode());
    TypeParser tp = new TypeParser(version.toCode());
    TypeRef t = tp.parse(dt, false, null, workerContext, true).get(0);
    ElementDefn e = definitions.getElementDefn(t.getName());
    if (e == null) {
        gen.close();
        throw new Exception("unable to find definition for " + dt);
    } else {
        gen.generate(e, false, false);
        gen.close();
    }
    String val = new String(b.toByteArray()) + "\r\n";
    return val;
}
Also used : JsonSpecGenerator(org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator) ITypeParser(org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) TypeRef(org.hl7.fhir.definitions.model.TypeRef) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UcumException(org.fhir.ucum.UcumException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) URISyntaxException(java.net.URISyntaxException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FileNotFoundException(java.io.FileNotFoundException) NotImplementedException(org.apache.commons.lang3.NotImplementedException)

Example 10 with TypeParser

use of org.hl7.fhir.definitions.parsers.TypeParser in project kindling by HL7.

the class OldSpreadsheetParser method processLine.

private ElementDefn processLine(ResourceDefn root, Sheet sheet, int row, Map<String, Invariant> invariants, boolean profile, Profile pack, boolean firstTime) throws Exception {
    ElementDefn e;
    String path = sheet.getColumn(row, "Element");
    if (path.startsWith("!"))
        return null;
    if (Utilities.noString(path))
        throw new Exception("Error reading definitions - no path found @ " + getLocation(row));
    if (path.contains("#"))
        throw new Exception("Old path style @ " + getLocation(row));
    String profileName = isProfile ? sheet.getColumn(row, "Profile Name") : "";
    String discriminator = isProfile ? sheet.getColumn(row, "Discriminator") : "";
    boolean isRoot = !path.contains(".");
    if (isRoot) {
        if (root.getRoot() != null)
            throw new Exception("Definitions in " + getLocation(row) + " contain two roots: " + path + " in " + root.getName());
        root.setName(path);
        e = new TypeDefn();
        e.setName(path);
        root.setRoot((TypeDefn) e);
        if (template != null)
            e.copyFrom(template.getRoot(), root.getName(), templateTitle);
    } else {
        e = makeFromPath(root.getRoot(), path, row, profileName, true);
        if (template != null) {
            ElementDefn ted = getTemplateDefinition(path);
            if (ted != null) {
                e.copyFrom(ted, root.getName(), templateTitle);
            }
        }
    }
    e.setStandardsStatus(StandardsStatus.fromCode(sheet.getColumn(row, "Standards-Status")));
    e.setNormativeVersion(sheet.getColumn(row, "Normative-Version"));
    if (e.getName().startsWith("@")) {
        e.setName(e.getName().substring(1));
        e.setXmlAttribute(true);
    }
    String c = sheet.getColumn(row, "Card.");
    if (c == null || c.equals("") || c.startsWith("!")) {
        if (!isRoot && !profile && (template == null))
            throw new Exception("Missing cardinality at " + getLocation(row) + " on " + path);
        if (isRoot && (template == null)) {
            e.setMinCardinality(0);
            e.setMaxCardinality(Integer.MAX_VALUE);
        }
    } else {
        String[] card = c.split("\\.\\.");
        if (card.length != 2 || !Utilities.isInteger(card[0]) || (!"*".equals(card[1]) && !Utilities.isInteger(card[1])))
            throw new Exception("Unable to parse Cardinality '" + c + "' " + c + " in " + getLocation(row) + " on " + path);
        e.setMinCardinality(Integer.parseInt(card[0]));
        e.setMaxCardinality("*".equals(card[1]) ? Integer.MAX_VALUE : Integer.parseInt(card[1]));
    }
    if (profileName.startsWith("#"))
        throw new Exception("blah: " + profileName);
    e.setProfileName(profileName);
    e.setSliceDescription(isProfile ? sheet.getColumn(row, "Slice Description") : "");
    for (String d : discriminator.split("\\,")) if (!Utilities.noString(d))
        e.getDiscriminator().add(d);
    doAliases(sheet, row, e);
    if (sheet.hasColumn(row, "Must Understand"))
        throw new Exception("Column 'Must Understand' has been renamed to 'Is Modifier'");
    if (sheet.hasColumn(row, "Is Modifier")) {
        e.setIsModifier(parseBoolean(sheet.getColumn(row, "Is Modifier"), row, null));
        String reason = sheet.getColumn(row, "Modifier Reason");
        if (Utilities.noString(reason) && e.getName().toLowerCase().contains("status") && sheet.getColumn(row, "Short Name").contains("error"))
            reason = "This element is labelled as a modifier because it is a status element that contains status entered-in-error which means that the resource should not be treated as valid";
        if (Utilities.noString(reason) && e.getName().equals("active"))
            reason = "This element is labelled as a modifier because it is a status element that can indicate that a record should not be treated as valid";
        if (Utilities.noString(reason)) {
            System.out.println("Missing IsModifierReason on " + path);
            reason = "Not known why this is labelled a modifier";
        }
        e.setModifierReason(reason);
    }
    if (isProfile) {
        // later, this will get hooked in from the underlying definitions, but we need to know this now to validate the extension modifier matching
        if (e.getName().equals("modifierExtension"))
            e.setIsModifier(true);
        e.setMustSupport(parseBoolean(sheet.getColumn(row, "Must Support"), row, null));
    }
    if (sheet.hasColumn(row, "Summary"))
        e.setSummaryItem(parseBoolean(sheet.getColumn(row, "Summary"), row, null));
    e.setRegex(sheet.getColumn(row, "Regex"));
    String uml = sheet.getColumn(row, "UML");
    if (uml != null) {
        if (uml.contains(";")) {
            String[] parts = uml.split("\\;");
            e.setSvgLeft(Integer.parseInt(parts[0]));
            e.setSvgTop(Integer.parseInt(parts[1]));
            if (parts.length > 2)
                e.setSvgWidth(Integer.parseInt(parts[2]));
            e.setUmlDir("");
        } else if (uml.startsWith("break:")) {
            e.setUmlBreak(true);
            e.setUmlDir(uml.substring(6));
        } else {
            e.setUmlDir(uml);
        }
    }
    String s = sheet.getColumn(row, "Condition");
    if (s != null && !s.equals(""))
        throw new Exception("Found Condition in spreadsheet " + getLocation(row));
    s = sheet.getColumn(row, "Inv.");
    if (s != null && !s.equals("")) {
        for (String sn : s.split(",")) {
            if (!sn.startsWith("!")) {
                Invariant inv = invariants.get(sn);
                if (inv == null)
                    throw new Exception("unable to find Invariant '" + sn + "' " + getLocation(row));
                e.getStatedInvariants().add(inv);
            }
        }
    }
    TypeParser tp = new TypeParser(version.toCode());
    e.getTypes().addAll(tp.parse(sheet.getColumn(row, "Type"), isProfile, profileExtensionBase, context, !path.contains("."), this.name));
    if (isRoot && e.getTypes().size() == 1 && definitions != null) {
        String t = e.getTypes().get(0).getName();
        if (definitions.getResourceTemplates().containsKey(t)) {
            // we've got a template in play.
            template = definitions.getResourceTemplates().get(t);
            templateTitle = Utilities.unCamelCase(e.getName());
            e.getTypes().get(0).setName(template.getRoot().getTypes().get(0).getName());
        } else if (definitions.getBaseResources().containsKey(t) && definitions.getBaseResources().get(t).isInterface()) {
            // we've got a template in play.
            template = definitions.getBaseResources().get(t);
            templateTitle = Utilities.unCamelCase(e.getName());
        }
    }
    if (isProfile && ((path.endsWith(".extension") || path.endsWith(".modifierExtension")) && (e.getTypes().size() == 1) && e.getTypes().get(0).hasProfile()) && Utilities.noString(profileName))
        throw new Exception("need to have a profile name if a profiled extension is referenced for " + e.getTypes().get(0).getProfile());
    if (sheet.hasColumn(row, "Concept Domain"))
        throw new Exception("Column 'Concept Domain' has been retired in " + path);
    String bindingName = sheet.getColumn(row, "Binding");
    if (!Utilities.noString(bindingName)) {
        BindingSpecification binding = bindings.get(bindingName);
        if (binding == null && definitions != null)
            binding = definitions.getCommonBindings().get(bindingName);
        if (binding == null) {
            if (bindingName.startsWith("!"))
                e.setNoBindingAllowed(true);
            else
                throw new Exception("Binding name " + bindingName + " could not be resolved in local spreadsheet");
        }
        e.setBinding(binding);
        if (binding != null && !binding.getUseContexts().contains(name))
            binding.getUseContexts().add(name);
    } else if (e.getBinding() != null) {
        if (!e.getBinding().getUseContexts().contains(name))
            e.getBinding().getUseContexts().add(name);
    }
    if (!Utilities.noString(sheet.getColumn(row, "Short Label")))
        throw new Exception("Short Label is no longer used");
    if (// todo: make this a warning when a fair chunk of the spreadsheets have been converted
    sheet.hasColumn(row, "Short Name"))
        if (sheet.getColumn(row, "Short Name").startsWith("&"))
            e.setShortDefn(e.getShortDefn() + sheet.getColumn(row, "Short Name").substring(1));
        else
            e.setShortDefn(sheet.getColumn(row, "Short Name"));
    if (!isProfile && e.getShortDefn() == null)
        throw new Exception("A short definition is required for " + e.getName() + " at " + getLocation(row));
    if (sheet.hasColumn(row, "Definition"))
        if (sheet.getColumn(row, "Definition").startsWith("&"))
            e.setDefinition(Utilities.appendPeriod(e.getDefinition() + processDefinition(sheet.getColumn(row, "Definition")).substring(1)));
        else
            e.setDefinition(Utilities.appendPeriod(processDefinition(sheet.getColumn(row, "Definition"))));
    if (isRoot) {
        root.setDefinition(e.getDefinition());
    }
    if (isProfile || isLogicalModel)
        e.setMaxLength(sheet.getColumn(row, "Max Length"));
    if (sheet.hasColumn(row, "Requirements"))
        if (sheet.getColumn(row, "Requirements").startsWith("&"))
            e.setRequirements(Utilities.appendPeriod(e.getRequirements() + sheet.getColumn(row, "Requirements").substring(1)));
        else
            e.setRequirements(Utilities.appendPeriod(sheet.getColumn(row, "Requirements")));
    if (sheet.hasColumn(row, "Comments"))
        if (sheet.getColumn(row, "Comments").startsWith("&"))
            e.setComments(Utilities.appendPeriod(e.getComments() + Utilities.appendPeriod(sheet.getColumn(row, "Comments").substring(1))));
        else
            e.setComments(Utilities.appendPeriod(Utilities.appendPeriod(sheet.getColumn(row, "Comments"))));
    for (String n : mappings.keySet()) {
        String ms = sheet.getColumn(row, mappings.get(n).getColumnName());
        if (mappings.get(n).getColumnName().equals("Snomed Code") && !Utilities.noString(ms))
            System.out.println("!!");
        e.addMapping(n, ms.trim());
    }
    if (pack != null) {
        for (String n : pack.getMappingSpaces().keySet()) {
            e.addMapping(n, sheet.getColumn(row, pack.getMappingSpaces().get(n).getColumnName()).trim());
        }
    }
    if (sheet.hasColumn("Hierarchy"))
        e.setHierarchy(parseBoolean(sheet.getColumn(row, "Hierarchy"), row, null));
    if (sheet.hasColumn(row, "To Do"))
        e.setTodo(Utilities.appendPeriod(sheet.getColumn(row, "To Do")));
    if (sheet.hasColumn(row, "Example"))
        e.setExample(processValue(sheet, row, "Example", sheet.getColumn(row, "Example"), e));
    processOtherExamples(e, sheet, row);
    if (sheet.hasColumn(row, "Committee Notes"))
        e.setCommitteeNotes(Utilities.appendPeriod(sheet.getColumn(row, "Committee Notes")));
    if (sheet.hasColumn(row, "Display Hint"))
        e.setDisplayHint(sheet.getColumn(row, "Display Hint"));
    if (isProfile) {
        e.setFixed(processValue(sheet, row, "Value", sheet.getColumn(row, "Value"), e));
        e.setPattern(processValue(sheet, row, "Pattern", sheet.getColumn(row, "Pattern"), e));
    } else {
        if (sheet.hasColumn(row, "Default Value"))
            errors.add(path + ": Default value '" + sheet.getColumn(row, "Default Value") + "' found @ " + getLocation(row));
        if (sheet.hasColumn(row, "Missing Meaning"))
            e.setMeaningWhenMissing(sheet.getColumn(row, "Missing Meaning"));
    }
    if (sheet.hasColumn(row, "w5"))
        e.setW5(checkW5(sheet.getColumn(row, "w5"), path));
    if (sheet.hasColumn(row, "Translatable"))
        e.setTranslatable(parseBoolean(sheet.getColumn(row, "Translatable"), row, false));
    if (sheet.hasColumn(row, "Order Meaning"))
        e.setOrderMeaning(sheet.getColumn(row, "Order Meaning"));
    return e;
}
Also used : TypeDefn(org.hl7.fhir.definitions.model.TypeDefn) Invariant(org.hl7.fhir.definitions.model.Invariant) TypeParser(org.hl7.fhir.definitions.parsers.TypeParser) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) BindingSpecification(org.hl7.fhir.definitions.model.BindingSpecification) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

TypeRef (org.hl7.fhir.definitions.model.TypeRef)10 TypeParser (org.hl7.fhir.definitions.parsers.TypeParser)9 FHIRException (org.hl7.fhir.exceptions.FHIRException)9 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 URISyntaxException (java.net.URISyntaxException)6 File (java.io.File)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 TransformerException (javax.xml.transform.TransformerException)5 NotImplementedException (org.apache.commons.lang3.NotImplementedException)5 UcumException (org.fhir.ucum.UcumException)5 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)5 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)5 ITypeParser (org.hl7.fhir.r5.renderers.utils.RenderingContext.ITypeParser)5 CSFile (org.hl7.fhir.utilities.CSFile)5 IniFile (org.hl7.fhir.utilities.IniFile)5 TextFile (org.hl7.fhir.utilities.TextFile)5 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2