001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.mailet.base;
021
022 import java.security.MessageDigest;
023 import java.security.NoSuchAlgorithmException;
024 import java.util.ArrayList;
025
026 /**
027 * Collects useful string utility methods.
028 */
029 public final class StringUtils {
030
031 private StringUtils() {
032 // make this class non instantiable
033 }
034
035 /**
036 * Splits a string given a pattern (regex), considering escapes.
037 * <p> For example considering a pattern "," we have:
038 * one,two,three => {one},{two},{three}
039 * one\,two\\,three => {one,two\\},{three}
040 * <p>
041 * NOTE: Untested with pattern regex as pattern and untested for escape chars in text or pattern.
042 */
043 public static String[] split(String text, String pattern) {
044 String[] array = text.split(pattern, -1);
045 ArrayList<String> list = new ArrayList<String>();
046 for (int i = 0; i < array.length; i++) {
047 boolean escaped = false;
048 if (i > 0 && array[i - 1].endsWith("\\")) {
049 // When the number of trailing "\" is odd then there was no separator and this pattern is part of
050 // the previous match.
051 int depth = 1;
052 while (depth < array[i-1].length() && array[i-1].charAt(array[i-1].length() - 1 - depth) == '\\') depth ++;
053 escaped = depth % 2 == 1;
054 }
055 if (!escaped) list.add(array[i]);
056 else {
057 String prev = list.remove(list.size() - 1);
058 list.add(prev.substring(0, prev.length() - 1) + pattern + array[i]);
059 }
060 }
061 return list.toArray(new String[list.size()]);
062 }
063
064 /**
065 * Creates an MD5 digest from the message.
066 * Note that this implementation is unsalted.
067 * @param message not null
068 * @return MD5 digest, not null
069 */
070 public static String md5(java.lang.String message) {
071 try {
072 MessageDigest md = MessageDigest.getInstance("MD5");
073 StringBuilder sb = new StringBuilder();
074 byte buf[] = message.getBytes();
075 byte[] md5 = md.digest(buf);
076 //System.out.println(message);
077 for (byte aMd5 : md5) {
078 String tmpStr = "0" + Integer.toHexString((0xff & aMd5));
079 sb.append(tmpStr.substring(tmpStr.length() - 2));
080 }
081 return sb.toString();
082
083 } catch (NoSuchAlgorithmException e) {
084 return null;
085 }
086 }
087
088 /**
089 * Capitalizes each word in the given text by converting the
090 * first letter to upper case.
091 * @param data text to be capitalize, possibly null
092 * @return text with each work capitalized,
093 * or null when the text is null
094 */
095 public static String capitalizeWords(String data) {
096 if (data==null) return null;
097 StringBuilder res = new StringBuilder();
098 char ch;
099 char prevCh = '.';
100 for ( int i = 0; i < data.length(); i++ ) {
101 ch = data.charAt(i);
102 if ( Character.isLetter(ch)) {
103 if (!Character.isLetter(prevCh) ) res.append( Character.toUpperCase(ch) );
104 else res.append( Character.toLowerCase(ch) );
105 } else res.append( ch );
106 prevCh = ch;
107 }
108 return res.toString();
109 }
110 }