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.myfaces.tobago.util;
021    
022    import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Locale;
027    import java.util.StringTokenizer;
028    
029    public class StringUtils {
030    
031      private StringUtils() {
032      }
033    
034      /**
035       * @deprecated Use commons-lang StringUtils.capitalize() instead.
036       */
037      @Deprecated
038      public static String firstToUpperCase(String string) {
039        if (Deprecation.LOG.isWarnEnabled()) {
040          Deprecation.LOG.warn("use commons-lang please");
041        }
042        if (string == null) {
043          return null;
044        }
045        switch (string.length()) {
046          case 0:
047            return string;
048          case 1:
049            return string.toUpperCase(Locale.ENGLISH);
050          default:
051            return string.substring(0, 1).toUpperCase(Locale.ENGLISH) + string.substring(1);
052        }
053      }
054    
055      public static List<Integer> parseIntegerList(String integerList)
056          throws NumberFormatException {
057        List<Integer> list = new ArrayList<Integer>();
058    
059        StringTokenizer tokenizer = new StringTokenizer(integerList, ", ");
060        while (tokenizer.hasMoreElements()) {
061          String token = tokenizer.nextToken().trim();
062          if (token.length() > 0) {
063            list.add(new Integer(token));
064          }
065        }
066    
067        return list;
068      }
069    
070      public static <T> String toString(List<T> list) {
071        StringBuilder buffer = new StringBuilder(",");
072        for (T t : list) {
073          buffer.append(t);
074          buffer.append(",");
075        }
076        return buffer.toString();
077      }
078    
079      @Deprecated
080      public static String escapeAccessKeyIndicator(String label) {
081        if (Deprecation.LOG.isWarnEnabled()) {
082          Deprecation.LOG.warn(label);
083        }
084        return org.apache.commons.lang.StringUtils.replace(label,
085            String.valueOf(LabelWithAccessKey.INDICATOR), LabelWithAccessKey.ESCAPED_INDICATOR);
086      }
087    
088      /**
089       * Checks if the String starts like a url, e.g. http: or xyz:
090       */
091      public static boolean isUrl(final String link) {
092        if (link == null) {
093          return false;
094        }
095        int colon = link.indexOf(':');
096        if (colon < 1) {
097          return false;
098        }
099        for (int i = 0; i < colon; i++) {
100          if (!Character.isLetter(link.charAt(i))) {
101            return false;
102          }
103        }
104        return true;
105      }
106    }