1 package org.apache.turbine.util.parser; 2 3 18 19 import org.apache.commons.configuration.Configuration; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.turbine.services.TurbineServices; 27 28 34 35 public abstract class ParserUtils 36 { 37 38 public static final String URL_CASE_FOLDING_KEY = "url.case.folding"; 39 40 41 public static final String URL_CASE_FOLDING_NONE_VALUE = "none"; 42 43 44 public static final String URL_CASE_FOLDING_LOWER_VALUE = "lower"; 45 46 47 public static final String URL_CASE_FOLDING_UPPER_VALUE = "upper"; 48 49 50 private static final int URL_CASE_FOLDING_UNSET = 0; 51 52 53 public static final int URL_CASE_FOLDING_NONE = 1; 54 55 56 public static final int URL_CASE_FOLDING_LOWER = 2; 57 58 59 public static final int URL_CASE_FOLDING_UPPER = 3; 60 61 62 private static Log log = LogFactory.getLog(ParserUtils.class); 63 64 65 private static int folding = URL_CASE_FOLDING_UNSET; 66 67 75 public static String convertAndTrim(String value) 76 { 77 return convertAndTrim(value, getUrlFolding()); 78 } 79 80 89 public static String convertAndTrim(String value, int fold) 90 { 91 String tmp = value.trim(); 92 93 switch (fold) 94 { 95 case URL_CASE_FOLDING_NONE: 96 { 97 break; 98 } 99 case URL_CASE_FOLDING_LOWER: 100 { 101 tmp = tmp.toLowerCase(); 102 break; 103 } 104 case URL_CASE_FOLDING_UPPER: 105 { 106 tmp = tmp.toUpperCase(); 107 break; 108 } 109 default: 110 { 111 log.error("Passed " + fold + " as fold rule, which is illegal!"); 112 break; 113 } 114 } 115 return tmp; 116 } 117 118 123 public static int getUrlFolding() 124 { 125 if (folding == URL_CASE_FOLDING_UNSET) 126 { 127 Configuration conf = TurbineServices.getInstance().getConfiguration(); 128 String foldString = conf.getString(URL_CASE_FOLDING_KEY, 129 URL_CASE_FOLDING_NONE_VALUE).toLowerCase(); 130 131 folding = URL_CASE_FOLDING_NONE; 132 133 log.debug("Setting folding from " + foldString); 134 if (StringUtils.isNotEmpty(foldString)) 135 { 136 if (foldString.equals(URL_CASE_FOLDING_NONE_VALUE)) 137 { 138 folding = URL_CASE_FOLDING_NONE; 139 } 140 else if (foldString.equals(URL_CASE_FOLDING_LOWER_VALUE)) 141 { 142 folding = URL_CASE_FOLDING_LOWER; 143 } 144 else if (foldString.equals(URL_CASE_FOLDING_UPPER_VALUE)) 145 { 146 folding = URL_CASE_FOLDING_UPPER; 147 } 148 else 149 { 150 log.error("Got " + foldString + " from " + URL_CASE_FOLDING_KEY + " property, which is illegal!"); 151 } 152 } 153 } 154 return folding; 155 } 156 } 157 | Popular Tags |