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: package org.apache.cocoon.util;
018:
019: /**
020: * A collection of <code>String</code> handling utility methods.
021: *
022: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
023: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
024: * @version CVS $Id: StringUtils.java 433543 2006-08-22 06:22:54Z crossley $
025: */
026: public class StringUtils {
027:
028: /**
029: * Split a string as an array using whitespace as separator
030: *
031: * @param line The string to be split
032: * @return An array of whitespace-separated tokens
033: */
034: public static String[] split(String line) {
035: return split(line, " \t\n\r");
036: }
037:
038: /**
039: * Split a string as an array using a given set of separators
040: *
041: * @param line The string to be split
042: * @param delimiter A string containing token separators
043: * @return An array of token
044: */
045: public static String[] split(String line, String delimiter) {
046: return Tokenizer.tokenize(line, delimiter, false);
047: }
048:
049: /**
050: * Tests whether a given character is alphabetic, numeric or
051: * underscore
052: *
053: * @param c The character to be tested
054: * @return whether the given character is alphameric or not
055: */
056: public static boolean isAlphaNumeric(char c) {
057: return c == '_' || (c >= 'a' && c <= 'z')
058: || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
059: }
060:
061: /**
062: * Counts the occurrence of the given char in the string.
063: *
064: * @param str The string to be tested
065: * @param c the char to be counted
066: * @return the occurrence of the character in the string.
067: * @deprecated Use {@link org.apache.commons.lang.StringUtils#countMatches(String, String)}
068: */
069: public static int count(String str, char c) {
070: int index = 0;
071: char[] chars = str.toCharArray();
072: for (int i = 0; i < chars.length; i++) {
073: if (chars[i] == c)
074: index++;
075: }
076: return index;
077: }
078:
079: /**
080: * Matches two strings.
081: *
082: * @param a The first string
083: * @param b The second string
084: * @return the index where the two strings stop matching starting from 0
085: * @deprecated Use {@link org.apache.commons.lang.StringUtils#indexOfDifference(String, String)}
086: */
087: public static int matchStrings(String a, String b) {
088: int i;
089: char[] ca = a.toCharArray();
090: char[] cb = b.toCharArray();
091: int len = (ca.length < cb.length) ? ca.length : cb.length;
092:
093: for (i = 0; i < len; i++) {
094: if (ca[i] != cb[i])
095: break;
096: }
097:
098: return i;
099: }
100:
101: /**
102: * Replaces tokens in input with Value present in System.getProperty
103: */
104: public static String replaceToken(String s) {
105: int startToken = s.indexOf("${");
106: int endToken = s.indexOf("}", startToken);
107: String token = s.substring(startToken + 2, endToken);
108: StringBuffer value = new StringBuffer();
109: value.append(s.substring(0, startToken));
110: value.append(System.getProperty(token));
111: value.append(s.substring(endToken + 1));
112: return value.toString();
113: }
114: }
|