use of org.exist.xquery.value.Item in project exist by eXist-db.
the class InspectModuleTest method xqDoc_withAtSignInline.
@Ignore("https://github.com/eXist-db/exist/issues/1386")
@Test
public void xqDoc_withAtSignInline() throws PermissionDeniedException, XPathException, EXistException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final XQuery xqueryService = pool.getXQueryService();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun1\"]";
final Sequence result = xqueryService.execute(broker, query, null);
assertNotNull(result);
assertEquals(1, result.getItemCount());
final Item item1 = result.itemAt(0);
assertTrue(item1 instanceof ElementImpl);
final Element function = (Element) item1;
final NodeList descriptions = function.getElementsByTagName("description");
assertEquals(1, descriptions.getLength());
assertEquals("Some description.", descriptions.item(0).getFirstChild().getTextContent());
final NodeList arguments = function.getElementsByTagName("argument");
assertEquals(0, arguments.getLength());
final NodeList returns = function.getElementsByTagName("returns");
assertEquals(1, returns.getLength());
assertEquals("taxonomy[@type = \"reign\"]", returns.item(0).getFirstChild().getTextContent());
transaction.commit();
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class InspectModuleTest method xqDoc_withParamsAndReturn.
@Test
public void xqDoc_withParamsAndReturn() throws PermissionDeniedException, XPathException, EXistException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final XQuery xqueryService = pool.getXQueryService();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun2\"]";
final Sequence result = xqueryService.execute(broker, query, null);
assertNotNull(result);
assertEquals(1, result.getItemCount());
final Item item1 = result.itemAt(0);
assertTrue(item1 instanceof ElementImpl);
final Element function = (Element) item1;
final NodeList descriptions = function.getElementsByTagName("description");
assertEquals(1, descriptions.getLength());
assertEquals("Some other description.", descriptions.item(0).getFirstChild().getNodeValue());
final NodeList arguments = function.getElementsByTagName("argument");
assertEquals(2, arguments.getLength());
assertEquals("first parameter", arguments.item(0).getFirstChild().getNodeValue());
assertEquals("second parameter", arguments.item(1).getFirstChild().getNodeValue());
final NodeList returns = function.getElementsByTagName("returns");
assertEquals(1, returns.getLength());
assertEquals("our result", returns.item(0).getFirstChild().getNodeValue());
transaction.commit();
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class CollectionName method eval.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
final Item item = args[0].itemAt(0);
if (item.getType() == Type.JAVA_OBJECT) {
final Object o = ((JavaObjectValue) item).getObject();
if (!(o instanceof Collection)) {
throw new XPathException(this, "Passed Java object should be of type org.xmldb.api.base.Collection");
}
final Collection collection = (Collection) o;
try {
return new StringValue(collection.getName());
} catch (final XMLDBException e) {
throw new XPathException(this, "Failed to retrieve collection name", e);
}
} else if (Type.subTypeOf(item.getType(), Type.STRING)) {
final String path = item.getStringValue();
try {
final XmldbURI uri = XmldbURI.xmldbUriFor(path).removeLastSegment();
return new StringValue(uri.toString());
} catch (final URISyntaxException e) {
throw new XPathException(this, "Illegal URI for resource path: " + path);
}
} else if (Type.subTypeOf(item.getType(), Type.NODE)) {
final NodeValue node = (NodeValue) item;
if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeProxy p = (NodeProxy) node;
// TODO: use xmldbUri
return new StringValue(p.getOwnerDocument().getCollection().getURI().toString());
}
} else {
throw new XPathException(this, "First argument to util:collection-name should be either " + "a Java object of type org.xmldb.api.base.Collection or a node; got: " + Type.getTypeName(item.getType()));
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DeepCopyFunction method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
final Item a = args[0].itemAt(0);
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
try {
final Properties props = new Properties();
a.toSAX(context.getBroker(), receiver, props);
} catch (final SAXException e) {
throw new XPathException(this, "Cannot Deep-copy Item");
}
builder.endDocument();
return (NodeValue) receiver.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class QNameIndexLookup method eval.
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (contextSequence == null || contextSequence.isEmpty()) {
// if the context sequence is empty, we create a default context
final RootNode rootNode = new RootNode(context);
contextSequence = rootNode.eval(null, null);
}
final Sequence[] args = getArguments(null, null);
final Item item = args[0].itemAt(0);
final QNameValue qval;
try {
// attempt to convert the first argument to a QName
qval = (QNameValue) item.convertTo(Type.QNAME);
} catch (final XPathException e) {
// wrong type: generate a diagnostic error
throw new XPathException(this, Messages.formatMessage(Error.FUNC_PARAM_TYPE, new Object[] { "1", getSignature().toString(), null, Type.getTypeName(Type.QNAME), Type.getTypeName(item.getType()) }));
}
QName qname = qval.getQName();
if (args.length == 3 && !(args[2].itemAt(0).toJavaObject(boolean.class))) {
qname = new QName(qname.getLocalPart(), qname.getNamespaceURI(), qname.getPrefix(), ElementValue.ATTRIBUTE);
}
final AtomicValue comparisonCriterion = args[1].itemAt(0).atomize();
final NativeValueIndex valueIndex = context.getBroker().getValueIndex();
final Sequence result = valueIndex.find(context.getWatchDog(), Comparison.EQ, contextSequence.getDocumentSet(), null, NodeSet.ANCESTOR, qname, comparisonCriterion);
return result;
}
Aggregations