1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.properties.internal.converter

File ColorConverter.java

 

Coverage histogram

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

Code metrics

16
33
5
1
140
77
15
0.45
6.6
5
3

Classes

Class Line # Actions
ColorConverter 41 33 0% 15 10
0.814814881.5%
 

Contributing tests

This file is covered by 15 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 org.xwiki.properties.internal.converter;
21   
22    import java.awt.Color;
23    import java.lang.reflect.Type;
24    import java.text.MessageFormat;
25    import java.util.StringTokenizer;
26   
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.properties.converter.AbstractConverter;
31    import org.xwiki.properties.converter.ConversionException;
32   
33    /**
34    * Converter that converts a value into an {@link Color} object.
35    *
36    * @version $Id: 3b446e146230a18a256eb7187ca661323052932b $
37    * @since 5.2M1
38    */
39    @Component
40    @Singleton
 
41    public class ColorConverter extends AbstractConverter<Color>
42    {
43    /**
44    * The String input supported by this {@link org.apache.commons.beanutils.Converter}.
45    */
46    private static final String USAGE = "Color value should be in the form of '#xxxxxx' or 'r,g,b'";
47   
 
48  15 toggle @Override
49    protected Color convertToType(Type type, Object value)
50    {
51  15 Color color = null;
52  15 if (value != null) {
53  15 color = parse(value.toString());
54    }
55   
56  14 return color;
57    }
58   
 
59  1 toggle @Override
60    protected String convertToString(Color value)
61    {
62  1 Color colorValue = value;
63   
64  1 return MessageFormat.format("{0}, {1}, {2}", colorValue.getRed(), colorValue.getGreen(), colorValue.getBlue());
65    }
66   
67    /**
68    * Parsers a String in the form "x, y, z" into an SWT RGB class.
69    *
70    * @param value the color as String
71    * @return RGB
72    */
 
73  1 toggle protected Color parseRGB(String value)
74    {
75  1 StringTokenizer items = new StringTokenizer(value, ",");
76   
77  1 try {
78  1 int red = 0;
79  1 if (items.hasMoreTokens()) {
80  1 red = Integer.parseInt(items.nextToken().trim());
81    }
82   
83  1 int green = 0;
84  1 if (items.hasMoreTokens()) {
85  1 green = Integer.parseInt(items.nextToken().trim());
86    }
87   
88  1 int blue = 0;
89  1 if (items.hasMoreTokens()) {
90  1 blue = Integer.parseInt(items.nextToken().trim());
91    }
92   
93  1 return new Color(red, green, blue);
94    } catch (NumberFormatException ex) {
95  0 throw new ConversionException(value + "is not a valid RGB colo", ex);
96    }
97    }
98   
99    /**
100    * Parsers a String in the form "#xxxxxx" into an SWT RGB class.
101    *
102    * @param value the color as String
103    * @return RGB
104    */
 
105  13 toggle protected Color parseHtml(String value)
106    {
107  13 if (value.length() != 7) {
108  0 throw new ConversionException(USAGE);
109    }
110   
111  13 int colorValue = 0;
112  13 try {
113  13 colorValue = Integer.parseInt(value.substring(1), 16);
114  13 return new Color(colorValue);
115    } catch (NumberFormatException ex) {
116  0 throw new ConversionException(value + "is not a valid Html color", ex);
117    }
118    }
119   
120    /**
121    * Convert a String in {@link Color}.
122    *
123    * @param value the String to parse
124    * @return the {@link Color}
125    */
 
126  15 toggle public Color parse(String value)
127    {
128  15 if (value.length() <= 1) {
129  0 throw new ConversionException(USAGE);
130    }
131   
132  15 if (value.charAt(0) == '#') {
133  13 return parseHtml(value);
134  2 } else if (value.indexOf(',') != -1) {
135  1 return parseRGB(value);
136    } else {
137  1 throw new ConversionException(USAGE);
138    }
139    }
140    }