1/*2 * $Id: StringTool.java 1044707 2010-12-11 20:35:57Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.autotag.tool;
2223import java.io.BufferedReader;
24import java.io.IOException;
25import java.io.Reader;
26import java.io.StringReader;
27import java.util.ArrayList;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
3132import org.apache.tiles.autotag.core.AutotagRuntimeException;
3334/**35 * A Velocity tools to manipulate strings.36 *37 * @version $Rev: 1044707 $ $Date: 2010-12-11 21:35:57 +0100 (Sat, 11 Dec 2010) $38 */39publicclassStringTool {
4041/**42 * Maps a primitive type to its default value as a string.43 */44private Map<String, String> type2default;
4546/**47 * Maps a primitive type to its boxed version.48 */49private Map<String, String> primitive2wrapped;
5051/**52 * Constructor.53 */54publicStringTool() {
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");
6465 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 }
7576/**77 * Creates a list of strings, separating a string when a newline is encountered.78 *79 * @param toSplit The string to split.80 * @return The list of splitted strings.81 */82public List<String> splitOnNewlines(String toSplit) {
83 List<String> retValue = new ArrayList<String>();
84if (toSplit == null) {
85return retValue;
86 }
87 Reader reader = new StringReader(toSplit);
88 BufferedReader bufReader = new BufferedReader(reader);
89try {
90 String line;
91while ((line = bufReader.readLine()) != null) {
92 retValue.add(line);
93 }
94 } catch (IOException e) {
95thrownewAutotagRuntimeException(
96"Cannot read the string completely", e);
97 }
98return retValue;
99 }
100101/**102 * Creates a string in which the first character is capitalized.103 *104 * @param string The string to use.105 * @return The same string with the first character capitalized.106 */107public String capitalizeFirstLetter(String string) {
108return string.substring(0, 1).toUpperCase() + string.substring(1);
109 }
110111/**112 * Returns the default value for a type.113 *114 * @param type The type.115 * @param overriddenDefaultValue The default value, as specified by developers.116 * @return The default value to use.117 */118public String getDefaultValue(String type, String overriddenDefaultValue) {
119if (overriddenDefaultValue != null) {
120return overriddenDefaultValue;
121 }
122123 String retValue = type2default.get(type);
124if (retValue == null) {
125 retValue = "null";
126 }
127return retValue;
128 }
129130/**131 * Returns the class to be used to cast an Object.132 *133 * @param type The type to use, even a primitive type.134 * @return The class to be used in casts.135 */136public String getClassToCast(String type) {
137 String retValue = primitive2wrapped.get(type);
138if (retValue == null) {
139 retValue = type;
140 }
141return retValue;
142 }
143 }