Source Code Cross Referenced for MyString.java in  » Net » QuickServer » org » quickserver » 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 » Net » QuickServer » org.quickserver.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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, "&nbsp;", " ");
121:                temp = replaceAll(temp, "&lt;", "<");
122:                temp = replaceAll(temp, "&gt;", ">");
123:                temp = replaceAll(temp, "&amp;", "&");
124:                temp = replaceAll(temp, "&quot;", "\"");
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("&lt; found before &gt;");
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.