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

File TagParamUtils.java

 

Coverage histogram

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

Code metrics

24
34
7
2
146
93
21
0.62
4.86
3.5
3

Classes

Class Line # Actions
TagParamUtils 33 3 0% 3 1
0.8571428785.7%
TagParamUtils.HQLInListParser 61 31 0% 18 4
0.931034593.1%
 

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 com.xpn.xwiki.plugin.tag;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import com.xpn.xwiki.XWikiException;
26   
27    /**
28    * TagParamUtils handles queries allowing to search and count tags within the wiki.
29    *
30    * @version $Id: 4c573c154cf2f6201cd19f69944fb2d2dd89dc78 $
31    * @since 8.2M1
32    */
 
33    public final class TagParamUtils
34    {
35    /**
36    * Utility class, private constructor.
37    */
 
38  0 toggle private TagParamUtils()
39    {
40    // no instances please, static utility
41    }
42   
43    /**
44    * Convert a single string, describing a list of comma separated space references,
45    * each single quoted, to a list of space references (without quotes).
46    * Used to convert the "spaces" parameter of the tags macro.
47    *
48    * @param spaces string describing list of spaces
49    * @return list of spaces
50    * @throws com.xpn.xwiki.XWikiException if the string is not well formatted
51    * @throws IllegalArgumentException if the string is null
52    */
 
53  8 toggle public static List<String> spacesParameterToList(String spaces) throws XWikiException
54    {
55  8 if (spaces == null) {
56  1 throw new IllegalArgumentException("Parameter 'spaces' should not be null");
57    }
58  7 return new HQLInListParser(spaces).getResults();
59    }
60   
 
61    private static class HQLInListParser
62    {
63   
64    private final String spaces;
65   
66    private boolean inQuotes;
67    private boolean commaSeen = true;
68    private int pos;
69    private char c;
70    private StringBuilder space;
71   
72    private final List<String> results = new ArrayList<>();
73   
 
74  7 toggle HQLInListParser(String spaces) throws XWikiException
75    {
76  7 this.spaces = spaces;
77  7 parse();
78    }
79   
 
80  4 toggle List<String> getResults()
81    {
82  4 return results;
83    }
84   
 
85  7 toggle private void parse() throws XWikiException
86    {
87  117 while (pos < spaces.length()) {
88  111 c = spaces.charAt(pos++);
89  111 if (inQuotes) {
90  88 handleInQuotes();
91    } else {
92  23 handleOutsideQuotes();
93    }
94    }
95   
96  6 if (inQuotes) {
97  1 throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
98    String.format("Missing closing quote in [%s]", spaces));
99    }
100  5 if (commaSeen && !results.isEmpty()) {
101  1 throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
102    String.format("Unexpected comma at end of [%s]", spaces));
103    }
104    }
105   
 
106  88 toggle private void handleInQuotes()
107    {
108  88 if (c == '\'') {
109  11 if (pos < spaces.length() && spaces.charAt(pos) == '\'') {
110  1 pos++;
111  1 space.append(c);
112    } else {
113  10 results.add(space.toString());
114  10 inQuotes = false;
115  10 commaSeen = false;
116    }
117    } else {
118  77 space.append(c);
119    }
120    }
121   
 
122  23 toggle private void handleOutsideQuotes() throws XWikiException
123    {
124  23 if (c == ',') {
125  7 if (commaSeen) {
126  0 throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
127    String.format("Unexpected comma at position %d in [%s]", pos, spaces));
128    } else {
129  7 commaSeen = true;
130    }
131  16 } else if (c == '\'') {
132  11 if (commaSeen) {
133  11 inQuotes = true;
134  11 space = new StringBuilder();
135    } else {
136  0 throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
137    String.format("Unexpected quote at position %d in [%s]", pos, spaces));
138    }
139  5 } else if (!Character.isWhitespace(c)) {
140  1 throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN,
141    String.format("Unexpected character `%s` at position %d in [%s]",
142    c, pos, spaces));
143    }
144    }
145    }
146    }