Search in sources :

Example 6 with NameTest

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>");
    }
}
Also used : NameTest(org.exist.xquery.NameTest) DBBroker(org.exist.storage.DBBroker) QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence) Test(org.junit.Test) NameTest(org.exist.xquery.NameTest)

Example 7 with NameTest

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NameTest(org.exist.xquery.NameTest) QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence) Test(org.junit.Test) NameTest(org.exist.xquery.NameTest)

Example 8 with NameTest

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NameTest(org.exist.xquery.NameTest) QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence) Test(org.junit.Test) NameTest(org.exist.xquery.NameTest)

Example 9 with NameTest

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;
}
Also used : XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) SAXException(org.xml.sax.SAXException) NameTest(org.exist.xquery.NameTest) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 10 with NameTest

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;
}
Also used : NameTest(org.exist.xquery.NameTest) EXistException(org.exist.EXistException) IllegalQNameException(org.exist.dom.QName.IllegalQNameException) IOException(java.io.IOException)

Aggregations

NameTest (org.exist.xquery.NameTest)19 QName (org.exist.dom.QName)16 DBBroker (org.exist.storage.DBBroker)16 Test (org.junit.Test)16 Sequence (org.exist.xquery.value.Sequence)12 NodeSelector (org.exist.xquery.NodeSelector)5 IOException (java.io.IOException)2 EXistException (org.exist.EXistException)2 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)2 AncestorSelector (org.exist.xquery.AncestorSelector)2 XMLStreamException (javax.xml.stream.XMLStreamException)1 ChildSelector (org.exist.xquery.ChildSelector)1 DescendantOrSelfSelector (org.exist.xquery.DescendantOrSelfSelector)1 DescendantSelector (org.exist.xquery.DescendantSelector)1 XPathException (org.exist.xquery.XPathException)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1