1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.autotag.tool; |
22 | |
|
23 | |
import java.io.BufferedReader; |
24 | |
import java.io.IOException; |
25 | |
import java.io.Reader; |
26 | |
import java.io.StringReader; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.HashMap; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import org.apache.tiles.autotag.core.AutotagRuntimeException; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class StringTool { |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private Map<String, String> type2default; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private Map<String, String> primitive2wrapped; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 16 | public StringTool() { |
55 | 16 | type2default = new HashMap<String, String>(); |
56 | 16 | type2default.put("byte", "0"); |
57 | 16 | type2default.put("short", "0"); |
58 | 16 | type2default.put("int", "0"); |
59 | 16 | type2default.put("long", "0L"); |
60 | 16 | type2default.put("float", "0.0f"); |
61 | 16 | type2default.put("double", "0.0d"); |
62 | 16 | type2default.put("char", "'\\u0000'"); |
63 | 16 | type2default.put("boolean", "false"); |
64 | |
|
65 | 16 | primitive2wrapped = new HashMap<String, String>(); |
66 | 16 | primitive2wrapped.put("byte", Byte.class.getName()); |
67 | 16 | primitive2wrapped.put("short", Short.class.getName()); |
68 | 16 | primitive2wrapped.put("int", Integer.class.getName()); |
69 | 16 | primitive2wrapped.put("long", Long.class.getName()); |
70 | 16 | primitive2wrapped.put("float", Float.class.getName()); |
71 | 16 | primitive2wrapped.put("double", Double.class.getName()); |
72 | 16 | primitive2wrapped.put("char", Character.class.getName()); |
73 | 16 | primitive2wrapped.put("boolean", Boolean.class.getName()); |
74 | 16 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public List<String> splitOnNewlines(String toSplit) { |
83 | 2 | List<String> retValue = new ArrayList<String>(); |
84 | 2 | if (toSplit == null) { |
85 | 1 | return retValue; |
86 | |
} |
87 | 1 | Reader reader = new StringReader(toSplit); |
88 | 1 | BufferedReader bufReader = new BufferedReader(reader); |
89 | |
try { |
90 | |
String line; |
91 | 4 | while ((line = bufReader.readLine()) != null) { |
92 | 3 | retValue.add(line); |
93 | |
} |
94 | 0 | } catch (IOException e) { |
95 | 0 | throw new AutotagRuntimeException( |
96 | |
"Cannot read the string completely", e); |
97 | 1 | } |
98 | 1 | return retValue; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public String capitalizeFirstLetter(String string) { |
108 | 1 | return string.substring(0, 1).toUpperCase() + string.substring(1); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public String getDefaultValue(String type, String overriddenDefaultValue) { |
119 | 4 | if (overriddenDefaultValue != null) { |
120 | 2 | return overriddenDefaultValue; |
121 | |
} |
122 | |
|
123 | 2 | String retValue = type2default.get(type); |
124 | 2 | if (retValue == null) { |
125 | 1 | retValue = "null"; |
126 | |
} |
127 | 2 | return retValue; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public String getClassToCast(String type) { |
137 | 2 | String retValue = primitive2wrapped.get(type); |
138 | 2 | if (retValue == null) { |
139 | 1 | retValue = type; |
140 | |
} |
141 | 2 | return retValue; |
142 | |
} |
143 | |
} |