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