Source Code Cross Referenced for StringUtil.java in  » Source-Control » gruntspud » gruntspud » 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 » Source Control » gruntspud » gruntspud 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Gruntspud
003:         *
004:         *  Copyright (C) 2002 Brett Smith.
005:         *
006:         *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007:         *
008:         *  This program is free software; you can redistribute it and/or
009:         *  modify it under the terms of the GNU Library General Public License
010:         *  as published by the Free Software Foundation; either version 2 of
011:         *  the License, or (at your option) any later version.
012:         *  This program is distributed in the hope that it will be useful,
013:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:         *  GNU Library General Public License for more details.
016:         *
017:         *  You should have received a copy of the GNU Library General Public
018:         *  License along with this program; if not, write to the Free Software
019:         *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020:         */
021:
022:        package gruntspud;
023:
024:        import java.awt.Color;
025:        import java.awt.Dimension;
026:        import java.awt.Font;
027:        import java.awt.Point;
028:        import java.awt.Rectangle;
029:        import java.util.HashMap;
030:        import java.util.StringTokenizer;
031:        import java.util.Vector;
032:
033:        /**
034:         * Some utilities for manipulating strings and coverting various basic objects
035:         * to and from strings
036:         */
037:        public class StringUtil {
038:            //  Can't be instantiated
039:            private StringUtil() {
040:            }
041:
042:            /**
043:             * DOCUMENT ME!
044:             *
045:             * @param string DOCUMENT ME!
046:             * @param size DOCUMENT ME!
047:             *
048:             * @return DOCUMENT ME!
049:             */
050:            public static String padAndTrim(String string, int size) {
051:                StringBuffer buf = new StringBuffer(size);
052:                int s = string.length();
053:
054:                for (int i = 0; i < size; i++) {
055:                    if (i < s) {
056:                        buf.append(string.charAt(i));
057:                    } else {
058:                        buf.append(' ');
059:
060:                    }
061:                }
062:                return buf.toString();
063:            }
064:
065:            /**
066:             * DOCUMENT ME!
067:             *
068:             * @param base DOCUMENT ME!
069:             * @param file DOCUMENT ME!
070:             *
071:             * @return DOCUMENT ME!
072:             */
073:            public static String concatenateURL(String base, String file) {
074:                StringBuffer buf = new StringBuffer();
075:                buf.append(base);
076:
077:                if (!base.endsWith("/") && (file != null)
078:                        && (file.length() > 0)) {
079:                    buf.append("/");
080:
081:                }
082:                buf.append(file.startsWith("/") ? file.substring(1) : file);
083:
084:                return buf.toString();
085:            }
086:
087:            /**
088:             * DOCUMENT ME!
089:             *
090:             * @param string DOCUMENT ME!
091:             * @param args DOCUMENT ME!
092:             *
093:             * @return DOCUMENT ME!
094:             */
095:            public static String resolveString(String string, String[] args) {
096:                StringBuffer buf = new StringBuffer();
097:
098:                for (int i = 0; i < string.length(); i++) {
099:                    char ch = string.charAt(i);
100:
101:                    if ((ch == '%') && ((i + 1) < string.length())) {
102:                        i++;
103:
104:                        char ch2 = string.charAt(i);
105:
106:                        if (ch2 == '%') {
107:                            buf.append('%');
108:                        } else {
109:                            try {
110:                                int z = Integer.parseInt(String.valueOf(ch2));
111:                                buf
112:                                        .append((args[z] == null) ? ""
113:                                                : args[z - 1]);
114:                            } catch (Exception e) {
115:                            }
116:                        }
117:                    } else {
118:                        buf.append(ch);
119:                    }
120:                }
121:
122:                return buf.toString();
123:            }
124:
125:            /**
126:             * DOCUMENT ME!
127:             *
128:             * @param string DOCUMENT ME!
129:             * @param arguments DOCUMENT ME!
130:             *
131:             * @return DOCUMENT ME!
132:             */
133:            public static String resolveString(String string, HashMap arguments) {
134:                StringBuffer buf = new StringBuffer();
135:
136:                for (int i = 0; i < string.length(); i++) {
137:                    char ch = string.charAt(i);
138:
139:                    if ((ch == '%') && ((i + 1) < string.length())) {
140:                        i++;
141:
142:                        char ch2 = string.charAt(i);
143:
144:                        if (ch2 == '%') {
145:                            buf.append('%');
146:                        } else {
147:                            try {
148:                                String val = (String) arguments.get(String
149:                                        .valueOf(ch2));
150:                                buf.append((val == null) ? "" : val);
151:                            } catch (Exception e) {
152:                            }
153:                        }
154:                    } else {
155:                        buf.append(ch);
156:                    }
157:                }
158:
159:                return buf.toString();
160:            }
161:
162:            /**
163:             * DOCUMENT ME!
164:             *
165:             * @param str DOCUMENT ME!
166:             * @param delim DOCUMENT ME!
167:             *
168:             * @return DOCUMENT ME!
169:             */
170:            public static String[] splitString(String str, char delim) {
171:                return splitString(str, delim, (char) -1, (char) -1);
172:            }
173:
174:            /**
175:             * DOCUMENT ME!
176:             *
177:             * @param str DOCUMENT ME!
178:             * @param delim DOCUMENT ME!
179:             * @param quote DOCUMENT ME!
180:             * @param escape DOCUMENT ME!
181:             *
182:             * @return DOCUMENT ME!
183:             */
184:            public static String[] splitString(String str, char delim,
185:                    char quote, char escape) {
186:                Vector v = new Vector();
187:                StringBuffer str1 = new StringBuffer();
188:                char ch = ' ';
189:                boolean inQuote = false;
190:                boolean escaped = false;
191:
192:                for (int i = 0; i < str.length(); i++) {
193:                    ch = str.charAt(i);
194:
195:                    if ((escape != -1) && (ch == escape) && !escaped) {
196:                        escaped = true;
197:                    } else {
198:                        if ((quote != -1) && (ch == quote) && !escaped) {
199:                            inQuote = !inQuote;
200:                        } else if (!inQuote && (ch == delim)) {
201:                            v.add(str1.toString());
202:                            str1.setLength(0);
203:                        } else {
204:                            str1.append(ch);
205:                        }
206:                    }
207:                }
208:
209:                if (str.length() > 0) {
210:                    v.add(str1.toString());
211:
212:                }
213:                String[] array;
214:                array = new String[v.size()];
215:                v.copyInto(array);
216:
217:                return array;
218:            }
219:
220:            /**
221:             * DOCUMENT ME!
222:             *
223:             * @param str DOCUMENT ME!
224:             * @param delim DOCUMENT ME!
225:             *
226:             * @return DOCUMENT ME!
227:             */
228:            public static String stringArrayToString(String[] str, char delim) {
229:                StringBuffer buf = new StringBuffer();
230:
231:                for (int i = 0; i < str.length; i++) {
232:                    if (buf.length() > 0) {
233:                        buf.append(delim);
234:
235:                    }
236:                    buf.append(str[i]);
237:                }
238:
239:                return buf.toString();
240:            }
241:
242:            /** Get the basename of a file
243:             *
244:             * @param directory to delete
245:             */
246:            public static String basename(String file, String extension,
247:                    char separator) {
248:                int idx = file.lastIndexOf(separator);
249:
250:                if (idx > -1) {
251:                    file = file.substring(idx + 1);
252:
253:                }
254:                if ((extension != null) && file.endsWith(extension)) {
255:                    file = file
256:                            .substring(0, file.length() - extension.length());
257:
258:                }
259:                return file;
260:            }
261:
262:            /**
263:             * Return a string representation of a dimension
264:             *
265:             * @param dimension dimension object
266:             * @return string representation of a dimension
267:             */
268:            public static String dimensionToString(Dimension dimension) {
269:                return dimension.width + "," + dimension.height;
270:            }
271:
272:            /**
273:             * Create a <code>Dimension</code> object given a string
274:             *
275:             * @param s dimension string
276:             * @return dimension object
277:             */
278:            public static Dimension stringToDimension(String s) {
279:                try {
280:                    StringTokenizer t = new StringTokenizer(s, ",");
281:
282:                    return new Dimension(Integer.parseInt(t.nextToken()),
283:                            Integer.parseInt(t.nextToken()));
284:                } catch (Exception e) {
285:                    e.printStackTrace();
286:
287:                    return null;
288:                }
289:            }
290:
291:            /**
292:             * Return a string representation of a point
293:             *
294:             * @param point point to turn into string
295:             * @return string representation of pooint
296:             */
297:            public static String pointToString(Point point) {
298:                return point.x + "," + point.y;
299:            }
300:
301:            /**
302:             * Create a <code>Point</code> object given a string
303:             *
304:             * @param s point a string
305:             * @return point ojbect
306:             */
307:            public static Point stringToPoint(String s) {
308:                try {
309:                    StringTokenizer t = new StringTokenizer(s, ",");
310:
311:                    return new Point(Integer.parseInt(t.nextToken()), Integer
312:                            .parseInt(t.nextToken()));
313:                } catch (Exception e) {
314:                    e.printStackTrace();
315:
316:                    return null;
317:                }
318:            }
319:
320:            /**
321:             * Return a string representation of a <code>Rectangle</code>
322:             *
323:             * @param rectangle rectangle to turn into string
324:             * @return string representation of rectangle
325:             */
326:            public static String rectangleToString(Rectangle rectangle) {
327:                return rectangle.x + "," + rectangle.y + "," + rectangle.width
328:                        + "," + rectangle.height;
329:            }
330:
331:            /**
332:             * Create a <code>Rectangle</code> object given a string
333:             *
334:             * @param s rectangle a string
335:             * @return rectangle ojbect
336:             */
337:            public static Rectangle rectangleToPoint(String s) {
338:                try {
339:                    StringTokenizer t = new StringTokenizer(s, ",");
340:
341:                    return new Rectangle(Integer.parseInt(t.nextToken()),
342:                            Integer.parseInt(t.nextToken()), Integer.parseInt(t
343:                                    .nextToken()), Integer.parseInt(t
344:                                    .nextToken()));
345:                } catch (Exception e) {
346:                    e.printStackTrace();
347:
348:                    return null;
349:                }
350:            }
351:
352:            /**
353:             * Return a string representation of a color
354:             *
355:             * @param color
356:             * @return string representation
357:             */
358:            public static String colorToString(Color color) {
359:                if (color == null)
360:                    return "";
361:                StringBuffer buf = new StringBuffer();
362:                buf.append('#');
363:                buf.append(numberToPaddedHexString(color.getRed(), 2));
364:                buf.append(numberToPaddedHexString(color.getGreen(), 2));
365:                buf.append(numberToPaddedHexString(color.getBlue(), 2));
366:
367:                return buf.toString();
368:            }
369:
370:            /**
371:             * Convert a font to a string. The string will be in format of
372:             * name,type,points.
373:             *
374:             * @param font the font
375:             * @return the string
376:             */
377:            public static String fontToString(Font font) {
378:                StringBuffer b = new StringBuffer(font.getName());
379:                b.append(",");
380:                b.append(font.getStyle());
381:                b.append(",");
382:                b.append(font.getSize());
383:
384:                return b.toString();
385:            }
386:
387:            /**
388:             * Convert a string to a font. The string should be in format of
389:             * name,type,points. <code>null</code> will be returned if the string
390:             * is invalid
391:             *
392:             * @param fontString the font as a string
393:             * @return the font
394:             */
395:            public static Font stringToFont(String fontString) {
396:                StringTokenizer st = new StringTokenizer(fontString, ",");
397:
398:                try {
399:                    return new Font(st.nextToken(), Integer.parseInt(st
400:                            .nextToken()), Integer.parseInt(st.nextToken()));
401:                } catch (Exception e) {
402:                    return null;
403:                }
404:            }
405:
406:            /**
407:             * Return a <code>Color</code> object given a string representation of it
408:             *
409:             * @param color
410:             * @return string representation
411:             * @throws IllegalArgumentException if string in bad format
412:             */
413:            public static Color stringToColor(String s) {
414:                try {
415:                    return new Color(Integer.decode("0x" + s.substring(1, 3))
416:                            .intValue(), Integer.decode(
417:                            "0x" + s.substring(3, 5)).intValue(), Integer
418:                            .decode("0x" + s.substring(5, 7)).intValue());
419:                } catch (Exception e) {
420:                    return null;
421:                }
422:            }
423:
424:            /**
425:             * Convert a number to a zero padded hex string
426:             *
427:             * @param int number
428:             * @return zero padded hex string
429:             * @throws IllegalArgumentException if number takes up more characters than
430:             *                                  <code>size</code>
431:             */
432:            public static String numberToPaddedHexString(int number, int size) {
433:                String s = Integer.toHexString(number);
434:
435:                if (s.length() > size) {
436:                    throw new IllegalArgumentException(
437:                            "Number too big for padded hex string");
438:                }
439:
440:                StringBuffer buf = new StringBuffer();
441:
442:                for (int i = 0; i < (size - s.length()); i++) {
443:                    buf.append('0');
444:
445:                }
446:                buf.append(s);
447:
448:                return buf.toString();
449:            }
450:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.