Search in sources :

Example 11 with XQuerySerializer

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

the class XqueryApiTest method serialize.

private String serialize(final Sequence sequence) throws ApiException {
    try (final DBBroker broker = server.getBrokerPool().getBroker();
        final StringWriter writer = new StringWriter()) {
        final XQuerySerializer serializer = new XQuerySerializer(broker, new Properties(), writer);
        serializer.serialize(sequence);
        return writer.toString();
    } catch (final EXistException | IOException | SAXException | XPathException e) {
        throw new ApiException(e.getMessage(), e);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuerySerializer(org.exist.util.serializer.XQuerySerializer) StringWriter(java.io.StringWriter) XPathException(org.exist.xquery.XPathException) EXistException(org.exist.EXistException) IOException(java.io.IOException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException)

Example 12 with XQuerySerializer

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

the class LogFunction method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Force adaptive serialization
    final Properties props = new Properties();
    props.setProperty(OutputKeys.METHOD, "adaptive");
    SequenceIterator i;
    final String calledAs = getName().getLocalPart();
    // Find query parameter with actual data
    switch(calledAs) {
        case FUNCTION_LOG:
            i = args[1].unorderedIterator();
            if (args[1].isEmpty()) {
                return (Sequence.EMPTY_SEQUENCE);
            }
            break;
        case FUNCTION_LOGAPP:
            i = args[2].unorderedIterator();
            if (args[2].isEmpty()) {
                return (Sequence.EMPTY_SEQUENCE);
            }
            break;
        default:
            i = args[0].unorderedIterator();
            if (args[0].isEmpty()) {
                return (Sequence.EMPTY_SEQUENCE);
            }
            break;
    }
    // Add line of the log statement
    final StringBuilder buf = new StringBuilder();
    buf.append("(");
    buf.append("Line: ");
    buf.append(this.getLine());
    // Add source information to the log statement when provided
    if (getSource() != null) {
        buf.append(' ').append(getSource().pathOrShortIdentifier());
    }
    buf.append(") ");
    // Iterate over all input
    while (i.hasNext()) {
        final Item next = i.nextItem();
        if (next instanceof StringValue) {
            // Use simple string value
            buf.append(next.getStringValue());
        } else {
            // Serialize data
            try (StringWriter writer = new StringWriter()) {
                XQuerySerializer xqs = new XQuerySerializer(context.getBroker(), props, writer);
                xqs.serialize(next.toSequence());
                buf.append(writer.toString());
            } catch (IOException | SAXException e) {
                throw new XPathException(this, e.getMessage());
            }
        }
    }
    // Finally write the log
    switch(calledAs) {
        case FUNCTION_LOG:
            final String loglevel = args[0].getStringValue().toLowerCase();
            writeLog(buf, loglevel, logger);
            break;
        case FUNCTION_LOG_SYSTEM_OUT:
            System.out.println(buf);
            break;
        case FUNCTION_LOG_SYSTEM_ERR:
            System.err.println(buf);
            break;
        case FUNCTION_LOGAPP:
            {
                final String loglevelapp = args[0].getStringValue().toLowerCase();
                final String logname = args[1].getStringValue();
                // Use specific logger when provided
                final Logger logger = (logname == null || logname.isEmpty()) ? LOG : LogManager.getLogger(logname);
                writeLog(buf, loglevelapp, logger);
                break;
            }
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) IOException(java.io.IOException) Properties(java.util.Properties) Logger(org.apache.logging.log4j.Logger) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter)

Example 13 with XQuerySerializer

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

the class RESTServer method writeResultXML.

private void writeResultXML(final HttpServletResponse response, final DBBroker broker, final Sequence results, final int howmany, final int start, final boolean typed, final Properties outputProperties, final boolean wrap, final long compilationTime, final long executionTime) throws BadRequestException {
    // serialize the results to the response output stream
    outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
    try {
        // set output headers
        final String encoding = outputProperties.getProperty(OutputKeys.ENCODING);
        if (!response.containsHeader("Content-Type")) {
            String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
            if (mimeType != null) {
                final int semicolon = mimeType.indexOf(';');
                if (semicolon != Constants.STRING_NOT_FOUND) {
                    mimeType = mimeType.substring(0, semicolon);
                }
                if (wrap) {
                    mimeType = "application/xml";
                }
                response.setContentType(mimeType + "; charset=" + encoding);
            }
        }
        if (wrap) {
            outputProperties.setProperty("method", "xml");
        }
        final Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
        final XQuerySerializer serializer = new XQuerySerializer(broker, outputProperties, writer);
        // Marshaller.marshall(broker, results, start, howmany, serializer.getContentHandler());
        serializer.serialize(results, start, howmany, wrap, typed, compilationTime, executionTime);
        writer.flush();
        writer.close();
    } catch (final SAXException e) {
        LOG.warn(e);
        throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
    } catch (final Exception e) {
        LOG.warn(e.getMessage(), e);
        throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) PermissionDeniedException(org.exist.security.PermissionDeniedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TriggerException(org.exist.collections.triggers.TriggerException) EXistException(org.exist.EXistException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 14 with XQuerySerializer

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

the class EXistSequence method serialize.

@Override
public void serialize(final OutputStream out, final SerialParameters params) throws ToolsException {
    final Properties props = params == null ? null : makeOutputProperties(params);
    props.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
    final String encoding = props.getProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
    try (final Writer writer = new OutputStreamWriter(new CloseShieldOutputStream(out), encoding)) {
        final XQuerySerializer xqSerializer = new XQuerySerializer(context.getBroker(), props, writer);
        xqSerializer.serialize(sequence);
    } catch (final SAXException | IOException | XPathException e) {
        throw new ToolsException("A problem occurred while serializing the node set: " + e.getMessage(), e);
    }
}
Also used : XQuerySerializer(org.exist.util.serializer.XQuerySerializer) ToolsException(org.expath.tools.ToolsException) XPathException(org.exist.xquery.XPathException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) SAXException(org.xml.sax.SAXException)

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