Search in sources :

Example 36 with ResourceReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Comment(org.w3c.dom.Comment) Element(org.w3c.dom.Element) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MimeTypeException(org.apache.tika.mime.MimeTypeException)

Example 37 with ResourceReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) HashMap(java.util.HashMap) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) Document(org.w3c.dom.Document) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 38 with ResourceReference

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-->");
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) Test(org.junit.Test)

Example 39 with ResourceReference

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));
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 40 with 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));
}
Also used : EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)51 DocumentReference (org.xwiki.model.reference.DocumentReference)27 Test (org.junit.Test)26 LinkBlock (org.xwiki.rendering.block.LinkBlock)20 XDOM (org.xwiki.rendering.block.XDOM)16 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)15 AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)13 Block (org.xwiki.rendering.block.Block)12 SpaceReference (org.xwiki.model.reference.SpaceReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 AttachmentReference (org.xwiki.model.reference.AttachmentReference)9 ImageBlock (org.xwiki.rendering.block.ImageBlock)8 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReference (org.xwiki.model.reference.EntityReference)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 GroupBlock (org.xwiki.rendering.block.GroupBlock)5