use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class ImageFilterTest method filterAddsImageMarkers.
@Test
public void filterAddsImageMarkers() {
AttachmentReference attachmentReference = new AttachmentReference("-foo--bar.png-", this.documentReference);
when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
ResourceReference resourceReference = new ResourceReference("-foo--bar.png-", ResourceType.ATTACHMENT);
resourceReference.setTyped(false);
when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|-foo--bar.png-");
filterAndAssertOutput("<img src=\"../../some/path/-foo--b%61r.png-\"/>", Collections.singletonMap("targetDocument", "Path.To.Page"), "<!--startimage:false|-|attach|-|-foo-\\-bar.png-\\--><img src=\"/path/to/foo.png\"/><!--stopimage-->");
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class DefaultOfficeViewerScriptServiceTest method view.
@Test
public void view() throws Exception {
Execution execution = mocker.getInstance(Execution.class);
when(execution.getContext()).thenReturn(new ExecutionContext());
AttachmentReference attachmentReference = new AttachmentReference("file.odt", new DocumentReference("wiki", "Space", "Page"));
DocumentModelBridge document = mock(DocumentModelBridge.class);
DocumentAccessBridge documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
when(documentAccessBridge.isDocumentViewable(attachmentReference.getDocumentReference())).thenReturn(true);
when(documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference())).thenReturn(document);
when(document.getSyntax()).thenReturn(Syntax.TEX_1_0);
XDOM xdom = new XDOM(Collections.<Block>emptyList());
OfficeViewer viewer = mocker.getInstance(OfficeViewer.class);
when(viewer.createView(attachmentReference, Collections.<String, String>emptyMap())).thenReturn(xdom);
BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0");
Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference));
TransformationManager transformationManager = mocker.getInstance(TransformationManager.class);
verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class));
verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class));
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class PdfURLFactory method saveAttachmentReference.
/**
* @param url the URL to save in the attachment map
* @param wiki the wiki where the attachment is located
* @param spaces a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If
* a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
* @param page the page where the attachment is located
* @param fileName the name of the attachment file
* @param context the XWiki Context where to find the attachment map that we add to
* @see PDFResourceResolver
*/
private void saveAttachmentReference(URL url, String wiki, String spaces, String page, String fileName, XWikiContext context) {
// Save Entity Reference pointed to by this URL in the Context so that it can be used at export time in the
// PDF URIResolver code.
Map<String, AttachmentReference> attachmentMap = (Map<String, AttachmentReference>) context.get(PDF_EXPORT_CONTEXT_KEY);
if (attachmentMap == null) {
attachmentMap = new HashMap<>();
context.put(PDF_EXPORT_CONTEXT_KEY, attachmentMap);
}
attachmentMap.put(url.toString(), new AttachmentReference(fileName, new DocumentReference(wiki, this.legacySpaceResolver.resolve(spaces), page)));
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class PDFURIResolver method resolve.
@Override
public Source resolve(String href, String base) throws TransformerException {
if (this.attachmentMap != null) {
// implemented for TemporaryResource...
if (href.contains(TEX_ACTION)) {
// Note: See the comments in FormulaMacro to understand why we do a replace...
AttachmentReference reference = this.attachmentMap.get(href.replace(TEX_ACTION, "/download/"));
if (reference != null) {
// Get the generated image's input stream
ImageStorage storage = Utils.getComponent(ImageStorage.class);
ImageData image = storage.get(reference.getName());
return new StreamSource(new ByteArrayInputStream(image.getData()));
}
}
// TODO: end HACK
AttachmentReference reference = this.attachmentMap.get(href);
if (reference != null) {
try {
XWikiDocument xdoc = this.context.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), this.context);
// TODO: handle revisions
XWikiAttachment attachment = xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName());
return new StreamSource(attachment.getContentInputStream(this.context));
} catch (Exception e) {
throw new TransformerException(String.format("Failed to resolve export URI [%s]", href), e);
}
}
}
// Defaults to the default URI Resolver in FO
return null;
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class DefaultResourceReferenceEntityReferenceResolverTest method resolveTypeAttachment.
@Test
public void resolveTypeAttachment() throws ComponentLookupException {
// When the page does not exist
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
// When the page exist
this.existingDocuments.add(new DocumentReference(WIKI, SPACE, PAGE));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, SPACE, PAGE));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
// When page is current page
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE));
assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
}
Aggregations