001: package org.jicengine.util;
002:
003: import java.awt.Font;
004: import java.awt.Color;
005: import java.awt.Dimension;
006: import java.awt.Point;
007: import java.util.*;
008:
009: /**
010: * <p>
011: * Converts various kinds of String values to corresponding Java objects.
012: * </p>
013: *
014: * @author timo laitinen
015: */
016: public class StringConverter {
017:
018: /**
019: * <p>
020: * Accepts 4 different kinds of values:
021: * </p>
022: * <ul>
023: * <li>RGB-value as hexadecimals: <code>0099FF</code>, etc.</li>
024: * <li>RGB-value as integers in range 0-255: <code>0,127,255</code>, etc.</li>
025: * <li>RGBa-value as hexadecimals: <code>0099FF66</code>, etc.</li>
026: * <li>RGBa-value as integers in range 0-255: <code>0,127,255,90</code>, etc.</li>
027: * </ul>
028: *
029: * @param colorSpec String
030: * @return Color
031: * @throws IllegalArgumentException
032: */
033: public static Color toColor(String colorSpec)
034: throws IllegalArgumentException {
035: try {
036: int red;
037: int green;
038: int blue;
039: int alpha = 255;
040:
041: if (colorSpec.indexOf(",") != -1) {
042: // a color as numbers
043: StringTokenizer tokenizer = new StringTokenizer(
044: colorSpec, ",");
045: red = Integer.parseInt(tokenizer.nextToken());
046: green = Integer.parseInt(tokenizer.nextToken());
047: blue = Integer.parseInt(tokenizer.nextToken());
048: if (tokenizer.hasMoreTokens()) {
049: alpha = Integer.parseInt(tokenizer.nextToken());
050: }
051: } else {
052: // a color as a hex
053: red = Integer.parseInt(colorSpec.substring(0, 2), 16);
054: green = Integer.parseInt(colorSpec.substring(2, 4), 16);
055: blue = Integer.parseInt(colorSpec.substring(4, 6), 16);
056: if (colorSpec.length() == 8) {
057: alpha = Integer
058: .parseInt(colorSpec.substring(6), 16);
059: }
060: }
061: return new Color(red, green, blue, alpha);
062: } catch (Exception e) {
063: throw new IllegalArgumentException("Failed to parse '"
064: + colorSpec + "' to a java.awt.Color : " + e);
065: }
066: }
067:
068: /**
069: *
070: * @param spec String in format <code><var>width</var> x <var>height</var></code>,
071: * where <code>width</code> and <code>height</code> are integers.
072: *
073: * @return Dimension
074: * @throws IllegalArgumentException
075: */
076: public static Dimension toDimension(String spec)
077: throws IllegalArgumentException {
078: try {
079: int separatorIndex = spec.indexOf("x");
080: String width = spec.substring(0, separatorIndex).trim();
081: String height = spec.substring(separatorIndex + 1).trim();
082: return new java.awt.Dimension(Integer.parseInt(width),
083: Integer.parseInt(height));
084: } catch (RuntimeException e) {
085: throw new IllegalArgumentException(
086: "Can't parse '"
087: + spec
088: + "' to java.awt.Dimension. Expected format 'width x height'.");
089: }
090: }
091:
092: /**
093: *
094: * @param spec String in format <code>(x,y)</code>, where <code>x</code> and
095: * <code>y</code> are integers.
096: *
097: * @return corresponding Point
098: * @throws IllegalArgumentException
099: */
100: public static Point toPoint(String spec)
101: throws IllegalArgumentException {
102: try {
103: int separatorIndex = spec.indexOf(",");
104: String x = spec.substring(spec.indexOf("(") + 1,
105: separatorIndex).trim();
106: String y = spec.substring(separatorIndex + 1,
107: spec.indexOf(")")).trim();
108: return new java.awt.Point(Integer.parseInt(x), Integer
109: .parseInt(y));
110: } catch (Exception e) {
111: throw new IllegalArgumentException("Can't parse '" + spec
112: + "' to java.awt.Point. Expected format '(x,y)'.");
113: }
114: }
115:
116: /**
117: *
118: * @param spec String in format <code><var>language</var>_<var>country</var></code>
119: * or <code><var>language</var>_<var>country</var>_<var>variant</var></code>,
120: * where <code>language</code>, <code>country</code> and <code>variant</code>
121: * are specified by <code>java.util.Locale</code> class.
122: *
123: * @return Locale
124: * @throws IllegalArgumentException
125: */
126: public static Locale toLocale(String spec)
127: throws IllegalArgumentException {
128: String[] params = spec.split("_");
129:
130: if (params.length == 2) {
131: return new java.util.Locale(params[0], params[1]);
132: } else if (params.length == 3) {
133: return new java.util.Locale(params[0], params[1], params[2]);
134: } else {
135: throw new IllegalArgumentException(
136: "Can't parse '"
137: + spec
138: + "' to java.util.Locale. Expected format 'lang_country' or 'lang_country_variant'");
139: }
140: }
141: }
|