001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.util;
016:
017: import java.io.*;
018:
019: /**
020: * Just a simple String utility class.
021: * @author Akshathkumar Shetty
022: */
023: public class MyString {
024: private static Runtime runtime = Runtime.getRuntime();
025: private static java.text.DecimalFormat doublePrcNum = new java.text.DecimalFormat(
026: "#,##0.00");
027:
028: public static String replace(String source, String key, String with) {
029: if (source == null)
030: throw new NullPointerException(
031: "Parameter -> source was null");
032: if (key == null)
033: throw new NullPointerException("Parameter -> key was null");
034: if (with == null)
035: throw new NullPointerException("Parameter -> with was null");
036:
037: int start = 0;
038: int end = 0;
039: String result = "";
040:
041: start = source.indexOf(key);
042: end = start + key.length();
043:
044: if (start == -1)
045: return null;
046:
047: result = source.substring(0, start);
048: result += with;
049: result += source.substring(end, source.length());
050:
051: return result;
052: }
053:
054: public static String replaceAll(String source, String key,
055: String with) {
056: if (source == null)
057: throw new NullPointerException(
058: "Parameter -> source was null");
059: if (key == null)
060: throw new NullPointerException("Parameter -> key was null");
061: if (with == null)
062: throw new NullPointerException("Parameter -> with was null");
063:
064: String temp = "";
065:
066: while (true) {
067: temp = "";
068: temp = replace(source, key, with);
069: if (temp == null)
070: break;
071: else
072: source = temp;
073: }
074:
075: return source;
076: }
077:
078: public static int replaceCount(String source, String key) {
079: if (source == null)
080: throw new NullPointerException(
081: "Parameter -> source was null");
082: if (key == null)
083: throw new NullPointerException("Parameter -> key was null");
084:
085: int count = 0;
086: String result = "";
087: String temp = "";
088:
089: result = source;
090: while (true) {
091: temp = "";
092: temp = replace(result, key, "");
093: if (temp == null) {
094: break;
095: } else {
096: result = temp;
097: count++;
098: }
099: }
100:
101: return count;
102: }
103:
104: public static String replaceAllNo(String source, String with) {
105: if (source == null)
106: throw new NullPointerException(
107: "One of parameter -> source was null");
108: if (with == null)
109: throw new NullPointerException(
110: "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: public static String removeAllHtmlSpChar(String source) {
119: String temp = source;
120: temp = replaceAll(temp, " ", " ");
121: temp = replaceAll(temp, "<", "<");
122: temp = replaceAll(temp, ">", ">");
123: temp = replaceAll(temp, "&", "&");
124: temp = replaceAll(temp, """, "\"");
125: return temp;
126: }
127:
128: ///////// tags ////////////
129: // needs more work
130: public static String replaceTags(String source, String with) {
131: if (source == null)
132: throw new NullPointerException(
133: "One of parameter -> source was null");
134: if (with == null)
135: throw new NullPointerException(
136: "One of parameter -> with was null");
137:
138: int start = 0;
139: int end = 0;
140: int error = 0;
141: String result = "";
142:
143: start = source.indexOf("<");
144: end = source.indexOf(">", start + 1);
145:
146: error = source.indexOf("<", start + 1);
147: if (error != -1 && error < end)
148: throw new IllegalArgumentException("< found before >");
149:
150: if (start == -1 || end == -1)
151: return null;
152:
153: result = source.substring(0, start);
154: result += with;
155: result += source.substring(end + 1, source.length());
156:
157: return result;
158: }
159:
160: public static String replaceAllTags(String source, String with) {
161: if (source == null)
162: throw new NullPointerException(
163: "One of parameter -> source was null");
164: if (with == null)
165: throw new NullPointerException(
166: "One of parameter -> with was null");
167:
168: String temp = "";
169:
170: while (true) {
171: temp = "";
172: temp = replaceTags(source, with);
173: if (temp == null)
174: break;
175: else
176: source = temp;
177: }
178:
179: return source;
180: }
181:
182: /**
183: * Returns String form of an exception.
184: * @since 1.3.3
185: */
186: public static String getStackTrace(Throwable e) {
187: StringWriter writer = new StringWriter(1024);
188: e.printStackTrace(new PrintWriter(writer));
189: return writer.toString();
190: }
191:
192: /**
193: * Returns formatted memory size.
194: * @since 1.4.5
195: */
196: public static String getMemInfo(float bytes) {
197: if (bytes < 1024) {
198: return doublePrcNum.format(bytes) + " B";
199: }
200:
201: bytes = bytes / 1024;
202: if (bytes < 1024) {
203: return doublePrcNum.format(bytes) + " KB";
204: }
205:
206: bytes = bytes / 1024;
207: if (bytes < 1024) {
208: return doublePrcNum.format(bytes) + " MB";
209: }
210:
211: bytes = bytes / 1024;
212: return doublePrcNum.format(bytes) + " GB";
213: }
214:
215: /**
216: * Returns System information.
217: * @since 1.4.5
218: */
219: public static String getSystemInfo(String version) {
220: StringBuffer sb = new StringBuffer();
221: sb.append("---- System Info Start ---");
222: sb.append("\r\n");
223:
224: sb.append("QuickServer v");
225: sb.append(version);
226: sb.append(" is being used.");
227: sb.append("\r\n");
228:
229: sb.append("Java VM v");
230: sb.append(System.getProperty("java.version"));
231: sb.append(" is being used.");
232: sb.append("\r\n");
233:
234: sb.append("Operating System: ");
235: sb.append(System.getProperty("os.name"));
236: sb.append(" ");
237: sb.append(System.getProperty("os.version"));
238: sb.append("\r\n");
239:
240: sb.append("Current working directory: ");
241: sb.append(System.getProperty("user.dir"));
242: sb.append("\r\n");
243:
244: sb.append("Class/s loaded from: ");
245: sb.append(new MyString().getClass().getProtectionDomain()
246: .getCodeSource().getLocation());
247: sb.append("\r\n");
248:
249: sb.append("Total memory currently available: ");
250: sb.append(MyString.getMemInfo(runtime.totalMemory()));
251: sb.append("\r\n");
252: sb.append("Memory currently in use: ");
253: sb.append(MyString.getMemInfo(runtime.totalMemory()
254: - runtime.freeMemory()));
255: sb.append("\r\n");
256: sb.append("Maximum memory available: ");
257: sb.append(MyString.getMemInfo(runtime.maxMemory()));
258: sb.append("\r\n");
259:
260: sb.append("---- System Info End ---");
261:
262: return sb.toString();
263: }
264:
265: public static String alignRight(String data, int len) {
266: StringBuffer sb = new StringBuffer(data);
267: while (sb.length() < len) {
268: sb.insert(0, ' ');
269: }
270: return sb.toString();
271: }
272:
273: public static String alignLeft(String data, int len) {
274: StringBuffer sb = new StringBuffer(data);
275: while (sb.length() < len) {
276: sb.append(' ');
277: }
278: return sb.toString();
279: }
280: }
|