Search in sources :

Example 16 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class Execute method createReport.

private ElementImpl createReport(int exitValue, List<String> output, List<String> cmdArgs) {
    context.pushDocumentContext();
    try {
        MemTreeBuilder builder = context.getDocumentBuilder();
        AttributesImpl attribs = new AttributesImpl();
        attribs.addAttribute("", "exitCode", "exitCode", "CDATA", Integer.toString(exitValue));
        builder.startDocument();
        int nodeNr = builder.startElement(RESULT_QNAME, attribs);
        // print command line
        StringBuilder cmdLine = new StringBuilder();
        for (String param : cmdArgs) {
            cmdLine.append(param).append(' ');
        }
        builder.startElement(COMMAND_LINE_QNAME, null);
        builder.characters(cmdLine.toString());
        builder.endElement();
        // print received output to <stdout>
        builder.startElement(STDOUT_QNAME, null);
        for (String line : output) {
            builder.startElement(LINE_QNAME, null);
            builder.characters(line);
            builder.endElement();
        }
        builder.endElement();
        builder.endElement();
        return (ElementImpl) builder.getDocument().getNode(nodeNr);
    } finally {
        context.popDocumentContext();
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder)

Example 17 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class JSON method toxml.

private Sequence toxml(Sequence json, String handleDuplicates, JsonFactory factory) throws XPathException {
    if (json.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    try (final JsonParser parser = factory.createParser(json.itemAt(0).getStringValue())) {
        context.pushDocumentContext();
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        factory.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, false);
        jsonToXml(builder, parser);
        return builder.getDocument() == null ? Sequence.EMPTY_SEQUENCE : builder.getDocument();
    } catch (IOException e) {
        throw new XPathException(this, ErrorCodes.FOJS0001, e.getMessage());
    } catch (XPathException e) {
        e.setLocation(getLine(), getColumn(), getSource());
        throw e;
    } finally {
        context.popDocumentContext();
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 18 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class ParsingFunctions method parse.

private Sequence parse(final String xmlContent, final Sequence[] args) throws XPathException {
    final SAXAdapter adapter = new FragmentSAXAdapter(context, isCalledAs("parse-xml-fragment"));
    final ValidationReport report = validate(xmlContent, adapter);
    if (report.isValid()) {
        return adapter.getDocument();
    } else {
        try {
            context.pushDocumentContext();
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeImpl(org.exist.dom.memtree.NodeImpl) ValidationReport(org.exist.validation.ValidationReport) SAXAdapter(org.exist.dom.memtree.SAXAdapter)

Example 19 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class InspectFunction method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
    final FunctionSignature sig = ref.getSignature();
    try {
        context.pushDocumentContext();
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final int nodeNr = InspectFunctionHelper.generateDocs(sig, null, builder);
        return builder.getDocument().getNode(nodeNr);
    } finally {
        context.popDocumentContext();
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder)

Example 20 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class IntersectTest method createInMemoryDocument.

private Document createInMemoryDocument() {
    final MemTreeBuilder memtree = new MemTreeBuilder();
    memtree.startDocument();
    memtree.startElement(new QName("m1", XMLConstants.NULL_NS_URI), null);
    memtree.startElement(new QName("m2", XMLConstants.NULL_NS_URI), null);
    memtree.characters("test data");
    memtree.endElement();
    memtree.endElement();
    memtree.endDocument();
    return memtree.getDocument();
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName)

Aggregations

MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)81 QName (org.exist.dom.QName)28 XPathException (org.exist.xquery.XPathException)23 Sequence (org.exist.xquery.value.Sequence)19 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)17 SAXException (org.xml.sax.SAXException)16 NodeValue (org.exist.xquery.value.NodeValue)15 IOException (java.io.IOException)14 NodeImpl (org.exist.dom.memtree.NodeImpl)14 Item (org.exist.xquery.value.Item)8 SequenceIterator (org.exist.xquery.value.SequenceIterator)8 Test (org.junit.Test)8 XQueryContext (org.exist.xquery.XQueryContext)7 ValueSequence (org.exist.xquery.value.ValueSequence)7 AttributesImpl (org.xml.sax.helpers.AttributesImpl)7 DocumentImpl (org.exist.dom.memtree.DocumentImpl)6 BrokerPool (org.exist.storage.BrokerPool)6 InputSource (org.xml.sax.InputSource)6 MalformedURLException (java.net.MalformedURLException)5 Path (java.nio.file.Path)5