Search in sources :

Example 11 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class XQueryDeclareContextItemTest method declareContextItemExternalElement.

@Test
public void declareContextItemExternalElement() throws EXistException, PermissionDeniedException, XPathException, SAXException {
    final String query = "xquery version \"3.0\";\n" + "declare namespace env=\"http://www.w3.org/2003/05/soap-envelope\";\n" + "declare context item as element(env:Envelope) external;\n" + "<wrap>{.}</wrap>";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xquery = pool.getXQueryService();
    try (final DBBroker broker = pool.getBroker()) {
        final MemTreeBuilder builder = new MemTreeBuilder();
        builder.startDocument();
        builder.startElement(new QName("Envelope", "http://www.w3.org/2003/05/soap-envelope"), null);
        builder.endElement();
        builder.endDocument();
        final ElementImpl elem = (ElementImpl) builder.getDocument().getDocumentElement();
        final Sequence result = xquery.execute(broker, query, elem);
        assertEquals(1, result.getItemCount());
        assertEquals("<wrap><Envelope xmlns=\"http://www.w3.org/2003/05/soap-envelope\"/></wrap>", serialize(broker, (NodeValue) result.itemAt(0)));
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 12 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class GetPermissionsTest method getPermissionsNestedXml.

/**
 * See https://github.com/eXist-db/exist/issues/3231
 */
@Test
public void getPermissionsNestedXml() throws EXistException, PermissionDeniedException, XPathException {
    final String query = "<outer><inner perm=\"{sm:get-permissions(xs:anyURI(\"/db\"))/sm:permission/@owner}\"/></outer>";
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.getBroker()) {
        final XQuery xquery = existEmbeddedServer.getBrokerPool().getXQueryService();
        final Sequence result = xquery.execute(broker, query, null);
        assertEquals(1, result.getItemCount());
        final Source expected = Input.fromString("<outer><inner perm=\"" + SecurityManagerImpl.SYSTEM + "\"/></outer>").build();
        final Source actual = Input.fromDocument(((ElementImpl) result.itemAt(0)).getOwnerDocument()).build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 13 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class InspectModuleTest method xqDoc_withAtSignInline.

@Ignore("https://github.com/eXist-db/exist/issues/1386")
@Test
public void xqDoc_withAtSignInline() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun1\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("Some description.", descriptions.item(0).getFirstChild().getTextContent());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("taxonomy[@type = \"reign\"]", returns.item(0).getFirstChild().getTextContent());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 14 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class InspectModuleTest method xqDoc_withParamsAndReturn.

@Test
public void xqDoc_withParamsAndReturn() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun2\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("Some other description.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(2, arguments.getLength());
        assertEquals("first parameter", arguments.item(0).getFirstChild().getNodeValue());
        assertEquals("second parameter", arguments.item(1).getFirstChild().getNodeValue());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("our result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 15 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class RESTServer method parseXML.

private ElementImpl parseXML(final BrokerPool pool, final String content, final NamespaceExtractor nsExtractor) throws SAXException, IOException {
    final InputSource src = new InputSource(new StringReader(content));
    final XMLReaderPool parserPool = pool.getParserPool();
    XMLReader reader = null;
    try {
        reader = parserPool.borrowXMLReader();
        final SAXAdapter adapter = new SAXAdapter();
        nsExtractor.setContentHandler(adapter);
        reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
        nsExtractor.setParent(reader);
        nsExtractor.parse(src);
        final Document doc = adapter.getDocument();
        return (ElementImpl) doc.getDocumentElement();
    } finally {
        if (reader != null) {
            parserPool.returnXMLReader(reader);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ElementImpl(org.exist.dom.memtree.ElementImpl) SAXAdapter(org.exist.dom.memtree.SAXAdapter) Document(org.w3c.dom.Document) XMLReader(org.xml.sax.XMLReader)

Aggregations

ElementImpl (org.exist.dom.memtree.ElementImpl)16 BrokerPool (org.exist.storage.BrokerPool)7 DBBroker (org.exist.storage.DBBroker)7 Sequence (org.exist.xquery.value.Sequence)7 QName (org.exist.dom.QName)6 XQuery (org.exist.xquery.XQuery)6 NodeList (org.w3c.dom.NodeList)6 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)5 Item (org.exist.xquery.value.Item)5 Test (org.junit.Test)5 Element (org.w3c.dom.Element)5 Txn (org.exist.storage.txn.Txn)4 XQueryContext (org.exist.xquery.XQueryContext)3 Properties (java.util.Properties)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Collection (org.exist.collections.Collection)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 XmldbURI (org.exist.xmldb.XmldbURI)2 Node (org.w3c.dom.Node)2 InputSource (org.xml.sax.InputSource)2