use of org.exist.xquery.value.Item in project exist by eXist-db.
the class AbstractUpdateTest method read.
private void read(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException {
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final Serializer serializer = broker.borrowSerializer();
try (final LockedDocument lockedDoc = broker.getXMLResource(TEST_COLLECTION_URI.append("test2/test.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + TEST_COLLECTION_URI.append("test2/test.xml") + "' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
} finally {
broker.returnSerializer(serializer);
}
final XQuery xquery = pool.getXQueryService();
final Sequence seq = xquery.execute(broker, "/products/product[last()]", null);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
Item next = i.nextItem();
}
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class XQueryTest method importExternalClasspathMainModule.
@Test
public void importExternalClasspathMainModule() throws EXistException, IOException, PermissionDeniedException, XPathException, QName.IllegalQNameException {
final long timestamp = System.currentTimeMillis();
final BrokerPool brokerPool = BrokerPool.getInstance();
try (final DBBroker broker = brokerPool.getBroker()) {
final org.exist.source.Source source = SourceFactory.getSource(broker, "/", "resource:org/exist/xquery/external-classpath-main-module.xq", false);
final XQuery xquery = brokerPool.getXQueryService();
final XQueryPool queryPool = brokerPool.getXQueryPool();
CompiledXQuery compiled = null;
XQueryContext context = null;
try {
compiled = queryPool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(brokerPool);
} else {
context = compiled.getContext();
context.prepareForReuse();
}
context.declareVariable(new QName("s"), new IntegerValue(timestamp));
if (compiled == null) {
compiled = xquery.compile(context, source);
}
final Sequence result = xquery.execute(broker, compiled, null, null);
assertEquals(1, result.getItemCount());
final Item item = result.itemAt(0);
assertTrue(Type.subTypeOf(item.getType(), Type.NODE));
final Source expected = Input.fromString("<echo>" + timestamp + "</echo>").build();
final Source actual = Input.fromNode((Node) item).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
} finally {
if (compiled != null) {
compiled.reset();
}
if (context != null) {
context.reset();
}
if (compiled != null) {
queryPool.returnCompiledXQuery(source, compiled);
}
}
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunCeiling method eval.
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());
}
}
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
Sequence result;
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final Item item = seq.itemAt(0);
NumericValue value;
if (item instanceof NumericValue) {
value = (NumericValue) item;
} else {
value = (NumericValue) item.convertTo(Type.NUMBER);
}
result = value.ceiling();
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class TypeswitchExpression method checkType.
private boolean checkType(SequenceType type, Sequence seq) throws XPathException {
final Cardinality requiredCardinality = type.getCardinality();
Cardinality actualCardinality;
if (seq.isEmpty()) {
actualCardinality = Cardinality.EMPTY_SEQUENCE;
} else if (seq.hasMany()) {
actualCardinality = Cardinality._MANY;
} else {
actualCardinality = Cardinality.EXACTLY_ONE;
}
if (!requiredCardinality.isSuperCardinalityOrEqualOf(actualCardinality)) {
return false;
}
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
if (!type.checkType(next)) {
return false;
}
}
return true;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DOMIndexerTest method xQuery.
@Test
public void xQuery() throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final StringWriter out = new StringWriter()) {
final XQuery xquery = pool.getXQueryService();
final Sequence result = xquery.execute(broker, XQUERY, null);
final int count = result.getItemCount();
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "yes");
final SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
out.toString();
}
}
Aggregations