Search in sources :

Example 1 with FhirFormat

use of org.hl7.fhir.r5.elementmodel.Manager.FhirFormat in project org.hl7.fhir.core by hapifhir.

the class R2R3ConversionManager method convertToR3.

private void convertToR3(InputStream source, OutputStream dest, FhirFormat format) throws FHIRException, IOException {
    org.hl7.fhir.dstu3.elementmodel.Element r2 = new org.hl7.fhir.dstu3.elementmodel.XmlParser(contextR2).parse(source);
    StructureMap map = library.get("http://hl7.org/fhir/StructureMap/" + r2.fhirType() + "2to3");
    if (map == null)
        throw new FHIRException("No Map Found from R2 to R3 for " + r2.fhirType());
    String tn = smu3.getTargetType(map).getType();
    Resource r3 = ResourceFactory.createResource(tn);
    smu3.transform(new TransformContextR2R3(contextR3, r2.getChildValue("id")), r2, map, r3);
    FormatUtilities.makeParser(format).setOutputStyle(style).compose(dest, r3);
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with FhirFormat

use of org.hl7.fhir.r5.elementmodel.Manager.FhirFormat in project org.hl7.fhir.core by hapifhir.

the class R2R3ConversionManager method main.

public static void main(String[] args) throws IOException, FHIRException {
    if (args.length == 0 || !hasParam(args, "-d2") || !hasParam(args, "-d3") || !hasParam(args, "-maps") || !hasParam(args, "-src") || !hasParam(args, "-dest") || (!hasParam(args, "-r2") && !hasParam(args, "-r3"))) {
        System.out.println("R2 <--> R3 Convertor");
        System.out.println("====================");
        System.out.println();
        System.out.println("parameters: -d2 [r2 definitions] -d3 [r3 definitions] -maps [map source] -src [source] -dest [dest] -r2/3 - fmt [format]");
        System.out.println();
        System.out.println("d2: definitions from http://hl7.org/fhir/DSTU2/downloads.html");
        System.out.println("d3: definitions from http://hl7.org/fhir/STU3/downloads.html");
        System.out.println("maps: R2/R3 maps from http://hl7.org/fhir/r2r3maps.zip");
        System.out.println("src: filename for source to convert");
        System.out.println("dest: filename for destination of conversion");
        System.out.println("-r2: source is r2, convert to r3");
        System.out.println("-r3: source is r3, convert to r2");
        System.out.println("-fmt: xml | json (xml is default)");
    } else {
        R2R3ConversionManager self = new R2R3ConversionManager();
        self.setR2Definitions(getNamedParam(args, "-d2"));
        self.setR3Definitions(getNamedParam(args, "-d3"));
        self.setMappingLibrary(getNamedParam(args, "-maps"));
        FhirFormat fmt = hasParam(args, "-fmt") ? getNamedParam(args, "-fmt").equalsIgnoreCase("json") ? FhirFormat.JSON : FhirFormat.XML : FhirFormat.XML;
        InputStream src = new FileInputStream(getNamedParam(args, "-src"));
        OutputStream dst = new FileOutputStream(getNamedParam(args, "-dest"));
        self.convert(src, dst, hasParam(args, "-r2"), fmt);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) FhirFormat(org.hl7.fhir.dstu3.elementmodel.Manager.FhirFormat)

Example 3 with FhirFormat

use of org.hl7.fhir.r5.elementmodel.Manager.FhirFormat in project org.hl7.fhir.core by hapifhir.

the class FormatUtilities method loadFile.

public static Resource loadFile(String path) throws FileNotFoundException, IOException, FHIRException {
    byte[] src = TextFile.fileToBytes(path);
    FhirFormat fmt = determineFormat(src);
    ParserBase parser = makeParser(fmt);
    return parser.parse(src);
}
Also used : FhirFormat(org.hl7.fhir.r5.elementmodel.Manager.FhirFormat)

Example 4 with FhirFormat

use of org.hl7.fhir.r5.elementmodel.Manager.FhirFormat in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method validate.

@Override
public org.hl7.fhir.r5.elementmodel.Element validate(Object appContext, List<ValidationMessage> errors, InputStream stream, FhirFormat format, List<StructureDefinition> profiles) throws FHIRException {
    ParserBase parser = Manager.makeParser(context, format);
    if (parser instanceof XmlParser)
        ((XmlParser) parser).setAllowXsiLocation(allowXsiLocation);
    parser.setupValidation(ValidationPolicy.EVERYTHING, errors);
    long t = System.nanoTime();
    List<NamedElement> list = null;
    try {
        list = parser.parse(stream);
    } catch (IOException e1) {
        throw new FHIRException(e1);
    }
    timeTracker.load(t);
    if (list != null && !list.isEmpty()) {
        String url = parser.getImpliedProfile();
        if (url != null) {
            StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
            if (sd == null) {
                rule(errors, IssueType.NOTFOUND, "Payload", false, "Implied profile " + url + " not known to validator");
            } else {
                profiles.add(sd);
            }
        }
        for (NamedElement ne : list) {
            validate(appContext, errors, ne.getName(), ne.getElement(), profiles);
        }
    }
    // todo: this is broken, but fixing it really complicates things elsewhere, so we do this for now
    return (list == null || list.isEmpty()) ? null : list.get(0).getElement();
}
Also used : XmlParser(org.hl7.fhir.r5.elementmodel.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ParserBase(org.hl7.fhir.r5.elementmodel.ParserBase) IOException(java.io.IOException) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 5 with FhirFormat

use of org.hl7.fhir.r5.elementmodel.Manager.FhirFormat in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method validate.

// testing entry point
public OperationOutcome validate(FhirFormat format, InputStream stream, List<String> profiles) throws FHIRException, IOException, EOperationOutcome {
    List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
    InstanceValidator validator = getValidator(format);
    validator.validate(null, messages, stream, format, asSdList(profiles));
    return ValidatorUtils.messagesToOutcome(messages, context, fhirPathEngine);
}
Also used : InstanceValidator(org.hl7.fhir.validation.instance.InstanceValidator) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Aggregations

InstanceValidator (org.hl7.fhir.validation.instance.InstanceValidator)6 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 Element (org.hl7.fhir.r5.elementmodel.Element)4 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)4 IOException (java.io.IOException)3 FhirFormat (org.hl7.fhir.r5.elementmodel.Manager.FhirFormat)3 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)3 ArrayList (java.util.ArrayList)2 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)2 FhirFormat (org.hl7.fhir.r4.elementmodel.Manager.FhirFormat)2 JsonParser (org.hl7.fhir.r5.formats.JsonParser)2 XmlParser (org.hl7.fhir.r5.formats.XmlParser)2 StructureMapUtilities (org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1