Search in sources :

Example 36 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class XQueryContext method getDocumentBuilder.

@Override
public MemTreeBuilder getDocumentBuilder() {
    if (documentBuilder == null) {
        documentBuilder = new MemTreeBuilder(this);
        documentBuilder.startDocument();
    }
    return documentBuilder;
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder)

Example 37 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class FunGMLProducers method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result = null;
    try {
        AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
        if (indexWorker == null) {
            logger.error("Unable to find a spatial index worker");
            throw new XPathException(this, "Unable to find a spatial index worker");
        }
        Geometry geometry = null;
        String targetSRS = null;
        if (isCalledAs("transform")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                String sourceSRS = null;
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    sourceSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    sourceSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                targetSRS = args[1].itemAt(0).getStringValue().trim();
                geometry = indexWorker.transformGeometry(geometry, sourceSRS, targetSRS);
            }
        } else if (isCalledAs("WKTtoGML")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                String wkt = args[0].itemAt(0).getStringValue();
                WKTReader wktReader = new WKTReader();
                try {
                    geometry = wktReader.read(wkt);
                } catch (ParseException e) {
                    logger.error(e.getMessage());
                    throw new XPathException(e);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                targetSRS = args[1].itemAt(0).getStringValue().trim();
            }
        } else if (isCalledAs("buffer")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                double distance = ((DoubleValue) args[1].itemAt(0)).getDouble();
                int quadrantSegments = 8;
                int endCapStyle = BufferOp.CAP_ROUND;
                if (getArgumentCount() > 2 && Type.subTypeOf(args[2].itemAt(0).getType(), Type.INTEGER))
                    quadrantSegments = ((IntegerValue) args[2].itemAt(0)).getInt();
                if (getArgumentCount() > 3 && Type.subTypeOf(args[3].itemAt(0).getType(), Type.INTEGER))
                    endCapStyle = ((IntegerValue) args[3].itemAt(0)).getInt();
                switch(endCapStyle) {
                    case BufferOp.CAP_ROUND:
                    case BufferOp.CAP_BUTT:
                    case BufferOp.CAP_SQUARE:
                        // OK
                        break;
                    default:
                        {
                            logger.error("Invalid line end style");
                            throw new XPathException("Invalid line end style");
                        }
                }
                geometry = geometry.buffer(distance, quadrantSegments, endCapStyle);
            }
        } else if (isCalledAs("getBbox")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.getEnvelope();
            }
        } else if (isCalledAs("convexHull")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.convexHull();
            }
        } else if (isCalledAs("boundary")) {
            if (args[0].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else {
                NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
                // Try to get the geometry from the index
                if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
                    geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
                    hasUsedIndex = true;
                }
                // Otherwise, build it
                if (geometry == null) {
                    targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
                    geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
                }
                if (geometry == null) {
                    logger.error("Unable to get a geometry from the node");
                    throw new XPathException("Unable to get a geometry from the node");
                }
                geometry = geometry.getBoundary();
            }
        } else {
            Geometry geometry1 = null;
            Geometry geometry2 = null;
            if (args[0].isEmpty() && args[1].isEmpty())
                result = Sequence.EMPTY_SEQUENCE;
            else if (!args[0].isEmpty() && args[1].isEmpty())
                result = args[0].itemAt(0).toSequence();
            else if (args[0].isEmpty() && !args[1].isEmpty())
                result = args[1].itemAt(0).toSequence();
            else {
                NodeValue geometryNode1 = (NodeValue) args[0].itemAt(0);
                NodeValue geometryNode2 = (NodeValue) args[1].itemAt(0);
                String srsName1 = null;
                String srsName2 = null;
                // Try to get the geometries from the index
                if (geometryNode1.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    srsName1 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode1, "SRS_NAME").getStringValue();
                    geometry1 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode1, false);
                    hasUsedIndex = true;
                }
                if (geometryNode2.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    srsName2 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode2, "SRS_NAME").getStringValue();
                    geometry2 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode2, false);
                    hasUsedIndex = true;
                }
                // Otherwise build them
                if (geometry1 == null) {
                    srsName1 = ((Element) geometryNode1.getNode()).getAttribute("srsName").trim();
                    geometry1 = indexWorker.streamNodeToGeometry(context, geometryNode1);
                }
                if (geometry2 == null) {
                    srsName2 = ((Element) geometryNode2.getNode()).getAttribute("srsName").trim();
                    geometry2 = indexWorker.streamNodeToGeometry(context, geometryNode2);
                }
                if (geometry1 == null) {
                    logger.error("Unable to get a geometry from the first node");
                    throw new XPathException("Unable to get a geometry from the first node");
                }
                if (geometry2 == null) {
                    logger.error("Unable to get a geometry from the second node");
                    throw new XPathException("Unable to get a geometry from the second node");
                }
                if (srsName1 == null)
                    throw new XPathException("Unable to get a SRS for the first geometry");
                if (srsName2 == null)
                    throw new XPathException("Unable to get a SRS for the second geometry");
                // Transform the second geometry in the SRS of the first one if necessary
                if (!srsName1.equalsIgnoreCase(srsName2)) {
                    geometry2 = indexWorker.transformGeometry(geometry2, srsName1, srsName2);
                }
                if (isCalledAs("intersection")) {
                    geometry = geometry1.intersection(geometry2);
                } else if (isCalledAs("union")) {
                    geometry = geometry1.union(geometry2);
                } else if (isCalledAs("difference")) {
                    geometry = geometry1.difference(geometry2);
                } else if (isCalledAs("symetricDifference")) {
                    geometry = geometry1.symDifference(geometry2);
                }
                targetSRS = srsName1;
            }
        }
        if (result == null) {
            String gmlPrefix = context.getPrefixForURI(AbstractGMLJDBCIndexWorker.GML_NS);
            if (gmlPrefix == null) {
                logger.error("namespace is not defined:" + SpatialModule.PREFIX);
                throw new XPathException("'" + AbstractGMLJDBCIndexWorker.GML_NS + "' namespace is not defined");
            }
            context.pushDocumentContext();
            try {
                MemTreeBuilder builder = context.getDocumentBuilder();
                DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
                result = (NodeValue) indexWorker.streamGeometryToElement(geometry, targetSRS, receiver);
            } finally {
                context.popDocumentContext();
            }
        }
    } catch (SpatialIndexException e) {
        logger.error(e.getMessage());
        throw new XPathException(e);
    }
    return result;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Element(org.w3c.dom.Element) IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence) SpatialIndexException(org.exist.indexing.spatial.SpatialIndexException) WKTReader(com.vividsolutions.jts.io.WKTReader) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) NodeProxy(org.exist.dom.persistent.NodeProxy) Geometry(com.vividsolutions.jts.geom.Geometry) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DoubleValue(org.exist.xquery.value.DoubleValue) AbstractGMLJDBCIndexWorker(org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker) ParseException(com.vividsolutions.jts.io.ParseException)

Example 38 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class HighlightMatches method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    context.pushDocumentContext();
    final Serializer serializer = context.getBroker().borrowSerializer();
    try (FunctionReference func = (FunctionReference) args[1].itemAt(0)) {
        MemTreeBuilder builder = context.getDocumentBuilder();
        NGramIndexWorker index = (NGramIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(NGramIndex.ID);
        DocumentBuilderReceiver docBuilder = new DocumentBuilderReceiver(builder);
        MatchCallback matchCb = new MatchCallback(func, docBuilder);
        ValueSequence result = new ValueSequence();
        for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            NodeValue v = (NodeValue) i.nextItem();
            try {
                int nodeNr = builder.getDocument().getLastNode();
                if (v.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
                    ((NodeImpl) v).copyTo(context.getBroker(), docBuilder);
                } else {
                    NodeProxy p = (NodeProxy) v;
                    MatchListener ml = index.getMatchListener(context.getBroker(), p, matchCb);
                    Receiver receiver;
                    if (ml == null)
                        receiver = docBuilder;
                    else {
                        ml.setNextInChain(docBuilder);
                        receiver = ml;
                    }
                    serializer.setReceiver(receiver);
                    serializer.toReceiver((NodeProxy) v, false);
                }
                result.add(builder.getDocument().getNode(++nodeNr));
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
                throw new XPathException(this, e.getMessage());
            }
        }
        return result;
    } finally {
        context.getBroker().returnSerializer(serializer);
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) NGramIndexWorker(org.exist.indexing.ngram.NGramIndexWorker) NGramMatchCallback(org.exist.indexing.ngram.NGramMatchCallback) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Receiver(org.exist.util.serializer.Receiver) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) FunctionReference(org.exist.xquery.value.FunctionReference) MatchListener(org.exist.indexing.MatchListener) Serializer(org.exist.storage.serializers.Serializer)

Example 39 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class JMXtoXML method generateXMLReport.

/**
 * Retrieve JMX output for the given categories and return it as an XML DOM. Valid categories are "memory",
 * "instances", "disk", "system", "caches", "locking", "processes", "sanity", "all".
 *
 * @param errcode    an optional error description
 * @param categories the categories to generate the report for
 * @return xml report
 */
public Element generateXMLReport(final String errcode, final String[] categories) {
    final MemTreeBuilder builder = new MemTreeBuilder();
    try {
        builder.startDocument();
        builder.startElement(JMX_ELEMENT, null);
        builder.addAttribute(VERSION_ATTR, Integer.toString(VERSION));
        if (url != null) {
            builder.addAttribute(JMX_CONNECTION_ATTR, url.toString());
        }
        if (errcode != null) {
            builder.startElement(JMX_ERROR, null);
            builder.characters(errcode);
            builder.endElement();
        }
        for (final String category : categories) {
            final ObjectName[] names = CATEGORIES.get(category);
            for (final ObjectName name : names) {
                queryMBeans(builder, name);
            }
        }
        builder.endElement();
        builder.endDocument();
    } catch (final Exception e) {
        e.printStackTrace();
        LOG.warn("Could not generate XML report from JMX: {}", e.getMessage());
    }
    return (Element) builder.getDocument().getNode(1);
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Element(org.w3c.dom.Element) TransformerException(javax.xml.transform.TransformerException) StartException(org.exist.start.StartException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 40 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class JMXtoXML method invoke.

public Element invoke(final String objectName, final String operation, String[] args) throws InstanceNotFoundException, MalformedObjectNameException, MBeanException, IOException, ReflectionException, IntrospectionException {
    final ObjectName name = new ObjectName(objectName);
    MBeanServerConnection conn = connection;
    MBeanInfo info;
    try {
        info = conn.getMBeanInfo(name);
    } catch (InstanceNotFoundException e) {
        conn = platformConnection;
        info = conn.getMBeanInfo(name);
    }
    final MBeanOperationInfo[] operations = info.getOperations();
    for (final MBeanOperationInfo op : operations) {
        if (operation.equals(op.getName())) {
            final MBeanParameterInfo[] sig = op.getSignature();
            final Object[] params = new Object[sig.length];
            final String[] types = new String[sig.length];
            for (int i = 0; i < sig.length; i++) {
                String type = sig[i].getType();
                types[i] = type;
                params[i] = mapParameter(type, args[i]);
            }
            final Object result = conn.invoke(name, operation, params, types);
            final MemTreeBuilder builder = new MemTreeBuilder();
            try {
                builder.startDocument();
                builder.startElement(JMX_ELEMENT, null);
                builder.addAttribute(VERSION_ATTR, Integer.toString(VERSION));
                if (url != null) {
                    builder.addAttribute(JMX_CONNECTION_ATTR, url.toString());
                }
                builder.startElement(JMX_RESULT, null);
                builder.addAttribute(JMX_RESULT_TYPE_ATTR, op.getReturnType());
                serializeObject(builder, result);
                builder.endElement();
                builder.endElement();
                builder.endDocument();
            } catch (final Exception e) {
                e.printStackTrace();
                LOG.warn("Could not generate XML report from JMX: {}", e.getMessage());
            }
            return (Element) builder.getDocument().getNode(1);
        }
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) TransformerException(javax.xml.transform.TransformerException) StartException(org.exist.start.StartException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder)

Aggregations

MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)82 QName (org.exist.dom.QName)29 XPathException (org.exist.xquery.XPathException)23 Sequence (org.exist.xquery.value.Sequence)19 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)17 SAXException (org.xml.sax.SAXException)16 NodeValue (org.exist.xquery.value.NodeValue)15 IOException (java.io.IOException)14 NodeImpl (org.exist.dom.memtree.NodeImpl)14 Item (org.exist.xquery.value.Item)8 SequenceIterator (org.exist.xquery.value.SequenceIterator)8 Test (org.junit.Test)8 XQueryContext (org.exist.xquery.XQueryContext)7 ValueSequence (org.exist.xquery.value.ValueSequence)7 AttributesImpl (org.xml.sax.helpers.AttributesImpl)7 DocumentImpl (org.exist.dom.memtree.DocumentImpl)6 BrokerPool (org.exist.storage.BrokerPool)6 InputSource (org.xml.sax.InputSource)6 MalformedURLException (java.net.MalformedURLException)5 Path (java.nio.file.Path)5