Source Code Cross Referenced for StringUtils.java in  » Net » j-ftp » net » sf » jftp » system » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » j ftp » net.sf.jftp.system 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.