Search in sources :

Example 11 with IntegerValue

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

the class ModifyFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Was context handle or DN specified?
    if (!(args[0].isEmpty()) && !(args[1].isEmpty())) {
        String dn = args[1].getStringValue();
        try {
            long ctxID = ((IntegerValue) args[0].itemAt(0)).getLong();
            DirContext ctx = (DirContext) JNDIModule.retrieveJNDIContext(context, ctxID);
            if (ctx == null) {
                logger.error("jndi:modify() - Invalid JNDI context handle provided: {}", ctxID);
            } else {
                ModificationItem[] items = parseAttributes(args[2]);
                if (items.length > 0) {
                    ctx.modifyAttributes(dn, items);
                }
            }
        } catch (NamingException ne) {
            logger.error("jndi:modify() Modify failed for dn [{}]: ", dn, ne);
            throw (new XPathException(this, "jndi:modify() Modify failed for dn [" + dn + "]: " + ne));
        }
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext)

Example 12 with IntegerValue

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

the class SearchFunction method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence xmlResult = Sequence.EMPTY_SEQUENCE;
    // Was context handle or DN specified?
    if (!(args[0].isEmpty()) && !(args[1].isEmpty())) {
        String dn = args[1].getStringValue();
        try {
            long ctxID = ((IntegerValue) args[0].itemAt(0)).getLong();
            DirContext ctx = (DirContext) JNDIModule.retrieveJNDIContext(context, ctxID);
            if (ctx == null) {
                logger.error("jndi:search() - Invalid JNDI context handle provided: {}", ctxID);
            } else {
                NamingEnumeration<SearchResult> results = null;
                if (args.length == 3) {
                    // Attributes search
                    BasicAttributes attributes = JNDIModule.parseAttributes(args[2]);
                    results = ctx.search(dn, attributes);
                } else {
                    // Filter search
                    int scopeCode = 0;
                    String filter = args[2].getStringValue();
                    String scope = args[3].getStringValue();
                    if (scope.equalsIgnoreCase("object")) {
                        scopeCode = 0;
                    } else if (scope.equalsIgnoreCase("onelevel")) {
                        scopeCode = 1;
                    } else if (scope.equalsIgnoreCase("subtree")) {
                        scopeCode = 2;
                    }
                    results = ctx.search(dn, filter, new SearchControls(scopeCode, 0, 0, null, false, false));
                }
                xmlResult = renderSearchResultsAsDSML(results, dn);
            }
        } catch (NameNotFoundException nf) {
            logger.warn("jndi:search() Not found for dn [{}]", dn, nf);
        } catch (NamingException ne) {
            logger.error("jndi:search() Search failed for dn [{}]: ", dn, ne);
            throw (new XPathException(this, "jndi:search() Search failed for dn [" + dn + "]: " + ne));
        }
    }
    return (xmlResult);
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) NameNotFoundException(javax.naming.NameNotFoundException) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) SearchResult(javax.naming.directory.SearchResult) Sequence(org.exist.xquery.value.Sequence) DirContext(javax.naming.directory.DirContext) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 13 with IntegerValue

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

the class ImplicitConnectionCloseIT method getJndiConnectionIsAutomaticallyClosed.

@Test
public void getJndiConnectionIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException {
    final String mainQuery = "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "sql:get-jndi-connection(\"" + JNDI_DS_NAME + "\", \"" + STUB_JDBC_USER + "\", \"" + STUB_JDBC_PASSWORD + "\")";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Source mainQuerySource = new StringSource(mainQuery);
    try (final DBBroker broker = pool.getBroker();
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final XQueryContext escapedMainQueryContext = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
            final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
            // execute the query
            final Sequence result = executeQuery(broker, mainCompiledQuery);
            // check that the handle for the sql connection that was created was valid
            assertEquals(1, result.getItemCount());
            assertTrue(result.itemAt(0) instanceof IntegerValue);
            assertEquals(Type.LONG, result.itemAt(0).getType());
            final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
            assertFalse(connectionHandle == 0);
            return mainQueryContext;
        });
        // check the connections map is empty
        final int connectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
        assertEquals(0, connectionsCount);
        // check the connections from our StubDataSource, they should all be closed
        final Deque<StubDataSource> createdDataSources = StubDataSourceFactory.CREATED_DATA_SOURCES;
        assertEquals(1, createdDataSources.size());
        final StubDataSource stubDataSource = createdDataSources.peek();
        final Deque<StubConnection> createdConnections = stubDataSource.createdConnections;
        assertEquals(1, createdConnections.size());
        final StubConnection stubConnection = createdConnections.peek();
        assertTrue(stubConnection.isClosed());
        transaction.commit();
    }
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) DataSource(javax.sql.DataSource) DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 14 with IntegerValue

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

the class FunSubstring method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    // start profiler
    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);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    // get arguments
    final Expression argSourceString = getArgument(0);
    final Expression argStartingLoc = getArgument(1);
    Expression argLength = null;
    // get the context sequence
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seqSourceString = argSourceString.eval(contextSequence);
    // If the value of $sourceString is the empty sequence return EMPTY_STRING, there must be a string to operate on!
    if (seqSourceString.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        // get the string to substring
        final String sourceString = seqSourceString.getStringValue();
        // check for a valid start position for the substring
        final NumericValue startingLoc = ((NumericValue) (argStartingLoc.eval(contextSequence).itemAt(0).convertTo(Type.NUMBER))).round();
        if (!validStartPosition(startingLoc, sourceString.length())) {
            // invalid start position
            result = StringValue.EMPTY_STRING;
        } else {
            // are there 2 or 3 arguments to this function?
            if (getArgumentCount() > 2) {
                argLength = getArgument(2);
                final NumericValue length = ((NumericValue) (argLength.eval(contextSequence).itemAt(0).convertTo(Type.NUMBER))).round();
                // Relocate length to position according to spec:
                // fn:round($startingLoc) <=
                // $p
                // < fn:round($startingLoc) + fn:round($length)
                NumericValue endingLoc;
                if (!length.isInfinite()) {
                    endingLoc = (NumericValue) new IntegerValue(startingLoc.getInt() + length.getInt());
                } else {
                    endingLoc = length;
                }
                // check for a valid end position for the substring
                if (!validEndPosition(endingLoc, startingLoc)) {
                    // invalid length
                    result = StringValue.EMPTY_STRING;
                } else {
                    if (endingLoc.getInt() > sourceString.length() || endingLoc.isInfinite()) {
                        // fallback to fn:substring(string, start)
                        result = substring(sourceString, startingLoc);
                    } else {
                        // three arguments fn:substring(string, start, end)
                        result = substring(sourceString, startingLoc, endingLoc);
                    }
                }
            } else {
                // No length argument
                // two arguments fn:substring(string, start)
                result = substring(sourceString, startingLoc);
            }
        }
    }
    // end profiler
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Expression(org.exist.xquery.Expression) IntegerValue(org.exist.xquery.value.IntegerValue) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 15 with IntegerValue

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

the class FunTranslate method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
public Sequence eval(Sequence contextSequence, Item contextItem) 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);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence);
    if (seq.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final ValueSequence arg = FunStringToCodepoints.getCodePoints(seq.getStringValue());
        final ValueSequence mapStr = FunStringToCodepoints.getCodePoints(getArgument(1).eval(contextSequence).getStringValue());
        final ValueSequence transStr = FunStringToCodepoints.getCodePoints(getArgument(2).eval(contextSequence).getStringValue());
        int p;
        IntegerValue ch;
        final StringBuilder buf = new StringBuilder(arg.getItemCount());
        for (int i = 0; i < arg.getItemCount(); i++) {
            ch = (IntegerValue) arg.itemAt(i);
            p = FunStringToCodepoints.indexOf(mapStr, ch);
            if (p == Constants.STRING_NOT_FOUND) {
                buf.append(FunStringToCodepoints.codePointToString(ch));
            } else {
                if (p < transStr.getItemCount()) {
                    buf.append(FunStringToCodepoints.codePointToString((IntegerValue) transStr.itemAt(p)));
                }
            }
        }
        result = new StringValue(buf.toString());
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Aggregations

IntegerValue (org.exist.xquery.value.IntegerValue)63 Sequence (org.exist.xquery.value.Sequence)33 XPathException (org.exist.xquery.XPathException)32 BrokerPool (org.exist.storage.BrokerPool)11 DBBroker (org.exist.storage.DBBroker)11 Test (org.junit.Test)11 Txn (org.exist.storage.txn.Txn)8 MessagingException (jakarta.mail.MessagingException)7 StringInputSource (org.exist.util.StringInputSource)7 NamingException (javax.naming.NamingException)6 DirContext (javax.naming.directory.DirContext)6 QName (org.exist.dom.QName)6 Source (org.exist.source.Source)6 StringSource (org.exist.source.StringSource)6 InputStream (java.io.InputStream)5 Map (java.util.Map)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 Folder (jakarta.mail.Folder)4 Image (java.awt.Image)4