use of org.exist.xquery.NameTest in project exist by eXist-db.
the class BasicNodeSetTest method selectParentChild.
@Test
public void selectParentChild() throws XPathException, SAXException, PermissionDeniedException, EXistException {
NameTest test = new NameTest(Type.ELEMENT, new QName("SPEAKER", ""));
try (final DBBroker broker = existEmbeddedServer.getBrokerPool().get(Optional.of(existEmbeddedServer.getBrokerPool().getSecurityManager().getSystemSubject()))) {
NodeSet speakers = broker.getStructuralIndex().findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null);
Sequence smallSet = executeQuery(broker, "//SPEECH/LINE[fn:contains(., 'perturbed spirit')]/ancestor::SPEECH", 1, null);
NodeSet result = NodeSetHelper.selectParentChild(speakers, smallSet.toNodeSet(), NodeSet.DESCENDANT, -1);
assertEquals(1, result.getLength());
String value = serialize(broker, result.itemAt(0));
assertEquals(value, "<SPEAKER>HAMLET</SPEAKER>");
}
}
use of org.exist.xquery.NameTest in project exist by eXist-db.
the class BasicNodeSetTest method extArrayNodeSet_selectParentChild_2.
@Test
public void extArrayNodeSet_selectParentChild_2() throws XPathException, SAXException, PermissionDeniedException, EXistException {
try (final DBBroker broker = existEmbeddedServer.getBrokerPool().get(Optional.of(existEmbeddedServer.getBrokerPool().getSecurityManager().getSystemSubject()))) {
Sequence nestedSet = executeQuery(broker, "//section[@n = ('1.1', '1.1.2', '1.2')]", 3, null);
NameTest test = new NameTest(Type.ELEMENT, new QName("para", ""));
NodeSet children = broker.getStructuralIndex().findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null);
NodeSet result = children.selectParentChild(nestedSet.toNodeSet(), NodeSet.DESCENDANT);
assertEquals(2, result.getLength());
}
}
use of org.exist.xquery.NameTest in project exist by eXist-db.
the class BasicNodeSetTest method extArrayNodeSet_selectParentChild_3.
@Test
public void extArrayNodeSet_selectParentChild_3() throws XPathException, SAXException, PermissionDeniedException, EXistException {
try (final DBBroker broker = existEmbeddedServer.getBrokerPool().get(Optional.of(existEmbeddedServer.getBrokerPool().getSecurityManager().getSystemSubject()))) {
Sequence nestedSet = executeQuery(broker, "//section[@n = ('1.1', '1.1.1', '1.2')]", 3, null);
NameTest test = new NameTest(Type.ELEMENT, new QName("para", ""));
NodeSet children = broker.getStructuralIndex().findElementsByTagName(ElementValue.ELEMENT, docs, test.getName(), null);
NodeSet result = children.selectParentChild(nestedSet.toNodeSet(), NodeSet.DESCENDANT);
assertEquals(4, result.getLength());
}
}
use of org.exist.xquery.NameTest in project exist by eXist-db.
the class Marshaller method demarshall.
public static Sequence demarshall(NodeImpl node) throws XMLStreamException, XPathException {
final String ns = node.getNamespaceURI();
if (ns == null || !NAMESPACE.equals(ns)) {
throw new XMLStreamException("Root element is not in the correct namespace. Expected: " + NAMESPACE);
}
if (!SEQ_ELEMENT.equals(node.getLocalName())) {
throw new XMLStreamException("Root element should be a " + SEQ_ELEMENT_QNAME);
}
final ValueSequence result = new ValueSequence();
final InMemoryNodeSet values = new InMemoryNodeSet();
node.selectChildren(new NameTest(Type.ELEMENT, VALUE_QNAME), values);
for (final SequenceIterator i = values.iterate(); i.hasNext(); ) {
final ElementImpl child = (ElementImpl) i.nextItem();
final String typeName = child.getAttribute(ATTR_TYPE);
if (typeName != null) {
final int type = Type.getType(typeName);
Item item;
if (Type.subTypeOf(type, Type.NODE)) {
item = (Item) child.getFirstChild();
if (type == Type.DOCUMENT) {
final DocumentImpl n = (DocumentImpl) item;
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver();
try {
receiver.startDocument();
n.copyTo(n, receiver);
receiver.endDocument();
} catch (final SAXException e) {
throw new XPathException("Error while demarshalling node: " + e.getMessage(), e);
}
item = (Item) receiver.getDocument();
}
} else {
final StringBuilder data = new StringBuilder();
Node txt = child.getFirstChild();
while (txt != null) {
if (!(txt.getNodeType() == Node.TEXT_NODE || txt.getNodeType() == Node.CDATA_SECTION_NODE)) {
throw new XMLStreamException("sx:value should only contain text if type is " + typeName);
}
data.append(txt.getNodeValue());
txt = txt.getNextSibling();
}
item = new StringValue(data.toString()).convertTo(type);
}
result.add(item);
}
}
return result;
}
use of org.exist.xquery.NameTest in project exist by eXist-db.
the class DocumentImpl method findElementsByTagName.
/**
* The method <code>findElementsByTagName</code>
*
* @param root a <code>NodeHandle</code> value
* @param qname a <code>QName</code> value
* @return a <code>NodeList</code> value
*/
protected NodeList findElementsByTagName(final NodeHandle root, final QName qname) {
try (final DBBroker broker = pool.getBroker()) {
final MutableDocumentSet docs = new DefaultDocumentSet();
docs.add(this);
final NewArrayNodeSet contextSet = new NewArrayNodeSet();
contextSet.add(new NodeProxy(this, root.getNodeId(), root.getInternalAddress()));
return broker.getStructuralIndex().scanByType(ElementValue.ELEMENT, Constants.DESCENDANT_AXIS, new NameTest(Type.ELEMENT, qname), false, docs, contextSet, Expression.NO_CONTEXT_ID);
} catch (final Exception e) {
LOG.warn("Exception while finding elements: {}", e.getMessage(), e);
}
return NodeSet.EMPTY_SET;
}
Aggregations