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 public StringTool() {
55 type2default = new HashMap<String, String>();
56 type2default.put("byte", "0");
57 type2default.put("short", "0");
58 type2default.put("int", "0");
59 type2default.put("long", "0L");
60 type2default.put("float", "0.0f");
61 type2default.put("double", "0.0d");
62 type2default.put("char", "'\\u0000'");
63 type2default.put("boolean", "false");
64
65 primitive2wrapped = new HashMap<String, String>();
66 primitive2wrapped.put("byte", Byte.class.getName());
67 primitive2wrapped.put("short", Short.class.getName());
68 primitive2wrapped.put("int", Integer.class.getName());
69 primitive2wrapped.put("long", Long.class.getName());
70 primitive2wrapped.put("float", Float.class.getName());
71 primitive2wrapped.put("double", Double.class.getName());
72 primitive2wrapped.put("char", Character.class.getName());
73 primitive2wrapped.put("boolean", Boolean.class.getName());
74 }
75
76
77
78
79
80
81
82 public List<String> splitOnNewlines(String toSplit) {
83 List<String> retValue = new ArrayList<String>();
84 if (toSplit == null) {
85 return retValue;
86 }
87 Reader reader = new StringReader(toSplit);
88 BufferedReader bufReader = new BufferedReader(reader);
89 try {
90 String line;
91 while ((line = bufReader.readLine()) != null) {
92 retValue.add(line);
93 }
94 } catch (IOException e) {
95 throw new AutotagRuntimeException(
96 "Cannot read the string completely", e);
97 }
98 return retValue;
99 }
100
101
102
103
104
105
106
107 public String capitalizeFirstLetter(String string) {
108 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 if (overriddenDefaultValue != null) {
120 return overriddenDefaultValue;
121 }
122
123 String retValue = type2default.get(type);
124 if (retValue == null) {
125 retValue = "null";
126 }
127 return retValue;
128 }
129
130
131
132
133
134
135
136 public String getClassToCast(String type) {
137 String retValue = primitive2wrapped.get(type);
138 if (retValue == null) {
139 retValue = type;
140 }
141 return retValue;
142 }
143 }