Search in sources :

Example 16 with ClassBlockMatcher

use of org.xwiki.rendering.block.match.ClassBlockMatcher 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;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) EntityReference(org.xwiki.model.reference.EntityReference) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ToString(org.suigeneris.jrcs.util.ToString) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Aggregations

ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)16 XDOM (org.xwiki.rendering.block.XDOM)10 Block (org.xwiki.rendering.block.Block)6 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)5 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)4 ImageBlock (org.xwiki.rendering.block.ImageBlock)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)3 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)3 File (java.io.File)2 InputStream (java.io.InputStream)2 ToString (org.suigeneris.jrcs.util.ToString)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2