KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > MyString


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util;
16
17 import java.io.*;
18
19 /**
20  * Just a simple String utility class.
21  * @author Akshathkumar Shetty
22  */

23 public class MyString {
24     private static Runtime JavaDoc runtime = Runtime.getRuntime();
25     private static java.text.DecimalFormat JavaDoc doublePrcNum =
26         new java.text.DecimalFormat JavaDoc("#,##0.00");
27
28     public static String JavaDoc replace(String JavaDoc source, String JavaDoc key,
29         String JavaDoc with) {
30         if(source==null)
31             throw new NullPointerException JavaDoc("Parameter -> source was null");
32         if(key==null)
33             throw new NullPointerException JavaDoc("Parameter -> key was null");
34         if(with==null)
35             throw new NullPointerException JavaDoc("Parameter -> with was null");
36         
37         int start=0;
38         int end=0;
39         String JavaDoc result="";
40         
41         start=source.indexOf(key);
42         end=start+key.length();
43         
44         if(start==-1)
45             return null;
46
47         result=source.substring(0,start);
48         result+=with;
49         result+=source.substring(end,source.length());
50         
51         return result;
52     }
53
54     public static String JavaDoc replaceAll(String JavaDoc source, String JavaDoc key,
55         String JavaDoc with) {
56         if(source==null)
57             throw new NullPointerException JavaDoc("Parameter -> source was null");
58         if(key==null)
59             throw new NullPointerException JavaDoc("Parameter -> key was null");
60         if(with==null)
61             throw new NullPointerException JavaDoc("Parameter -> with was null");
62         
63         String JavaDoc temp="";
64         
65         while(true) {
66             temp="";
67             temp=replace(source,key,with);
68             if(temp==null)
69                 break;
70             else
71                 source=temp;
72         }
73
74         return source;
75     }
76
77     
78     public static int replaceCount(String JavaDoc source,
79         String JavaDoc key) {
80         if(source==null)
81             throw new NullPointerException JavaDoc("Parameter -> source was null");
82         if(key==null)
83             throw new NullPointerException JavaDoc("Parameter -> key was null");
84
85         int count=0;
86         String JavaDoc result="";
87         String JavaDoc temp="";
88         
89         result=source;
90         while(true) {
91             temp="";
92             temp=replace(result,key,"");
93             if(temp==null) {
94                 break;
95             }
96             else {
97                 result=temp;
98                 count++;
99             }
100         }
101
102         return count;
103     }
104     
105     public static String JavaDoc replaceAllNo(String JavaDoc source,
106         String JavaDoc with) {
107         if(source==null)
108             throw new NullPointerException JavaDoc("One of parameter -> source was null");
109         if(with==null)
110             throw new NullPointerException JavaDoc("One of parameter -> with was null");
111         
112         for(int i=0;i<10;i++)
113             source=replaceAll(source,""+i,with);
114
115         return source;
116     }
117     
118
119     public static String JavaDoc removeAllHtmlSpChar(String JavaDoc source) {
120         String JavaDoc temp = source;
121         temp = replaceAll(temp,"&nbsp;"," ");
122         temp = replaceAll(temp,"&lt;","<");
123         temp = replaceAll(temp,"&gt;",">");
124         temp = replaceAll(temp,"&amp;","&");
125         temp = replaceAll(temp,"&quot;","\"");
126         return temp;
127     }
128
129     ///////// tags ////////////
130
// needs more work
131
public static String JavaDoc replaceTags(String JavaDoc source, String JavaDoc with) {
132         if(source==null)
133             throw new NullPointerException JavaDoc("One of parameter -> source was null");
134         if(with==null)
135             throw new NullPointerException JavaDoc("One of parameter -> with was null");
136
137         int start=0;
138         int end=0;
139         int error=0;
140         String JavaDoc result="";
141         
142         start=source.indexOf("<");
143         end=source.indexOf(">",start+1);
144         
145         error=source.indexOf("<",start+1);
146         if(error!=-1 && error<end)
147             throw new IllegalArgumentException JavaDoc("&lt; found before &gt;");
148
149         if(start==-1 || end==-1)
150             return null;
151
152         result=source.substring(0,start);
153         result+=with;
154         result+=source.substring(end+1,source.length());
155
156         return result;
157     }
158
159     public static String JavaDoc replaceAllTags(String JavaDoc source, String JavaDoc with) {
160         if(source==null)
161             throw new NullPointerException JavaDoc("One of parameter -> source was null");
162         if(with==null)
163             throw new NullPointerException JavaDoc("One of parameter -> with was null");
164             
165         String JavaDoc temp="";
166         
167         while(true) {
168             temp="";
169             temp=replaceTags(source,with);
170             if(temp==null)
171                 break;
172             else
173                 source=temp;
174         }
175
176         return source;
177     }
178     
179     /**
180      * Returns String form of an exception.
181      * @since 1.3.3
182      */

183     public static String JavaDoc getStackTrace(Throwable JavaDoc e) {
184         StringWriter writer = new StringWriter(1024);
185         e.printStackTrace(new PrintWriter(writer));
186         return writer.toString();
187     }
188
189     /**
190      * Returns formatted memory size.
191      * @since 1.4.5
192      */

193     public static String JavaDoc getMemInfo(float bytes) {
194         if(bytes<1024) {
195             return doublePrcNum.format(bytes)+" B";
196         }
197
198         bytes = bytes/1024;
199         if(bytes<1024) {
200             return doublePrcNum.format(bytes)+" KB";
201         }
202
203         bytes = bytes/1024;
204         if(bytes<1024) {
205             return doublePrcNum.format(bytes)+" MB";
206         }
207
208         bytes = bytes/1024;
209         return doublePrcNum.format(bytes)+" GB";
210     }
211
212     /**
213      * Returns System information.
214      * @since 1.4.5
215      */

216     public static String JavaDoc getSystemInfo(String JavaDoc version) {
217         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
218         sb.append("---- System Info Start ---");
219         sb.append("\r\n");
220
221         sb.append("QuickServer v");
222         sb.append(version);
223         sb.append(" is being used.");
224         sb.append("\r\n");
225
226         sb.append("Java VM v");
227         sb.append(System.getProperty("java.version"));
228         sb.append(" is being used.");
229         sb.append("\r\n");
230
231         sb.append("Operating System: ");
232         sb.append(System.getProperty("os.name"));
233         sb.append(" ");
234         sb.append(System.getProperty("os.version"));
235         sb.append("\r\n");
236
237         sb.append("Current working directory: ");
238         sb.append(System.getProperty("user.dir"));
239         sb.append("\r\n");
240
241         sb.append("Class/s loaded from: ");
242         sb.append(new MyString().getClass().getProtectionDomain().getCodeSource().getLocation());
243         sb.append("\r\n");
244
245         sb.append("Total memory currently available: ");
246         sb.append(MyString.getMemInfo(runtime.totalMemory()));
247         sb.append("\r\n");
248         sb.append("Memory currently in use: ");
249         sb.append(MyString.getMemInfo(runtime.totalMemory()-runtime.freeMemory()));
250         sb.append("\r\n");
251         sb.append("Maximum memory available: ");
252         sb.append(MyString.getMemInfo(runtime.maxMemory()));
253         sb.append("\r\n");
254         
255
256         sb.append("---- System Info End ---");
257
258         return sb.toString();
259     }
260
261     public static String JavaDoc alignRight(String JavaDoc data, int len) {
262         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(data);
263         while(sb.length()<len) {
264             sb.insert(0, ' ');
265         }
266         return sb.toString();
267     }
268
269     public static String JavaDoc alignLeft(String JavaDoc data, int len) {
270         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(data);
271         while(sb.length()<len) {
272             sb.append( ' ');
273         }
274         return sb.toString();
275     }
276 }
277
Popular Tags