001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.util;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.PrintWriter;
022: import java.util.Locale;
023:
024: /**
025: * Convenient class used only for uncapitalization Usually use commons lang but here we do not want
026: * to have such a dependency for only one feature
027: */
028: public final class StringUtils {
029:
030: private StringUtils() {
031: //Utility class
032: }
033:
034: public static String uncapitalize(String string) {
035: if (string == null || string.length() == 0) {
036: return string;
037: }
038: if (string.length() == 1) {
039: return string.toLowerCase(Locale.US);
040: }
041: return string.substring(0, 1).toLowerCase(Locale.US)
042: + string.substring(1);
043: }
044:
045: /**
046: * Returns the error message associated with the given exception. Th error message returned will
047: * try to be as precise as possible, handling cases where e.getMessage() is not meaningful, like
048: * {@link NullPointerException} for instance.
049: *
050: * @param e
051: * the exception to get the error message from
052: * @return the error message of the given exception
053: */
054: public static String getErrorMessage(Exception e) {
055: if (e == null) {
056: return "";
057: }
058: String errMsg = e instanceof RuntimeException ? e.getMessage()
059: : e.toString();
060: if (errMsg == null || errMsg.length() == 0
061: || "null".equals(errMsg)) {
062: errMsg = e.getClass().getName() + " at "
063: + e.getStackTrace()[0].toString();
064: }
065: return errMsg;
066: }
067:
068: /**
069: * Returns the exception stack trace as a String.
070: *
071: * @param e
072: * the exception to get the stack trace from.
073: * @return the exception stack trace
074: */
075: public static String getStackTrace(Exception e) {
076: if (e == null) {
077: return "";
078: }
079: ByteArrayOutputStream baos = new ByteArrayOutputStream();
080: PrintWriter printWriter = new PrintWriter(baos);
081: e.printStackTrace(printWriter);
082: printWriter.flush();
083: String stackTrace = new String(baos.toByteArray());
084: printWriter.close();
085: return stackTrace;
086: }
087:
088: /**
089: * Joins the given object array in one string, each separated by the given separator.
090: *
091: * Example:
092: * <pre>
093: * join(new String[] {"one", "two", "three"}, ", ") -> "one, two, three"
094: * </pre>
095: *
096: * @param objs The array of objects (<code>toString()</code> is used).
097: * @param sep The separator to use.
098: * @return The concatinated string.
099: */
100: public static String join(Object[] objs, String sep) {
101: StringBuffer buf = new StringBuffer();
102: for (int i = 0; i < objs.length; i++) {
103: buf.append(objs[i]).append(sep);
104: }
105: if (objs.length > 0) {
106: buf.setLength(buf.length() - sep.length()); // delete sep
107: }
108: return buf.toString();
109: }
110:
111: // basic string codec (same algo as CVS passfile, inspired by ant CVSPass class
112: /** Array contain char conversion data */
113: private static final char[] SHIFTS = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
114: 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
115: 24, 25, 26, 27, 28, 29, 30, 31, 114, 120, 53, 79, 96, 109,
116: 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, 111, 52, 75, 119,
117: 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105, 41,
118: 57, 83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91,
119: 35, 125, 55, 54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88,
120: 107, 106, 56, 36, 121, 117, 104, 101, 100, 69, 73, 99, 63,
121: 94, 93, 39, 37, 61, 48, 58, 113, 32, 90, 44, 98, 60, 51,
122: 33, 97, 62, 77, 84, 80, 85, 223, 225, 216, 187, 166, 229,
123: 189, 222, 188, 141, 249, 148, 200, 184, 136, 248, 190, 199,
124: 170, 181, 204, 138, 232, 218, 183, 255, 234, 220, 247, 213,
125: 203, 226, 193, 174, 172, 228, 252, 217, 201, 131, 230, 197,
126: 211, 145, 238, 161, 179, 160, 212, 207, 221, 254, 173, 202,
127: 146, 224, 151, 140, 196, 205, 130, 135, 133, 143, 246, 192,
128: 159, 244, 239, 185, 168, 215, 144, 139, 165, 180, 157, 147,
129: 186, 214, 176, 227, 231, 219, 169, 175, 156, 206, 198, 129,
130: 164, 150, 210, 154, 177, 134, 127, 182, 128, 158, 208, 162,
131: 132, 167, 209, 149, 241, 153, 251, 237, 236, 171, 195, 243,
132: 233, 253, 240, 194, 250, 191, 155, 142, 137, 245, 235, 163,
133: 242, 178, 152 };
134:
135: /**
136: * Encrypt the given string in a way which anybody having access to this method algorithm can
137: * easily decrypt. This is useful only to avoid clear string storage in a file for example, but
138: * shouldn't be considered as a real mean of security. This only works with simple characters
139: * (char < 256).
140: *
141: * @param str
142: * the string to encrypt
143: * @return the encrypted version of the string
144: */
145: public static final String encrypt(String str) {
146: if (str == null) {
147: return null;
148: }
149: StringBuffer buf = new StringBuffer();
150: for (int i = 0; i < str.length(); i++) {
151: char c = str.charAt(i);
152: if (c >= SHIFTS.length) {
153: throw new IllegalArgumentException(
154: "encrypt method can only be used with simple characters. '"
155: + c + "' not allowed");
156: }
157: buf.append(SHIFTS[c]);
158: }
159: return buf.toString();
160: }
161:
162: /**
163: * Decrypts a string encrypted with encrypt.
164: *
165: * @param str
166: * the encrypted string to decrypt
167: * @return The decrypted string.
168: */
169: public static final String decrypt(String str) {
170: if (str == null) {
171: return null;
172: }
173: StringBuffer buf = new StringBuffer();
174: for (int i = 0; i < str.length(); i++) {
175: buf.append(decrypt(str.charAt(i)));
176: }
177: return buf.toString();
178: }
179:
180: private static char decrypt(char c) {
181: for (char i = 0; i < SHIFTS.length; i++) {
182: if (SHIFTS[i] == c) {
183: return i;
184: }
185: }
186: throw new IllegalArgumentException("Impossible to decrypt '"
187: + c + "'. Unhandled character.");
188: }
189:
190: }
|