Search in sources :

Example 1 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class STXTemplatesCache method compileTemplate.

private Templates compileTemplate(final DBBroker broker, final DocumentImpl stylesheet) throws TransformerConfigurationException, SAXException {
    final Serializer serializer = broker.borrowSerializer();
    try {
        final TemplatesHandler thandler = factory.newTemplatesHandler();
        serializer.setSAXHandlers(thandler, null);
        serializer.toSAX(stylesheet);
        return thandler.getTemplates();
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) Serializer(org.exist.storage.serializers.Serializer)

Example 2 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class RESTServer method writeResultJSON.

private void writeResultJSON(final HttpServletResponse response, final DBBroker broker, final Txn transaction, final Sequence results, int howmany, int start, final Properties outputProperties, final boolean wrap, final long compilationTime, final long executionTime) throws BadRequestException {
    // calculate number of results to return
    final int rlen = results.getItemCount();
    if (!results.isEmpty()) {
        if ((start < 1) || (start > rlen)) {
            throw new BadRequestException("Start parameter out of range");
        }
        // FD : correct bound evaluation
        if (((howmany + start) > rlen) || (howmany <= 0)) {
            howmany = rlen - start + 1;
        }
    } else {
        howmany = 0;
    }
    final Serializer serializer = broker.borrowSerializer();
    outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
    try {
        serializer.setProperties(outputProperties);
        try (Writer writer = new OutputStreamWriter(response.getOutputStream(), outputProperties.getProperty(OutputKeys.ENCODING))) {
            final JSONObject root = new JSONObject();
            root.addObject(new JSONSimpleProperty("start", Integer.toString(start), true));
            root.addObject(new JSONSimpleProperty("count", Integer.toString(howmany), true));
            root.addObject(new JSONSimpleProperty("hits", Integer.toString(results.getItemCount()), true));
            if (outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID) != null) {
                root.addObject(new JSONSimpleProperty("session", outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID)));
            }
            root.addObject(new JSONSimpleProperty("compilationTime", Long.toString(compilationTime), true));
            root.addObject(new JSONSimpleProperty("executionTime", Long.toString(executionTime), true));
            final JSONObject data = new JSONObject("data");
            root.addObject(data);
            Item item;
            for (int i = --start; i < start + howmany; i++) {
                item = results.itemAt(i);
                if (Type.subTypeOf(item.getType(), Type.NODE)) {
                    final NodeValue value = (NodeValue) item;
                    JSONValue json;
                    if ("json".equals(outputProperties.getProperty("method", "xml"))) {
                        json = new JSONValue(serializer.serialize(value), false);
                        json.setSerializationDataType(JSONNode.SerializationDataType.AS_LITERAL);
                    } else {
                        json = new JSONValue(serializer.serialize(value));
                        json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
                    }
                    data.addObject(json);
                } else {
                    final JSONValue json = new JSONValue(item.getStringValue());
                    json.setSerializationType(JSONNode.SerializationType.AS_ARRAY);
                    data.addObject(json);
                }
            }
            root.serialize(writer, true);
            writer.flush();
        }
    } catch (final IOException | XPathException | SAXException e) {
        throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : SAXException(org.xml.sax.SAXException) JSONValue(org.exist.util.serializer.json.JSONValue) JSONObject(org.exist.util.serializer.json.JSONObject) JSONSimpleProperty(org.exist.util.serializer.json.JSONSimpleProperty) XQuerySerializer(org.exist.util.serializer.XQuerySerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 3 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class NodeProxy method toSAX.

@Override
public void toSAX(final DBBroker broker, final ContentHandler handler, final Properties properties) throws SAXException {
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
        if (properties != null) {
            serializer.setProperties(properties);
        }
        if (handler instanceof LexicalHandler) {
            serializer.setSAXHandlers(handler, (LexicalHandler) handler);
        } else {
            serializer.setSAXHandlers(handler, null);
        }
        serializer.toSAX(this);
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) Serializer(org.exist.storage.serializers.Serializer)

Example 4 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class ExtendedDOMSerializer method startNode.

@Override
protected void startNode(Node node) throws TransformerException {
    if (node.getNodeType() == NodeImpl.REFERENCE_NODE) {
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        handler.setReceiver(receiver);
        final Serializer serializer = broker.borrowSerializer();
        serializer.setSAXHandlers(handler, handler);
        try {
            serializer.setProperties(outputProperties);
            serializer.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
        } catch (final SAXNotRecognizedException | SAXNotSupportedException e) {
        // Nothing to do ?
        }
        try {
            serializer.toSAX(((ReferenceNode) node).getReference());
        } catch (final SAXException e) {
            throw new TransformerException(e.getMessage(), e);
        } finally {
            SerializerPool.getInstance().returnObject(handler);
            broker.returnSerializer(serializer);
        }
    } else {
        super.startNode(node);
    }
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) TransformerException(javax.xml.transform.TransformerException) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 5 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class XQuerySerializer method serializeXML.

private void serializeXML(final Sequence sequence, final int start, final int howmany, final boolean wrap, final boolean typed, final long compilationTime, final long executionTime) throws SAXException, XPathException {
    final Serializer serializer = broker.borrowSerializer();
    SAXSerializer sax = null;
    try {
        sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        sax.setOutput(writer, outputProperties);
        serializer.setProperties(outputProperties);
        serializer.setSAXHandlers(sax, sax);
        serializer.toSAX(sequence, start, howmany, wrap, typed, compilationTime, executionTime);
    } catch (SAXNotSupportedException | SAXNotRecognizedException e) {
        throw new SAXException(e.getMessage(), e);
    } finally {
        if (sax != null) {
            SerializerPool.getInstance().returnObject(sax);
        }
        broker.returnSerializer(serializer);
    }
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) JSONSerializer(org.exist.util.serializer.json.JSONSerializer) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)64 SAXException (org.xml.sax.SAXException)21 DBBroker (org.exist.storage.DBBroker)16 LockedDocument (org.exist.dom.persistent.LockedDocument)15 SAXSerializer (org.exist.util.serializer.SAXSerializer)14 BrokerPool (org.exist.storage.BrokerPool)11 Properties (java.util.Properties)10 Sequence (org.exist.xquery.value.Sequence)10 Txn (org.exist.storage.txn.Txn)8 XPathException (org.exist.xquery.XPathException)8 StringWriter (java.io.StringWriter)7 Collection (org.exist.collections.Collection)7 IOException (java.io.IOException)6 Item (org.exist.xquery.value.Item)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 SequenceIterator (org.exist.xquery.value.SequenceIterator)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 TransactionManager (org.exist.storage.txn.TransactionManager)4 NodeValue (org.exist.xquery.value.NodeValue)4