use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultDocumentSplitterTest method updateAnchors.
@Test
public void updateAnchors() throws Exception {
SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(false, true, true);
when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(false, false, false);
NamingCriterion namingCriterion = mock(NamingCriterion.class);
when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
// [[link>>||anchor="chapter1"]]
// = {{id name="chapter1"}}Chapter 1 =
// = Chapter 2 ==
// [[link>>path:#chapter1]]
ResourceReference reference = new ResourceReference("", ResourceType.DOCUMENT);
reference.setParameter("anchor", "chapter1");
LinkBlock link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(link));
HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new IdBlock("chapter1"), new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
SectionBlock section1 = new SectionBlock(Arrays.<Block>asList(header));
header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 2")), HeaderLevel.LEVEL1);
reference = new ResourceReference("#chapter1", ResourceType.PATH);
link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
SectionBlock section2 = new SectionBlock(Arrays.<Block>asList(header, new ParagraphBlock(Arrays.<Block>asList(link))));
XDOM xdom = new XDOM(Arrays.<Block>asList(paragraph, section1, section2));
WikiDocument document = new WikiDocument("Space.Page", xdom, null);
List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
ResourceReference updatedReference = document.getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
assertEquals("chapter1", updatedReference.getParameter("anchor"));
assertEquals(result.get(1).getFullName(), updatedReference.getReference());
updatedReference = result.get(2).getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
assertEquals(ResourceType.DOCUMENT, updatedReference.getType());
assertEquals("chapter1", updatedReference.getParameter("anchor"));
assertEquals(result.get(1).getFullName(), updatedReference.getReference());
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoring method renameLink.
private boolean renameLink(Block block, DocumentReference currentDocumentReference, DocumentReference oldTarget, DocumentReference newTarget) throws IllegalArgumentException {
boolean modified = false;
ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
if (resourceReference == null) {
// Skip invalid blocks.
throw new IllegalArgumentException();
}
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.
throw new IllegalArgumentException();
}
// Resolve the resource reference.
EntityReference linkEntityReference = resourceReferenceResolver.resolve(resourceReference, null, currentDocumentReference);
// Resolve the document of the reference.
DocumentReference linkTargetDocumentReference = defaultReferenceDocumentReferenceResolver.resolve(linkEntityReference);
EntityReference newTargetReference = newTarget;
ResourceType newResourceType = resourceType;
// If the link was resolved to a space...
if (EntityType.SPACE.equals(linkEntityReference.getType())) {
if (XWiki.DEFAULT_SPACE_HOMEPAGE.equals(newTarget.getName())) {
// If the new document reference is also a space (non-terminal doc), be careful to keep it
// serialized as a space still (i.e. without ".WebHome") and not serialize it as a doc by mistake
// (i.e. with ".WebHome").
newTargetReference = newTarget.getLastSpaceReference();
} else {
// If the new target is a non-terminal document, we can not use a "space:" resource type to access
// it anymore. To fix it, we need to change the resource type of the link reference "doc:".
newResourceType = ResourceType.DOCUMENT;
}
}
// If the link targets the old (renamed) document reference, we must update it.
if (linkTargetDocumentReference.equals(oldTarget)) {
modified = true;
String newReferenceString = this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
// Update the reference in the XDOM.
linkedResourceHelper.setResourceReferenceString(block, newReferenceString);
linkedResourceHelper.setResourceType(block, newResourceType);
}
return modified;
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoring method updateRelativeLinks.
private void updateRelativeLinks(XWikiDocument document, DocumentReference oldDocumentReference) throws XWikiException {
// We support only the syntaxes for which there is an available renderer.
if (!this.contextComponentManagerProvider.get().hasComponent(BlockRenderer.class, document.getSyntax().toIdString())) {
this.logger.warn("We can't update the relative links from [{}]" + " because there is no renderer available for its syntax [{}].", document.getDocumentReference(), document.getSyntax());
return;
}
DocumentReference newDocumentReference = document.getDocumentReference();
XDOM xdom = document.getXDOM();
List<Block> blocks = linkedResourceHelper.getBlocks(xdom);
boolean modified = false;
for (Block block : blocks) {
ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
if (resourceReference == null || StringUtils.isEmpty(resourceReference.getReference())) {
// 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 = this.resourceReferenceResolver.resolve(resourceReference, null, oldDocumentReference);
// new link, use the new document's reference to fill in blanks.
EntityReference newLinkReference = this.resourceReferenceResolver.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 = this.compactEntityReferenceSerializer.serialize(oldLinkReference, newDocumentReference);
// Update the reference in the XDOM.
linkedResourceHelper.setResourceReferenceString(block, serializedLinkReference);
}
}
if (modified) {
document.setContent(xdom);
saveDocumentPreservingContentAuthor(document, "Updated the relative links.", true);
this.logger.info("Updated the relative links from [{}].", document.getDocumentReference());
} else {
this.logger.info("No relative links to update in [{}].", document.getDocumentReference());
}
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method renameNonTerminalDocumentLinks.
@Test
public void renameNonTerminalDocumentLinks() 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 non-terminal document to another non-terminal document.
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "WebHome");
XDOM xdom = mock(XDOM.class);
when(document.getXDOM()).thenReturn(xdom);
ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
// Doc link
when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(oldLinkTarget);
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.WebHome");
// Space link
SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(spaceReference);
when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)).thenReturn("X");
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
assertEquals("X.WebHome", documentLinkBlock.getReference().getReference());
assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
assertEquals("X", spaceLinkBlock.getReference().getReference());
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
verifyDocumentSave(document, "Renamed back-links.", false);
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultDocumentSplitter method updateAnchors.
/**
* @param document the document whose anchors to update
* @param fragments see {@link #collectDocumentFragments(List)}
*/
private void updateAnchors(WikiDocument document, Map<String, String> fragments) {
for (LinkBlock linkBlock : document.getXdom().<LinkBlock>getBlocks(new ClassBlockMatcher(LinkBlock.class), Axes.DESCENDANT)) {
ResourceReference reference = linkBlock.getReference();
ResourceType resoureceType = reference.getType();
String fragment = null;
if ((ResourceType.DOCUMENT.equals(resoureceType) || ResourceType.SPACE.equals(resoureceType)) && StringUtils.isEmpty(reference.getReference())) {
fragment = reference.getParameter(ANCHOR_PARAMETER);
} else if (StringUtils.startsWith(reference.getReference(), "#") && (ResourceType.PATH.equals(resoureceType) || ResourceType.URL.equals(resoureceType))) {
fragment = reference.getReference().substring(1);
}
String targetDocument = fragments.get(fragment);
if (targetDocument != null && !targetDocument.equals(document.getFullName())) {
// The fragment has been moved so we need to update the link.
reference.setType(ResourceType.DOCUMENT);
reference.setReference(targetDocument);
reference.setParameter(ANCHOR_PARAMETER, fragment);
}
}
}
Aggregations