Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
PageIndexNamingCriterion | 31 | 8 | 0% | 3 | 2 |
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 org.xwiki.bridge.DocumentAccessBridge; | |
23 | import org.xwiki.rendering.block.XDOM; | |
24 | ||
25 | /** | |
26 | * A {@link NamingCriterion} based on the name of the main document being split. | |
27 | * | |
28 | * @version $Id: d921937a154f881984c3d640c99de6d548a3905d $ | |
29 | * @since 1.9M1 | |
30 | */ | |
31 | public class PageIndexNamingCriterion implements NamingCriterion | |
32 | { | |
33 | /** | |
34 | * {@link DocumentAccessBridge} used to lookup for existing wiki pages and avoid name clashes. | |
35 | */ | |
36 | private DocumentAccessBridge docBridge; | |
37 | ||
38 | /** | |
39 | * Base name to be used for generating new document names. | |
40 | */ | |
41 | private String baseDocumentName; | |
42 | ||
43 | /** | |
44 | * Current value of the post-fix appended to new document names. | |
45 | */ | |
46 | private int index = 0; | |
47 | ||
48 | /** | |
49 | * Constructs a new {@link PageIndexNamingCriterion}. | |
50 | * | |
51 | * @param baseDocumentName base name to be used for generating new document names. | |
52 | * @param docBridge {@link DocumentAccessBridge} used to lookup for documents. | |
53 | */ | |
54 | 3 | public PageIndexNamingCriterion(String baseDocumentName, DocumentAccessBridge docBridge) |
55 | { | |
56 | 3 | this.baseDocumentName = baseDocumentName; |
57 | 3 | this.docBridge = docBridge; |
58 | } | |
59 | ||
60 | 11 | @Override |
61 | public String getDocumentName(XDOM newDoc) | |
62 | { | |
63 | 11 | int newIndex = ++index; |
64 | 11 | String newDocumentName = baseDocumentName + INDEX_SEPERATOR + newIndex; |
65 | // Resolve any name clashes. | |
66 | 11 | int localIndex = 0; |
67 | 11 | while (docBridge.exists(newDocumentName)) { |
68 | // Append a trailing local index if the page already exists | |
69 | 0 | newDocumentName = |
70 | baseDocumentName + INDEX_SEPERATOR + newIndex + INDEX_SEPERATOR + (++localIndex); | |
71 | } | |
72 | 11 | return newDocumentName; |
73 | } | |
74 | } |