Source Code Cross Referenced for StringTool.java in  » Installer » IzPack » com » izforge » izpack » util » 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 » Installer » IzPack » com.izforge.izpack.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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