| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 35 |
|
|
| 36 |
|
@version |
| 37 |
|
@since |
| 38 |
|
|
| 39 |
|
@Component |
| 40 |
|
@Singleton |
| |
|
| 81.5% |
Uncovered Elements: 10 (54) |
Complexity: 15 |
Complexity Density: 0.45 |
|
| 41 |
|
public class ColorConverter extends AbstractConverter<Color> |
| 42 |
|
{ |
| 43 |
|
|
| 44 |
|
@link |
| 45 |
|
|
| 46 |
|
private static final String USAGE = "Color value should be in the form of '#xxxxxx' or 'r,g,b'"; |
| 47 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 48 |
15 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 59 |
1 |
@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 |
|
|
| 69 |
|
|
| 70 |
|
@param |
| 71 |
|
@return |
| 72 |
|
|
| |
|
| 78.9% |
Uncovered Elements: 4 (19) |
Complexity: 5 |
Complexity Density: 0.38 |
|
| 73 |
1 |
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 |
|
|
| 101 |
|
|
| 102 |
|
@param |
| 103 |
|
@return |
| 104 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 105 |
13 |
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 |
|
@link |
| 122 |
|
|
| 123 |
|
@param |
| 124 |
|
@return@link |
| 125 |
|
|
| |
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 126 |
15 |
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 |
|
} |