use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.
the class DefaultDocumentSplitterTest method split.
@Test
public void split() throws Exception {
SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
NamingCriterion namingCriterion = mock(NamingCriterion.class);
when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
// = Chapter 1 =
// Once upon a time..
// == Section 1.1 ==
// In a kingdom far away..
HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("In a kingdom far away..")));
SectionBlock subSection = new SectionBlock(Arrays.<Block>asList(header, paragraph));
header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("Once upon a time..")));
SectionBlock section = new SectionBlock(Arrays.<Block>asList(header, paragraph, subSection));
XDOM xdom = new XDOM(Arrays.<Block>asList(section));
WikiDocument document = new WikiDocument("Space.Page", xdom, null);
List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
assertEquals(3, result.size());
assertSame(document, result.get(0));
assertEquals("Child1", result.get(1).getFullName());
assertSame(document, result.get(1).getParent());
assertEquals("Child2", result.get(2).getFullName());
assertSame(result.get(1), result.get(2).getParent());
ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
assertTrue(document.getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
assertEquals(1, result.get(1).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
assertEquals(1, result.get(2).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
}
use of org.xwiki.rendering.block.LinkBlock 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.block.LinkBlock 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);
}
}
}
use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.
the class DefaultDocumentSplitter method createLink.
/**
* Creates a {@link LinkBlock} suitable to be placed in the parent document.
*
* @param block the {@link Block} that has just been split into a separate document.
* @param target name of the target wiki document.
* @return a {@link LinkBlock} representing the link from the parent document to new document.
*/
private LinkBlock createLink(Block block, String target) {
Block firstBlock = block.getChildren().get(0);
if (firstBlock instanceof HeaderBlock) {
DocumentResourceReference reference = new DocumentResourceReference(target);
// Clone the header block and remove any unwanted stuff
Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() {
@Override
public List<Block> filter(Block block) {
List<Block> blocks = new ArrayList<Block>();
if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
blocks.add(block);
}
return blocks;
}
});
return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false);
} else if (firstBlock instanceof SectionBlock) {
return createLink(firstBlock, target);
} else {
throw new IllegalArgumentException("A SectionBlock should either begin with a HeaderBlock or another SectionBlock.");
}
}
use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.
the class XWikiDocument method getUniqueLinkedPages.
/**
* Extract all the unique static (i.e. not generated by macros) wiki links (pointing to wiki page) from this
* document's content to others documents.
*
* @param context the XWiki context.
* @return the document names for linked pages, if null an error append.
* @since 1.9M2
*/
public Set<String> getUniqueLinkedPages(XWikiContext context) {
Set<String> pageNames;
XWikiDocument contextDoc = context.getDoc();
String contextWiki = context.getWikiId();
try {
// Make sure the right document is used as context document
context.setDoc(this);
// Make sure the right wiki is used as context document
context.setWikiId(getDatabase());
if (is10Syntax()) {
pageNames = getUniqueLinkedPages10(context);
} else {
XDOM dom = getXDOM();
// TODO: Add support for macro as well.
List<LinkBlock> linkBlocks = dom.getBlocks(new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT);
pageNames = new LinkedHashSet<String>(linkBlocks.size());
DocumentReference currentDocumentReference = getDocumentReference();
for (LinkBlock linkBlock : linkBlocks) {
ResourceReference reference = linkBlock.getReference();
String referenceString = reference.getReference();
ResourceType resourceType = reference.getType();
// TODO: Add support for ATTACHMENT as well.
if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
// We are only interested in Document or Space references.
continue;
}
// Optimisation: If the reference is empty, the link is an autolink and we don`t include it.
if (StringUtils.isEmpty(referenceString)) {
continue;
}
// FIXME?: pass this.getDocumentReference as parameter to the resolve so that relative references
// get resolved relative to this and not to the context document.
EntityReference documentReference = getResourceReferenceEntityReferenceResolver().resolve(reference, EntityType.DOCUMENT);
// Verify after resolving it that the link is not an autolink(i.e. a link to the current document)
if (!documentReference.equals(currentDocumentReference)) {
// Since this method is used for saving backlinks and since backlinks must be
// saved with the space and page name but without the wiki part, we remove the wiki
// part before serializing.
// This is a bit of a hack since the default serializer should theoretically fail
// if it's passed an invalid reference.
pageNames.add(getCompactWikiEntityReferenceSerializer().serialize(documentReference));
}
}
}
} finally {
context.setDoc(contextDoc);
context.setWikiId(contextWiki);
}
return pageNames;
}
Aggregations