use of org.xwiki.rendering.listener.reference.ResourceReference 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