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

File BooleanClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

70
177
16
1
376
294
57
0.32
11.06
16
3.56

Classes

Class Line # Actions
BooleanClass 39 177 0% 57 44
0.832699683.3%
 

Contributing tests

This file is covered by 33 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  13669 toggle public BooleanClass(PropertyMetaClass wclass)
50    {
51  13669 super(XCLASSNAME, "Boolean", wclass);
52    }
53   
 
54  10037 toggle public BooleanClass()
55    {
56  10037 this(null);
57  10037 setDisplayFormType("select");
58    }
59   
 
60  5096 toggle public void setDisplayType(String type)
61    {
62  5096 setStringValue("displayType", type);
63    }
64   
 
65  1945 toggle public String getDisplayType()
66    {
67  1945 String dtype = getStringValue("displayType");
68  1945 if ((dtype == null) || (dtype.equals(""))) {
69  160 return "yesno";
70    }
71  1785 return dtype;
72    }
73   
 
74  589 toggle public String getDisplayFormType()
75    {
76  589 String dtype = getStringValue("displayFormType");
77  589 if ((dtype == null) || (dtype.equals(""))) {
78  0 return "radio";
79    }
80  589 return dtype;
81    }
82   
 
83  12784 toggle public void setDisplayFormType(String type)
84    {
85  12784 setStringValue("displayFormType", type);
86    }
87   
 
88  1975 toggle public void setDefaultValue(int dvalue)
89    {
90  1975 setIntValue("defaultValue", dvalue);
91    }
92   
 
93  601 toggle public int getDefaultValue()
94    {
95  601 return getIntValue("defaultValue", -1);
96    }
97   
 
98  104984 toggle @Override
99    public BaseProperty fromString(String value)
100    {
101  104985 BaseProperty property = newProperty();
102  104988 Number nvalue = null;
103  104987 if (StringUtils.isNotEmpty(value)) {
104  104688 if (StringUtils.isNumeric(value)) {
105  104681 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  104991 property.setValue(nvalue);
114  104991 return property;
115    }
116   
 
117  105125 toggle @Override
118    public BaseProperty newProperty()
119    {
120  105127 BaseProperty property = new IntegerProperty();
121  105129 property.setName(getName());
122  105133 return property;
123    }
124   
 
125  62 toggle @Override
126    public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context)
127    {
128  62 IntegerProperty prop = (IntegerProperty) object.safeget(name);
129  62 if (prop == null) {
130  0 return;
131    }
132   
133  62 Integer iValue = (Integer) prop.getValue();
134  62 if (iValue != null) {
135  61 int value = iValue.intValue();
136  61 buffer.append(getDisplayValue(context, value));
137    }
138    }
139   
 
140  532 toggle @Override
141    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context)
142    {
143  532 String displayFormType = getDisplayFormType();
144   
145  532 if (getDisplayType().equals("checkbox")) {
146  0 displayFormType = "checkbox";
147    }
148   
149  532 if (displayFormType.equals("checkbox")) {
150  375 displayCheckboxEdit(buffer, name, prefix, object, context);
151  157 } else if (displayFormType.equals("select")) {
152  152 displaySelectEdit(buffer, name, prefix, object, context);
153    } else {
154  5 displayRadioEdit(buffer, name, prefix, object, context);
155    }
156    }
157   
 
158  152 toggle public void displaySelectEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
159    XWikiContext context)
160    {
161  152 select select = new select(prefix + name, 1);
162  152 select.setAttributeFilter(new XMLAttributeValueFilter());
163  152 select.setName(prefix + name);
164  152 select.setID(prefix + name);
165  152 select.setDisabled(isDisabled());
166   
167  152 String String0 = getDisplayValue(context, 0);
168  152 String String1 = getDisplayValue(context, 1);
169  152 int nb1 = 1;
170  152 int nb2 = 2;
171   
172  152 option[] options;
173   
174  152 if (getDefaultValue() == -1) {
175  139 options = new option[] { new option("---", ""), new option(String1, "1"), new option(String0, "0") };
176  139 options[0].addElement("---");
177  139 options[1].addElement(XMLUtils.escape(String1));
178  139 options[2].addElement(XMLUtils.escape(String0));
179    } else {
180  13 options = new option[] { new option(String1, "1"), new option(String0, "0") };
181  13 options[0].addElement(XMLUtils.escape(String1));
182  13 options[1].addElement(XMLUtils.escape(String0));
183  13 nb1 = 0;
184  13 nb2 = 1;
185    }
186   
187  152 for (option option : options) {
188  443 option.setAttributeFilter(new XMLAttributeValueFilter());
189    }
190   
191  152 try {
192  152 IntegerProperty prop = (IntegerProperty) object.safeget(name);
193  152 Integer ivalue = (prop == null) ? null : (Integer) prop.getValue();
194  152 if (ivalue != null) {
195  13 int value = ivalue.intValue();
196  13 if (value == 1) {
197  7 options[nb1].setSelected(true);
198  6 } else if (value == 0) {
199  6 options[nb2].setSelected(true);
200    }
201    } else {
202  139 int value = getDefaultValue();
203  139 if (value == 1) {
204  4 options[nb1].setSelected(true);
205  135 } else if (value == 0) {
206  1 options[nb2].setSelected(true);
207  134 } else if (value == -1) {
208  134 options[0].setSelected(true);
209    }
210    }
211    } catch (Exception e) {
212    // This should not happen
213  0 e.printStackTrace();
214    }
215  152 select.addElement(options);
216  152 buffer.append(select.toString());
217    }
218   
 
219  5 toggle public void displayRadioEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
220    XWikiContext context)
221    {
222  5 String StringNone = getDisplayValue(context, 2);
223  5 String StringTrue = getDisplayValue(context, 1);
224  5 String StringFalse = getDisplayValue(context, 0);
225  5 div[] inputs;
226   
227  5 input radioNone = new input(input.radio, prefix + name, "");
228  5 radioNone.setAttributeFilter(new XMLAttributeValueFilter());
229  5 input radioTrue = new input(input.radio, prefix + name, "1");
230  5 radioTrue.setAttributeFilter(new XMLAttributeValueFilter());
231  5 input radioFalse = new input(input.radio, prefix + name, "0");
232  5 radioFalse.setAttributeFilter(new XMLAttributeValueFilter());
233  5 radioNone.setDisabled(isDisabled());
234  5 radioTrue.setDisabled(isDisabled());
235  5 radioFalse.setDisabled(isDisabled());
236  5 label labelNone = new label();
237  5 label labelTrue = new label();
238  5 label labelFalse = new label();
239  5 div divNone = new div();
240  5 div divTrue = new div();
241  5 div divFalse = new div();
242  5 labelNone.addElement(radioNone);
243  5 labelNone.addElement(StringNone);
244  5 divNone.addElement(labelNone);
245  5 labelTrue.addElement(radioTrue);
246  5 labelTrue.addElement(StringTrue);
247  5 divTrue.addElement(labelTrue);
248  5 labelFalse.addElement(radioFalse);
249  5 labelFalse.addElement(StringFalse);
250  5 divFalse.addElement(labelFalse);
251   
252  5 radioNone.setID(prefix + name + "_none");
253  5 labelNone.setFor(prefix + name + "_none");
254   
255  5 radioTrue.setID(prefix + name);
256  5 labelTrue.setFor(prefix + name);
257   
258  5 radioFalse.setID(prefix + name + "_false");
259  5 labelFalse.setFor(prefix + name + "_false");
260   
261  5 if (getDefaultValue() == -1) {
262  0 inputs = new div[] { divNone, divTrue, divFalse };
263    } else {
264  5 inputs = new div[] { divTrue, divFalse };
265    }
266   
267  5 try {
268  5 IntegerProperty prop = (IntegerProperty) object.safeget(name);
269  5 Integer ivalue = (prop == null) ? null : (Integer) prop.getValue();
270  5 if (ivalue != null) {
271  5 int value = ivalue.intValue();
272  5 if (value == 1) {
273  3 radioTrue.setChecked(true);
274  2 } else if (value == 0) {
275  2 radioFalse.setChecked(true);
276    }
277    } else {
278  0 int value = getDefaultValue();
279  0 if (value == 1) {
280  0 radioTrue.setChecked(true);
281  0 } else if (value == 0) {
282  0 radioFalse.setChecked(true);
283  0 } else if (value == -1) {
284  0 radioNone.setChecked(true);
285    }
286    }
287    } catch (Exception e) {
288    // This should not happen
289  0 e.printStackTrace();
290    }
291   
292  5 for (div input : inputs) {
293  10 buffer.append(input.toString());
294    }
295    }
296   
 
297  375 toggle public void displayCheckboxEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
298    XWikiContext context)
299    {
300  375 org.apache.ecs.xhtml.input check = new input(input.checkbox, prefix + name, 1);
301  375 check.setAttributeFilter(new XMLAttributeValueFilter());
302  375 check.setID(prefix + name);
303  375 check.setDisabled(isDisabled());
304    // If the (visible) checkbox is unchecked, it will not post anything back so the hidden input by the same
305    // name will post back 0 and the save function will save the first entry it finds.
306  375 org.apache.ecs.xhtml.input checkNo = new input(input.hidden, prefix + name, 0);
307  375 checkNo.setAttributeFilter(new XMLAttributeValueFilter());
308   
309  375 try {
310  375 IntegerProperty prop = (IntegerProperty) object.safeget(name);
311  375 if (prop != null) {
312  256 Integer ivalue = (Integer) prop.getValue();
313  256 if (ivalue != null) {
314  256 int value = ivalue.intValue();
315  256 if (value == 1) {
316  4 check.setChecked(true);
317  252 } else if (value == 0) {
318  252 check.setChecked(false);
319    }
320    } else {
321  0 int value = getDefaultValue();
322  0 if (value == 1) {
323  0 check.setChecked(true);
324    } else {
325  0 check.setChecked(false);
326    }
327    }
328    }
329    } catch (Exception e) {
330    // This should not happen
331  0 e.printStackTrace();
332    }
333  375 buffer.append(check.toString());
334  375 buffer.append(checkNo.toString());
335    }
336   
337    /**
338    * Search for an internationalizable display text for the current value. The search process is:
339    * <ol>
340    * <li>let V = the internal value of the option, 0 1 or 2, T = the value of the displayType meta-property, and D =
341    * the displayed value</li>
342    * <li>if a message with the key <fieldFullName>_<V> exists, return it as D</li>
343    * <li>else, if a message with the key <T>_<V> exists, return it as D</li>
344    * <li>else return V if V is 0 or 1, or --- if V is 2 (undecided)</li>
345    * </ol>
346    *
347    * @param context The request context.
348    * @param value The internal value.
349    * @return The text that should be displayed, representing a human-understandable name for the internal value.
350    */
 
351  380 toggle private String getDisplayValue(XWikiContext context, int value)
352    {
353  380 try {
354    // <classname>_<property>_<value>
355  380 String key = getFieldFullName() + "_" + value;
356  380 String result = localizePlain(key);
357  380 if (result == null) {
358    // <display type>_<value>
359  377 key = getDisplayType() + "_" + value;
360  377 result = localizePlain(key);
361  377 if (result == null) {
362    // Just return the value
363  24 if (value == 2) {
364  8 result = "---";
365    } else {
366  16 result = "" + value;
367    }
368    }
369    }
370  380 return result;
371    } catch (Exception e) {
372  0 e.printStackTrace();
373  0 return "" + value;
374    }
375    }
376    }