001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2003 Marc Eppelmann
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.util;
023:
024: import java.io.File;
025: import java.util.ArrayList;
026:
027: /**
028: * A extended Java Implementation of Pythons string.replace()
029: *
030: * @author marc.eppelmann@gmx.de
031: */
032: public class StringTool {
033:
034: // ~ Constructors
035: // *********************************************************************************
036:
037: /**
038: * Default Constructor
039: */
040: public StringTool() {
041: super ();
042: }
043:
044: // ~ Methods
045: // **************************************************************************************
046:
047: /**
048: * Standalone callable Test method
049: *
050: * @param args Commandline Args
051: */
052: public static void main(String[] args) {
053: System.out.println("Test: string.replace(abc$defg,$de ,null ):"
054: + StringTool.replace("abc$defg", "$de", null, true));
055: }
056:
057: /**
058: * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
059: *
060: * @param value original String
061: * @param from Search Pattern
062: * @param to Replace with this
063: *
064: * @return the replaced String
065: */
066: public static String replace(String value, String from, String to) {
067: return replace(value, from, to, true);
068: }
069:
070: /**
071: * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
072: *
073: * @param value original String
074: * @param from Search Pattern
075: * @param to Replace with this
076: * @param aCaseSensitiveFlag set to true be case sensitive.
077: *
078: * @return the replaced String
079: */
080: public static String replace(String value, String from, String to,
081: boolean aCaseSensitiveFlag) {
082: if ((value == null) || (value.length() == 0) || (from == null)
083: || (from.length() == 0)) {
084: return value;
085: }
086:
087: if (to == null) {
088: to = "";
089: }
090:
091: if (!aCaseSensitiveFlag) {
092: from = from.toLowerCase();
093: }
094:
095: String result = value;
096: int lastIndex = 0;
097: int index = value.indexOf(from);
098:
099: if (index != -1) {
100: StringBuffer buffer = new StringBuffer();
101:
102: while (index != -1) {
103: buffer.append(value.substring(lastIndex, index))
104: .append(to);
105: lastIndex = index + from.length();
106: index = value.indexOf(from, lastIndex);
107: }
108:
109: buffer.append(value.substring(lastIndex));
110: result = buffer.toString();
111: }
112:
113: return result;
114: }
115:
116: /**
117: * Normalizes a Windows or Unix Path.
118: *
119: * Reason: Javas File accepts / or \ for Pathes. Batches or ShellScripts does it not!
120: *
121: * TODO: implement support for MAC < MacOSX
122: *
123: * @param destination
124: * @param fileSeparator a target-system fileseparator
125: *
126: * @return the normalized path
127: */
128: public static String normalizePath(String destination,
129: String fileSeparator) {
130: String FILESEP = (fileSeparator == null) ? File.separator
131: : fileSeparator;
132:
133: destination = StringTool.replace(destination, "\\", "/");
134:
135: // all occs of "//" by "/"
136: destination = StringTool.replace(destination, "//", "/");
137:
138: destination = StringTool.replace(destination, ":", ";");
139: destination = StringTool.replace(destination, ";", ":");
140:
141: destination = StringTool.replace(destination, "/", FILESEP);
142:
143: if ("\\".equals(FILESEP)) {
144: destination = StringTool.replace(destination, ":", ";");
145:
146: // results in "C;\" instead of "C:\"
147: // so correct it:
148: destination = StringTool.replace(destination, ";\\", ":\\");
149: }
150:
151: // Convert the file separator characters
152: return (destination);
153: }
154:
155: /**
156: * Normalizes a mixed Windows/Unix Path. Does Only work for Windows or Unix Pathes Reason:
157: * Java.File accepts / or \ for Pathes. Batches or ShellScripts does it not!
158: *
159: * @param destination accepted mixed form by java.File like "C:/a/mixed\path\accepted/by\Java"
160: *
161: * @return the normalized Path
162: */
163: public static String normalizePath(String destination) {
164: return (normalizePath(destination, null));
165: }
166:
167: /**
168: * Converts an String Array to a space separated String w/o any check
169: *
170: * @param args The StringArray
171: * @return the space separated result.
172: */
173: public static String stringArrayToSpaceSeparatedString(String[] args) {
174: String result = "";
175: for (String arg : args) {
176: result += arg + " ";
177: }
178: return result;
179: }
180:
181: public static String getPlatformEncoding() {
182: // TODO Auto-generated method stub
183: return System.getProperty("file.encoding");
184: }
185:
186: public static String UTF16() {
187: return "UTF-16";
188: }
189:
190: /**
191: * Transforms a (Array)List of Strings into a line.separator="\n" separated Stringlist.
192: *
193: * @param aStringList
194: *
195: * @return a printable list
196: */
197: public static String stringArrayListToString(ArrayList aStringList) {
198: return stringArrayListToString(aStringList, null);
199: }
200:
201: /**
202: * Transforms a (Array)List of Strings into an aLineSeparator separated Stringlist.
203: *
204: * @param aStringList
205: *
206: * @return a printable list
207: */
208: public static String stringArrayListToString(ArrayList aStringList,
209: String aLineSeparator) {
210: String LineSeparator = aLineSeparator;
211: if (LineSeparator == null)
212: LineSeparator = System.getProperty("line.separator", "\n");
213:
214: StringBuffer temp = new StringBuffer();
215:
216: for (Object anAStringList : aStringList) {
217: temp.append(anAStringList).append(LineSeparator);
218: }
219:
220: return temp.toString();
221: }
222:
223: /**
224: * True if a given string starts with the another given String
225: *
226: * @param str The String to search in
227: * @param prefix The string to search for
228: *
229: * @return True if str starts with prefix
230: */
231: public static boolean startsWith(String str, String prefix) {
232: return (str != null) && str.startsWith(prefix);
233: }
234:
235: /**
236: * The same as startsWith but ignores the case.
237: *
238: * @param str The String to search in
239: * @param prefix The string to search for
240: *
241: * @return rue if str starts with prefix
242: */
243: public static boolean startsWithIgnoreCase(String str, String prefix) {
244: return (str != null) && (prefix != null)
245: && str.toUpperCase().startsWith(prefix.toUpperCase());
246: }
247:
248: }
|