1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.objects.classes

File BooleanClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

44
147
17
1
338
255
42
0.29
8.65
17
2.47

Classes

Class Line # Actions
BooleanClass 39 147 0% 42 67
0.6778846467.8%
 

Contributing tests

This file is covered by 35 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.objects.classes;
21   
22    import java.util.regex.Pattern;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.apache.ecs.xhtml.div;
26    import org.apache.ecs.xhtml.input;
27    import org.apache.ecs.xhtml.label;
28    import org.apache.ecs.xhtml.option;
29    import org.apache.ecs.xhtml.select;
30    import org.xwiki.xml.XMLUtils;
31   
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.internal.xml.XMLAttributeValueFilter;
34    import com.xpn.xwiki.objects.BaseCollection;
35    import com.xpn.xwiki.objects.BaseProperty;
36    import com.xpn.xwiki.objects.IntegerProperty;
37    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
38   
 
39    public class BooleanClass extends PropertyClass
40    {
41    private static final String XCLASSNAME = "boolean";
42   
43    /** Other string values that might be used to represent "true" values. */
44    private static final Pattern TRUE_PATTERN = Pattern.compile("yes|true", Pattern.CASE_INSENSITIVE);
45   
46    /** Other string values that might be used to represent "false" values. */
47    private static final Pattern FALSE_PATTERN = Pattern.compile("no|false", Pattern.CASE_INSENSITIVE);
48   
 
49  29202 toggle public BooleanClass(PropertyMetaClass wclass)
50    {
51  29202 super(XCLASSNAME, "Boolean", wclass);
52    }
53   
 
54  24127 toggle public BooleanClass()
55    {
56  24127 this(null);
57  24127 setDisplayFormType("select");
58    }
59   
 
60  12591 toggle public void setDisplayType(String type)
61    {
62  12591 setStringValue("displayType", type);
63    }
64   
 
65  5707 toggle public String getDisplayType()
66    {
67  5707 String dtype = getStringValue("displayType");
68  5707 if ((dtype == null) || (dtype.equals(""))) {
69  1270 return "yesno";
70    }
71  4437 return dtype;
72    }
73   
 
74  2332 toggle public String getDisplayFormType()
75    {
76  2332 String dtype = getStringValue("displayFormType");
77  2332 if ((dtype == null) || (dtype.equals(""))) {
78  0 return "radio";
79    }
80  2332 return dtype;
81    }
82   
 
83  28167 toggle public void setDisplayFormType(String type)
84    {
85  28167 setStringValue("displayFormType", type);
86    }
87   
 
88  4671 toggle public void setDefaultValue(int dvalue)
89    {
90  4671 setIntValue("defaultValue", dvalue);
91    }
92   
 
93  1392 toggle public int getDefaultValue()
94    {
95  1392 return getIntValue("defaultValue", -1);
96    }
97   
 
98  272083 toggle @Override
99    public BaseProperty fromString(String value)
100    {
101  272082 BaseProperty property = newProperty();
102  272079 Number nvalue = null;
103  272080 if (StringUtils.isNotEmpty(value)) {
104  271449 if (StringUtils.isNumeric(value)) {
105  271449 nvalue = Integer.valueOf(value);
106  1 } else if (TRUE_PATTERN.matcher(value).matches()) {
107  1 nvalue = 1;
108  0 } else if (FALSE_PATTERN.matcher(value).matches()) {
109  0 nvalue = 0;
110    }
111    }
112    // We don't return null if there's no value, since the BooleanProperty really works that way
113  272082 property.setValue(nvalue);
114  272082 return property;
115    }
116   
 
117  283323 toggle @Override
118    public BaseProperty newProperty()
119    {
120  283323 BaseProperty property = new IntegerProperty();
121  283322 property.setName(getName());
122  283322 return property;
123    }
124   
 
125  1663 toggle @Override
126    public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
127    XWikiContext context)
128    {
129  1663 IntegerProperty prop = (IntegerProperty) object.safeget(name);
130  1663 if (prop == null) {
131  0 return;
132    }
133   
134  1663 Integer iValue = (Integer) prop.getValue();
135  1663 if (iValue != null) {
136  754 int value = iValue.intValue();
137  754 buffer.append(getDisplayValue(context, value));
138    }
139    }
140   
 
141  2256 toggle private int getValue(String name, BaseCollection object)
142    {
143  2256 IntegerProperty prop = (IntegerProperty) object.safeget(name);
144  2256 if (prop != null) {
145  1995 Integer ivalue = (Integer) prop.getValue();
146  1995 if (ivalue != null) {
147  1576 return ivalue.intValue();
148    }
149    }
150   
151  680 return getDefaultValue();
152    }
153   
 
154  2256 toggle @Override
155    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
156    XWikiContext context)
157    {
158  2256 String displayFormType = getDisplayFormType();
159   
160  2256 if (getDisplayType().equals("checkbox")) {
161  0 displayFormType = "checkbox";
162    }
163   
164  2256 if (displayFormType.equals("checkbox")) {
165  1544 displayCheckboxEdit(buffer, name, prefix, object, context);
166  712 } else if (displayFormType.equals("select")) {
167  712 displaySelectEdit(buffer, name, prefix, object, context);
168    } else {
169  0 displayRadioEdit(buffer, name, prefix, object, context);
170    }
171    }
172   
 
173  712 toggle public void displaySelectEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
174    XWikiContext context)
175    {
176  712 select select = new select(prefix + name, 1);
177  712 select.setAttributeFilter(new XMLAttributeValueFilter());
178  712 select.setName(prefix + name);
179  712 select.setID(prefix + name);
180  712 select.setDisabled(isDisabled());
181   
182  712 String String0 = getDisplayValue(context, 0);
183  712 String String1 = getDisplayValue(context, 1);
184  712 int nb1 = 1;
185  712 int nb2 = 2;
186   
187  712 option[] options;
188   
189  712 if (getDefaultValue() == -1) {
190  602 options = new option[] { new option("---", ""), new option(String1, "1"), new option(String0, "0") };
191  602 options[0].addElement("---");
192  602 options[1].addElement(XMLUtils.escape(String1));
193  602 options[2].addElement(XMLUtils.escape(String0));
194    } else {
195  110 options = new option[] { new option(String1, "1"), new option(String0, "0") };
196  110 options[0].addElement(XMLUtils.escape(String1));
197  110 options[1].addElement(XMLUtils.escape(String0));
198  110 nb1 = 0;
199  110 nb2 = 1;
200    }
201   
202  712 for (option option : options) {
203  2026 option.setAttributeFilter(new XMLAttributeValueFilter());
204    }
205   
206  712 int value = getValue(name, object);
207  712 if (value == 1) {
208  98 options[nb1].setSelected(true);
209  614 } else if (value == 0) {
210  158 options[nb2].setSelected(true);
211    } else {
212  456 options[0].setSelected(true);
213    }
214   
215  712 select.addElement(options);
216  712 buffer.append(select.toString());
217    }
218   
 
219  0 toggle public void displayRadioEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
220    XWikiContext context)
221    {
222  0 String StringNone = getDisplayValue(context, 2);
223  0 String StringTrue = getDisplayValue(context, 1);
224  0 String StringFalse = getDisplayValue(context, 0);
225  0 div[] inputs;
226   
227  0 input radioNone = new input(input.radio, prefix + name, "");
228  0 radioNone.setAttributeFilter(new XMLAttributeValueFilter());
229  0 input radioTrue = new input(input.radio, prefix + name, "1");
230  0 radioTrue.setAttributeFilter(new XMLAttributeValueFilter());
231  0 input radioFalse = new input(input.radio, prefix + name, "0");
232  0 radioFalse.setAttributeFilter(new XMLAttributeValueFilter());
233  0 radioNone.setDisabled(isDisabled());
234  0 radioTrue.setDisabled(isDisabled());
235  0 radioFalse.setDisabled(isDisabled());
236  0 label labelNone = new label();
237  0 label labelTrue = new label();
238  0 label labelFalse = new label();
239  0 div divNone = new div();
240  0 div divTrue = new div();
241  0 div divFalse = new div();
242  0 labelNone.addElement(radioNone);
243  0 labelNone.addElement(StringNone);
244  0 divNone.addElement(labelNone);
245  0 labelTrue.addElement(radioTrue);
246  0 labelTrue.addElement(StringTrue);
247  0 divTrue.addElement(labelTrue);
248  0 labelFalse.addElement(radioFalse);
249  0 labelFalse.addElement(StringFalse);
250  0 divFalse.addElement(labelFalse);
251   
252  0 radioNone.setID(prefix + name + "_none");
253  0 labelNone.setFor(prefix + name + "_none");
254   
255  0 radioTrue.setID(prefix + name);
256  0 labelTrue.setFor(prefix + name);
257   
258  0 radioFalse.setID(prefix + name + "_false");
259  0 labelFalse.setFor(prefix + name + "_false");
260   
261  0 if (getDefaultValue() == -1) {
262  0 inputs = new div[] { divNone, divTrue, divFalse };
263    } else {
264  0 inputs = new div[] { divTrue, divFalse };
265    }
266   
267  0 int value = getValue(name, object);
268  0 if (value == 1) {
269  0 radioTrue.setChecked(true);
270  0 } else if (value == 0) {
271  0 radioFalse.setChecked(true);
272    } else {
273  0 radioNone.setChecked(true);
274    }
275   
276  0 for (div input : inputs) {
277  0 buffer.append(input.toString());
278    }
279    }
280   
 
281  1544 toggle public void displayCheckboxEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
282    XWikiContext context)
283    {
284  1544 org.apache.ecs.xhtml.input check = new input(input.checkbox, prefix + name, 1);
285  1544 check.setAttributeFilter(new XMLAttributeValueFilter());
286  1544 check.setID(prefix + name);
287  1544 check.setDisabled(isDisabled());
288    // If the (visible) checkbox is unchecked, it will not post anything back so the hidden input by the same
289    // name will post back 0 and the save function will save the first entry it finds.
290  1544 org.apache.ecs.xhtml.input checkNo = new input(input.hidden, prefix + name, 0);
291  1544 checkNo.setAttributeFilter(new XMLAttributeValueFilter());
292   
293  1544 check.setChecked(getValue(name, object) == 1);
294   
295  1544 buffer.append(check.toString());
296  1544 buffer.append(checkNo.toString());
297    }
298   
299    /**
300    * Search for an internationalizable display text for the current value. The search process is:
301    * <ol>
302    * <li>let V = the internal value of the option, 0 1 or 2, T = the value of the displayType meta-property, and D =
303    * the displayed value</li>
304    * <li>if a message with the key <fieldFullName>_<V> exists, return it as D</li>
305    * <li>else, if a message with the key <T>_<V> exists, return it as D</li>
306    * <li>else return V if V is 0 or 1, or --- if V is 2 (undecided)</li>
307    * </ol>
308    *
309    * @param context The request context.
310    * @param value The internal value.
311    * @return The text that should be displayed, representing a human-understandable name for the internal value.
312    */
 
313  2178 toggle private String getDisplayValue(XWikiContext context, int value)
314    {
315  2178 try {
316    // <classname>_<property>_<value>
317  2178 String key = getFieldFullName() + "_" + value;
318  2178 String result = localizePlain(key);
319  2178 if (result == null) {
320    // <display type>_<value>
321  2175 key = getDisplayType() + "_" + value;
322  2175 result = localizePlain(key);
323  2175 if (result == null) {
324    // Just return the value
325  41 if (value == 2) {
326  3 result = "---";
327    } else {
328  38 result = "" + value;
329    }
330    }
331    }
332  2178 return result;
333    } catch (Exception e) {
334  0 e.printStackTrace();
335  0 return "" + value;
336    }
337    }
338    }