KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > util > StringConverter


1 package org.jicengine.util;
2 import java.awt.Font JavaDoc;
3 import java.awt.Color JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Point JavaDoc;
6 import java.util.*;
7
8 /**
9  * <p>
10  * Converts various kinds of String values to corresponding Java objects.
11  * </p>
12  *
13  * @author timo laitinen
14  */

15 public class StringConverter {
16
17         /**
18          * <p>
19          * Accepts 4 different kinds of values:
20          * </p>
21          * <ul>
22          * <li>RGB-value as hexadecimals: <code>0099FF</code>, etc.</li>
23          * <li>RGB-value as integers in range 0-255: <code>0,127,255</code>, etc.</li>
24          * <li>RGBa-value as hexadecimals: <code>0099FF66</code>, etc.</li>
25          * <li>RGBa-value as integers in range 0-255: <code>0,127,255,90</code>, etc.</li>
26          * </ul>
27          *
28          * @param colorSpec String
29          * @return Color
30          * @throws IllegalArgumentException
31          */

32         public static Color JavaDoc toColor(String JavaDoc colorSpec) throws IllegalArgumentException JavaDoc
33     {
34         try {
35             int red;
36             int green;
37             int blue;
38             int alpha = 255;
39
40             if( colorSpec.indexOf(",") != -1 ){
41                 // a color as numbers
42
StringTokenizer tokenizer = new StringTokenizer(colorSpec, ",");
43                 red = Integer.parseInt(tokenizer.nextToken());
44                 green = Integer.parseInt(tokenizer.nextToken());
45                 blue = Integer.parseInt(tokenizer.nextToken());
46                 if( tokenizer.hasMoreTokens()){
47                     alpha = Integer.parseInt(tokenizer.nextToken());
48                 }
49             }
50             else {
51                 // a color as a hex
52
red = Integer.parseInt(colorSpec.substring(0,2),16);
53                 green = Integer.parseInt(colorSpec.substring(2,4),16);
54                 blue = Integer.parseInt(colorSpec.substring(4,6),16);
55                 if( colorSpec.length() == 8 ){
56                     alpha = Integer.parseInt(colorSpec.substring(6),16);
57                 }
58             }
59             return new Color JavaDoc(red, green, blue, alpha);
60         } catch (Exception JavaDoc e){
61             throw new IllegalArgumentException JavaDoc("Failed to parse '" + colorSpec + "' to a java.awt.Color : " + e);
62         }
63     }
64
65     /**
66      *
67      * @param spec String in format <code><var>width</var> x <var>height</var></code>,
68      * where <code>width</code> and <code>height</code> are integers.
69      *
70      * @return Dimension
71      * @throws IllegalArgumentException
72      */

73     public static Dimension JavaDoc toDimension(String JavaDoc spec) throws IllegalArgumentException JavaDoc
74     {
75         try {
76             int separatorIndex = spec.indexOf("x");
77             String JavaDoc width = spec.substring(0, separatorIndex).trim();
78             String JavaDoc height = spec.substring(separatorIndex + 1).trim();
79             return new java.awt.Dimension JavaDoc(Integer.parseInt(width), Integer.parseInt(height));
80         } catch (RuntimeException JavaDoc e){
81             throw new IllegalArgumentException JavaDoc("Can't parse '" + spec + "' to java.awt.Dimension. Expected format 'width x height'.");
82         }
83     }
84
85     /**
86      *
87      * @param spec String in format <code>(x,y)</code>, where <code>x</code> and
88      * <code>y</code> are integers.
89      *
90      * @return corresponding Point
91      * @throws IllegalArgumentException
92      */

93     public static Point JavaDoc toPoint(String JavaDoc spec) throws IllegalArgumentException JavaDoc
94     {
95         try {
96             int separatorIndex = spec.indexOf(",");
97             String JavaDoc x = spec.substring(spec.indexOf("(") + 1, separatorIndex).trim();
98             String JavaDoc y = spec.substring(separatorIndex + 1, spec.indexOf(")")).trim();
99             return new java.awt.Point JavaDoc(Integer.parseInt(x), Integer.parseInt(y));
100         } catch (Exception JavaDoc e){
101             throw new IllegalArgumentException JavaDoc("Can't parse '" + spec + "' to java.awt.Point. Expected format '(x,y)'.");
102         }
103     }
104
105     /**
106      *
107      * @param spec String in format <code><var>language</var>_<var>country</var></code>
108      * or <code><var>language</var>_<var>country</var>_<var>variant</var></code>,
109      * where <code>language</code>, <code>country</code> and <code>variant</code>
110      * are specified by <code>java.util.Locale</code> class.
111      *
112      * @return Locale
113      * @throws IllegalArgumentException
114      */

115     public static Locale toLocale(String JavaDoc spec) throws IllegalArgumentException JavaDoc
116     {
117         String JavaDoc[] params = spec.split("_");
118
119         if( params.length == 2){
120             return new java.util.Locale JavaDoc(params[0], params[1]);
121         }
122         else if( params.length == 3){
123             return new java.util.Locale JavaDoc(params[0], params[1], params[2]);
124         }
125         else {
126             throw new IllegalArgumentException JavaDoc("Can't parse '" + spec + "' to java.util.Locale. Expected format 'lang_country' or 'lang_country_variant'");
127         }
128     }
129 }
130
Popular Tags