use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class ImageFilter method filterImageSource.
private void filterImageSource(Attr source, DocumentReference targetDocumentReference) {
String fileName = null;
try {
fileName = getFileName(source);
} catch (Exception e) {
this.logger.warn("Failed to extract the image file name. Root cause is [{}]", ExceptionUtils.getRootCauseMessage(e));
this.logger.debug("Full stacktrace is: ", e);
}
if (StringUtils.isEmpty(fileName)) {
return;
}
// Set image source attribute relative to the reference document.
AttachmentReference attachmentReference = new AttachmentReference(fileName, targetDocumentReference);
source.setValue(this.documentAccessBridge.getAttachmentURL(attachmentReference, false));
ResourceReference imageReference = new ResourceReference(fileName, ResourceType.ATTACHMENT);
imageReference.setTyped(false);
Comment beforeComment = source.getOwnerDocument().createComment(XMLUtils.escapeXMLComment("startimage:" + this.xhtmlMarkerSerializer.serialize(imageReference)));
Comment afterComment = source.getOwnerDocument().createComment("stopimage");
Element image = source.getOwnerElement();
image.getParentNode().insertBefore(beforeComment, image);
image.getParentNode().insertBefore(afterComment, image.getNextSibling());
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class ImageFilterTest method filterCollectsEmbeddedImages.
@Test
public void filterCollectsEmbeddedImages() {
AttachmentReference attachmentReference = new AttachmentReference("foo.png", this.documentReference);
when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
ResourceReference resourceReference = new ResourceReference("foo.png", ResourceType.ATTACHMENT);
resourceReference.setTyped(false);
when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|foo.png");
String fileName = DataUri.parse("data:image/jpeg;base64,GgoAAAAN==", Charset.forName("UTF-8")).hashCode() + ".jpg";
attachmentReference = new AttachmentReference(fileName, this.documentReference);
when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/" + fileName);
resourceReference = new ResourceReference(fileName, ResourceType.ATTACHMENT);
resourceReference.setTyped(false);
when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|" + fileName);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("targetDocument", "Path.To.Page");
parameters.put("attachEmbeddedImages", "true");
Document document = filterAndAssertOutput("<img src=\"data:image/png;fileName=foo.png;base64,iVBORw0K==\"/>" + "<img src=\"data:image/jpeg;base64,GgoAAAAN==\"/>", parameters, "<!--startimage:false|-|attach|-|foo.png--><img src=\"/path/to/foo.png\"/><!--stopimage-->" + "<!--startimage:false|-|attach|-|" + fileName + "--><img src=\"/path/to/" + fileName + "\"/><!--stopimage-->");
@SuppressWarnings("unchecked") Map<String, byte[]> embeddedImages = (Map<String, byte[]>) document.getUserData("embeddedImages");
assertEquals(new HashSet<String>(Arrays.asList("foo.png", fileName)), embeddedImages.keySet());
}
use of org.xwiki.rendering.listener.reference.ResourceReference 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.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class XWikiLinkLabelGeneratorTest method generateWhenNestedPage.
@Test
public void generateWhenNestedPage() throws Exception {
ResourceReference resourceReference = new DocumentResourceReference("WebHome");
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("space1", "NestedPage"), "WebHome");
EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentModelBridge dmb = mock(DocumentModelBridge.class);
when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
when(dmb.getTitle()).thenReturn("My title");
EntityReferenceSerializer<String> localSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
when(localSerializer.serialize(new SpaceReference("wiki", "space1", "NestedPage"))).thenReturn("space1.NestedPage");
assertEquals("%l%la%n%na%N%NA " + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title) " + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title)", this.mocker.getComponentUnderTest().generate(resourceReference));
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class XWikiLinkLabelGeneratorTest method generateWhenDocumentFailsToLoad.
@Test
public void generateWhenDocumentFailsToLoad() throws Exception {
ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
DocumentReference documentReference = new DocumentReference("xwiki", "Main", "HelloWorld");
EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
when(dab.getTranslatedDocumentInstance(documentReference)).thenThrow(new Exception("error"));
EntityReferenceSerializer<String> localSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
when(localSerializer.serialize(new SpaceReference("xwiki", "Main"))).thenReturn("Main");
assertEquals("%l%la%n%na%N%NA " + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld) " + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld)", this.mocker.getComponentUnderTest().generate(resourceReference));
}
Aggregations