org.xwiki.rendering.internal.macro.toc.AbstractTocMacro Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xwiki-rendering-macro-toc Show documentation
Show all versions of xwiki-rendering-macro-toc Show documentation
Generates a Table Of Content for titles (headings)
The newest version!
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.macro.toc;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Provider;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.listener.reference.DocumentResourceReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.macro.AbstractMacro;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.toc.TocMacroParameters;
import org.xwiki.rendering.macro.toc.TocTreeBuilderFactory;
import org.xwiki.rendering.transformation.MacroTransformationContext;
import org.xwiki.rendering.wiki.WikiModel;
import org.xwiki.rendering.wiki.WikiModelException;
/**
* Base class for macros that generate a Table Of Contents based on the document sections.
*
* @param the type that describes the macro parameters
* @version $Id: bf74ffb272b0612b72889c68ac762af6741cb000 $
* @since 11.5RC1
*/
public abstract class AbstractTocMacro extends AbstractMacro
{
/**
* The description of the macro.
*/
private static final String DESCRIPTION = "Generates a Table Of Contents.";
private TocTreeBuilder tocTreeBuilder;
@Inject
private Provider wikiModelProvider;
@Inject
private TocTreeBuilderFactory tocTreeBuilderFactory;
/**
* Create and initialize the descriptor of the macro.
*/
protected AbstractTocMacro(Class parametersBeanClass)
{
super("Table Of Contents", DESCRIPTION, parametersBeanClass);
// Make sure this macro is executed as one of the last macros to be executed since
// other macros can generate headers which need to be taken into account by the TOC
// macro.
setPriority(2000);
setDefaultCategories(Set.of(DEFAULT_CATEGORY_NAVIGATION));
}
@Override
public void initialize() throws InitializationException
{
super.initialize();
try {
this.tocTreeBuilder = this.tocTreeBuilderFactory.build();
} catch (ComponentLookupException e) {
throw new InitializationException(String.format("Failed to initialize [%s]", TocTreeBuilder.class), e);
}
}
@Override
public boolean supportsInlineMode()
{
return false;
}
@Override
public List execute(T parameters, String content, MacroTransformationContext context)
throws MacroExecutionException
{
Block rootBlock = null;
WikiModel wikiModel = this.wikiModelProvider.get();
if (parameters.getReference() != null) {
if (wikiModel != null) {
// Remote TOC always has a PAGE scope since a LOCAL scope would have no meaning
parameters.setScope(TocMacroParameters.Scope.PAGE);
// Get the referenced source's XDOM
rootBlock = getXDOM(new DocumentResourceReference(parameters.getReference()), wikiModel);
} else {
throw new MacroExecutionException(
"The \"reference\" parameter can only be used when a WikiModel implementation is available");
}
}
TreeParametersBuilder builder = new TreeParametersBuilder();
TreeParameters treeParameters = builder.build(rootBlock, parameters, context);
return this.tocTreeBuilder.build(treeParameters);
}
private XDOM getXDOM(ResourceReference reference, WikiModel wikiModel) throws MacroExecutionException
{
try {
return wikiModel.getXDOM(reference);
} catch (WikiModelException e) {
throw new MacroExecutionException(String.format("Failed to get XDOM for [%s]", reference), e);
}
}
}