Coverage Report - org.apache.tiles.autotag.tool.StringTool
 
Classes in this File Line Coverage Branch Coverage Complexity
StringTool
95%
40/42
100%
10/10
2.8
 
 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  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  
      * 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  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  
      * 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  1
         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  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  
      * 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  2
         String retValue = primitive2wrapped.get(type);
 138  2
         if (retValue == null) {
 139  1
             retValue = type;
 140  
         }
 141  2
         return retValue;
 142  
     }
 143  
 }