Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.transformation.glossary;
- import java.util.Collections;
- import java.util.List;
- import javax.inject.Inject;
- import javax.inject.Named;
- import javax.inject.Singleton;
- import org.xwiki.component.annotation.Component;
- import org.xwiki.query.QueryManager;
- import org.xwiki.query.Query;
- import org.xwiki.query.QueryException;
- import org.xwiki.rendering.block.Block;
- import org.xwiki.rendering.block.LinkBlock;
- import org.xwiki.rendering.block.WordBlock;
- import org.xwiki.rendering.internal.block.ProtectedBlockFilter;
- import org.xwiki.rendering.listener.reference.DocumentResourceReference;
- import org.xwiki.rendering.listener.reference.ResourceReference;
- import org.xwiki.rendering.transformation.AbstractTransformation;
- import org.xwiki.rendering.transformation.TransformationContext;
- import org.xwiki.rendering.transformation.TransformationException;
- /**
- * Automatically replace words representing a glossary Item with a link.
- *
- * @version $Id$
- * @since 1.0
- */
- @Component
- @Named("glossaryTransformation")
- @Singleton
- public class GlossaryTransformation extends AbstractTransformation
- {
- @Inject
- private QueryManager queryManager;
- /**
- * Used to filter protected blocks (code macro marker block, etc).
- */
- private ProtectedBlockFilter filter = new ProtectedBlockFilter();
- private List<String> getAllGlossaryClassDocumentNames()
- {
- try {
- // for finding the name of docunments with
- // GlossaryCode.GlossaryClass
- String statement = "select doc.name from doc.object(GlossaryCode.GlossaryClass)";
- Query query = this.queryManager.createQuery(statement, Query.XWQL);
- return query.execute();
- } catch (QueryException e) {
- // If no object of type GlossaryCode.GlossaryClass is found.
- return Collections.emptyList();
- }
- }
- @Override
- public void transform(Block block, TransformationContext transformationContext) throws TransformationException
- {
- for (String docName : getAllGlossaryClassDocumentNames()) {
- for (WordBlock wordBlock : this.filter.getChildrenByType(block, WordBlock.class, true)) {
- if (wordBlock.equals(docName)) {
- String doc = wordBlock.getWord();
- // create a linkReference to page named 'doc' in 'Glossary' Space
- ResourceReference linkReference = new DocumentResourceReference("Glossary.doc");
- wordBlock.getParent().replaceChild(new LinkBlock(wordBlock.getChildren(), linkReference, false),
- wordBlock);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment