Search in sources :

Example 11 with BooleanValue

use of org.exist.xquery.value.BooleanValue 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 12 with BooleanValue

use of org.exist.xquery.value.BooleanValue 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 13 with BooleanValue

use of org.exist.xquery.value.BooleanValue 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)

Example 14 with BooleanValue

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

the class FunNilled method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item arg = args[0].itemAt(0);
        if (!Type.subTypeOf(arg.getType(), Type.ELEMENT)) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            final Node n = ((NodeValue) arg).getNode();
            if (n.hasAttributes()) {
                final Node nilled = n.getAttributes().getNamedItemNS(Namespaces.SCHEMA_INSTANCE_NS, "nil");
                if (nilled != null) {
                    result = new BooleanValue(nilled.getNodeValue().equals("true"));
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = BooleanValue.FALSE;
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) Node(org.w3c.dom.Node) BooleanValue(org.exist.xquery.value.BooleanValue) Sequence(org.exist.xquery.value.Sequence)

Example 15 with BooleanValue

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

the class Jing method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Check input parameters
    if (args.length != 2) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValidationReport report = new ValidationReport();
    InputSource instance = null;
    InputSource grammar = null;
    try {
        report.start();
        // Get inputstream of XML instance document
        instance = Shared.getInputSource(args[0].itemAt(0), context);
        // Validate using resource specified in second parameter
        grammar = Shared.getInputSource(args[1].itemAt(0), context);
        // Special setup for compact notation
        final String grammarUrl = grammar.getSystemId();
        final SchemaReader schemaReader = ((grammarUrl != null) && (grammarUrl.endsWith(".rnc"))) ? CompactSchemaReader.getInstance() : null;
        // Setup validation properties. see Jing interface
        final PropertyMapBuilder properties = new PropertyMapBuilder();
        ValidateProperty.ERROR_HANDLER.put(properties, report);
        // Register resolver for xmldb:exist:/// embedded URLs
        final ExistResolver resolver = new ExistResolver(brokerPool);
        ValidateProperty.URI_RESOLVER.put(properties, resolver);
        ValidateProperty.ENTITY_RESOLVER.put(properties, resolver);
        // Setup driver
        final ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
        // Load schema
        driver.loadSchema(grammar);
        // Validate XML instance
        driver.validate(instance);
    } catch (final MalformedURLException ex) {
        LOG.error(ex.getMessage());
        report.setException(ex);
    } catch (final Throwable ex) {
        LOG.error(ex);
        report.setException(ex);
    } finally {
        Shared.closeInputSource(instance);
        Shared.closeInputSource(grammar);
        report.stop();
    }
    // Create response
    if (isCalledAs("jing")) {
        final Sequence result = new ValueSequence();
        result.add(new BooleanValue(report.isValid()));
        return result;
    } else /* isCalledAs("jing-report") */
    {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            return result;
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) MalformedURLException(java.net.MalformedURLException) NodeImpl(org.exist.dom.memtree.NodeImpl) ValidationDriver(com.thaiopensource.validate.ValidationDriver) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) ValidationReport(org.exist.validation.ValidationReport) BooleanValue(org.exist.xquery.value.BooleanValue) ExistResolver(org.exist.validation.resolver.unstable.ExistResolver) ValueSequence(org.exist.xquery.value.ValueSequence) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder)

Aggregations

BooleanValue (org.exist.xquery.value.BooleanValue)15 XPathException (org.exist.xquery.XPathException)9 Sequence (org.exist.xquery.value.Sequence)9 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)6 ValueSequence (org.exist.xquery.value.ValueSequence)5 NodeValue (org.exist.xquery.value.NodeValue)4 MalformedURLException (java.net.MalformedURLException)3 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)3 ValidationReport (org.exist.validation.ValidationReport)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3 IOException (java.io.IOException)2 Date (java.util.Date)2 QName (org.exist.dom.QName)2 NodeImpl (org.exist.dom.memtree.NodeImpl)2 IntegerValue (org.exist.xquery.value.IntegerValue)2 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)1 SchemaReader (com.thaiopensource.validate.SchemaReader)1 ValidationDriver (com.thaiopensource.validate.ValidationDriver)1