/** * Get an iterator over the results of the query * @throws SaxonApiUncheckedException if a dynamic error is detected while constructing the iterator. * It is also possible for an SaxonApiUncheckedException to be thrown by the hasNext() method of the * returned iterator if a dynamic error occurs while evaluating the result sequence. */ public Iterator<XdmItem> iterator() throws SaxonApiUncheckedException { try { return new XdmSequenceIterator(expression.iterator(context)); } catch (XPathException e) { throw new SaxonApiUncheckedException(e); } }
/** * Execute a the compiled Query, returning the results as a List. * * @param env Provides the dynamic query evaluation context * @return The results of the expression, as a List. The List represents the sequence * of items returned by the expression. Each item in the list will either be an * object representing a node, or an object representing an atomic value. * For the types of Java object that may be returned, see the description of the * {@link net.sf.saxon.xpath.XPathEvaluator#evaluate evaluate} method * of class XPathProcessor */ public List evaluate(DynamicQueryContext env) throws XPathException { SequenceIterator iterator = iterator(env); ArrayList list = new ArrayList(100); while (true) { Item item = iterator.next(); if (item == null) { return list; } list.add(Value.convertToJava(item)); } }
/** * Execute the compiled Query, returning the first item in the result. * This is useful where it is known that the expression will only return * a singleton value (for example, a single node, or a boolean). * * @param env Provides the dynamic query evaluation context * @return The first item in the sequence returned by the expression. If the expression * returns an empty sequence, this method returns null. Otherwise, it returns the first * item in the result sequence, represented as a Java object using the same mapping as for * the {@link XQueryExpression#evaluate evaluate} method */ public Object evaluateSingle(DynamicQueryContext env) throws XPathException { SequenceIterator iterator = iterator(env); Item item = iterator.next(); if (item == null) { return null; } return Value.convertToJava(item); }
/** * Execute a the compiled Query, returning the results as a List. * * @param env Provides the dynamic query evaluation context * @return The results of the expression, as a List. The List represents the sequence * of items returned by the expression. Each item in the list will either be an * object representing a node, or an object representing an atomic value. * For the types of Java object that may be returned, see the description of the * {@link net.sf.saxon.xpath.XPathEvaluator#evaluate evaluate} method * of class XPathProcessor */ public List evaluate(DynamicQueryContext env) throws XPathException { if (isUpdating) { throw new XPathException("Cannot call evaluate() on an updating query"); } SequenceIterator iterator = iterator(env); ArrayList list = new ArrayList(100); while (true) { Item item = iterator.next(); if (item == null) { return list; } list.add(Value.convertToJava(item)); } }
/** * Execute a the compiled Query, returning the results as a List of objects * obtained by converting the items in the query result to the nearest appropriate * Java type * * @param env Provides the dynamic query evaluation context * @return The results of the expression, as a List. The List represents the sequence * of items returned by the expression. Each item in the list will either be an * object representing a node, or an object representing an atomic value. * For the types of Java object that may be returned, see the description of the * {@link net.sf.saxon.xpath.XPathEvaluator#evaluate evaluate} method * of class XPathProcessor * @throws XPathException if a dynamic error occurs during query evaluation */ /*@NotNull*/ public List<Object> evaluate(/*@NotNull*/ DynamicQueryContext env) throws XPathException { if (isUpdateQuery()) { throw new XPathException("Cannot call evaluate() on an updating query"); } ArrayList<Object> list = new ArrayList<>(100); iterator(env).forEachOrFail(item -> list.add(SequenceTool.convertToJava(item))); return list; }
/** * Evaluate the query, and return an iterator over its results. * <p>This method must not be used with an updating query.</p> * * @return an Iterator<XdmItem>. The XdmSequenceIterator class is a standard Java Iterator with an additional close() * method, which should be called to release resources if the client does not intend to read any more items * from the iterator. * * @throws SaxonApiUncheckedException if a dynamic error is detected while constructing the iterator. * It is also possible for an SaxonApiUncheckedException to be thrown by the hasNext() method of the * returned iterator if a dynamic error occurs while evaluating the result sequence. * @throws IllegalStateException if this is an updating query * @since 9.5.1.5. Previously the method returned Iterator<XdmItem>; the signature changed in Saxon 9.5.1.5 for reasons * described in bug 2016. */ public XdmSequenceIterator<XdmItem> iterator() throws SaxonApiUncheckedException { if (expression.isUpdateQuery()) { throw new IllegalStateException("Query is updating"); } try { return new XdmSequenceIterator<>(expression.iterator(context)); } catch (XPathException e) { throw new SaxonApiUncheckedException(e); } }
/** * Execute a the compiled Query, returning the results as a List of objects * obtained by converting the items in the query result to the nearest appropriate * Java type * * @param env Provides the dynamic query evaluation context * @return The results of the expression, as a List. The List represents the sequence * of items returned by the expression. Each item in the list will either be an * object representing a node, or an object representing an atomic value. * For the types of Java object that may be returned, see the description of the * {@link net.sf.saxon.xpath.XPathEvaluator#evaluate evaluate} method * of class XPathProcessor * @throws XPathException if a dynamic error occurs during query evaluation */ /*@NotNull*/ public List<Object> evaluate(/*@NotNull*/ DynamicQueryContext env) throws XPathException { if (isUpdateQuery()) { throw new XPathException("Cannot call evaluate() on an updating query"); } ArrayList<Object> list = new ArrayList<>(100); iterator(env).forEachOrFail(item -> list.add(SequenceTool.convertToJava(item))); return list; }
/** * Evaluate the XQuery expression, returning the result as an <code>XdmItem</code> (that is, * a single node or atomic value). * * @return an <code>XdmItem</code> representing the result of the query, or null if the query * returns an empty sequence. If the expression returns a sequence of more than one item, * any items after the first are ignored. * @throws SaxonApiException if a dynamic error occurs during the query evaluation. * @since 9.2 */ public XdmItem evaluateSingle() throws SaxonApiException { try { SequenceIterator<?> iter = expression.iterator(context); Item<?> next = iter.next(); return next == null ? null : (XdmItem) XdmValue.wrap(next); } catch (XPathException e) { throw new SaxonApiException(e); } }
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
/** * Perform the query, returning the results as an XdmValue. This method * must not be used with an updating query * * @return an XdmValue representing the results of the query * @throws SaxonApiException if the query fails with a dynamic error * @throws IllegalStateException if this is an updating query */ public XdmValue evaluate() throws SaxonApiException { if (expression.isUpdateQuery()) { throw new IllegalStateException("Query is updating"); } try { SequenceIterator<?> iter = expression.iterator(context); return XdmValue.wrap(iter.materialize()); } catch (XPathException e) { throw new SaxonApiException(e); } }
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
/** * Run a previously-compiled query * * @param context The dynamic context * @param query The compiled query * @param source The initial context item for the query (may be null) * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query, Item source) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); if (source != null) { dqc.setContextItem(source); } return query.iterator(dqc); }
/** * Execute the compiled Query, returning the first item in the result. * This is useful where it is known that the expression will only return * a singleton value (for example, a single node, or a boolean). * * @param env Provides the dynamic query evaluation context * @return The first item in the sequence returned by the expression. If the expression * returns an empty sequence, this method returns null. Otherwise, it returns the first * item in the result sequence, represented as a Java object using the same mapping as for * the {@link XQueryExpression#evaluate evaluate} method */ public Object evaluateSingle(DynamicQueryContext env) throws XPathException { if (isUpdating) { throw new XPathException("Cannot call evaluateSingle() on an updating query"); } SequenceIterator iterator = iterator(env); Item item = iterator.next(); if (item == null) { return null; } return Value.convertToJava(item); }
/** * Perform the query, returning the results as an XdmValue. This method * must not be used with an updating query * * @return an XdmValue representing the results of the query * @throws SaxonApiException if the query fails with a dynamic error * @throws IllegalStateException if this is an updating query */ public XdmValue evaluate() throws SaxonApiException { if (expression.isUpdateQuery()) { throw new IllegalStateException("Query is updating"); } try { SequenceIterator<?> iter = expression.iterator(context); return XdmValue.wrap(iter.materialize()); } catch (XPathException e) { throw new SaxonApiException(e); } }
/** * Evaluate the XQuery expression, returning the result as an <code>XdmItem</code> (that is, * a single node or atomic value). * * @return an <code>XdmItem</code> representing the result of the query, or null if the query * returns an empty sequence. If the expression returns a sequence of more than one item, * any items after the first are ignored. * @throws SaxonApiException if a dynamic error occurs during the query evaluation. * @since 9.2 */ public XdmItem evaluateSingle() throws SaxonApiException { try { SequenceIterator<?> iter = expression.iterator(context); Item<?> next = iter.next(); return next == null ? null : (XdmItem) XdmValue.wrap(next); } catch (XPathException e) { throw new SaxonApiException(e); } }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Run a previously-compiled query. The initial context item for the query is taken from the context * in which the query is called (if there is one); no parameters are supplied * * @param context The dynamic context * @param query The compiled query * @return the sequence representing the result of the query */ public static SequenceIterator query(XPathContext context, XQueryExpression query) throws XPathException { if (query == null) { return null; } DynamicQueryContext dqc = new DynamicQueryContext(context.getConfiguration()); Item c = context.getContextItem(); if (c != null) { dqc.setContextItem(c); } return query.iterator(dqc); }
/** * Execute the compiled Query, returning the first item in the result. * This is useful where it is known that the expression will only return * a singleton value (for example, a single node, or a boolean). * * @param env Provides the dynamic query evaluation context * @return The first item in the sequence returned by the expression. If the expression * returns an empty sequence, this method returns null. Otherwise, it returns the first * item in the result sequence, represented as a Java object using the same mapping as for * the {@link XQueryExpression#evaluate evaluate} method * @throws XPathException if evaluation fails with a dynamic error */ /*@Nullable*/ public Object evaluateSingle(/*@NotNull*/ DynamicQueryContext env) throws XPathException { if (isUpdateQuery()) { throw new XPathException("Cannot call evaluateSingle() on an updating query"); } SequenceIterator iterator = iterator(env); Item item = iterator.next(); if (item == null) { return null; } return SequenceTool.convertToJava(item); }
/** * Perform the query, returning the results as an XdmValue * @return an XdmValue representing the results of the query * @throws SaxonApiException if the query fails with a dynamic error */ public XdmValue evaluate() throws SaxonApiException { try { SequenceIterator iter = expression.iterator(context); ValueRepresentation result = SequenceExtent.makeSequenceExtent(iter); if (result instanceof NodeInfo) { return new XdmNode((NodeInfo)result); } else if (result instanceof AtomicValue) { return new XdmAtomicValue((AtomicValue)result); } else if (result instanceof EmptySequence) { return XdmEmptySequence.getInstance(); } else { return new XdmValue(result); } } catch (XPathException e) { throw new SaxonApiException(e); } }