1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package inc.che.common.string;
20
21
22 import inc.che.common.math.MathUtil;
23 import inc.che.common.resource.ResourceManager;
24 import inc.che.common.resource.StringResources;
25
26 import java.awt.Color;
27 import java.text.ParseException;
28 import java.util.Iterator;
29
30 import org.apache.log4j.Logger;
31
32 /***
33 * <b>Utilitymethoden für Strings</b>
34 * @version $Id: StringUtil.java,v 1.1 2005/03/06 12:57:03 stevemcmee Exp $
35 * @author <address> Steve McMee <stevemcmee@users.sourceforge.net></address>
36 */
37
38 public final class StringUtil {
39
40 /*** CVS ID of this file */
41 public static final String CVS_ID = "$Id:";
42
43 /***
44 * logger instance for this class
45 */
46
47 private static Logger log = Logger.getLogger(StringUtil.class);
48
49 /***
50 * The ResourceManager
51 */
52 private static ResourceManager resourceManager =
53 ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
54
55 /***
56 * forbidden Construcor
57 */
58
59 private StringUtil() {
60 }
61 /***
62 * Methode zum ersetzen aller Vorkommen eines
63 * Teilstrings in einem String.
64 *@param source Der Text, in dem der Teilstring ersetzt werden soll
65 *@param what Der zu ersetzende Teilstring
66 *@param with Der neue Teilstring
67 *@return String mit dem ersetzen Text
68 */
69 public static String replace(String source, String what, String with) {
70 if (log.isDebugEnabled()) {
71 log.debug("replace(" + source + "," + what + "," + with + ")");
72 }
73 return replace(source, what, with, 0);
74 }
75 /***
76 * Methode zum ersetzen aller Vorkommen eines
77 * Teilstrings in einem String.
78 *@param source Der Text, in dem der Teilstring ersetzt werden soll
79 *@param what Der zu ersetzende Teilstring
80 *@param with Der neue Teilstring
81 * @param start Beginn der Suche
82 *@return String mit dem ersetzen Text
83 */
84
85 public static String replace(
86 String source,
87 String what,
88 String with,
89 int start) {
90 if (log.isDebugEnabled()) {
91 log.debug(
92 "replace("
93 + source
94 + ","
95 + what
96 + ","
97 + with
98 + ","
99 + start
100 + ")");
101 }
102
103 StringBuffer buffer = new StringBuffer(source);
104 int offset = 0;
105 while ((start = source.indexOf(what, start)) != -1) {
106 buffer.replace(
107 start + offset,
108 start + offset + what.length(),
109 with);
110 offset += (with.length() - what.length());
111 start += what.length();
112 }
113 return buffer.toString();
114 }
115
116 /***
117 * creates a String containing a specified number of Blanks
118 * @param n number of blanks
119 * @return String containing exactly n blanks
120 */
121 public static String createBlankString(int n) {
122 if (log.isDebugEnabled()) {
123 log.debug("createBlankString(" + n + ")");
124 }
125 StringBuffer buffer = new StringBuffer();
126 for (int i = 0; i < n; i++) {
127 buffer.append(" ");
128 }
129 return buffer.toString();
130 }
131
132 /***
133 * converts a color secified by a RGB String into a color Object
134 * @param color the color represented by a RGB String (#RRGGBB)
135 * @return Color
136 */
137 public static Color toColor(String color) throws ParseException {
138 if (!color.startsWith("#")) {
139 log.error(
140 resourceManager.getText("color_false", new String[] {color }));
141 throw new ParseException(
142 resourceManager.getText("color_false", new String[] {color }),
143 0);
144 }
145 if (color.length() != 7) {
146 log.error(
147 resourceManager.getText(
148 "color_wrong_length",
149 new String[] {color }));
150 throw new ParseException(
151 resourceManager.getText(
152 "color_wrong_length",
153 new String[] {color }),
154 0);
155 }
156
157 return new Color(
158 MathUtil.toDecBase(color.substring(1, 3), 16),
159 MathUtil.toDecBase(color.substring(3, 5), 16),
160 MathUtil.toDecBase(color.substring(5, 7), 16));
161 }
162
163 /***
164 * converts a color Object int a RGB String of the form #RRGGBB
165 * @param color the color
166 * @return String represented the color by a RGB String (#RRGGBB)
167 */
168 public static String toRgbString(Color color) throws ParseException {
169 int r = color.getRed();
170 int g = color.getGreen();
171 int b = color.getBlue();
172 StringBuffer buffer = new StringBuffer();
173 buffer.append("#");
174 buffer.append(MathUtil.toNumberBase(r, 16, 2));
175 buffer.append(MathUtil.toNumberBase(g, 16, 2));
176 buffer.append(MathUtil.toNumberBase(b, 16, 2));
177
178 return buffer.toString();
179 }
180
181 /***
182 * replace all property-references in an string.
183 * property-reference ${prop} is replaced by the value System.getProperty(prop)
184 * @param str the string
185 * @return String with replaced properties
186 */
187 public static String replaceSystemProperties(String str) {
188 for (Iterator sit = System.getProperties().keySet().iterator();
189 sit.hasNext();
190 ) {
191 if (str.indexOf("${") != -1) {
192 String sysKey = (String) sit.next();
193 str =
194 StringUtil.replace(
195 str,
196 "${" + sysKey + "}",
197 System.getProperty(sysKey));
198 }
199 break;
200 }
201 return str;
202 }
203 }