Search in sources :

Example 11 with Serializer

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

the class LocalXMLResource method getContentAsSAX.

@Override
public void getContentAsSAX(final ContentHandler handler) throws XMLDBException {
    // case 1: content is an external DOM node
    if (root != null && !(root instanceof NodeValue)) {
        try {
            final String option = collection.getProperty(Serializer.GENERATE_DOC_EVENTS, "false");
            final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
            try {
                streamer.setContentHandler(handler);
                streamer.setLexicalHandler(lexicalHandler);
                streamer.serialize(root, option.equalsIgnoreCase("true"));
            } finally {
                SerializerPool.getInstance().returnObject(streamer);
            }
        } catch (final Exception e) {
            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage(), e);
        }
    } else {
        withDb((broker, transaction) -> {
            try {
                // case 2: content is an atomic value
                if (value != null) {
                    value.toSAX(broker, handler, getProperties());
                // case 3: content is an internal node or a document
                } else {
                    final Serializer serializer = broker.borrowSerializer();
                    try {
                        serializer.setUser(user);
                        serializer.setProperties(getProperties());
                        serializer.setSAXHandlers(handler, lexicalHandler);
                        if (root != null) {
                            serializer.toSAX((NodeValue) root);
                        } else if (proxy != null) {
                            serializer.toSAX(proxy);
                        } else {
                            read(broker, transaction).apply((document, broker1, transaction1) -> {
                                try {
                                    serializer.toSAX(document);
                                    return null;
                                } catch (final SAXException e) {
                                    throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
                                }
                            });
                        }
                    } finally {
                        broker.returnSerializer(serializer);
                    }
                }
                return null;
            } catch (final SAXException e) {
                throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
            }
        });
    }
}
Also used : Tuple3(com.evolvedbinary.j8fu.tuple.Tuple3) StoredNode(org.exist.dom.persistent.StoredNode) Txn(org.exist.storage.txn.Txn) SAXSerializer(org.exist.util.serializer.SAXSerializer) net.sf.cglib.proxy(net.sf.cglib.proxy) BrokerPool(org.exist.storage.BrokerPool) TransformerException(javax.xml.transform.TransformerException) NodeProxy(org.exist.dom.persistent.NodeProxy) AtomicValue(org.exist.xquery.value.AtomicValue) MimeType(org.exist.util.MimeType) Tuple(com.evolvedbinary.j8fu.tuple.Tuple.Tuple) StringValue(org.exist.xquery.value.StringValue) LexicalHandler(org.xml.sax.ext.LexicalHandler) SerializerPool(org.exist.util.serializer.SerializerPool) AttrImpl(org.exist.dom.memtree.AttrImpl) Subject(org.exist.security.Subject) Node(org.w3c.dom.Node) NodeValue(org.exist.xquery.value.NodeValue) org.xml.sax(org.xml.sax) DocumentImpl(org.exist.dom.memtree.DocumentImpl) NodeImpl(org.exist.dom.memtree.NodeImpl) DOMSerializer(org.exist.util.serializer.DOMSerializer) XMLResource(org.xmldb.api.modules.XMLResource) Method(java.lang.reflect.Method) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) XMLDBException(org.xmldb.api.base.XMLDBException) NodeList(org.w3c.dom.NodeList) Properties(java.util.Properties) UTF_8(java.nio.charset.StandardCharsets.UTF_8) DOMStreamer(org.exist.util.serializer.DOMStreamer) StringWriter(java.io.StringWriter) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) DocumentType(org.w3c.dom.DocumentType) Stream(java.util.stream.Stream) XMLUtil(org.exist.dom.persistent.XMLUtil) NodeId(org.exist.numbering.NodeId) DBBroker(org.exist.storage.DBBroker) Serializer(org.exist.storage.serializers.Serializer) Optional(java.util.Optional) ErrorCodes(org.xmldb.api.base.ErrorCodes) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) XPathException(org.exist.xquery.XPathException) NodeValue(org.exist.xquery.value.NodeValue) DOMStreamer(org.exist.util.serializer.DOMStreamer) XMLDBException(org.xmldb.api.base.XMLDBException) TransformerException(javax.xml.transform.TransformerException) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) SAXSerializer(org.exist.util.serializer.SAXSerializer) DOMSerializer(org.exist.util.serializer.DOMSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 12 with Serializer

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

the class Modification method deepCopy.

protected Sequence deepCopy(Sequence inSeq) throws XPathException {
    context.pushDocumentContext();
    final MemTreeBuilder builder = context.getDocumentBuilder();
    final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
    final Serializer serializer = context.getBroker().borrowSerializer();
    serializer.setReceiver(receiver);
    try {
        final Sequence out = new ValueSequence();
        for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
            Item item = i.nextItem();
            if (item.getType() == Type.DOCUMENT) {
                if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    final NodeHandle root = (NodeHandle) ((NodeProxy) item).getOwnerDocument().getDocumentElement();
                    item = new NodeProxy(root);
                } else {
                    item = (Item) ((Document) item).getDocumentElement();
                }
            }
            if (Type.subTypeOf(item.getType(), Type.NODE)) {
                if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    final int last = builder.getDocument().getLastNode();
                    final NodeProxy p = (NodeProxy) item;
                    serializer.toReceiver(p, false, false);
                    if (p.getNodeType() == Node.ATTRIBUTE_NODE) {
                        item = builder.getDocument().getLastAttr();
                    } else {
                        item = builder.getDocument().getNode(last + 1);
                    }
                } else {
                    ((org.exist.dom.memtree.NodeImpl) item).deepCopy();
                }
            }
            out.add(item);
        }
        return out;
    } catch (final SAXException | DOMException e) {
        throw new XPathException(this, e.getMessage(), e);
    } finally {
        context.getBroker().returnSerializer(serializer);
        context.popDocumentContext();
    }
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) DOMException(org.w3c.dom.DOMException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeHandle(org.exist.dom.persistent.NodeHandle) ValueSequence(org.exist.xquery.value.ValueSequence) Serializer(org.exist.storage.serializers.Serializer)

Example 13 with Serializer

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

the class StoreResourceTest method replaceXmlDoc.

private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            final String docXml = serializer.serialize(lockedDoc.getDocument());
            final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
        } finally {
            broker.returnSerializer(serializer);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 14 with Serializer

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

the class DebuggeeJointImpl method evalution.

@Override
public String evalution(String script) throws Exception {
    Database db = compiledXQuery.getContext().getDatabase();
    // TODO: account required
    try (final DBBroker broker = db.getBroker()) {
        XQueryContext context = compiledXQuery.getContext().copyContext();
        context.setDebuggeeJoint(null);
        context.undeclareGlobalVariable(Debuggee.SESSION);
        context.undeclareGlobalVariable(Debuggee.IDEKEY);
        XQuery service = broker.getBrokerPool().getXQueryService();
        CompiledXQuery compiled = service.compile(broker, context, script);
        Sequence resultSequence = service.execute(broker, compiled, null);
        SAXSerializer sax = null;
        Serializer serializer = broker.getSerializer();
        serializer.reset();
        try {
            sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            Properties outputProps = new Properties();
            StringWriter writer = new StringWriter();
            sax.setOutput(writer, outputProps);
            serializer.setSAXHandlers(sax, sax);
            for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
                Item next = i.nextItem();
                if (Type.subTypeOf(next.getType(), Type.NODE))
                    serializer.toSAX((NodeValue) next);
                else
                    writer.write(next.getStringValue());
            }
            return writer.toString();
        } finally {
            if (sax != null) {
                SerializerPool.getInstance().returnObject(sax);
            }
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) Database(org.exist.Database) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 15 with Serializer

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

the class PropertyGet method getPropertyString.

protected static StringBuilder getPropertyString(Variable variable, XQueryContext context) {
    Sequence value = variable.getValue();
    Database db = context.getDatabase();
    try (final DBBroker broker = db.getBroker()) {
        Serializer serializer = broker.getSerializer();
        serializer.reset();
        try {
            StringBuilder property = new StringBuilder();
            if (value.hasOne()) {
                String strVal = getPropertyValue(value.itemAt(0), serializer);
                String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "node" : Type.getTypeName(value.getItemType());
                property.append("<property name=\"");
                property.append(variable.getQName().toString());
                property.append("\" fullname=\"");
                property.append(variable.getQName().toString());
                property.append("\" type=\"");
                property.append(type);
                property.append("\" size=\"");
                property.append(strVal.length());
                property.append("\" encoding=\"none\">");
                property.append(strVal);
                property.append("</property>");
            } else {
                property.append("<property name=\"");
                property.append(variable.getQName().toString());
                property.append("\" fullname=\"");
                property.append(variable.getQName().toString());
                property.append("\" type=\"array\" children=\"true\" numchildren=\"");
                property.append(value.getItemCount());
                property.append("\">");
                int idx = 0;
                for (SequenceIterator si = value.iterate(); si.hasNext(); idx++) {
                    Item item = si.nextItem();
                    String strVal = getPropertyValue(item, serializer);
                    String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "xs:string" : Type.getTypeName(value.getItemType());
                    property.append("<property name=\"");
                    property.append(idx);
                    property.append("\" type=\"");
                    property.append(type);
                    property.append("\" size=\"");
                    property.append(strVal.length());
                    property.append("\" encoding=\"none\">");
                    property.append(strVal);
                    property.append("</property>");
                }
                property.append("</property>");
            }
            return property;
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (XPathException e) {
            e.printStackTrace();
        }
    } catch (EXistException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) Database(org.exist.Database) EXistException(org.exist.EXistException) 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