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.pluto.util;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: /**
024: * Static class hosting a couple of utility methods around strings.
025: */
026: public class StringUtils {
027:
028: // Private Constructor -----------------------------------------------------
029:
030: /**
031: * Private constructor that prevents external instantiation.
032: */
033: private StringUtils() {
034: // Do nothing.
035: }
036:
037: // Static Utility Methods --------------------------------------------------
038:
039: /**
040: * Replaces all occurrences of a pattern within a string by a replacement.
041: * @param source the string that should be searched.
042: * @param pattern the pattern that should be replaced.
043: * @param replace the replacement that should be inserted instead of the
044: * pattern.
045: * @return The updated source string.
046: */
047: public static String replace(String source, String pattern,
048: String replace) {
049: if (source == null || source.length() == 0 || pattern == null
050: || pattern.length() == 0) {
051: return source;
052: }
053:
054: int k = source.indexOf(pattern);
055:
056: if (k == -1) {
057: return source;
058: }
059:
060: StringBuffer out = new StringBuffer();
061: int i = 0, l = pattern.length();
062:
063: while (k != -1) {
064: out.append(source.substring(i, k));
065:
066: if (replace != null) {
067: out.append(replace);
068: }
069:
070: i = k + l;
071: k = source.indexOf(pattern, i);
072: }
073: out.append(source.substring(i));
074: return out.toString();
075: }
076:
077: /**
078: * TODO: can't we just use String[].clone()?
079: * @param source
080: * @return
081: */
082: public static String[] copy(String[] source) {
083: if (source == null) {
084: return null;
085: }
086: int length = source.length;
087: String[] result = new String[length];
088: System.arraycopy(source, 0, result, 0, length);
089: return result;
090: }
091:
092: /**
093: * Deep-clones a parameter map. The key is the parameter name as a String
094: * instance, while the value is a String array (String[]) instance.
095: * @param parameters the parameter map to deep-clone.
096: * @return the deep-cloned parameter map.
097: */
098: public static Map copyParameters(Map parameters) {
099: Map result = new HashMap(parameters);
100: for (Iterator it = result.entrySet().iterator(); it.hasNext();) {
101: Map.Entry entry = (Map.Entry) it.next();
102: if (!(entry.getKey() instanceof String)) {
103: throw new IllegalArgumentException(
104: "Parameter map keys "
105: + "must not be null and of type java.lang.String.");
106: }
107: try {
108: entry.setValue(copy((String[]) entry.getValue()));
109: } catch (ClassCastException ex) {
110: throw new IllegalArgumentException(
111: "Parameter map values "
112: + "must not be null and of type java.lang.String[].");
113: }
114: }
115: return result;
116: }
117:
118: /**
119: * Strips the specified mime type by removing the character encoding
120: * specified at the end of the mime type (all characters after the ';').
121: * The stripped mime type is trimmed string which contains no white
122: * spaces at the beginning and the end.
123: * @param mimeType the mime type to strip.
124: * @return the stripped mime type.
125: */
126: public static String getMimeTypeWithoutEncoding(String mimeType) {
127: int index = mimeType.indexOf(';');
128: String strippedType = null;
129: if (index == -1) {
130: strippedType = mimeType;
131: } else {
132: strippedType = mimeType.substring(0, index);
133: }
134: return strippedType.trim();
135: }
136:
137: }
|