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);
}
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());
}
}
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);
}
}
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();
}
}
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;
}
Aggregations