1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.util

File TOCGenerator.java

 

Coverage histogram

../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

22
43
2
1
128
78
14
0.33
21.5
2
7

Classes

Class Line # Actions
TOCGenerator 32 43 0% 14 59
0.1194029811.9%
 

Contributing tests

This file is covered by 1 test. .

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 com.xpn.xwiki.util;
21   
22    import java.util.HashMap;
23    import java.util.LinkedHashMap;
24    import java.util.Map;
25    import java.util.regex.Matcher;
26    import java.util.regex.Pattern;
27   
28    import org.xwiki.rendering.util.IdGenerator;
29   
30    import com.xpn.xwiki.XWikiContext;
31   
 
32    public class TOCGenerator
33    {
34    public static final String TOC_DATA_NUMBERING = "numbering";
35   
36    public static final String TOC_DATA_LEVEL = "level";
37   
38    public static final String TOC_DATA_TEXT = "text";
39   
 
40  0 toggle public static Map<String, Map<String, Object>> generateTOC(String content, int init, int max, boolean numbered,
41    XWikiContext context)
42    {
43  0 IdGenerator idGenerator = new IdGenerator();
44   
45  0 LinkedHashMap<String, Map<String, Object>> tocData = new LinkedHashMap<String, Map<String, Object>>();
46   
47  0 int previousNumbers[] = { 0, 0, 0, 0, 0, 0, 0 };
48   
49  0 Pattern pattern = Pattern.compile("(?-s)^[ \\t]*+(1(\\.1){0,5}+)[ \\t]++(.++)$", Pattern.MULTILINE);
50  0 Matcher matcher = pattern.matcher(content);
51  0 while (matcher.find()) {
52  0 int level = (matcher.group(1).lastIndexOf("1") + 2) / 2;
53  0 String text = matcher.group(3);
54  0 text = context.getWiki().parseContent(text, context);
55   
56  0 String id = idGenerator.generateUniqueId("H", text);
57   
58  0 Map<String, Object> tocEntry = new HashMap<String, Object>();
59  0 tocEntry.put(TOC_DATA_LEVEL, level);
60  0 tocEntry.put(TOC_DATA_TEXT, text);
61   
62  0 if (level >= init && level <= max) {
63  0 if (numbered) {
64  0 String number = "";
65  0 int currentNumber = 0;
66  0 for (int i = previousNumbers.length - 1; i >= init; i--) {
67  0 int num = 0;
68  0 int previousNumber = previousNumbers[i];
69    // if there is already a number previously assigned to a level
70  0 if (previousNumber > 0) {
71    // copy parent level from previous number
72  0 num = previousNumber;
73  0 if (i == level) {
74    // increment the number if there was already previous number on the
75    // same leaf level
76  0 num = previousNumber + 1;
77  0 } else if (i > level) {
78    // reset numbers of all deeper levels
79  0 previousNumbers[i] = 0;
80    }
81    } else {
82  0 num = 1;
83    // increment the previous number if there was already a number assigned
84    // to any of the deeper levels
85  0 if (i < level) {
86  0 previousNumbers[i] = previousNumbers[i] + 1;
87    }
88    }
89   
90    // construct the string representation of the number
91  0 if (i <= level) {
92  0 if ((number.length()) == 0) {
93    // start new number
94  0 number = num + number;
95  0 currentNumber = num;
96    } else {
97    // append to the existing number
98  0 number = num + "." + number;
99    }
100    }
101    }
102    // remember the number for this leaf level
103  0 previousNumbers[level] = currentNumber;
104   
105  0 tocEntry.put(TOC_DATA_NUMBERING, number);
106    }
107  0 tocData.put(id, tocEntry);
108    }
109    }
110  0 return tocData;
111    }
112   
113    /**
114    * @deprecated use {@link IdGenerator} instead
115    */
 
116  3 toggle @Deprecated
117    public static String makeHeadingID(String text, int occurence, XWikiContext context)
118    {
119  3 text = "H" + text;
120  3 text = text.replaceAll("[^a-zA-Z0-9]", "");
121   
122  3 if (occurence > 0) {
123  1 return text + "-" + occurence;
124    } else {
125  2 return text;
126    }
127    }
128    }