Search in sources :

Example 51 with StringValue

use of org.exist.xquery.value.StringValue in project exist by eXist-db.

the class GetUploadedFileName method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    final String uploadParamName = args[0].getStringValue();
    final List<String> fnames = request.getUploadedFileName(uploadParamName);
    if (fnames == null || fnames.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    for (final String name : fnames) {
        result.add(new StringValue(name));
    }
    return result;
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Example 52 with StringValue

use of org.exist.xquery.value.StringValue in project exist by eXist-db.

the class GetParameterNames method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    final Enumeration<String> parameterNames = request.getParameterNames();
    if (!parameterNames.hasMoreElements()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    while (parameterNames.hasMoreElements()) {
        final String parameterName = parameterNames.nextElement();
        result.add(new StringValue(parameterName));
    }
    return result;
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Example 53 with StringValue

use of org.exist.xquery.value.StringValue in project exist by eXist-db.

the class GMLHSQLIndexWorker method getGeometricPropertyForNode.

@Override
protected AtomicValue getGeometricPropertyForNode(XQueryContext context, NodeProxy p, Connection conn, String propertyName) throws SQLException, XPathException {
    PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + " FROM " + GMLHSQLIndex.TABLE_NAME + " WHERE DOCUMENT_URI = ? AND NODE_ID_UNITS = ? AND NODE_ID = ?");
    ps.setString(1, p.getOwnerDocument().getURI().toString());
    ps.setInt(2, p.getNodeId().units());
    byte[] bytes = new byte[p.getNodeId().size()];
    p.getNodeId().serialize(bytes, 0);
    ps.setBytes(3, bytes);
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
        if (!rs.next())
            // Nothing returned
            return AtomicValue.EMPTY_VALUE;
        AtomicValue result = null;
        if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
            result = new BooleanValue(rs.getBoolean(1));
        } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
            result = new DoubleValue(rs.getDouble(1));
        } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
            result = new StringValue(rs.getString(1));
        } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
            result = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1)));
        } else
            throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
        if (rs.next()) {
            // Should be impossible
            throw new SQLException("More than one geometry for node " + p);
        }
        return result;
    } finally {
        if (rs != null)
            rs.close();
        ps.close();
    }
}
Also used : DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) AtomicValue(org.exist.xquery.value.AtomicValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) StringValue(org.exist.xquery.value.StringValue)

Example 54 with StringValue

use of org.exist.xquery.value.StringValue in project exist by eXist-db.

the class GMLHSQLIndexWorker method getGeometricPropertyForNodes.

@Override
protected ValueSequence getGeometricPropertyForNodes(XQueryContext context, NodeSet contextSet, Connection conn, String propertyName) throws SQLException, XPathException {
    // TODO : generate it in AbstractGMLJDBCIndexWorker
    String docConstraint = "";
    boolean refine_query_on_doc = false;
    if (contextSet != null) {
        if (contextSet.getDocumentSet().getDocumentCount() <= index.getMaxDocsInContextToRefineQuery()) {
            DocumentImpl doc;
            Iterator<DocumentImpl> it = contextSet.getDocumentSet().getDocumentIterator();
            doc = it.next();
            docConstraint = "(DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            while (it.hasNext()) {
                doc = it.next();
                docConstraint = docConstraint + " OR (DOCUMENT_URI = '" + doc.getURI().toString() + "')";
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is enabled.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Refine query on documents is disabled.");
            }
        }
    }
    PreparedStatement ps = conn.prepareStatement("SELECT " + propertyName + ", DOCUMENT_URI, NODE_ID_UNITS, NODE_ID" + " FROM " + GMLHSQLIndex.TABLE_NAME + (refine_query_on_doc ? " WHERE " + docConstraint : ""));
    ResultSet rs = null;
    try {
        rs = ps.executeQuery();
        ValueSequence result;
        if (contextSet == null)
            result = new ValueSequence();
        else
            result = new ValueSequence(contextSet.getLength());
        while (rs.next()) {
            DocumentImpl doc = null;
            try {
                doc = (DocumentImpl) broker.getXMLResource(XmldbURI.create(rs.getString("DOCUMENT_URI")));
            } catch (PermissionDeniedException e) {
                LOG.debug(e);
                // Untested, but that is roughly what should be returned.
                if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
                    result.add(AtomicValue.EMPTY_VALUE);
                } else
                    throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
                // Ignore since the broker has no right on the document
                continue;
            }
            if (contextSet.getDocumentSet().contains(doc.getDocId())) {
                NodeId nodeId = new DLN(rs.getInt("NODE_ID_UNITS"), rs.getBytes("NODE_ID"), 0);
                NodeProxy p = new NodeProxy(doc, nodeId);
                // VirtualNodeSet when on the DESCENDANT_OR_SELF axis
                if (contextSet.get(p) != null) {
                    if (rs.getMetaData().getColumnClassName(1).equals(Boolean.class.getName())) {
                        result.add(new BooleanValue(rs.getBoolean(1)));
                    } else if (rs.getMetaData().getColumnClassName(1).equals(Double.class.getName())) {
                        result.add(new DoubleValue(rs.getDouble(1)));
                    } else if (rs.getMetaData().getColumnClassName(1).equals(String.class.getName())) {
                        result.add(new StringValue(rs.getString(1)));
                    } else if (rs.getMetaData().getColumnType(1) == java.sql.Types.BINARY) {
                        result.add(BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(rs.getBytes(1))));
                    } else
                        throw new SQLException("Unable to make an atomic value from '" + rs.getMetaData().getColumnClassName(1) + "'");
                }
            }
        }
        return result;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
    }
}
Also used : DLN(org.exist.numbering.DLN) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) DoubleValue(org.exist.xquery.value.DoubleValue) BooleanValue(org.exist.xquery.value.BooleanValue) ValueSequence(org.exist.xquery.value.ValueSequence) NodeId(org.exist.numbering.NodeId) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringValue(org.exist.xquery.value.StringValue)

Example 55 with StringValue

use of org.exist.xquery.value.StringValue in project exist by eXist-db.

the class FunGeometricProperties method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result = null;
    Sequence nodes = args[0];
    if (nodes.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        try {
            Geometry geometry = null;
            String sourceCRS = null;
            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");
            }
            String propertyName = null;
            if (isCalledAs("getWKT")) {
                propertyName = "WKT";
            } else if (isCalledAs("getWKB")) {
                propertyName = "WKB";
            } else if (isCalledAs("getMinX")) {
                propertyName = "MINX";
            } else if (isCalledAs("getMaxX")) {
                propertyName = "MAXX";
            } else if (isCalledAs("getMinY")) {
                propertyName = "MINY";
            } else if (isCalledAs("getMaxY")) {
                propertyName = "MAXY";
            } else if (isCalledAs("getCentroidX")) {
                propertyName = "CENTROID_X";
            } else if (isCalledAs("getCentroidY")) {
                propertyName = "CENTROID_Y";
            } else if (isCalledAs("getArea")) {
                propertyName = "AREA";
            } else if (isCalledAs("getEPSG4326WKT")) {
                propertyName = "EPSG4326_WKT";
            } else if (isCalledAs("getEPSG4326WKB")) {
                propertyName = "EPSG4326_WKB";
            } else if (isCalledAs("getEPSG4326MinX")) {
                propertyName = "EPSG4326_MINX";
            } else if (isCalledAs("getEPSG4326MaxX")) {
                propertyName = "EPSG4326_MAXX";
            } else if (isCalledAs("getEPSG4326MinY")) {
                propertyName = "EPSG4326_MINY";
            } else if (isCalledAs("getEPSG4326MaxY")) {
                propertyName = "EPSG4326_MAXY";
            } else if (isCalledAs("getEPSG4326CentroidX")) {
                propertyName = "EPSG4326_CENTROID_X";
            } else if (isCalledAs("getEPSG4326CentroidY")) {
                propertyName = "EPSG4326_CENTROID_Y";
            } else if (isCalledAs("getEPSG4326Area")) {
                propertyName = "EPSG4326_AREA";
            } else if (isCalledAs("getSRS")) {
                propertyName = "SRS_NAME";
            } else if (isCalledAs("getGeometryType")) {
                propertyName = "GEOMETRY_TYPE";
            } else if (isCalledAs("isClosed")) {
                propertyName = "IS_CLOSED";
            } else if (isCalledAs("isSimple")) {
                propertyName = "IS_SIMPLE";
            } else if (isCalledAs("isValid")) {
                propertyName = "IS_VALID";
            } else {
                logger.error("Unknown spatial property: {}", getName().getLocalPart());
                throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
            }
            NodeValue geometryNode = (NodeValue) nodes.itemAt(0);
            if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                // The node should be indexed : get its property
                result = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, propertyName);
                hasUsedIndex = true;
            } else {
                // builds the geometry
                sourceCRS = ((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");
                }
                // Transform the geometry to EPSG:4326 if relevant
                if (propertyName.contains("EPSG4326")) {
                    geometry = indexWorker.transformGeometry(geometry, sourceCRS, "EPSG:4326");
                    if (isCalledAs("getEPSG4326WKT")) {
                        result = new StringValue(wktWriter.write(geometry));
                    } else if (isCalledAs("getEPSG4326WKB")) {
                        byte[] data = wkbWriter.write(geometry);
                        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
                    } else if (isCalledAs("getEPSG4326MinX")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
                    } else if (isCalledAs("getEPSG4326MaxX")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
                    } else if (isCalledAs("getEPSG4326MinY")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
                    } else if (isCalledAs("getEPSG4326MaxY")) {
                        result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
                    } else if (isCalledAs("getEPSG4326CentroidX")) {
                        result = new DoubleValue(geometry.getCentroid().getX());
                    } else if (isCalledAs("getEPSG4326CentroidY")) {
                        result = new DoubleValue(geometry.getCentroid().getY());
                    } else if (isCalledAs("getEPSG4326Area")) {
                        result = new DoubleValue(geometry.getArea());
                    }
                } else if (isCalledAs("getWKT")) {
                    result = new StringValue(wktWriter.write(geometry));
                } else if (isCalledAs("getWKB")) {
                    byte[] data = wkbWriter.write(geometry);
                    return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
                } else if (isCalledAs("getMinX")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
                } else if (isCalledAs("getMaxX")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
                } else if (isCalledAs("getMinY")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
                } else if (isCalledAs("getMaxY")) {
                    result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
                } else if (isCalledAs("getCentroidX")) {
                    result = new DoubleValue(geometry.getCentroid().getX());
                } else if (isCalledAs("getCentroidY")) {
                    result = new DoubleValue(geometry.getCentroid().getY());
                } else if (isCalledAs("getArea")) {
                    result = new DoubleValue(geometry.getArea());
                } else if (isCalledAs("getSRS")) {
                    result = new StringValue(((Element) geometryNode).getAttribute("srsName"));
                } else if (isCalledAs("getGeometryType")) {
                    result = new StringValue(geometry.getGeometryType());
                } else if (isCalledAs("isClosed")) {
                    result = new BooleanValue(!geometry.isEmpty());
                } else if (isCalledAs("isSimple")) {
                    result = new BooleanValue(geometry.isSimple());
                } else if (isCalledAs("isValid")) {
                    result = new BooleanValue(geometry.isValid());
                } else {
                    logger.error("Unknown spatial property: {}", getName().getLocalPart());
                    throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
                }
            }
        } 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) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Sequence(org.exist.xquery.value.Sequence) SpatialIndexException(org.exist.indexing.spatial.SpatialIndexException) NodeProxy(org.exist.dom.persistent.NodeProxy) Geometry(com.vividsolutions.jts.geom.Geometry) DoubleValue(org.exist.xquery.value.DoubleValue) AbstractGMLJDBCIndexWorker(org.exist.indexing.spatial.AbstractGMLJDBCIndexWorker) BooleanValue(org.exist.xquery.value.BooleanValue) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) StringValue(org.exist.xquery.value.StringValue)

Aggregations

StringValue (org.exist.xquery.value.StringValue)96 Sequence (org.exist.xquery.value.Sequence)49 XPathException (org.exist.xquery.XPathException)40 ValueSequence (org.exist.xquery.value.ValueSequence)27 IOException (java.io.IOException)11 PermissionDeniedException (org.exist.security.PermissionDeniedException)10 Item (org.exist.xquery.value.Item)10 XQueryContext (org.exist.xquery.XQueryContext)8 Txn (org.exist.storage.txn.Txn)7 AnyURIValue (org.exist.xquery.value.AnyURIValue)7 QName (org.exist.dom.QName)6 LockException (org.exist.util.LockException)6 NodeValue (org.exist.xquery.value.NodeValue)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 TriggerException (org.exist.collections.triggers.TriggerException)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 StoredNode (org.exist.dom.persistent.StoredNode)5 NotificationService (org.exist.storage.NotificationService)5 MimeType (org.exist.util.MimeType)5