sgc_072

Transformation

Jun 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. /*
  2.  * See the NOTICE file distributed with this work for additional
  3.  * information regarding copyright ownership.
  4.  *
  5.  * This is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU Lesser General Public License as
  7.  * published by the Free Software Foundation; either version 2.1 of
  8.  * the License, or (at your option) any later version.
  9.  *
  10.  * This software is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this software; if not, write to the Free
  17.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  19.  */
  20. package org.xwiki.rendering.internal.transformation.glossary;
  21.  
  22. import java.util.Collections;
  23. import java.util.List;
  24. import javax.inject.Inject;
  25. import javax.inject.Named;
  26. import javax.inject.Singleton;
  27.  
  28. import org.xwiki.component.annotation.Component;
  29. import org.xwiki.query.QueryManager;
  30. import org.xwiki.query.Query;
  31. import org.xwiki.query.QueryException;
  32. import org.xwiki.rendering.block.Block;
  33. import org.xwiki.rendering.block.LinkBlock;
  34. import org.xwiki.rendering.block.WordBlock;
  35. import org.xwiki.rendering.internal.block.ProtectedBlockFilter;
  36. import org.xwiki.rendering.listener.reference.DocumentResourceReference;
  37. import org.xwiki.rendering.listener.reference.ResourceReference;
  38. import org.xwiki.rendering.transformation.AbstractTransformation;
  39. import org.xwiki.rendering.transformation.TransformationContext;
  40. import org.xwiki.rendering.transformation.TransformationException;
  41.  
  42. /**
  43.  * Automatically replace words representing a glossary Item with a link.
  44.  *
  45.  * @version $Id$
  46.  * @since 1.0
  47.  */
  48. @Component
  49. @Named("glossaryTransformation")
  50. @Singleton
  51. public class GlossaryTransformation extends AbstractTransformation
  52. {
  53.  
  54.     @Inject
  55.     private QueryManager queryManager;
  56.  
  57.     /**
  58.      * Used to filter protected blocks (code macro marker block, etc).
  59.      */
  60.     private ProtectedBlockFilter filter = new ProtectedBlockFilter();
  61.  
  62.     private List<String> getAllGlossaryClassDocumentNames()
  63.     {
  64.         try {
  65.             // for finding the name of docunments with
  66.             // GlossaryCode.GlossaryClass
  67.             String statement = "select doc.name from doc.object(GlossaryCode.GlossaryClass)";
  68.             Query query = this.queryManager.createQuery(statement, Query.XWQL);
  69.             return query.execute();
  70.         } catch (QueryException e) {
  71.             // If no object of type GlossaryCode.GlossaryClass is found.
  72.             return Collections.emptyList();
  73.         }
  74.  
  75.     }
  76.  
  77.     @Override
  78.     public void transform(Block block, TransformationContext transformationContext) throws TransformationException
  79.     {
  80.  
  81.         for (String docName : getAllGlossaryClassDocumentNames()) {
  82.             for (WordBlock wordBlock : this.filter.getChildrenByType(block, WordBlock.class, true)) {
  83.  
  84.                 if (wordBlock.equals(docName)) {
  85.                     String doc = wordBlock.getWord();
  86.                     // create a linkReference to page named 'doc' in 'Glossary' Space
  87.                     ResourceReference linkReference = new DocumentResourceReference("Glossary.doc");
  88.                     wordBlock.getParent().replaceChild(new LinkBlock(wordBlock.getChildren(), linkReference, false),
  89.                         wordBlock);
  90.                 }
  91.             }
  92.  
  93.         }
  94.  
  95.     }
  96. }
Add Comment
Please, Sign In to add comment