Coverage report

  %line %branch
org.apache.commons.jelly.tags.swt.converters.ColorConverter
0% 
0% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.jelly.tags.swt.converters;
 17  
 
 18  
 import java.util.StringTokenizer;
 19  
 
 20  
 import org.apache.commons.beanutils.Converter;
 21  
 import org.eclipse.swt.graphics.RGB;
 22  
 
 23  
 /**
 24  
  * A Converter that converts Strings in the form "#uuuuuu" or "x,y,z" into a RGB object
 25  
  *
 26  
  * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
 27  
  * @version $Revision: 1.5 $
 28  
  */
 29  0
 public class ColorConverter implements Converter {
 30  
 
 31  0
     private static final ColorConverter instance = new ColorConverter();
 32  
 
 33  0
     private static String usageText =
 34  
         "Color value should be in the form of '#xxxxxx' or 'x,y,z'";
 35  
 
 36  
     public static ColorConverter getInstance() {
 37  0
         return instance;
 38  
     }
 39  
 
 40  
     /**
 41  
      * Parsers a String in the form "x, y, z" into an SWT RGB class
 42  
      * @param value
 43  
      * @return RGB
 44  
      */
 45  
     protected RGB parseRGB(String value) {
 46  0
         StringTokenizer items = new StringTokenizer(value, ",");
 47  0
         int red = 0;
 48  0
         int green = 0;
 49  0
         int blue = 0;
 50  0
         if (items.hasMoreTokens()) {
 51  0
             red = parseNumber(items.nextToken());
 52  
         }
 53  0
         if (items.hasMoreTokens()) {
 54  0
             green = parseNumber(items.nextToken());
 55  
         }
 56  0
         if (items.hasMoreTokens()) {
 57  0
             blue = parseNumber(items.nextToken());
 58  
         }
 59  0
         return new RGB(red, green, blue);
 60  
     }
 61  
 
 62  
     /**
 63  
      * Parsers a String in the form "#xxxxxx" into an SWT RGB class
 64  
      * @param value
 65  
      * @return RGB
 66  
      */
 67  
     protected RGB parseHtml(String value) {
 68  0
         if (value.length() != 7) {
 69  0
             throw new IllegalArgumentException(usageText);
 70  
         }
 71  0
         int colorValue = 0;
 72  
         try {
 73  0
             colorValue = Integer.parseInt(value.substring(1), 16);
 74  0
             java.awt.Color swingColor = new java.awt.Color(colorValue);
 75  0
             return new RGB(
 76  
                 swingColor.getRed(),
 77  
                 swingColor.getGreen(),
 78  
                 swingColor.getBlue());
 79  
         } catch (NumberFormatException ex) {
 80  0
             throw new IllegalArgumentException(
 81  
                 value + "is not a valid Html color\n " + ex);
 82  
         }
 83  
     }
 84  
 
 85  
     /**
 86  
      * Parse a String
 87  
      */
 88  
     public RGB parse(String value) {
 89  0
         if (value.length() <= 1) {
 90  0
             throw new IllegalArgumentException(usageText);
 91  
         }
 92  
 
 93  0
         if (value.charAt(0) == '#') {
 94  0
             return parseHtml(value);
 95  0
         } else if (value.indexOf(',') != -1) {
 96  0
             return parseRGB(value);
 97  
         } else {
 98  0
             throw new IllegalArgumentException(usageText);
 99  
         }
 100  
     }
 101  
 
 102  
     // Converter interface
 103  
     //-------------------------------------------------------------------------
 104  
     public Object convert(Class type, Object value) {
 105  0
         Object answer = null;
 106  0
         if (value != null) {
 107  0
             String text = value.toString();
 108  0
             answer = parse(text);
 109  
         }
 110  
 
 111  0
         System.out.println("Converting value: " + value + " into: " + answer);
 112  
 
 113  0
         return answer;
 114  
     }
 115  
 
 116  
     protected int parseNumber(String text) {
 117  0
         text = text.trim();
 118  0
         return Integer.parseInt(text.trim());
 119  
     }
 120  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.