Search in sources :

Example 1 with DOMStreamer

use of org.exist.util.serializer.DOMStreamer 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 2 with DOMStreamer

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

the class MutableCollection method validateXMLResource.

@Deprecated
@Override
public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final Node node) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
    return validateXMLResourceInternal(transaction, broker, name, getConfiguration(broker), (info) -> {
        info.setDOMStreamer(new DOMStreamer());
        info.getDOMStreamer().serialize(node, true);
    });
}
Also used : DOMStreamer(org.exist.util.serializer.DOMStreamer)

Example 3 with DOMStreamer

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

the class XPathUtil method javaObjectToXPath.

public static final Sequence javaObjectToXPath(Object obj, XQueryContext context, boolean expandChars) throws XPathException {
    if (obj == null) {
        // return Sequence.EMPTY_SEQUENCE;
        return null;
    } else if (obj instanceof Sequence) {
        return (Sequence) obj;
    } else if (obj instanceof String) {
        final StringValue v = new StringValue((String) obj);
        return (expandChars ? v.expand() : v);
    } else if (obj instanceof Boolean) {
        return BooleanValue.valueOf(((Boolean) obj));
    } else if (obj instanceof Float) {
        return new FloatValue(((Float) obj));
    } else if (obj instanceof Double) {
        return new DoubleValue(((Double) obj));
    } else if (obj instanceof Short) {
        return new IntegerValue(((Short) obj), Type.SHORT);
    } else if (obj instanceof Integer) {
        return new IntegerValue(((Integer) obj), Type.INT);
    } else if (obj instanceof Long) {
        return new IntegerValue(((Long) obj), Type.LONG);
    } else if (obj instanceof byte[]) {
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream((byte[]) obj));
    } else if (obj instanceof ResourceSet) {
        final Sequence seq = new AVLTreeNodeSet();
        try {
            final DBBroker broker = context.getBroker();
            for (final ResourceIterator it = ((ResourceSet) obj).getIterator(); it.hasMoreResources(); ) {
                seq.add(getNode(broker, (XMLResource) it.nextResource()));
            }
        } catch (final XMLDBException xe) {
            throw new XPathException("Failed to convert ResourceSet to node: " + xe.getMessage());
        }
        return seq;
    } else if (obj instanceof XMLResource) {
        return getNode(context.getBroker(), (XMLResource) obj);
    } else if (obj instanceof Node) {
        context.pushDocumentContext();
        final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.startDocument();
            final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
            streamer.setContentHandler(receiver);
            streamer.serialize((Node) obj, false);
            if (obj instanceof Document) {
                return builder.getDocument();
            } else {
                return builder.getDocument().getNode(1);
            }
        } catch (final SAXException e) {
            throw new XPathException("Failed to transform node into internal model: " + e.getMessage());
        } finally {
            context.popDocumentContext();
            SerializerPool.getInstance().returnObject(streamer);
        }
    } else if (obj instanceof List<?>) {
        boolean createNodeSequence = true;
        for (Object next : ((List<?>) obj)) {
            if (!(next instanceof NodeProxy)) {
                createNodeSequence = false;
                break;
            }
        }
        Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
        for (Object o : ((List<?>) obj)) {
            seq.add((Item) javaObjectToXPath(o, context, expandChars));
        }
        return seq;
    } else if (obj instanceof NodeList) {
        context.pushDocumentContext();
        final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.startDocument();
            final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
            streamer.setContentHandler(receiver);
            final ValueSequence seq = new ValueSequence();
            final NodeList nl = (NodeList) obj;
            int last = builder.getDocument().getLastNode();
            for (int i = 0; i < nl.getLength(); i++) {
                final Node n = nl.item(i);
                streamer.serialize(n, false);
                final NodeImpl created = builder.getDocument().getNode(last + 1);
                seq.add(created);
                last = builder.getDocument().getLastNode();
            }
            return seq;
        } catch (final SAXException e) {
            throw new XPathException("Failed to transform node into internal model: " + e.getMessage());
        } finally {
            context.popDocumentContext();
            SerializerPool.getInstance().returnObject(streamer);
        }
    } else if (obj instanceof Object[]) {
        boolean createNodeSequence = true;
        final Object[] array = (Object[]) obj;
        for (Object arrayItem : array) {
            if (!(arrayItem instanceof NodeProxy)) {
                createNodeSequence = false;
                break;
            }
        }
        Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
        for (Object arrayItem : array) {
            seq.add((Item) javaObjectToXPath(arrayItem, context, expandChars));
        }
        return seq;
    } else {
        return new JavaObjectValue(obj);
    }
}
Also used : Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) Document(org.w3c.dom.Document) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) NodeList(org.w3c.dom.NodeList) List(java.util.List) NodeImpl(org.exist.dom.memtree.NodeImpl) NodeList(org.w3c.dom.NodeList) ResourceSet(org.xmldb.api.base.ResourceSet) AVLTreeNodeSet(org.exist.dom.persistent.AVLTreeNodeSet) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) RemoteXMLResource(org.exist.xmldb.RemoteXMLResource) XMLResource(org.xmldb.api.modules.XMLResource) LocalXMLResource(org.exist.xmldb.LocalXMLResource) DBBroker(org.exist.storage.DBBroker) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DOMStreamer(org.exist.util.serializer.DOMStreamer) ResourceIterator(org.xmldb.api.base.ResourceIterator)

Aggregations

DOMStreamer (org.exist.util.serializer.DOMStreamer)3 NodeImpl (org.exist.dom.memtree.NodeImpl)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 DBBroker (org.exist.storage.DBBroker)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)1 Tuple (com.evolvedbinary.j8fu.tuple.Tuple.Tuple)1 Tuple3 (com.evolvedbinary.j8fu.tuple.Tuple3)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Path (java.nio.file.Path)1 List (java.util.List)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 TransformerException (javax.xml.transform.TransformerException)1