use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class FormulaMacro method render.
/**
* Renders the formula using the specified renderer.
*
* @param formula the formula text
* @param inline is the formula supposed to be used inline or as a block-level element
* @param fontSize the specified font size
* @param imageType the specified resulting image type
* @param rendererHint the hint for the renderer to use
* @return the resulting block holding the generated image, or {@code null} in case of an error.
* @throws MacroExecutionException if no renderer exists for the passed hint or if that rendered failed to render
* the formula
* @throws IllegalArgumentException if the formula is not valid, according to the LaTeX syntax
*/
private Block render(String formula, boolean inline, FontSize fontSize, Type imageType, String rendererHint) throws MacroExecutionException, IllegalArgumentException {
try {
FormulaRenderer renderer = this.manager.getInstance(FormulaRenderer.class, rendererHint);
String imageName = renderer.process(formula, inline, fontSize, imageType);
// TODO: HACK!!
// We're going through the getAttachmentURL() API so that when the PdfURLFactory is used, the generated
// image is saved and then embedded in the exported PDF thanks to PDFURIResolver. In the future we need
// to remove this hack by introduce a proper Resource for generated image (say TemporaryResource),
// implement a TemporaryResourceSerializer<URL> and introduce a ResourceLoader interface and have it
// implemented for TemporaryResource...
AttachmentReference attachmentReference = new AttachmentReference(imageName, this.dab.getCurrentDocumentReference());
String url = this.dab.getAttachmentURL(attachmentReference, false);
// Note that we have to replace the download action by the tex action since the getAttachmentURL() API
// will use the "download" action but when the generated URL is called by the browser it needs to point to
// the TexAction...
url = url.replace("/download/", "/tex/");
// TODO: end HACK!!
ResourceReference imageReference = new ResourceReference(url, ResourceType.URL);
ImageBlock result = new ImageBlock(imageReference, false);
// Set the alternative text for the image to be the original formula
result.setParameter("alt", formula);
return result;
} catch (Exception e) {
throw new MacroExecutionException(String.format("Failed to render formula using the [%s] renderer", rendererHint), e);
}
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class XWikiDocument method rename.
/**
* Same as {@link #rename(DocumentReference, List, XWikiContext)} but the list of documents having the current
* document as their parent is passed in parameter.
*
* @param newDocumentReference the new document reference
* @param backlinkDocumentReferences the list of references of documents to parse and for which links will be
* modified to point to the new document reference
* @param childDocumentReferences the list of references of document whose parent field will be set to the new
* document reference
* @param context the ubiquitous XWiki Context
* @throws XWikiException in case of an error
* @since 2.2M2
*/
public void rename(DocumentReference newDocumentReference, List<DocumentReference> backlinkDocumentReferences, List<DocumentReference> childDocumentReferences, XWikiContext context) throws XWikiException {
// If the user is trying to rename to the same name... In that case, simply exits for efficiency.
if (isNew() || getDocumentReference().equals(newDocumentReference)) {
return;
}
// Grab the xwiki object, it gets used a few times.
XWiki xwiki = context.getWiki();
// Step 1: Copy the document and all its translations under a new document with the new reference.
xwiki.copyDocument(getDocumentReference(), newDocumentReference, false, context);
// Step 2: For each child document, update its parent reference.
if (childDocumentReferences != null) {
for (DocumentReference childDocumentReference : childDocumentReferences) {
XWikiDocument childDocument = xwiki.getDocument(childDocumentReference, context);
String compactReference = getCompactEntityReferenceSerializer().serialize(newDocumentReference);
childDocument.setParent(compactReference);
String saveMessage = localizePlainOrKey("core.comment.renameParent", compactReference);
childDocument.setAuthorReference(context.getUserReference());
xwiki.saveDocument(childDocument, saveMessage, true, context);
}
}
// Step 3: For each backlink to rename, parse the backlink document and replace the links with the new name.
for (DocumentReference backlinkDocumentReference : backlinkDocumentReferences) {
XWikiDocument backlinkRootDocument = xwiki.getDocument(backlinkDocumentReference, context);
// Update default locale instance
renameLinks(backlinkRootDocument, getDocumentReference(), newDocumentReference, context);
// Update translations
for (Locale locale : backlinkRootDocument.getTranslationLocales(context)) {
XWikiDocument backlinkDocument = backlinkRootDocument.getTranslatedDocument(locale, context);
renameLinks(backlinkDocument, getDocumentReference(), newDocumentReference, context);
}
}
// Get new document
XWikiDocument newDocument = xwiki.getDocument(newDocumentReference, context);
// document's location.
if (Utils.getContextComponentManager().hasComponent(BlockRenderer.class, getSyntax().toIdString())) {
// Only support syntax for which a renderer is provided
LinkedResourceHelper linkedResourceHelper = Utils.getComponent(LinkedResourceHelper.class);
DocumentReference oldDocumentReference = getDocumentReference();
XDOM newDocumentXDOM = newDocument.getXDOM();
List<Block> blocks = linkedResourceHelper.getBlocks(newDocumentXDOM);
// FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#updateRelativeLinks in
// xwiki-platform-refactoring-default
boolean modified = false;
for (Block block : blocks) {
ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
if (resourceReference == null) {
// Skip invalid blocks.
continue;
}
ResourceType resourceType = resourceReference.getType();
// TODO: support ATTACHMENT as well.
if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
// We are currently only interested in Document or Space references.
continue;
}
// current link, use the old document's reference to fill in blanks.
EntityReference oldLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, oldDocumentReference);
// new link, use the new document's reference to fill in blanks.
EntityReference newLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, newDocumentReference);
// If the new and old link references don`t match, then we must update the relative link.
if (!newLinkReference.equals(oldLinkReference)) {
modified = true;
// Serialize the old (original) link relative to the new document's location, in compact form.
String serializedLinkReference = getCompactWikiEntityReferenceSerializer().serialize(oldLinkReference, newDocumentReference);
// Update the reference in the XDOM.
linkedResourceHelper.setResourceReferenceString(block, serializedLinkReference);
}
}
// Set the new content and save document if needed
if (modified) {
newDocument.setContent(newDocumentXDOM);
newDocument.setAuthorReference(context.getUserReference());
xwiki.saveDocument(newDocument, context);
}
}
// Step 5: Delete the old document
xwiki.deleteDocument(this, context);
// Step 6: The current document needs to point to the renamed document as otherwise it's pointing to an
// invalid XWikiDocument object as it's been deleted...
clone(newDocument);
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method renameLinksFromMacros.
@Test
public void renameLinksFromMacros() throws Exception {
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
XWikiDocument document = mock(XWikiDocument.class);
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
when(document.getDocumentReference()).thenReturn(documentReference);
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
// From a terminal document to another terminal document.
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
XDOM xdom = mock(XDOM.class);
when(document.getXDOM()).thenReturn(xdom);
Map<String, String> includeParameters = new HashMap<String, String>();
includeParameters.put("reference", "A.B");
MacroBlock includeMacroBlock1 = new MacroBlock("include", includeParameters, false);
Map<String, String> includeOldParameters = new HashMap<String, String>();
includeOldParameters.put("document", "A.B");
MacroBlock includeMacroBlock2 = new MacroBlock("include", includeOldParameters, false);
Map<String, String> displayParameters = new HashMap<String, String>();
displayParameters.put("reference", "A.B");
MacroBlock displayMacroBlock = new MacroBlock("display", displayParameters, false);
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(includeMacroBlock1, includeMacroBlock2, displayMacroBlock));
ResourceReference macroResourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
when(this.resourceReferenceResolver.resolve(macroResourceReference, null, documentReference)).thenReturn(oldLinkTarget);
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
assertEquals("X.Y", includeMacroBlock1.getParameter("reference"));
assertEquals("X.Y", includeMacroBlock2.getParameter("document"));
assertEquals("X.Y", displayMacroBlock.getParameter("reference"));
verifyDocumentSave(document, "Renamed back-links.", false);
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method updateRelativeLinksAcrossWikis.
@Test
public void updateRelativeLinksAcrossWikis() throws Exception {
DocumentReference oldReference = new DocumentReference("wiki1", "A", "B");
DocumentReference newReference = new DocumentReference("wiki2", "X", "Y");
XWikiDocument newDocument = mock(XWikiDocument.class);
when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
when(newDocument.getDocumentReference()).thenReturn(newReference);
when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
XDOM xdom = mock(XDOM.class);
when(newDocument.getXDOM()).thenReturn(xdom);
ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
SpaceReference originalSpaceReference = new SpaceReference("wiki1", "Z");
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
SpaceReference newSpaceReference = new SpaceReference("wiki2", "Z");
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(newSpaceReference);
when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("wiki1:A.C");
when(this.compactEntityReferenceSerializer.serialize(originalSpaceReference, newReference)).thenReturn("wiki1:Z");
this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
// Document link block is updated.
assertEquals("wiki1:A.C", docLinkBlock.getReference().getReference());
assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
// Space link is also updated, since they were referring entities on a different wiki.
assertEquals("wiki1:Z", spaceLinkBlock.getReference().getReference());
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method updateRelativeLinks.
@Test
public void updateRelativeLinks() throws Exception {
DocumentReference oldReference = new DocumentReference("wiki", "A", "B");
DocumentReference newReference = new DocumentReference("wiki", "X", "Y");
XWikiDocument newDocument = mock(XWikiDocument.class);
when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
when(newDocument.getDocumentReference()).thenReturn(newReference);
when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
XDOM xdom = mock(XDOM.class);
when(newDocument.getXDOM()).thenReturn(xdom);
ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z");
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(originalSpaceReference);
when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C");
this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
// Document link block is updated.
assertEquals("A.C", docLinkBlock.getReference().getReference());
assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
// Space link block stays the same, since they were on the same wiki.
assertEquals("Z", spaceLinkBlock.getReference().getReference());
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
Aggregations