Search in sources :

Example 6 with Serializer

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

the class LocalResourceSet method getMembersAsResource.

@Override
public Resource getMembersAsResource() throws XMLDBException {
    return this.<Resource>withDb((broker, transaction) -> {
        final Serializer serializer = broker.borrowSerializer();
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        final StringWriter writer = new StringWriter();
        handler.setOutput(writer, outputProperties);
        try {
            // configure the serializer
            collection.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
            serializer.setProperties(outputProperties);
            serializer.setUser(user);
            serializer.setSAXHandlers(handler, handler);
            // serialize results
            handler.startDocument();
            handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(resources.size()));
            handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
            Item current;
            char[] value;
            for (Object resource : resources) {
                current = (Item) resource;
                if (Type.subTypeOf(current.getType(), Type.NODE)) {
                    ((NodeValue) current).toSAX(broker, handler, outputProperties);
                } else {
                    value = current.toString().toCharArray();
                    handler.characters(value, 0, value.length);
                }
            }
            handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
            handler.endPrefixMapping("exist");
            handler.endDocument();
            final Resource res = new LocalXMLResource(user, brokerPool, collection, XmldbURI.EMPTY_URI);
            res.setContent(writer.toString());
            return res;
        } catch (final SAXException e) {
            throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "serialization error", e);
        } finally {
            SerializerPool.getInstance().returnObject(handler);
            broker.returnSerializer(serializer);
        }
    });
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 7 with Serializer

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

the class TestDataGenerator method generate.

public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
    try {
        final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
        final XQuery service = broker.getBrokerPool().getXQueryService();
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        context.declareVariable("filename", "");
        context.declareVariable("count", "0");
        context.setStaticallyKnownDocuments(docs);
        final String query = IMPORT + xqueryContent;
        final CompiledXQuery compiled = service.compile(context, query);
        for (int i = 0; i < count; i++) {
            generatedFiles[i] = Files.createTempFile(prefix, ".xml");
            context.declareVariable("filename", generatedFiles[i].getFileName().toString());
            context.declareVariable("count", new Integer(i));
            final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
            final Serializer serializer = broker.borrowSerializer();
            try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
                final SAXSerializer sax = new SAXSerializer(out, outputProps);
                serializer.setSAXHandlers(sax, sax);
                for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
                    final Item item = iter.nextItem();
                    if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                        continue;
                    }
                    serializer.toSAX((NodeValue) item);
                }
            } finally {
                broker.returnSerializer(serializer);
            }
        }
    } catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new SAXException(e.getMessage(), e);
    }
    return generatedFiles;
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 8 with Serializer

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

the class StylesheetResolverAndCompiler method compileTemplates.

private <E extends Exception> Templates compileTemplates(DBBroker broker, DocumentImpl stylesheet, XSLTErrorsListener<E> errorListener) throws E, TransformerConfigurationException, SAXException {
    // factory.setURIResolver(new EXistURIResolver(broker, stylesheet.getCollection().getURI().toString()));
    final TemplatesHandler handler = factory(broker.getBrokerPool(), errorListener).newTemplatesHandler();
    handler.setSystemId(stylesheet.getBaseURI());
    handler.startDocument();
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.setSAXHandlers(handler, null);
        serializer.toSAX(stylesheet);
    } finally {
        broker.returnSerializer(serializer);
    }
    handler.endDocument();
    final Templates t = handler.getTemplates();
    // check for errors
    errorListener.checkForErrors();
    return t;
}
Also used : Templates(javax.xml.transform.Templates) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) Serializer(org.exist.storage.serializers.Serializer)

Example 9 with Serializer

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

the class CollectionRemovalTest method retrieveDoc.

private void retrieveDoc(final XmldbURI uri) throws EXistException, PermissionDeniedException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Collection test = broker.openCollection(uri, LockMode.READ_LOCK)) {
        assertNotNull(test);
        try (final LockedDocument lockedDoc = test.getDocumentWithLock(broker, XmldbURI.createInternal("document.xml"), LockMode.READ_LOCK)) {
            assertNotNull(lockedDoc);
            final Serializer serializer = broker.borrowSerializer();
            try {
                String xml = serializer.serialize(lockedDoc.getDocument());
            } finally {
                broker.returnSerializer(serializer);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            test.close();
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 10 with Serializer

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

the class ExistDocument method serialize.

private void serialize(final DBBroker broker, final DocumentImpl document, final OutputStream os) throws SAXException, IOException {
    final Serializer serializer = broker.borrowSerializer();
    // Set custom serialization options when available
    if (!configuration.isEmpty()) {
        serializer.setProperties(configuration);
    }
    SAXSerializer saxSerializer = null;
    try {
        saxSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        // Serialize document
        try (final Writer writer = new OutputStreamWriter(os, UTF_8)) {
            saxSerializer.setOutput(writer, configuration.isEmpty() ? null : configuration);
            serializer.setSAXHandlers(saxSerializer, saxSerializer);
            serializer.toSAX(document);
            writer.flush();
        }
    } finally {
        if (saxSerializer != null) {
            SerializerPool.getInstance().returnObject(saxSerializer);
        }
        broker.returnSerializer(serializer);
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SAXSerializer(org.exist.util.serializer.SAXSerializer) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

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