Search in sources :

Example 6 with XQuerySerializer

use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.

the class ExtTestFailureFunction method seqToString.

private String seqToString(final Sequence seq) throws IOException, XPathException, SAXException {
    try (final StringWriter writer = new StringWriter()) {
        final XQuerySerializer xquerySerializer = new XQuerySerializer(context.getBroker(), new Properties(), writer);
        xquerySerializer.serialize(seq);
        return writer.toString();
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) StringWriter(java.io.StringWriter) Properties(java.util.Properties)

Example 7 with XQuerySerializer

use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.

the class ForwardReferenceTest method test1.

@Test
public void test1() throws EXistException, PermissionDeniedException, IOException, TriggerException, XPathException {
    final StringSource testXquerySource = new StringSource("xquery version \"3.1\";\n" + "\n" + "import module namespace xqsuite = \"http://exist-db.org/xquery/xqsuite\"\n" + "    at \"resource:org/exist/xquery/lib/xqsuite/xqsuite.xql\";\n" + "\n" + "xqsuite:suite((\n" + "    inspect:module-functions(xs:anyURI(\"xmldb:exist://" + TEST_PAGES_MODULE_URI + "\"))\n" + "))");
    final BrokerPool brokerPool = EXIST_EMBEDDED_SERVER.getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        final String xqSuiteXmlResult = withCompiledQuery(broker, testXquerySource, compiledQuery -> {
            final Sequence result = executeQuery(broker, compiledQuery);
            try (final StringWriter writer = new StringWriter()) {
                final XQuerySerializer xquerySerializer = new XQuerySerializer(broker, new Properties(), writer);
                xquerySerializer.serialize(result);
                return writer.toString();
            } catch (final IOException | SAXException e) {
                throw new XPathException(e);
            }
        });
        assertNotNull(xqSuiteXmlResult);
        transaction.commit();
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) StringSource(org.exist.source.StringSource) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 8 with XQuerySerializer

use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.

the class FunSerialize method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final Properties outputProperties;
    if (getArgumentCount() == 2 && !args[1].isEmpty()) {
        outputProperties = getSerializationProperties(this, args[1].itemAt(0));
    } else {
        outputProperties = new Properties();
    }
    try (final StringWriter writer = new StringWriter()) {
        final XQuerySerializer xqSerializer = new XQuerySerializer(context.getBroker(), outputProperties, writer);
        Sequence seq = args[0];
        if (xqSerializer.normalize()) {
            seq = normalize(this, context, seq);
        }
        xqSerializer.serialize(seq);
        return new StringValue(writer.toString());
    } catch (final IOException | SAXException e) {
        throw new XPathException(this, FnModule.SENR0001, e.getMessage());
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException)

Example 9 with XQuerySerializer

use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.

the class FunTrace method eval.

/*
 * (non-Javadoc)
 * @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
 */
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    // Get value for label, default to "-"
    final String label = (args.length == 2) ? StringUtils.defaultIfBlank(args[1].getStringValue(), "-") : "-";
    final Sequence result;
    if (args[0].isEmpty()) {
        // Write to log
        LOG.debug("{} [{}] [{}]: {}", label, "-", Type.getTypeName(Type.EMPTY), "-");
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        // Copy all Items from input to output sequence
        result = new ValueSequence();
        int position = 0;
        // Force adaptive serialization
        final Properties props = new Properties();
        props.setProperty(OutputKeys.METHOD, "adaptive");
        for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            // Get item
            final Item next = i.nextItem();
            // Only write if logger is set to debug mode
            if (LOG.isDebugEnabled()) {
                position++;
                try (final StringWriter writer = new StringWriter()) {
                    final XQuerySerializer xqs = new XQuerySerializer(context.getBroker(), props, writer);
                    xqs.serialize(next.toSequence());
                    // Write to log
                    LOG.debug("{} [{}] [{}]: {}", label, position, Type.getTypeName(next.getType()), writer.toString());
                } catch (final IOException | SAXException e) {
                    throw new XPathException(this, e.getMessage());
                }
            }
            // Add to result
            result.add(next);
        }
    }
    return result;
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) IOException(java.io.IOException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter)

Example 10 with XQuerySerializer

use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.

the class FnDocSecurityTest method serialize.

private String serialize(final DBBroker broker, final Sequence sequence) throws IOException, XPathException, SAXException {
    try (final StringWriter writer = new StringWriter()) {
        final XQuerySerializer serializer = new XQuerySerializer(broker, new Properties(), writer);
        serializer.serialize(sequence);
        return writer.toString();
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) StringWriter(java.io.StringWriter) Properties(java.util.Properties)

Aggregations

XQuerySerializer (org.exist.util.serializer.XQuerySerializer)14 Properties (java.util.Properties)12 SAXException (org.xml.sax.SAXException)10 IOException (java.io.IOException)9 StringWriter (java.io.StringWriter)9 EXistException (org.exist.EXistException)4 DBBroker (org.exist.storage.DBBroker)4 OutputStreamWriter (java.io.OutputStreamWriter)3 XPathException (org.exist.xquery.XPathException)3 Writer (java.io.Writer)2 Logger (org.apache.logging.log4j.Logger)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 StringSource (org.exist.source.StringSource)2 Sequence (org.exist.xquery.value.Sequence)2 PrintWriter (java.io.PrintWriter)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpSession (javax.servlet.http.HttpSession)1