Search in sources :

Example 11 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class XmlParser method parse.

public List<NamedElement> parse(InputStream stream) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
    List<NamedElement> res = new ArrayList<>();
    Document doc = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // xxe protection
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        factory.setXIncludeAware(false);
        factory.setExpandEntityReferences(false);
        factory.setNamespaceAware(true);
        if (policy == ValidationPolicy.EVERYTHING) {
            // if we can, we'll inspect the header/encoding ourselves
            if (stream.markSupported()) {
                stream.mark(1024);
                version = checkHeader(stream);
                stream.reset();
            }
            // use a slower parser that keeps location data
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer nullTransformer = transformerFactory.newTransformer();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            doc = docBuilder.newDocument();
            DOMResult domResult = new DOMResult(doc);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            spf.setNamespaceAware(true);
            spf.setValidating(false);
            // xxe protection
            spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
            spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            SAXParser saxParser = spf.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            // xxe protection
            xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
            xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            XmlLocationAnnotator locationAnnotator = new XmlLocationAnnotator(xmlReader, doc);
            InputSource inputSource = new InputSource(stream);
            SAXSource saxSource = new SAXSource(locationAnnotator, inputSource);
            nullTransformer.transform(saxSource, domResult);
        } else {
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(stream);
        }
    } catch (Exception e) {
        logError(0, 0, "(syntax)", IssueType.INVALID, e.getMessage(), IssueSeverity.FATAL);
        doc = null;
    }
    if (doc != null) {
        Element e = parse(doc);
        if (e != null) {
            res.add(new NamedElement(null, e));
        }
    }
    return res;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) SAXSource(javax.xml.transform.sax.SAXSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlLocationAnnotator(org.hl7.fhir.r5.utils.formats.XmlLocationAnnotator) SAXParser(javax.xml.parsers.SAXParser) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 12 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class VerticalBarParser method parse.

@Override
public List<NamedElement> parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException {
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, "http://hl7.org/fhir/v2/StructureDefinition/Message");
    Element message = new Element("Message", new Property(context, sd.getSnapshot().getElementFirstRep(), sd));
    VerticalBarParserReader reader = new VerticalBarParserReader(new BufferedInputStream(stream), charset);
    preDecode(reader);
    while (// && (getOptions().getSegmentLimit() == 0 || getOptions().getSegmentLimit() > message.getSegments().size()))
    !reader.isFinished()) readSegment(message, reader);
    List<NamedElement> res = new ArrayList<>();
    res.add(new NamedElement(null, message));
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) BufferedInputStream(java.io.BufferedInputStream) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ArrayList(java.util.ArrayList) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)

Example 13 with NamedElement

use of org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement in project org.hl7.fhir.core by hapifhir.

the class ObjectConverter method convert.

public Element convert(Resource ig) throws IOException, FHIRException {
    if (ig == null)
        return null;
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    org.hl7.fhir.r5.formats.JsonParser jp = new org.hl7.fhir.r5.formats.JsonParser();
    jp.compose(bs, ig);
    ByteArrayInputStream bi = new ByteArrayInputStream(bs.toByteArray());
    List<NamedElement> list = new JsonParser(context).parse(bi);
    if (list.size() != 1) {
        throw new FHIRException("Unable to convert because the source contains multieple resources");
    }
    return list.get(0).getElement();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)5 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)4 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)4 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)3 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)3 JsonArray (com.google.gson.JsonArray)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataFormatException (java.util.zip.DataFormatException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 SAXParser (javax.xml.parsers.SAXParser)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2