root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / StringTool.java @ 15280
History | View | Annotate | Download (5.07 KB)
1 | 15280 | rgaitan | /*
|
---|---|---|---|
2 | * Created on 11.11.2003
|
||
3 | *
|
||
4 | * $Id: StringTool.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
|
||
5 | * IzPack
|
||
6 | * Copyright (C) 2002 by Marc Eppelmann
|
||
7 | *
|
||
8 | * File : StringTool.java
|
||
9 | * Description : Extended implemenation of Pythons string.replace and more...
|
||
10 | * Author's email : marc.eppelmann@gmx.de
|
||
11 | * Website : http://www.izforge.com
|
||
12 | *
|
||
13 | * This program is free software; you can redistribute it and/or
|
||
14 | * modify it under the terms of the GNU General Public License
|
||
15 | * as published by the Free Software Foundation; either version 2
|
||
16 | * of the License, or any later version.
|
||
17 | *
|
||
18 | * This program is distributed in the hope that it will be useful,
|
||
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
21 | * GNU General Public License for more details.
|
||
22 | *
|
||
23 | * You should have received a copy of the GNU General Public License
|
||
24 | * along with this program; if not, write to the Free Software
|
||
25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
26 | *
|
||
27 | */
|
||
28 | package com.izforge.izpack.util; |
||
29 | |||
30 | import java.io.File; |
||
31 | |||
32 | |||
33 | /**
|
||
34 | * A extended Java Implementation of Pythons string.replace()
|
||
35 | *
|
||
36 | * @author marc.eppelmann@gmx.de
|
||
37 | */
|
||
38 | public class StringTool |
||
39 | { |
||
40 | //~ Constructors *********************************************************************************
|
||
41 | |||
42 | /**
|
||
43 | * Default Constructor
|
||
44 | */
|
||
45 | public StringTool( )
|
||
46 | { |
||
47 | super( );
|
||
48 | } |
||
49 | |||
50 | //~ Methods **************************************************************************************
|
||
51 | |||
52 | /**
|
||
53 | * Test method
|
||
54 | *
|
||
55 | * @param args Commandline Args
|
||
56 | */
|
||
57 | public static void main( String[] args ) |
||
58 | { |
||
59 | System.out.println( "Test: string.replace(abc$defg,$de ,null ):" + StringTool.replace( "abc$defg", "$de", null, true ) ); |
||
60 | } |
||
61 | |||
62 | /**
|
||
63 | * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
|
||
64 | *
|
||
65 | * @param value original String
|
||
66 | * @param from Search Pattern
|
||
67 | * @param to Replace with this
|
||
68 | *
|
||
69 | * @return the replaced String
|
||
70 | */
|
||
71 | public static String replace( String value, String from, String to ) |
||
72 | { |
||
73 | return replace( value, from, to, true ); |
||
74 | } |
||
75 | |||
76 | /**
|
||
77 | * Replaces <b>from</b> with <b>to</b> in given String: <b>value</b>
|
||
78 | *
|
||
79 | * @param value original String
|
||
80 | * @param from Search Pattern
|
||
81 | * @param to Replace with this
|
||
82 | * @param If set to true be case sensitive.
|
||
83 | *
|
||
84 | * @return the replaced String
|
||
85 | */
|
||
86 | public static String replace( String value, String from, String to, boolean aCaseSensitiveFlag ) |
||
87 | { |
||
88 | if( ( value == null ) || ( value.length( ) == 0 ) || ( from == null ) || ( from.length( ) == 0 ) ) |
||
89 | { |
||
90 | return value;
|
||
91 | } |
||
92 | |||
93 | if( to == null ) |
||
94 | { |
||
95 | to = "";
|
||
96 | } |
||
97 | |||
98 | String searchData = value;
|
||
99 | |||
100 | if( ! aCaseSensitiveFlag )
|
||
101 | { |
||
102 | searchData = value.toLowerCase( ); |
||
103 | from = from.toLowerCase( ); |
||
104 | } |
||
105 | |||
106 | String result = value;
|
||
107 | int lastIndex = 0; |
||
108 | int index = value.indexOf( from );
|
||
109 | |||
110 | if( index != -1 ) |
||
111 | { |
||
112 | StringBuffer buffer = new StringBuffer( ); |
||
113 | |||
114 | while( index != -1 ) |
||
115 | { |
||
116 | buffer.append( value.substring( lastIndex, index ) ).append( to ); |
||
117 | lastIndex = index + from.length( ); |
||
118 | index = value.indexOf( from, lastIndex ); |
||
119 | } |
||
120 | |||
121 | buffer.append( value.substring( lastIndex ) ); |
||
122 | result = buffer.toString( ); |
||
123 | } |
||
124 | |||
125 | return result;
|
||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * Normalizes a Windows or Unix Path.
|
||
130 | *
|
||
131 | * Reason: Javas File accepts / or \ for Pathes.
|
||
132 | * Batches or ShellScripts does it not!
|
||
133 | *
|
||
134 | * TODO: implement support for MAC < MacOSX
|
||
135 | *
|
||
136 | * @param destination
|
||
137 | * @param Force a target-system fileseparator
|
||
138 | *
|
||
139 | * @return
|
||
140 | */
|
||
141 | public static String normalizePath( String destination, String fileSeparator ) |
||
142 | { |
||
143 | String FILESEP = (fileSeparator == null) ? File.separator : fileSeparator; |
||
144 | |||
145 | destination = StringTool.replace( destination, "\\", "/" ); |
||
146 | |||
147 | //all occs of "//" by "/"
|
||
148 | destination = StringTool.replace( destination, "//", "/" ); |
||
149 | |||
150 | destination = StringTool.replace( destination, ":", ";" ); |
||
151 | destination = StringTool.replace( destination, ";", ":" ); |
||
152 | |||
153 | destination = StringTool.replace( destination, "/", FILESEP );
|
||
154 | |||
155 | if( "\\".equals( FILESEP ) ) |
||
156 | { |
||
157 | destination = StringTool.replace( destination, ":", ";" ); |
||
158 | |||
159 | // results in "C;\" instead of "C:\"
|
||
160 | // so correct it:
|
||
161 | destination = StringTool.replace( destination, ";\\", ":\\" ); |
||
162 | } |
||
163 | |||
164 | |||
165 | // Convert the file separator characters
|
||
166 | return ( destination );
|
||
167 | } |
||
168 | |||
169 | /**
|
||
170 | * Normalizes a mixed Windows/Unix Path.
|
||
171 | * Does Only work for Windows or Unix Pathes
|
||
172 | * Reason: Java.File accepts / or \ for Pathes.
|
||
173 | * Batches or ShellScripts does it not!
|
||
174 | *
|
||
175 | * @param A accepted mixed form by java.File like "C:/a/mixed\path\accepted/by\Java"
|
||
176 | *
|
||
177 | * @return the normalized Path
|
||
178 | */
|
||
179 | public static String normalizePath( String destination ) |
||
180 | { |
||
181 | return( normalizePath( destination, null ) ); |
||
182 | } |
||
183 | } |