Search in sources :

Example 6 with SequenceIterator

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

the class Expand method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // apply serialization options set on the XQuery context
    final Properties serializeOptions = new Properties();
    serializeOptions.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes");
    serializeOptions.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
    if (getArgumentCount() == 2) {
        final String serOpts = args[1].getStringValue();
        final String[] contents = Option.tokenize(serOpts);
        for (String content : contents) {
            final String[] pair = Option.parseKeyValuePair(content);
            if (pair == null) {
                throw new XPathException(this, "Found invalid serialization option: " + content);
            }
            logger.debug("Setting serialization property: {} = {}", pair[0], pair[1]);
            serializeOptions.setProperty(pair[0], pair[1]);
        }
    } else {
        context.checkOptions(serializeOptions);
    }
    context.pushDocumentContext();
    try {
        final InMemoryNodeSet result = new InMemoryNodeSet();
        final MemTreeBuilder builder = new MemTreeBuilder(getContext());
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        int attrNr = -1;
        for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            final NodeValue next = (NodeValue) i.nextItem();
            final short nodeType = ((INodeHandle) next).getNodeType();
            builder.startDocument();
            if (nodeType == Node.ATTRIBUTE_NODE) {
                // NOTE: Attributes nodes need special handling as they cannot be directly serialized via SAX to a ContentHandler
                final Attr attr = (Attr) next.getNode();
                String ns = attr.getNamespaceURI();
                if (ns == null || ns.isEmpty()) {
                    ns = XMLConstants.NULL_NS_URI;
                }
                attrNr = builder.addAttribute(new QName(attr.getLocalName(), ns), attr.getValue());
            } else {
                next.toSAX(context.getBroker(), receiver, serializeOptions);
            }
            builder.endDocument();
            if (Node.DOCUMENT_NODE == nodeType) {
                result.add(builder.getDocument());
            } else if (Node.ATTRIBUTE_NODE == nodeType) {
                result.add(builder.getDocument().getAttribute(attrNr));
            } else {
                result.add(builder.getDocument().getNode(1));
            }
            builder.reset(getContext());
        }
        return result;
    } catch (final SAXException e) {
        throw new XPathException(this, e);
    } finally {
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) INodeHandle(org.exist.dom.INodeHandle) QName(org.exist.dom.QName) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) Properties(java.util.Properties) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator)

Example 7 with SequenceIterator

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

the class Shared method getUrls.

/**
 * Get URL values of sequence items.
 *
 * @param s Sequence
 * @return URLs of items in sequence
 * @throws XPathException Thrown when an item does not have an associated URL.
 */
public static String[] getUrls(Sequence s) throws XPathException {
    final ArrayList<String> urls = new ArrayList<>();
    final SequenceIterator i = s.iterate();
    while (i.hasNext()) {
        final Item next = i.nextItem();
        final String url = getUrl(next);
        urls.add(url);
    }
    String[] returnUrls = new String[urls.size()];
    returnUrls = urls.toArray(returnUrls);
    return returnUrls;
}
Also used : Item(org.exist.xquery.value.Item) ValidationReportItem(org.exist.validation.ValidationReportItem) SequenceIterator(org.exist.xquery.value.SequenceIterator) ArrayList(java.util.ArrayList)

Example 8 with SequenceIterator

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

the class Insert method seq2nodeList.

private NodeList seq2nodeList(Sequence contentSeq) throws XPathException {
    final NodeListImpl nl = new NodeListImpl();
    for (final SequenceIterator i = contentSeq.iterate(); i.hasNext(); ) {
        final Item item = i.nextItem();
        if (Type.subTypeOf(item.getType(), Type.NODE)) {
            final NodeValue val = (NodeValue) item;
            nl.add(val.getNode());
        }
    }
    return nl;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) NodeListImpl(org.exist.dom.NodeListImpl) SequenceIterator(org.exist.xquery.value.SequenceIterator)

Example 9 with SequenceIterator

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

the class TestCase method catchError.

public Exception catchError(Sequence result) {
    try {
        for (SequenceIterator i = result.iterate(); i.hasNext(); ) {
            Resource xmldbResource = getResource(i.nextItem());
            xmldbResource.getContent().toString();
        }
    } catch (Exception e) {
        return e;
    }
    return null;
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) LocalXMLResource(org.exist.xmldb.LocalXMLResource) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 10 with SequenceIterator

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

the class Deployment method isResourceDir.

private boolean isResourceDir(final XmldbURI target, final InMemoryNodeSet resources) {
    // iterate here or pass into scandirectory directly or even save as class property???
    for (final SequenceIterator i = resources.iterate(); i.hasNext(); ) {
        final ElementImpl child = (ElementImpl) i.nextItem();
        final String resourcePath = child.getAttribute(RESOURCES_PATH_ATTRIBUTE);
        if (target.toString().endsWith(resourcePath)) {
            return true;
        }
    }
    return false;
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator)

Aggregations

SequenceIterator (org.exist.xquery.value.SequenceIterator)75 Sequence (org.exist.xquery.value.Sequence)40 Item (org.exist.xquery.value.Item)35 XPathException (org.exist.xquery.XPathException)19 ValueSequence (org.exist.xquery.value.ValueSequence)16 Test (org.junit.Test)16 SAXException (org.xml.sax.SAXException)11 XQuery (org.exist.xquery.XQuery)10 Collator (com.ibm.icu.text.Collator)9 AtomicValue (org.exist.xquery.value.AtomicValue)9 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 DBBroker (org.exist.storage.DBBroker)8 NumericValue (org.exist.xquery.value.NumericValue)8 Properties (java.util.Properties)7 StringWriter (java.io.StringWriter)6 EXistException (org.exist.EXistException)6 NodeProxy (org.exist.dom.persistent.NodeProxy)6 NodeSet (org.exist.dom.persistent.NodeSet)6 BrokerPool (org.exist.storage.BrokerPool)6 IOException (java.io.IOException)5