1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.transformation.wikiword

File WikiWordTransformation.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
5
1
1
74
36
2
0.4
5
1
2

Classes

Class Line # Actions
WikiWordTransformation 48 5 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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.wikiword;
21   
22    import java.util.regex.Matcher;
23    import java.util.regex.Pattern;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.rendering.block.Block;
30    import org.xwiki.rendering.block.LinkBlock;
31    import org.xwiki.rendering.block.WordBlock;
32    import org.xwiki.rendering.internal.block.ProtectedBlockFilter;
33    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
34    import org.xwiki.rendering.listener.reference.ResourceReference;
35    import org.xwiki.rendering.transformation.AbstractTransformation;
36    import org.xwiki.rendering.transformation.TransformationContext;
37    import org.xwiki.rendering.transformation.TransformationException;
38   
39    /**
40    * Automatically replace words representing Wiki Words with a link.
41    *
42    * @version $Id: 6aa16d8a681f24584e0f5d85c8d9d8c2b6977da8 $
43    * @since 2.6RC1
44    */
45    @Component
46    @Named("wikiword")
47    @Singleton
 
48    public class WikiWordTransformation extends AbstractTransformation
49    {
50    /**
51    * Regex Pattern to recognize a WikiWord.
52    */
53    private static final Pattern WIKIWORD_PATTERN = Pattern.compile(
54    "\\p{javaUpperCase}+\\p{javaLowerCase}+(\\p{javaUpperCase}\\p{javaLowerCase}*)+");
55   
56    /**
57    * Used to filter protected blocks (code macro marker block, etc).
58    */
59    private ProtectedBlockFilter filter = new ProtectedBlockFilter();
60   
 
61  2 toggle @Override
62    public void transform(Block block, TransformationContext transformationContext) throws TransformationException
63    {
64    // Find all Word blocks and for each of them check if they're a wiki word or not
65  2 for (WordBlock wordBlock : this.filter.getChildrenByType(block, WordBlock.class, true)) {
66  9 Matcher matcher = WIKIWORD_PATTERN.matcher(wordBlock.getWord());
67  9 if (matcher.matches()) {
68  3 ResourceReference linkReference = new DocumentResourceReference(wordBlock.getWord());
69  3 wordBlock.getParent().replaceChild(new LinkBlock(wordBlock.getChildren(), linkReference, false),
70    wordBlock);
71    }
72    }
73    }
74    }