Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/StringUtil.java


1   /*
2    * StringUtil.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import com.flexstor.common.Constants;
14  
15  /**
16   * String Utility Class
17   * @author Dan Schroeder
18   * @version 1.1
19   */
20  public class StringUtil
21  {
22     /**
23      * Returns a copy of the string array.
24      */
25     public static String[] copyArray ( String[] saOrg )
26     {
27        String[] saNew = new String[saOrg.length];
28  
29        for ( int nLoop = 0; nLoop < saOrg.length; nLoop++ )
30           saNew[nLoop] = saOrg[nLoop];
31  
32        return saNew;
33     }
34     
35     /**
36      * Check if a string contains invalid characters (caret, pipe, carriage return).
37      * @param aString the string to be examined
38      * @return true if aString contains an invalid character
39      * @return false if aString contains only valid characters
40      */
41     public static boolean hasInvalidChars(String aString)
42     {
43        char[] charArray = aString.toCharArray();
44        for (int i = 0; i < charArray.length; i++)
45        {
46           if (charArray[i] == '^'
47           ||  charArray[i] == '|'
48           ||  charArray[i] == 13
49           ||  charArray[i] == 10)
50              return true;
51        }
52  
53        return false;
54     }  // hasInvalidChars
55  
56     /**
57      * Gets the file name from an URL string, e.g.
58      * getFileNameFromURL("http://www.flexstor.com/images/thumb.gif" would
59      * return "thumb.gif"
60      * @param the URL string to parse
61      */
62     public static String getFileNameFromURL(String sURL)
63     {
64        int lastSlash = sURL.lastIndexOf("/");
65        if (lastSlash == -1)
66           return Constants.EMPTY_STRING;
67        else
68           return sURL.substring(lastSlash + 1);
69     } // getNamefromUrl
70     
71     /**
72      * Replaces all occurences of one string within the source string.
73      * Does not modify the souce.
74      * @param sSource the string to modify
75      * @param sSubString the substring to search for
76      * @param sReplacement the replacement string for substring
77      * @return a copy of the modified target
78     **/
79     public static String replaceString(String sSource, String sSubString, String sReplacement)
80    {
81       int nStartIndex = 0;
82       int nEndIndex   = 0;
83       StringBuffer sTarget = new StringBuffer();
84  
85       while((nEndIndex = sSource.indexOf(sSubString, nStartIndex)) >= 0)
86       {
87          if (nEndIndex > nStartIndex)
88             sTarget.append(sSource.substring(nStartIndex, nEndIndex));
89          sTarget.append(sReplacement);
90          nStartIndex = nEndIndex + sSubString.length();
91       }
92       if (nStartIndex < sSource.length())
93          sTarget.append(sSource.substring(nStartIndex));
94  
95       return sTarget.toString();
96     }
97  
98     /**
99      * Encodes a URL string, by replacing invalid characters with %<something>.
100     * Please note that the slash, backslash, and colon are not being replaced by
101     * this function, as this would screw up the URL.
102     *
103     * @param sInput the URL string to be converted.
104     * @return the converted string.
105     */
106    public static String encodeURLString ( String sInput )
107    {
108       if ( sInput == null )
109          return null;
110 
111       int    nLength = sInput.length();
112       char   c;
113       String sReturn = "";
114 
115       for ( int i = 0; i < nLength; i++ )
116       {
117          c = sInput.charAt(i);
118          sReturn += replaceChar( c );
119 
120       }
121       return sReturn;
122    }
123 
124    /**
125     * Decodes a URL string, by replacing %<something> with the actual character.
126     * Please note that the slash, backslash, and colon are not being replaced by
127     * this function, as this would screw up the URL.
128     *
129     * @param sInput the URL string to be converted.
130     * @return the converted string.
131     */
132    public static String decodeURLString ( String sInput )
133    {
134 
135        if ( sInput == null )
136          return null;
137 
138       int    nLength = sInput.length();
139       String   sCode;
140       char cCurrent;
141       String sReturn = "";
142 
143       for ( int i = 0; i < nLength; i++ )
144       {
145          cCurrent = sInput.charAt(i);
146 
147          if(cCurrent == '%')
148          {
149             if(i + 2 < nLength) // make sure there's enough characters left
150             {
151                sCode = sInput.substring(i , i + 3);
152                sReturn += replacePercent( sCode );
153             }
154 
155              i += 2; // increment counter to skip over the code part
156          }
157          else // % was not the current char
158          {
159             sReturn += cCurrent;
160          }
161       }
162       return sReturn;
163    }
164    
165    /**
166    * In accordance with RFC 2396...
167    * 2.2. Reserved Characters
168    * 
169    * Many URI include components consisting of or delimited by, certain
170    * special characters.  These characters are called "reserved", since
171    * their usage within the URI component is limited to their reserved
172    * purpose.  If the data for a URI component would conflict with the
173    * reserved purpose, then the conflicting data must be escaped before
174    * forming the URI.
175    * 
176    * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |"$" | ","
177    *
178    * Others have also been added as needed.  And ":" and "/" were omitted
179    **/
180 
181    public static String replaceChar( char c )
182    {
183       if(c == ' ')
184          return "%20";
185 
186       else if (c == '#')
187          return "%23";
188 
189       else if (c == ';')
190          return "%3b";
191 
192       else if (c == '=')
193          return "%3d";
194 
195       else if (c == '?')
196          return "%3f";
197 
198       else if (c == '&')
199          return "%26";
200 
201       else if (c == '@')
202          return "%40";
203 
204       else if (c == '<')
205          return "%3c";
206 
207       else if (c == '>')
208          return "%3e";
209 
210       else if (c == '\"')
211          return "%22";
212 
213       else if (c == '%')
214          return "%25";
215 
216       else if (c == '{')
217          return "%7b";
218 
219       else if (c == '}')
220          return "%7d";
221 
222       else if (c == '|')
223          return "%7c";
224 
225       else if (c == '^')
226          return "%5e";
227 
228       else if (c == '~')
229          return "%7e";
230 
231       else if (c == '[')
232          return "%5b";
233 
234       else if (c == ']')
235          return "%5d";
236 
237       else if (c == '`')
238          return "%60";
239 
240       else if (c == '+')
241          return "%2b";
242          
243       else if (c == '$')
244          return "%24";
245          
246       else if (c == ',')
247          return "%2c";
248          
249       else
250          return String.valueOf( c );
251    }
252    
253    public static String replacePercent( String sCode )
254    {
255       if(sCode.equals("%20"))
256          return " " ;
257 
258       else if (sCode.equals("%23"))
259          return "#";
260 
261       else if (sCode.equals("%3b"))
262          return ";";
263 
264       else if (sCode.equals("%3d"))
265          return "=";
266 
267       else if (sCode.equals("%3f"))
268          return "?";
269 
270       else if (sCode.equals("%26"))
271          return "&";
272 
273       else if (sCode.equals("%40"))
274          return "@";
275 
276       else if (sCode.equals("%3c"))
277          return "<";
278 
279       else if (sCode.equals("%3e"))
280          return ">";
281 
282       else if (sCode.equals("%22"))
283          return "\"";
284 
285       else if (sCode.equals("%25"))
286          return "%";
287 
288       else if (sCode.equals("%7b"))
289          return "{";
290 
291       else if (sCode.equals("%7d"))
292          return "}";
293 
294       else if (sCode.equals("%7c"))
295          return "|";
296 
297       else if (sCode.equals("%5e"))
298          return "^";
299 
300       else if (sCode.equals("%7e"))
301          return "~";
302 
303       else if (sCode.equals("%5b"))
304          return "[";
305 
306       else if (sCode.equals("%5d"))
307          return "]";
308 
309       else if (sCode.equals("%60"))
310          return "`";
311                       
312       else if (sCode.equals("%2b"))
313          return "+";
314       
315       else if (sCode.equals("%24"))
316          return "$";
317          
318       else if (sCode.equals("%2c"))
319          return ",";
320          
321       else
322          return sCode;
323    }
324 
325    /**
326     * Takes a String and capitalizes its first letter
327     * 
328     * @param s The String to capitalize
329     * @return The String with its first letter capitalized
330     */
331    public static String toProper( String s )
332    {
333       char c;
334       StringBuffer sb = new StringBuffer(s.length());
335       
336       c = s.charAt(0);
337       sb.append( Character.toUpperCase(c) );
338       
339       for ( int i = 1; i < s.length(); i++ )
340       {
341          c = s.charAt(i);
342          sb.append( Character.toLowerCase(c) );
343       }
344       return sb.toString();
345    }
346 }  // end of class