KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > StringTools


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util;
8
9
10 /**
11  * This is a static toolkit class that provides common string
12  * methods for comparison and manipulation
13  *
14  * @author Brian Pontarelli
15  * @since 1.0
16  * @version 2.0
17  */

18 public class StringTools {
19
20     /**
21      * Returns true if the string pass in is null or empty
22      */

23     public static boolean isEmpty(String JavaDoc str) {
24         return (str == null || str.length() == 0);
25     }
26
27     /**
28      * Returns true if the string pass in is null or empty or trimmed and empty
29      */

30     public static boolean isTrimmedEmpty(String JavaDoc str) {
31         return (str == null || str.trim().length() == 0);
32     }
33
34     /**
35      * Returns true if the string is a valid boolean expression according to the
36      * valueOf method on java.lang.Boolean, false otherwise
37      */

38     public static boolean isValidBoolean(String JavaDoc str) {
39         return
40             (str != null &&
41                 (str.equalsIgnoreCase(StringConstants.TRUE_STRING) ||
42                  str.equalsIgnoreCase(StringConstants.FALSE_STRING))
43              );
44     }
45
46     /**
47      * Returns the value of the string or if the string is null, it returns an empty
48      * String
49      */

50     public static String JavaDoc cleanString(String JavaDoc str) {
51         return (str == null) ? "" : str;
52     }
53
54     /**
55      * <p>
56      * If the values given only contains a single element and this element is
57      * an empty string, this normally means that the values should be null. What
58      * happens is the since the HTML spec sucks really bad, it sends over empty
59      * text boxes as empty Strings rather than not sending them over. The reason
60      * this sucks is that every other input type, if unselected or empty, does
61      * not get sent over, text boxes do.
62      * </p>
63      *
64      * <p>
65      * So, if the array given has one element and it is empty or null (not trimmed
66      * empty), then this method returns null.
67      * </p>
68      *
69      * @param values The values to check for null
70      * @return Maybe null (see above)
71      */

72     public static String JavaDoc [] convertEmpty(String JavaDoc [] values) {
73         if (values != null && values.length == 1 && StringTools.isEmpty(values[0])) {
74             values = null;
75         }
76
77         return values;
78     }
79
80     /**
81      * Converts the contents of the given array of bytes to a String hexidecimal
82      * representations. Each character of the String is a single hex value. Therefore,
83      * the the pair of characters equals a single byte. This method is little-endian
84      * (I think).
85      *
86      * @param bytes The bytes to convert
87      * @return A String representation of the hex
88      */

89     public static String JavaDoc toHex(int[] bytes) {
90         int dataLength = (bytes.length * 2);
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(dataLength);
92         for (int i = 0; i < bytes.length; i++) {
93             if (bytes[i] < 0 || bytes[i] > 255) {
94                 throw new IllegalArgumentException JavaDoc("Invalid byte value " + bytes[i]);
95             }
96
97             if (bytes[i] >= 16) {
98                 buf.append(Integer.toHexString(bytes[i] / 16));
99                 buf.append(Integer.toHexString(bytes[i] - ((bytes[i] / 16) * 16)));
100             } else {
101                 buf.append(Integer.toHexString(bytes[i]));
102             }
103         }
104
105         return buf.toString();
106     }
107
108     /**
109      * Converts the given character from a hexidecimal value to a byte, which will
110      * be a value in the set {0, 15} inclusive. This means the character must be
111      * in the hexidecimal set {'0', 'f'}
112      *
113      * @param hexValue The hex value to convert
114      * @return A byte that is equal to the hex value
115      */

116     public static char toHex(byte hexValue) {
117         if (hexValue < 0 || hexValue >= 16) {
118             throw new IllegalArgumentException JavaDoc("Invalid hex value");
119         }
120
121         String JavaDoc str = Integer.toHexString(hexValue);
122         if (str.length() > 1) {
123             throw new IllegalStateException JavaDoc("JVM toHexString method incorrect");
124         }
125
126         return str.charAt(0);
127     }
128
129     /**
130      * Converts the contents of the given String from hexidecimal to an array
131      * of bytes. Each character of the String is a single hex value. Therefore,
132      * the the pair of characters equals a single byte. This method is little-endian
133      * (I think).
134      *
135      * @param hexString The hex String to convert
136      * @return An array of bytes
137      */

138     public static byte[] fromHex(String JavaDoc hexString) {
139         int length = hexString.length();
140
141         if ((length & 0x01) != 0) {
142             throw new IllegalArgumentException JavaDoc("odd number of characters.");
143         }
144
145         byte[] out = new byte[length >> 1];
146
147         // two characters form the hex value.
148
for (int i = 0, j = 0; j < length; i++) {
149             int f = Character.digit(hexString.charAt(j++), 16) << 4;
150             f = f | Character.digit(hexString.charAt(j++), 16);
151             out[i] = (byte) (f & 0xFF);
152         }
153
154         return out;
155     }
156
157     /**
158      * Converts the given character from a hexidecimal value to a byte, which will
159      * be a value in the set {0, 15} inclusive. This means the character must be
160      * in the hexidecimal set {'0', 'F'}
161      *
162      * @param hexValue The hex value to convert
163      * @return A byte that is equal to the hex value
164      */

165     public static byte fromHex(char hexValue) {
166         byte b = (byte) Character.digit(hexValue, 16);
167         if (b == -1) {
168             throw new IllegalArgumentException JavaDoc("Invalid hex character");
169         }
170
171         return b;
172     }
173 }
Popular Tags