View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.mailet.base;
21  
22  import java.security.MessageDigest;
23  import java.security.NoSuchAlgorithmException;
24  import java.util.ArrayList;
25  
26  /**
27   * Collects useful string utility methods.
28   */
29  public final class StringUtils {
30  
31      private StringUtils() {
32          // make this class non instantiable
33      }
34      
35      /**
36       * Splits a string given a pattern (regex), considering escapes.
37       * <p> For example considering a pattern "," we have:
38       * one,two,three => {one},{two},{three}
39       * one\,two\\,three => {one,two\\},{three}
40       * <p>
41       * NOTE: Untested with pattern regex as pattern and untested for escape chars in text or pattern.
42       */
43      public static String[] split(String text, String pattern) {
44          String[] array = text.split(pattern, -1);
45          ArrayList list = new ArrayList();
46          for (int i = 0; i < array.length; i++) {
47              boolean escaped = false;
48              if (i > 0 && array[i - 1].endsWith("\\")) {
49                  // When the number of trailing "\" is odd then there was no separator and this pattern is part of
50                  // the previous match.
51                  int depth = 1;
52                  while (depth < array[i-1].length() && array[i-1].charAt(array[i-1].length() - 1 - depth) == '\\') depth ++;
53                  escaped = depth % 2 == 1;
54              }
55              if (!escaped) list.add(array[i]);
56              else {
57                  String prev = (String) list.remove(list.size() - 1); 
58                  list.add(prev.substring(0, prev.length() - 1) + pattern + array[i]);
59              }
60          }
61          return (String[]) list.toArray(new String[0]);
62      }
63  
64      /**
65       * Creates an MD5 digest from the message.
66       * Note that this implementation is unsalted.
67       * @param message not null
68       * @return MD5 digest, not null
69       */
70      public static String md5(java.lang.String message) {
71          try {
72              MessageDigest md = MessageDigest.getInstance("MD5");
73              StringBuffer sb = new StringBuffer();
74              byte buf[] = message.getBytes();
75              byte[] md5 = md.digest(buf);
76              //System.out.println(message);
77              for (int i = 0; i < md5.length; i++) {
78                  String tmpStr = "0" + Integer.toHexString((0xff & md5[i]));
79                  sb.append(tmpStr.substring(tmpStr.length() - 2));
80              }
81              return sb.toString();
82              
83          } catch (NoSuchAlgorithmException e) {
84              return null;
85          }
86      }
87  
88      /**
89       * Capitalizes each word in the given text by converting the
90       * first letter to upper case.
91       * @param data text to be capitalize, possibly null
92       * @return text with each work capitalized, 
93       * or null when the text is null
94       */
95      public static String capitalizeWords(String data) {
96          if (data==null) return null;
97          StringBuffer res = new StringBuffer();
98          char ch;
99          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 }