Search in sources :

Example 96 with Source

use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method executeDependency.

private void executeDependency(String indent, TransformContext context, StructureMap map, Variables vin, StructureMapGroupComponent group, StructureMapGroupRuleDependentComponent dependent) throws FHIRException {
    ResolvedGroup rg = resolveGroupReference(map, group, dependent.getName());
    if (rg.target.getInput().size() != dependent.getVariable().size()) {
        throw new FHIRException("Rule '" + dependent.getName() + "' has " + Integer.toString(rg.target.getInput().size()) + " but the invocation has " + Integer.toString(dependent.getVariable().size()) + " variables");
    }
    Variables v = new Variables();
    for (int i = 0; i < rg.target.getInput().size(); i++) {
        StructureMapGroupInputComponent input = rg.target.getInput().get(i);
        StringType rdp = dependent.getVariable().get(i);
        String var = rdp.asStringValue();
        VariableMode mode = input.getMode() == StructureMapInputMode.SOURCE ? VariableMode.INPUT : VariableMode.OUTPUT;
        Base vv = vin.get(mode, var);
        if (// * once source, always source. but target can be treated as source at user convenient
        vv == null && mode == VariableMode.INPUT)
            vv = vin.get(VariableMode.OUTPUT, var);
        if (vv == null)
            throw new FHIRException("Rule '" + dependent.getName() + "' " + mode.toString() + " variable '" + input.getName() + "' named as '" + var + "' has no value");
        v.add(mode, input.getName(), vv);
    }
    executeGroup(indent + "  ", context, rg.targetMap, v, rg.target);
}
Also used : StringType(org.hl7.fhir.dstu3.model.StringType) StructureMapGroupInputComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupInputComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) ContactPoint(org.hl7.fhir.dstu3.model.ContactPoint) Base(org.hl7.fhir.dstu3.model.Base)

Example 97 with Source

use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseConceptMap.

private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
    lexer.token("conceptmap");
    ConceptMap map = new ConceptMap();
    String id = lexer.readConstant("map id");
    if (!id.startsWith("#"))
        lexer.error("Concept Map identifier must start with #");
    map.setId(id);
    // todo: how to add this to the text format
    map.setStatus(PublicationStatus.DRAFT);
    result.getContained().add(map);
    lexer.token("{");
    lexer.skipComments();
    // lexer.token("source");
    // map.setSource(new UriType(lexer.readConstant("source")));
    // lexer.token("target");
    // map.setSource(new UriType(lexer.readConstant("target")));
    Map<String, String> prefixes = new HashMap<String, String>();
    while (lexer.hasToken("prefix")) {
        lexer.token("prefix");
        String n = lexer.take();
        lexer.token("=");
        String v = lexer.readConstant("prefix url");
        prefixes.put(n, v);
    }
    while (lexer.hasToken("unmapped")) {
        lexer.token("unmapped");
        lexer.token("for");
        String n = readPrefix(prefixes, lexer);
        ConceptMapGroupComponent g = getGroup(map, n, null);
        lexer.token("=");
        String v = lexer.take();
        if (v.equals("provided")) {
            g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
        } else
            lexer.error("Only unmapped mode PROVIDED is supported at this time");
    }
    while (!lexer.hasToken("}")) {
        String srcs = readPrefix(prefixes, lexer);
        lexer.token(":");
        String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
        ConceptMapEquivalence eq = readEquivalence(lexer);
        String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
        ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
        SourceElementComponent e = g.addElement();
        e.setCode(sc);
        if (e.getCode().startsWith("\""))
            e.setCode(lexer.processConstant(e.getCode()));
        TargetElementComponent tgt = e.addTarget();
        if (eq != ConceptMapEquivalence.EQUIVALENT)
            tgt.setEquivalence(eq);
        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
            lexer.token(":");
            tgt.setCode(lexer.take());
            if (tgt.getCode().startsWith("\""))
                tgt.setCode(lexer.processConstant(tgt.getCode()));
        }
        if (lexer.hasComment())
            tgt.setComment(lexer.take().substring(2).trim());
    }
    lexer.token("}");
}
Also used : HashMap(java.util.HashMap) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) ConceptMapEquivalence(org.hl7.fhir.dstu3.model.Enumerations.ConceptMapEquivalence) ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Example 98 with Source

use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.

the class ToolsHelper method executeRoundTrip.

public void executeRoundTrip(String[] args) throws IOException, FHIRException {
    FileInputStream in;
    File source = new CSFile(args[1]);
    File dest = new CSFile(args[2]);
    if (args.length >= 4) {
        Utilities.copyFile(args[1], args[3]);
    }
    if (!source.exists())
        throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
    in = new CSFileInputStream(source);
    XmlParser p = new XmlParser();
    JsonParser parser = new JsonParser();
    JsonParser pj = parser;
    Resource rf = p.parse(in);
    ByteArrayOutputStream json = new ByteArrayOutputStream();
    parser.setOutputStyle(OutputStyle.PRETTY);
    parser.compose(json, rf);
    json.close();
    TextFile.stringToFile(new String(json.toByteArray()), Utilities.changeFileExt(dest.getAbsolutePath(), ".json"));
    rf = pj.parse(new ByteArrayInputStream(json.toByteArray()));
    FileOutputStream s = new FileOutputStream(dest);
    new XmlParser().compose(s, rf, true);
    s.close();
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 99 with Source

use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.

the class ToolsHelper method testRoundTrip.

public void testRoundTrip(String rootDir, String tmpDir, Collection<String> names) throws Throwable {
    try {
        System.err.println("Round trip from " + rootDir + " to " + tmpDir + ":" + Integer.toString(names.size()) + " files");
        for (String n : names) {
            System.err.print("  " + n);
            String source = rootDir + n + ".xml";
            // String tmpJson = tmpDir + n + ".json";
            String tmp = tmpDir + n.replace(File.separator, "-") + ".tmp";
            String dest = tmpDir + n.replace(File.separator, "-") + ".java.xml";
            FileInputStream in = new FileInputStream(source);
            XmlParser xp = new XmlParser();
            Resource r = xp.parse(in);
            System.err.print(".");
            JsonParser jp = new JsonParser();
            FileOutputStream out = new FileOutputStream(tmp);
            jp.setOutputStyle(OutputStyle.PRETTY);
            jp.compose(out, r);
            out.close();
            r = null;
            System.err.print(".");
            in = new FileInputStream(tmp);
            System.err.print(",");
            r = jp.parse(in);
            System.err.print(".");
            out = new FileOutputStream(dest);
            new XmlParser().compose(out, r, true);
            System.err.println("!");
            out.close();
            r = null;
            System.gc();
        }
    } catch (Throwable e) {
        System.err.println("Error: " + e.getMessage());
        throw e;
    }
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser)

Example 100 with Source

use of org.hl7.fhir.utilities.validation.ValidationMessage.Source in project org.hl7.fhir.core by hapifhir.

the class ToolsHelper method executeJson.

public String executeJson(String[] args) throws IOException, FHIRException {
    FileInputStream in;
    File source = new CSFile(args[1]);
    File dest = new CSFile(args[2]);
    File destc = new CSFile(Utilities.changeFileExt(args[2], ".canonical.json"));
    File destt = new CSFile(args[2] + ".tmp");
    File destr = new CSFile(Utilities.changeFileExt(args[2], ".ttl"));
    if (!source.exists())
        throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
    in = new CSFileInputStream(source);
    XmlParser p = new XmlParser();
    Resource rf = p.parse(in);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    FileOutputStream s = new FileOutputStream(dest);
    json.compose(s, rf);
    s.close();
    json.setOutputStyle(OutputStyle.CANONICAL);
    s = new FileOutputStream(destc);
    json.compose(s, rf);
    s.close();
    json.setSuppressXhtml("Snipped for Brevity");
    json.setOutputStyle(OutputStyle.PRETTY);
    s = new FileOutputStream(destt);
    json.compose(s, rf);
    s.close();
    RdfParserBase rdf = new RdfParser();
    s = new FileOutputStream(destr);
    rdf.compose(s, rf);
    s.close();
    return TextFile.fileToString(destt.getAbsolutePath());
}
Also used : XmlParser(org.hl7.fhir.dstu3.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.dstu3.model.Resource) CSFile(org.hl7.fhir.utilities.CSFile) RdfParserBase(org.hl7.fhir.dstu3.formats.RdfParserBase) CSFile(org.hl7.fhir.utilities.CSFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileInputStream(java.io.FileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.dstu3.formats.JsonParser) RdfParser(org.hl7.fhir.dstu3.formats.RdfParser)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)125 FileInputStream (java.io.FileInputStream)59 FileOutputStream (java.io.FileOutputStream)55 IOException (java.io.IOException)55 ArrayList (java.util.ArrayList)48 File (java.io.File)45 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)45 TextFile (org.hl7.fhir.utilities.TextFile)41 CSFile (org.hl7.fhir.utilities.CSFile)35 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)35 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)30 XmlParser (org.hl7.fhir.r5.formats.XmlParser)28 Date (java.util.Date)27 HashMap (java.util.HashMap)26 Reference (org.hl7.fhir.r4.model.Reference)26 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)24 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)24 Coding (org.hl7.fhir.r4.model.Coding)24 JsonObject (com.google.gson.JsonObject)22 NotImplementedException (org.apache.commons.lang3.NotImplementedException)22