Search in sources :

Example 1 with LocalXMLResource

use of org.exist.xmldb.LocalXMLResource in project exist by eXist-db.

the class TestCase method getResource.

public Resource getResource(Object r) throws XMLDBException {
    LocalCollection collection = null;
    Subject user = null;
    LocalXMLResource res = null;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    if (r instanceof NodeProxy) {
        NodeProxy p = (NodeProxy) r;
        res = new LocalXMLResource(user, pool, collection, p);
    } else if (r instanceof Node) {
        res = new LocalXMLResource(user, pool, collection, XmldbURI.EMPTY_URI);
        res.setContentAsDOM((Node) r);
    } else if (r instanceof AtomicValue) {
        res = new LocalXMLResource(user, pool, collection, XmldbURI.EMPTY_URI);
        res.setContent(r);
    } else if (r instanceof LocalXMLResource)
        res = (LocalXMLResource) r;
    else
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "unknown object " + r.getClass());
    try {
        Field field = res.getClass().getDeclaredField("outputProperties");
        field.setAccessible(true);
        field.set(res, new Properties(defaultProperties));
    } catch (Exception e) {
    }
    return res;
}
Also used : Field(java.lang.reflect.Field) LocalXMLResource(org.exist.xmldb.LocalXMLResource) LocalCollection(org.exist.xmldb.LocalCollection) Node(org.w3c.dom.Node) XMLDBException(org.xmldb.api.base.XMLDBException) AtomicValue(org.exist.xquery.value.AtomicValue) Properties(java.util.Properties) NodeProxy(org.exist.dom.persistent.NodeProxy) Subject(org.exist.security.Subject) BrokerPool(org.exist.storage.BrokerPool) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 2 with LocalXMLResource

use of org.exist.xmldb.LocalXMLResource in project exist by eXist-db.

the class InternalModuleTest method moduleVariablesQuery.

private void moduleVariablesQuery(final EXistXQueryService queryService, final Source query, final long expectedCount) throws XMLDBException {
    // this variation of execute will use the XQueryPool for caching
    final ResourceSet result = queryService.execute(query);
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final LocalXMLResource resource = (LocalXMLResource) result.getResource(0);
    assertNotNull(resource);
    final Node actualDoc = resource.getContentAsDOM();
    final javax.xml.transform.Source expected = Input.fromString("<variables><var1>" + expectedCount + "</var1></variables>").build();
    final javax.xml.transform.Source actual = Input.fromNode(actualDoc).build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : LocalXMLResource(org.exist.xmldb.LocalXMLResource) Diff(org.xmlunit.diff.Diff) Node(org.w3c.dom.Node) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 3 with LocalXMLResource

use of org.exist.xmldb.LocalXMLResource in project exist by eXist-db.

the class XPathUtil method getNode.

/**
 * Converts an XMLResource into a NodeProxy.
 *
 * @param broker The DBBroker to use to access the database
 * @param xres The XMLResource to convert
 * @return A NodeProxy for accessing the content represented by xres
 * @throws XPathException if an XMLDBException is encountered
 */
public static final NodeProxy getNode(DBBroker broker, XMLResource xres) throws XPathException {
    if (xres instanceof LocalXMLResource) {
        final LocalXMLResource lres = (LocalXMLResource) xres;
        try {
            return lres.getNode();
        } catch (final XMLDBException xe) {
            throw new XPathException("Failed to convert LocalXMLResource to node: " + xe.getMessage());
        }
    }
    DocumentImpl document;
    try {
        document = broker.getCollection(XmldbURI.xmldbUriFor(xres.getParentCollection().getName())).getDocument(broker, XmldbURI.xmldbUriFor(xres.getDocumentId()));
    } catch (final URISyntaxException xe) {
        throw new XPathException(xe);
    } catch (final XMLDBException xe) {
        throw new XPathException("Failed to get document for RemoteXMLResource: " + xe.getMessage());
    } catch (final PermissionDeniedException pde) {
        throw new XPathException("Failed to get document: " + pde.getMessage());
    }
    final NodeId nodeId = broker.getBrokerPool().getNodeFactory().createFromString(((RemoteXMLResource) xres).getNodeId());
    return new NodeProxy(document, nodeId);
}
Also used : LocalXMLResource(org.exist.xmldb.LocalXMLResource) NodeId(org.exist.numbering.NodeId) XMLDBException(org.xmldb.api.base.XMLDBException) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 4 with LocalXMLResource

use of org.exist.xmldb.LocalXMLResource in project exist by eXist-db.

the class InternalModuleTest method requestResponseSessionVariablesQuery_4_x_X_Api.

private void requestResponseSessionVariablesQuery_4_x_X_Api(final EXistXQueryService queryService, final Source query) throws XMLDBException {
    // this variation of execute will use the XQueryPool for caching
    final ResourceSet result = queryService.execute(query);
    assertNotNull(result);
    assertEquals(1, result.getSize());
    final LocalXMLResource resource = (LocalXMLResource) result.getResource(0);
    assertNotNull(resource);
    final Node actualDoc = resource.getContentAsDOM();
    final javax.xml.transform.Source expected = Input.fromString("<vars><request>XPDY0002</request><session>XPDY0002</session><response>XPDY0002</response></vars>").build();
    final javax.xml.transform.Source actual = Input.fromNode(actualDoc).build();
    final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : LocalXMLResource(org.exist.xmldb.LocalXMLResource) Diff(org.xmlunit.diff.Diff) Node(org.w3c.dom.Node) ResourceSet(org.xmldb.api.base.ResourceSet)

Example 5 with LocalXMLResource

use of org.exist.xmldb.LocalXMLResource in project exist by eXist-db.

the class DocTest method testURIResolveWithEval.

@Test
public void testURIResolveWithEval() throws XMLDBException {
    String query = "util:eval(xs:anyURI('/db/test/test.xq'), false(), ())";
    ResourceSet result = existEmbeddedServer.executeQuery(query);
    LocalXMLResource res = (LocalXMLResource) result.getResource(0);
    assertNotNull(res);
    Node n = res.getContentAsDOM();
    assertEquals("y", n.getLocalName());
    query = "util:eval(xs:anyURI('/db/test/test1.xq'), false(), ())";
    result = existEmbeddedServer.executeQuery(query);
    res = (LocalXMLResource) result.getResource(0);
    assertNotNull(res);
    n = res.getContentAsDOM();
    assertEquals("x", n.getLocalName());
    query = "util:eval(xs:anyURI('/db/test/test2.xq'), false(), ())";
    result = existEmbeddedServer.executeQuery(query);
    res = (LocalXMLResource) result.getResource(0);
    assertNotNull(res);
    n = res.getContentAsDOM();
    assertEquals("x", n.getLocalName());
}
Also used : LocalXMLResource(org.exist.xmldb.LocalXMLResource) Node(org.w3c.dom.Node) ResourceSet(org.xmldb.api.base.ResourceSet)

Aggregations

LocalXMLResource (org.exist.xmldb.LocalXMLResource)5 Node (org.w3c.dom.Node)4 ResourceSet (org.xmldb.api.base.ResourceSet)3 NodeProxy (org.exist.dom.persistent.NodeProxy)2 XMLDBException (org.xmldb.api.base.XMLDBException)2 Diff (org.xmlunit.diff.Diff)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 NodeId (org.exist.numbering.NodeId)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 Subject (org.exist.security.Subject)1 BrokerPool (org.exist.storage.BrokerPool)1 LocalCollection (org.exist.xmldb.LocalCollection)1 AtomicValue (org.exist.xquery.value.AtomicValue)1 SAXException (org.xml.sax.SAXException)1