Search in sources :

Example 96 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class ToolsHelper method executeFragments.

public void executeFragments(String[] args) throws IOException {
    try {
        File source = new CSFile(args[1]);
        if (!source.exists())
            throw new FHIRException("Source File \"" + source.getAbsolutePath() + "\" not found");
        XmlPullParser xpp = loadXml(new FileInputStream(source));
        nextNoWhitespace(xpp);
        if (!xpp.getName().equals("tests"))
            throw new FHIRFormatError("Unable to parse file - starts with " + xpp.getName());
        xpp.next();
        nextNoWhitespace(xpp);
        StringBuilder s = new StringBuilder();
        s.append("<results>\r\n");
        int fail = 0;
        while (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("test")) {
            String id = xpp.getAttributeValue(null, "id");
            String type = xpp.getAttributeValue(null, "type");
            // test
            xpp.next();
            nextNoWhitespace(xpp);
            // pre
            xpp.next();
            nextNoWhitespace(xpp);
            XmlParser p = new XmlParser();
            try {
                p.parseFragment(xpp, type);
                s.append("<result id=\"" + id + "\" outcome=\"ok\"/>\r\n");
                nextNoWhitespace(xpp);
            } catch (Exception e) {
                s.append("<result id=\"" + id + "\" outcome=\"error\" msg=\"" + Utilities.escapeXml(e.getMessage()) + "\"/>\r\n");
                fail++;
            }
            while (xpp.getEventType() != XmlPullParser.END_TAG || !xpp.getName().equals("pre")) xpp.next();
            xpp.next();
            nextNoWhitespace(xpp);
            xpp.next();
            nextNoWhitespace(xpp);
        }
        s.append("</results>\r\n");
        TextFile.stringToFile(s.toString(), args[2]);
    } catch (Exception e) {
        e.printStackTrace();
        TextFile.stringToFile(e.getMessage(), args[2]);
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) CSFile(org.hl7.fhir.utilities.CSFile) 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) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 97 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class LoincToDEConvertor method saveBundle.

private void saveBundle() throws FHIRFormatError, IOException, XmlPullParserException {
    XmlParser xml = new XmlParser();
    FileOutputStream s = new FileOutputStream(dest);
    xml.compose(s, bundle, true);
    s.close();
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream)

Example 98 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class ResourceTests method testSchemaLocation.

@Test
void testSchemaLocation() throws IOException {
    XmlParser xml = new XmlParser();
    xml.setSchemaPath("http://test.org");
    xml.setOutputStyle(OutputStyle.NORMAL);
    Resource res = xml.parse(SRC);
    String output = xml.composeString(res);
    assertEquals(TGT, output);
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) Test(org.junit.jupiter.api.Test)

Example 99 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class SnapShotGenerationTests method testGen.

private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
    if (!Utilities.noString(test.register)) {
        List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
        ProfileUtilities pu = new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), messages, null);
        pu.setNewSlicingProcessing(true);
        for (StructureDefinition sd : test.included) {
            pu.setIds(sd, false);
        }
        for (StructureDefinition sd : test.included) {
            if (!TestingUtilities.getSharedWorkerContext().hasResource(StructureDefinition.class, sd.getUrl())) {
                TestingUtilities.getSharedWorkerContext().cacheResource(sd);
            }
        }
        StructureDefinition base = TestingUtilities.getSharedWorkerContext().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
        if (base != null) {
            pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
        }
        int ec = 0;
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                System.out.println(vm.summary());
                ec++;
            }
        }
        if (ec > 0)
            throw new FHIRException("register gen failed: " + messages.toString());
    }
    StructureDefinition base = getSD(test.getSource().getBaseDefinition(), context);
    if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
        throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
    StructureDefinition output = test.getSource().copy();
    ProfileUtilities pu = new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), messages, new TestPKP());
    pu.setNewSlicingProcessing(test.isNewSliceProcessing());
    pu.setThrowException(false);
    pu.setDebug(test.isDebug());
    pu.setIds(test.getSource(), false);
    if (!TestingUtilities.getSharedWorkerContext().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
        NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
        TestingUtilities.getSharedWorkerContext().loadFromPackage(npm, new TestPackageLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
    }
    pu.setXver(new XVerExtensionManager(TestingUtilities.getSharedWorkerContext()));
    if (test.isSort()) {
        List<String> errors = new ArrayList<String>();
        int lastCount = output.getDifferential().getElement().size();
        pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
        if (errors.size() > 0)
            throw new FHIRException("Sort failed: " + errors.toString());
    }
    try {
        messages.clear();
        pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
        List<ValidationMessage> ml = new ArrayList<>();
        for (ValidationMessage vm : messages) {
            if (vm.getLevel() == IssueSeverity.ERROR) {
                ml.add(vm);
            }
        }
        if (ml.size() > 0) {
            throw new FHIRException("Snapshot Generation failed: " + ml.toString());
        }
    } catch (Throwable e) {
        System.out.println("\r\nException: " + e.getMessage());
        throw e;
    }
    if (output.getDifferential().hasElement()) {
        RenderingContext rc = new RenderingContext(TestingUtilities.getSharedWorkerContext(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
        rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
        rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), null, new TestPKP()));
        RendererFactory.factory(output, rc).render(output);
    }
    if (!fail) {
        test.output = output;
        TestingUtilities.getSharedWorkerContext().cacheResource(output);
        File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
        if (dst.exists())
            dst.delete();
        IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("snapshot", test.getId() + "-actual.xml")), output);
        StructureDefinition t1 = test.expected.copy();
        t1.setText(null);
        StructureDefinition t2 = test.output.copy();
        t2.setText(null);
        Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
    }
}
Also used : FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TestPackageLoader(org.hl7.fhir.r5.test.utils.TestPackageLoader) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) XVerExtensionManager(org.hl7.fhir.r5.utils.XVerExtensionManager) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 100 with XmlParser

use of org.hl7.fhir.r4b.formats.XmlParser in project org.hl7.fhir.core by hapifhir.

the class VocabTests method test.

@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
    Resource source;
    if (test.getSource().endsWith(".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getSource()));
    }
    Resource target;
    if (test.getTarget().endsWith(".json")) {
        target = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
    } else {
        target = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "vocab", test.getTarget()));
    }
    if (test.getType() == TestType.Expansion) {
        testExpansion(test, (ValueSet) source, (ValueSet) target);
    } else {
        Assertions.fail("not done yet");
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

FileOutputStream (java.io.FileOutputStream)130 XmlParser (org.hl7.fhir.r5.formats.XmlParser)97 FHIRException (org.hl7.fhir.exceptions.FHIRException)84 FileInputStream (java.io.FileInputStream)77 File (java.io.File)64 IOException (java.io.IOException)59 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)51 CSFile (org.hl7.fhir.utilities.CSFile)48 JsonParser (org.hl7.fhir.r5.formats.JsonParser)45 ArrayList (java.util.ArrayList)35 XmlParser (org.hl7.fhir.dstu3.formats.XmlParser)35 TextFile (org.hl7.fhir.utilities.TextFile)33 XmlParser (org.hl7.fhir.r4.formats.XmlParser)31 FileNotFoundException (java.io.FileNotFoundException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Resource (org.hl7.fhir.r5.model.Resource)24 XmlParser (org.hl7.fhir.r4b.formats.XmlParser)23 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)19 NotImplementedException (org.apache.commons.lang3.NotImplementedException)19