001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.system;
017:
018: import java.io.*;
019:
020: public class StringUtils {
021: /**
022: * Makes a (path) string shorter to get it displayed correctly
023: */
024: public static String cutPath(String s) {
025: int maxlabel = 64;
026:
027: if (s.length() > maxlabel) {
028: while (s.indexOf("/") >= 0) {
029:
030: s = StringUtils.cutAfter(s, '/');
031:
032: if (s.length() < 16) {
033: StringBuffer sb = new StringBuffer(s);
034: sb.insert(0, ".../");
035:
036: return sb.toString();
037: }
038: }
039: }
040:
041: return s;
042: }
043:
044: /**
045: * Removes the a string at the beginning of a string
046: */
047: public static String removeStart(String str, String what) {
048: if (str.startsWith(what)) {
049: int x = what.length();
050:
051: return str.substring(x);
052: } else {
053: return str;
054: }
055: }
056:
057: /**
058: * Returns the rest of a string after a given character
059: */
060: public static String cutAfter(String str, char c) {
061: for (int i = 0; i < str.length(); i++) {
062: if (str.charAt(i) == c) {
063: // System.out.println(str.substring(i+1));
064: return str.substring(i + 1);
065: }
066: }
067:
068: return str;
069: }
070:
071: /**
072: * Used to search for return codes in a string array.
073: * Returns the first one found
074: */
075: public static String contains(String[] tmp, String[] str) {
076: for (int i = 0; i < tmp.length; i++) {
077: for (int j = 0; j < str.length; j++) {
078: if (tmp[i].startsWith(str[j])) {
079: return tmp[i];
080: }
081: }
082: }
083:
084: return "";
085: }
086:
087: /**
088: * Returns true if the given string contains the given character
089: */
090: public static boolean strstr(String tmp, char str) {
091: for (int i = 0; i < tmp.length(); i++) {
092: if (tmp.charAt(i) == str) {
093: return true;
094: }
095: }
096:
097: return false;
098: }
099:
100: /**
101: * Returns a string representing a given character
102: */
103: public static String string(char c) {
104: char[] buf = new char[1];
105: buf[0] = c;
106:
107: return new String(buf);
108: }
109:
110: /**
111: * Get a filename out of a full path string
112: */
113: public static String getFile(String file) {
114: int x = file.lastIndexOf("/");
115:
116: // unix
117: if (x >= 0) {
118: file = file.substring(x + 1);
119: }
120:
121: // windows
122: x = file.lastIndexOf("\\");
123:
124: if (x >= 0) {
125: file = file.substring(x + 1);
126: }
127:
128: // may work, but can test the other method better
129: //int x = file.lastIndexOf(File.separatorChar);
130: //if(x >= 0) file = file.substring(x+1);
131: //System.out.println(file);
132: return file;
133: }
134:
135: /**
136: * Returns a string representing a relative directory path.
137: * Examples: "/tmp/dir/" -> "dir/" and "/tmp/dir" -> "dir"
138: */
139: public static String getDir(String tmp) {
140: int x;
141:
142: while (true) {
143: x = tmp.indexOf("/");
144:
145: if ((x == (tmp.length() - 1)) || (x < 0)) {
146: break;
147: } else {
148: tmp = tmp.substring(x + 1);
149: }
150: }
151:
152: while (true) {
153: x = tmp.indexOf("\\");
154:
155: if ((x == (tmp.length() - 1)) || (x < 0)) {
156: break;
157: } else {
158: tmp = tmp.substring(x + 1);
159: }
160: }
161:
162: return tmp;
163: }
164:
165: /*
166: * Returns true if the string represents a relative filename, false otherwise
167: */
168: public static boolean isRelative(String file) {
169: // unix
170: if (file.startsWith("/")) {
171: return false;
172: }
173:
174: // windows
175: if ((file.length() > 2) && (file.charAt(1) == ':')) {
176: return false;
177: }
178:
179: //System.out.println("true: " + file);
180: // default
181: return true;
182: }
183:
184: /**
185: * Main method containing a few testcases for getFile() / isRelative()
186: */
187: public static void main(String[] argv) {
188: String a1 = "E:\\programme\\test.html";
189: String a2 = "programme\\test.html";
190: String a3 = "test.html";
191: String a4 = "/programme/test.html";
192: String a5 = "programme/test.html";
193:
194: System.out.println("getfile: " + getFile(a1) + " - false, "
195: + isRelative(a1));
196: System.out.println("getfile: " + getFile(a2) + " - true, "
197: + isRelative(a2));
198: System.out.println("getfile: " + getFile(a3) + " - true, "
199: + isRelative(a3));
200: System.out.println("getfile: " + getFile(a4) + " - false, "
201: + isRelative(a4));
202: System.out.println("getfile: " + getFile(a5) + " - true, "
203: + isRelative(a5));
204: }
205:
206: public static String cut(String tmp, String where) {
207: StringBuffer ret = new StringBuffer();
208:
209: for (int i = 0; i < tmp.length(); i++) {
210: if (!string(tmp.charAt(i)).equals(where)) {
211: ret.append(string(tmp.charAt(i)));
212: }
213:
214: if (string(tmp.charAt(i)).equals(where)) {
215: return ret.toString();
216: }
217: }
218:
219: return ret.toString();
220: }
221: }
|