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);
}
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);
}
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();
}
}
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;
}
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;
}
Aggregations