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.ws.security.util;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  public final class StringUtil {
26      
27      private StringUtil() {
28          // Complete
29      }
30  
31      /**
32       * Tests if this string starts with the specified prefix (Ignoring whitespaces)
33       *
34       * @param prefix
35       * @param string
36       * @return boolean
37       */
38      public static boolean startsWithIgnoreWhitespaces(String prefix, String string) {
39          int index1 = 0;
40          int index2 = 0;
41          int length1 = prefix.length();
42          int length2 = string.length();
43          char ch1 = ' ';
44          char ch2 = ' ';
45          while (index1 < length1 && index2 < length2) {
46              while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) {
47                  index1++;
48              }
49              while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) {
50                  index2++;
51              }
52              if (index1 == length1 && index2 == length2) {
53                  return true;
54              }
55              if (ch1 != ch2) {
56                  return false;
57              }
58              index1++;
59              index2++;
60          }
61          if (index1 < length1 && index2 >= length2) {
62              return false;
63          }
64          return true;
65      }
66  
67      /**
68       * <p>Splits the provided text into an array, separator specified.
69       * This is an alternative to using StringTokenizer.</p>
70       * <p/>
71       * <p>The separator is not included in the returned String array.
72       * Adjacent separators are treated as one separator.</p>
73       * <p/>
74       * <p>A <code>null</code> input String returns <code>null</code>.</p>
75       * <p/>
76       * <pre>
77       * StringUtils.split(null, *)         = null
78       * StringUtils.split("", *)           = []
79       * StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
80       * StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
81       * StringUtils.split("a:b:c", '.')    = ["a:b:c"]
82       * StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
83       * StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
84       * </pre>
85       *
86       * @param str           the String to parse, may be null
87       * @param separatorChar the character used as the delimiter,
88       *                      <code>null</code> splits on whitespace
89       * @return an array of parsed Strings, <code>null</code> if null String input
90       */
91      public static String[] split(String str, char separatorChar) {
92          if (str == null) {
93              return null;
94          }
95          int len = str.length();
96          if (len == 0) {
97              return new String[0];
98          }
99          List<String> list = new ArrayList<String>();
100         int i = 0, start = 0;
101         boolean match = false;
102         while (i < len) {
103             if (str.charAt(i) == separatorChar) {
104                 if (match) {
105                     list.add(str.substring(start, i));
106                     match = false;
107                 }
108                 start = ++i;
109                 continue;
110             }
111             match = true;
112             i++;
113         }
114         if (match) {
115             list.add(str.substring(start, i));
116         }
117         return (String[]) list.toArray(new String[list.size()]);
118     }
119 }