Search in sources :

Example 1 with DocumentImpl

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

the class IdFunctionTest method sameRealAndEffectiveUsers.

/**
 * Test of eval method, of class IdFunction.
 * when real and effective users are the same
 */
@Test
public void sameRealAndEffectiveUsers() throws XPathException, XpathException {
    final XQueryContext mckContext = createMockBuilder(XQueryContext.class).addMockedMethod("pushDocumentContext").addMockedMethod("getDocumentBuilder", new Class[0]).addMockedMethod("popDocumentContext").addMockedMethod("getRealUser").addMockedMethod("getEffectiveUser").createMock();
    final Subject mckUser = EasyMock.createMock(Subject.class);
    final String username = "user1";
    mckContext.pushDocumentContext();
    expectLastCall().once();
    expect(mckContext.getDocumentBuilder()).andReturn(new MemTreeBuilder());
    mckContext.popDocumentContext();
    expectLastCall().once();
    expect(mckContext.getRealUser()).andReturn(mckUser).times(2);
    expect(mckUser.getName()).andReturn(username);
    expect(mckUser.getGroups()).andReturn(new String[] { "group1", "group2" });
    expect(mckUser.getId()).andReturn(1);
    expect(mckContext.getEffectiveUser()).andReturn(mckUser);
    expect(mckUser.getId()).andReturn(1);
    replay(mckUser, mckContext);
    final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
    final Sequence result = idFunctions.eval(new Sequence[] { Sequence.EMPTY_SEQUENCE }, null);
    assertEquals(1, result.getItemCount());
    final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
    final Map<String, String> namespaces = new HashMap<>();
    namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
    xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));
    final DocumentImpl resultDoc = (DocumentImpl) result.itemAt(0);
    final String actualRealUsername = xpathEngine.evaluate("/sm:id/sm:real/sm:username", resultDoc);
    assertEquals(username, actualRealUsername);
    final String actualEffectiveUsername = xpathEngine.evaluate("/sm:id/sm:effective/sm:username", resultDoc);
    assertEquals("", actualEffectiveUsername);
    verify(mckUser, mckContext);
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XpathEngine(org.custommonkey.xmlunit.XpathEngine) HashMap(java.util.HashMap) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) SimpleNamespaceContext(org.custommonkey.xmlunit.SimpleNamespaceContext) DocumentImpl(org.exist.dom.memtree.DocumentImpl) Subject(org.exist.security.Subject) Test(org.junit.Test)

Example 2 with DocumentImpl

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

the class ParseHtmlTest method parseHtml.

@Test
public void parseHtml() throws EXistException, PermissionDeniedException, XPathException {
    final String query = "util:parse-html(\"<p>hello <img src='1.jpg'></p>\")";
    final XQuery xquery = server.getBrokerPool().getXQueryService();
    try (final DBBroker broker = server.getBrokerPool().getBroker()) {
        final Sequence result = xquery.execute(broker, query, null);
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof DocumentImpl);
        final Source expected = Input.fromString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><HTML><head xmlns=\"http://www.w3.org/1999/xhtml\"/><BODY><p>hello <img src=\"1.jpg\"/></p></BODY></HTML>").build();
        final Source actual = Input.fromDocument((DocumentImpl) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForIdentical().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.memtree.DocumentImpl) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 3 with DocumentImpl

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

the class EXistResult method add.

@Override
public void add(final HttpResponse response) throws HttpClientException {
    final EXistTreeBuilder builder = new EXistTreeBuilder(context);
    response.outputResponseElement(builder);
    final DocumentImpl doc = builder.close();
    try {
        // we add the root *element* to the result sequence
        final NodeTest kind = new TypeTest(Type.ELEMENT);
        // add the original items after
        if (result.isEmpty()) {
            doc.selectChildren(kind, result);
        } else {
            final ValueSequence newResult = new ValueSequence();
            doc.selectChildren(kind, newResult);
            newResult.addAll(result);
            result = newResult;
        }
    } catch (final XPathException xpe) {
        throw new HttpClientException("Unable to add HttpResponse to result:" + xpe.getMessage(), xpe);
    }
}
Also used : HttpClientException(org.expath.httpclient.HttpClientException) XPathException(org.exist.xquery.XPathException) TypeTest(org.exist.xquery.TypeTest) DocumentImpl(org.exist.dom.memtree.DocumentImpl) NodeTest(org.exist.xquery.NodeTest)

Example 4 with DocumentImpl

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

the class MessageFunctions method handlePart.

private void handlePart(Part part, MemTreeBuilder builder) throws MessagingException, IOException, XPathException, SAXException {
    Object content = part.getContent();
    if (content instanceof Multipart) {
        handleMultipart((Multipart) content, builder);
        return;
    }
    String disposition = part.getDisposition();
    String contentType = part.getContentType();
    // Check if plain
    if (contentType.contains("text/plain")) {
        builder.startElement(new QName("text", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        mimeParamsToAttributes(builder, part.getContentType());
        builder.characters(part.getContent().toString());
        builder.endElement();
    } else if (contentType.contains("text/html")) {
        builder.startElement(new QName("xhtml", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        mimeParamsToAttributes(builder, part.getContentType());
        // extract and clean up the html
        DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
        try (InputStream inputStream = part.getInputStream()) {
            DocumentImpl html = ModuleUtils.htmlToXHtml(context, new StreamSource(inputStream), null, null);
            ElementImpl rootElem = (ElementImpl) html.getDocumentElement();
            html.copyTo(rootElem, receiver);
            builder.endElement();
        }
    } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
        builder.startElement(new QName("attachment", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        builder.addAttribute(new QName("filename", null, null), part.getFileName());
        mimeParamsToAttributes(builder, part.getContentType());
        handleBinaryContent(part, builder);
        builder.endElement();
    } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
        builder.startElement(new QName("inline", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        MimeBodyPart mbp = (MimeBodyPart) part;
        builder.addAttribute(new QName("filename", null, null), mbp.getFileName());
        // fix content id so that it matches the cid: format within the html
        if (mbp.getContentID() != null) {
            builder.addAttribute(new QName("content-id", null, null), "cid:" + mbp.getContentID().replaceAll("^<|>$", ""));
        }
        mimeParamsToAttributes(builder, part.getContentType());
        handleBinaryContent(part, builder);
        builder.endElement();
    } else {
        // Should never happen
        builder.startElement(new QName("other", MailModule.NAMESPACE_URI, MailModule.PREFIX), null);
        mimeParamsToAttributes(builder, part.getContentType());
        builder.addAttribute(new QName("disposition", null, null), part.getDisposition());
        builder.characters(part.getContent().toString());
        builder.endElement();
    }
}
Also used : ElementImpl(org.exist.dom.memtree.ElementImpl) QName(org.exist.dom.QName) StreamSource(javax.xml.transform.stream.StreamSource) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 5 with DocumentImpl

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

the class FunRoot method eval.

/* (non-Javadoc)
         * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
         */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence seq;
    Sequence result;
    Item item;
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    if (contextSequence == null || contextSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    }
    // If we have one argumment, we take it into account
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else // Otherwise, we take the context sequence and we iterate over it
    {
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    if (seq.isPersistentSet()) {
        result = new ExtArrayNodeSet(seq.getItemCount());
    } else {
        result = new ValueSequence(seq.getItemCount());
    }
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        item = i.nextItem();
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
        }
        final Sequence s = item.toSequence();
        if (s.isPersistentSet()) {
            final NodeProxy p = s.toNodeSet().get(0);
            result.add(new NodeProxy(p.getOwnerDocument()));
        } else {
            if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
                result.add(item);
            } else if (item.getType() == Type.DOCUMENT) {
                result.add((DocumentImpl) item);
            } else {
                result.add(((NodeImpl) item).getOwnerDocument());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.memtree.DocumentImpl)23 NodeImpl (org.exist.dom.memtree.NodeImpl)9 XPathException (org.exist.xquery.XPathException)8 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)6 Document (org.w3c.dom.Document)6 Sequence (org.exist.xquery.value.Sequence)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 NodeId (org.exist.numbering.NodeId)4 IOException (java.io.IOException)3 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)3 SAXException (org.xml.sax.SAXException)3 HashMap (java.util.HashMap)2 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)2 SimpleNamespaceContext (org.custommonkey.xmlunit.SimpleNamespaceContext)2 XpathEngine (org.custommonkey.xmlunit.XpathEngine)2 QName (org.exist.dom.QName)2 SAXAdapter (org.exist.dom.memtree.SAXAdapter)2 org.exist.dom.persistent (org.exist.dom.persistent)2 NodeSet (org.exist.dom.persistent.NodeSet)2 Subject (org.exist.security.Subject)2