1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.refactoring.splitter.criterion.naming

File HeadingNameNamingCriterion.java

 

Coverage histogram

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

Code metrics

18
39
4
1
176
87
18
0.46
9.75
4
4.5

Classes

Class Line # Actions
HeadingNameNamingCriterion 43 39 0% 18 5
0.9180327791.8%
 

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.refactoring.splitter.criterion.naming;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.xwiki.bridge.DocumentAccessBridge;
26    import org.xwiki.rendering.block.Block;
27    import org.xwiki.rendering.block.BlockFilter;
28    import org.xwiki.rendering.block.HeaderBlock;
29    import org.xwiki.rendering.block.SpaceBlock;
30    import org.xwiki.rendering.block.SpecialSymbolBlock;
31    import org.xwiki.rendering.block.WordBlock;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.renderer.BlockRenderer;
34    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
35    import org.xwiki.rendering.renderer.printer.WikiPrinter;
36   
37    /**
38    * A {@link NamingCriterion} based on the opening heading (if present) of the document.
39    *
40    * @version $Id: 134008a0c3e8cb0f6e2e878bd6ad4486c9e3a1f2 $
41    * @since 1.9M1
42    */
 
43    public class HeadingNameNamingCriterion implements NamingCriterion
44    {
45    /**
46    * Used to render block to plain text.
47    */
48    private BlockRenderer plainSyntaxRenderer;
49   
50    /**
51    * {@link DocumentAccessBridge} used to lookup for existing wiki pages and avoid name clashes.
52    */
53    private DocumentAccessBridge docBridge;
54   
55    /**
56    * In case if we cannot find a heading name present in the document, we will revert back to
57    * {@link PageIndexNamingCriterion}.
58    */
59    private NamingCriterion mainPageNameAndNumberingNamingCriterion;
60   
61    /**
62    * A list containing all the document names generated so far. This is used to avoid name clashes.
63    */
64    private List<String> documentNames;
65   
66    /**
67    * Name of the base page name.
68    */
69    private String basePageName;
70   
71    /**
72    * Space name to be used with generated page names.
73    */
74    private String spaceName;
75   
76    /**
77    * Flag indicating if each generated page name should be prepended with base page name.
78    */
79    private boolean prependBasePageName;
80   
81    /**
82    * Constructs a new {@link HeadingNameNamingCriterion}.
83    *
84    * @param baseDocumentName name of the document that is being split.
85    * @param docBridge {@link DocumentAccessBridge} used to lookup for documents.
86    * @param plainSyntaxRenderer the renderer to convert to plain text
87    * @param prependBasePageName a flag indicating if each generated page name should be prepended with base page name.
88    */
 
89  2 toggle public HeadingNameNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge,
90    BlockRenderer plainSyntaxRenderer, boolean prependBasePageName)
91    {
92  2 this.mainPageNameAndNumberingNamingCriterion = new PageIndexNamingCriterion(baseDocumentName, docBridge);
93  2 this.docBridge = docBridge;
94  2 this.plainSyntaxRenderer = plainSyntaxRenderer;
95  2 this.documentNames = new ArrayList<String>();
96  2 int dot = baseDocumentName.lastIndexOf('.');
97  2 this.spaceName = (dot != -1) ? baseDocumentName.substring(0, dot) : "Main";
98  2 this.basePageName = baseDocumentName.substring(dot + 1);
99  2 this.prependBasePageName = prependBasePageName;
100    }
101   
 
102  11 toggle @Override
103    public String getDocumentName(XDOM newDoc)
104    {
105  11 String documentName = null;
106  11 String prefix = spaceName + ".";
107  11 if (newDoc.getChildren().size() > 0) {
108  11 Block firstChild = newDoc.getChildren().get(0);
109  11 if (firstChild instanceof HeaderBlock) {
110    // Clone the header block and remove any unwanted stuff
111  10 Block clonedHeaderBlock = firstChild.clone(new BlockFilter()
112    {
 
113  24 toggle public List<Block> filter(Block block)
114    {
115  24 List<Block> blocks = new ArrayList<Block>();
116  24 if (block instanceof WordBlock || block instanceof SpaceBlock
117    || block instanceof SpecialSymbolBlock) {
118  24 blocks.add(block);
119    }
120  24 return blocks;
121    }
122    });
123  10 XDOM xdom = new XDOM(clonedHeaderBlock.getChildren());
124   
125  10 WikiPrinter printer = new DefaultWikiPrinter();
126  10 this.plainSyntaxRenderer.render(xdom, printer);
127   
128  10 documentName = cleanPageName(printer.toString());
129    }
130    }
131   
132    // Fall back if necessary.
133  11 if (null == documentName || documentName.equals("")) {
134  2 documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc);
135  9 } else if (prependBasePageName) {
136  0 documentName = prefix + basePageName + INDEX_SEPERATOR + documentName;
137    } else {
138  9 documentName = prefix + documentName;
139    }
140   
141    // Truncate long document names.
142  11 int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255;
143  11 if (documentName.length() > maxWidth) {
144  1 documentName = documentName.substring(0, maxWidth);
145    }
146   
147    // Resolve any name clashes.
148  11 String newDocumentName = documentName;
149  11 int localIndex = 0;
150  12 while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) {
151    // Append a trailing local index if the page already exists
152  1 newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex);
153    }
154   
155    // Add the newly generated document name into the pool of generated document names.
156  11 documentNames.add(newDocumentName);
157   
158  11 return newDocumentName;
159    }
160   
161    /**
162    * Utility method for cleaning out invalid / dangerous characters from page names.
163    *
164    * @param originalName the page name to be cleaned.
165    * @return the cleaned page name.
166    */
 
167  10 toggle private String cleanPageName(String originalName)
168    {
169    // These characters are reserved for xwiki internal use.
170  10 String replaced = originalName.trim().replaceAll("[\\.:]", "-");
171    // Links to documents containing these characters are not rendered correctly at the moment.
172  10 replaced = replaced.replaceAll("[@?#~/]", "");
173  10 return replaced;
174   
175    }
176    }