1 | package com.mysql.management.util; |
2 | |
3 | import java.io.BufferedReader; |
4 | import java.io.IOException; |
5 | import java.io.StringReader; |
6 | import java.util.ArrayList; |
7 | import java.util.List; |
8 | |
9 | import com.mysql.management.util.Exceptions.Block; |
10 | |
11 | /* |
12 | Copyright (C) 2004 MySQL AB |
13 | |
14 | This program is free software; you can redistribute it and/or modify |
15 | it under the terms of the GNU General Public License version 2 as |
16 | published by the Free Software Foundation. |
17 | |
18 | This program is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | GNU General Public License for more details. |
22 | |
23 | You should have received a copy of the GNU General Public License |
24 | along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
26 | |
27 | */ |
28 | |
29 | /** |
30 | * String utility methods. |
31 | * |
32 | * This class is final simply as a hint to the compiler, it may be un-finalized |
33 | * safely. |
34 | * |
35 | * @author Eric Herman <eric@mysql.com> |
36 | * @version $Id: Str.java,v 1.11 2005/08/31 01:21:16 eherman Exp $ |
37 | */ |
38 | public final class Str { |
39 | /* merge with other string utility */ |
40 | |
41 | private String newLine; |
42 | |
43 | public Str() { |
44 | newLine = System.getProperty("line.separator"); |
45 | } |
46 | |
47 | public boolean containsIgnoreCase(String searchIn, String searchFor) { |
48 | |
49 | return searchIn.toLowerCase().indexOf(searchFor.toLowerCase()) != -1; |
50 | } |
51 | |
52 | public String newLine() { |
53 | return newLine; |
54 | } |
55 | |
56 | public String[] toStringArray(List strings) { |
57 | return (String[]) strings.toArray(new String[strings.size()]); |
58 | } |
59 | |
60 | /** |
61 | * convienence method: |
62 | * |
63 | * @return shortClassName(obj.getClass()); |
64 | */ |
65 | public String shortClassName(Object obj) { |
66 | return shortClassName(obj.getClass()); |
67 | } |
68 | |
69 | /** |
70 | * returns the unquallified "short" name of a class (no package info) |
71 | * returns "String" for java.lang.String.class returns "Bar" for |
72 | * foo.Bar.class returns "Foo" for Foo.class (in the default package) |
73 | */ |
74 | public String shortClassName(Class aClass) { |
75 | String name = aClass.getName(); |
76 | int lastDot = name.lastIndexOf('.'); |
77 | return name.substring(lastDot + 1); |
78 | } |
79 | |
80 | /** |
81 | * wrapper method for Class.forName(string) which converts |
82 | * ClassNotFoundException to RuntimeException |
83 | */ |
84 | public Class classForName(final String className) { |
85 | return (Class) new Exceptions.Block() { |
86 | protected Object inner() throws ClassNotFoundException { |
87 | return Class.forName(className); |
88 | } |
89 | }.exec(); |
90 | } |
91 | |
92 | /** |
93 | * returns an array of strings as read via a StringReader |
94 | */ |
95 | public String[] splitLines(String str) { |
96 | List lines = new ArrayList(); |
97 | StringReader stringReader = new StringReader(str); |
98 | final BufferedReader reader = new BufferedReader(stringReader); |
99 | Exceptions.StringBlock block = new Exceptions.StringBlock() { |
100 | protected String inner() throws IOException { |
101 | return reader.readLine(); |
102 | } |
103 | }; |
104 | while (true) { |
105 | String line = block.exec(); |
106 | if (line == null) { |
107 | break; |
108 | } |
109 | lines.add(line); |
110 | } |
111 | return (String[]) lines.toArray(new String[lines.size()]); |
112 | } |
113 | |
114 | public boolean parseDefaultTrue(Object obj) { |
115 | return obj == null |
116 | || !obj.toString().equalsIgnoreCase(Boolean.FALSE.toString()); |
117 | } |
118 | } |