This project has retired. For details please refer to its Attic page.
StringTool xref
View Javadoc

1   /*
2    * $Id: StringTool.java 1044707 2010-12-11 20:35:57Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A Velocity tools to manipulate strings.
36   *
37   * @version $Rev: 1044707 $ $Date: 2010-12-11 21:35:57 +0100 (Sat, 11 Dec 2010) $
38   */
39  public class StringTool {
40  
41      /**
42       * Maps a primitive type to its default value as a string.
43       */
44      private Map<String, String> type2default;
45  
46      /**
47       * Maps a primitive type to its boxed version.
48       */
49      private Map<String, String> primitive2wrapped;
50  
51      /**
52       * Constructor.
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       * 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       */
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      * 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      */
107     public String capitalizeFirstLetter(String string) {
108         return string.substring(0, 1).toUpperCase() + string.substring(1);
109     }
110 
111     /**
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      */
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      * 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      */
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 }