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

File NumberClass.java

 

Coverage histogram

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

Code metrics

22
49
9
1
148
109
25
0.51
5.44
9
2.78

Classes

Class Line # Actions
NumberClass 36 49 0% 25 6
0.92592.5%
 

Contributing tests

This file is covered by 37 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 org.apache.ecs.xhtml.input;
23    import org.slf4j.Logger;
24    import org.slf4j.LoggerFactory;
25   
26    import com.xpn.xwiki.XWikiContext;
27    import com.xpn.xwiki.internal.xml.XMLAttributeValueFilter;
28    import com.xpn.xwiki.objects.BaseCollection;
29    import com.xpn.xwiki.objects.BaseProperty;
30    import com.xpn.xwiki.objects.DoubleProperty;
31    import com.xpn.xwiki.objects.FloatProperty;
32    import com.xpn.xwiki.objects.IntegerProperty;
33    import com.xpn.xwiki.objects.LongProperty;
34    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
35   
 
36    public class NumberClass extends PropertyClass
37    {
38    private static final String XCLASSNAME = "number";
39   
40    /** Logging helper object. */
41    private static final Logger LOG = LoggerFactory.getLogger(NumberClass.class);
42   
 
43  5084 toggle public NumberClass(PropertyMetaClass wclass)
44    {
45  5084 super(XCLASSNAME, "Number", wclass);
46  5084 setSize(30);
47  5084 setNumberType("long");
48    }
49   
 
50  2610 toggle public NumberClass()
51    {
52  2610 this(null);
53    }
54   
 
55  1247 toggle public int getSize()
56    {
57  1247 return getIntValue("size");
58    }
59   
 
60  6883 toggle public void setSize(int size)
61    {
62  6883 setIntValue("size", size);
63    }
64   
 
65  369039 toggle public String getNumberType()
66    {
67  369039 return getStringValue("numberType");
68    }
69   
 
70  8083 toggle public void setNumberType(String ntype)
71    {
72  8083 setStringValue("numberType", ntype);
73    }
74   
 
75  185154 toggle @Override
76    public BaseProperty newProperty()
77    {
78  185154 String ntype = getNumberType();
79  185154 BaseProperty property;
80  185154 if (ntype.equals("integer")) {
81  183949 property = new IntegerProperty();
82  1205 } else if (ntype.equals("float")) {
83  2 property = new FloatProperty();
84  1203 } else if (ntype.equals("double")) {
85  43 property = new DoubleProperty();
86    } else {
87  1160 property = new LongProperty();
88    }
89  185154 property.setName(getName());
90  185152 return property;
91    }
92   
 
93  183882 toggle @Override
94    public BaseProperty fromString(String value)
95    {
96  183882 BaseProperty property = newProperty();
97  183881 String ntype = getNumberType();
98  183881 Number nvalue = null;
99   
100  183880 try {
101  183881 if (ntype.equals("integer")) {
102  183580 if ((value != null) && (!value.equals(""))) {
103  177995 nvalue = Integer.valueOf(value);
104    }
105  302 } else if (ntype.equals("float")) {
106  0 if ((value != null) && (!value.equals(""))) {
107  0 nvalue = Float.valueOf(value);
108    }
109  302 } else if (ntype.equals("double")) {
110  1 if ((value != null) && (!value.equals(""))) {
111  1 nvalue = Double.valueOf(value);
112    }
113    } else {
114  301 if ((value != null) && (!value.equals(""))) {
115  274 nvalue = Long.valueOf(value);
116    }
117    }
118    } catch (NumberFormatException e) {
119  2 LOG.warn("Invalid number entered for property " + getName() + " of class " + getObject().getName() + ": "
120    + value);
121    // Returning null makes sure that the old value (if one exists) will not be discarded/replaced
122  2 return null;
123    }
124   
125  183879 property.setValue(nvalue);
126  183880 return property;
127    }
128   
 
129  1245 toggle @Override
130    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context)
131    {
132  1245 input input = new input();
133  1245 input.setAttributeFilter(new XMLAttributeValueFilter());
134   
135  1245 BaseProperty prop = (BaseProperty) object.safeget(name);
136  1245 if (prop != null) {
137  1179 input.setValue(prop.toText());
138    }
139   
140  1245 input.setType("text");
141  1245 input.setName(prefix + name);
142  1245 input.setID(prefix + name);
143  1245 input.setSize(getSize());
144  1245 input.setDisabled(isDisabled());
145  1245 buffer.append(input.toString());
146    }
147   
148    }