1 package org.apache.turbine.util.parser;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 /***
29 * Static helpers for folding fields to upper or lower case
30 *
31 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32 * @version $Id: ParserUtils.java 279778 2005-09-09 14:04:11Z henning $
33 */
34
35 public abstract class ParserUtils
36 {
37 /*** Property for setting the URL folding value */
38 public static final String URL_CASE_FOLDING_KEY = "url.case.folding";
39
40 /*** No folding */
41 public static final String URL_CASE_FOLDING_NONE_VALUE = "none";
42
43 /*** Fold to lower case */
44 public static final String URL_CASE_FOLDING_LOWER_VALUE = "lower";
45
46 /*** Fold to upper case */
47 public static final String URL_CASE_FOLDING_UPPER_VALUE = "upper";
48
49 /*** No folding set */
50 private static final int URL_CASE_FOLDING_UNSET = 0;
51
52 /*** Folding set to "no folding" */
53 public static final int URL_CASE_FOLDING_NONE = 1;
54
55 /*** Folding set to "lowercase" */
56 public static final int URL_CASE_FOLDING_LOWER = 2;
57
58 /*** Folding set to "uppercase" */
59 public static final int URL_CASE_FOLDING_UPPER = 3;
60
61 /*** Logging */
62 private static Log log = LogFactory.getLog(ParserUtils.class);
63
64 /*** The folding from the properties */
65 private static int folding = URL_CASE_FOLDING_UNSET;
66
67 /***
68 * Convert a String value according to the url.case.folding property.
69 *
70 * @param value the String to convert
71 *
72 * @return a new String.
73 *
74 */
75 public static String convertAndTrim(String value)
76 {
77 return convertAndTrim(value, getUrlFolding());
78 }
79
80 /***
81 * A static version of the convert method, which
82 * trims the string data and applies the conversion specified in
83 * the property given by URL_CASE_FOLDING. It returns a new
84 * string so that it does not destroy the value data.
85 *
86 * @param value A String to be processed.
87 * @return A new String converted to lowercase and trimmed.
88 */
89 public static String convertAndTrim(String value, int fold)
90 {
91 String tmp = null;
92
93 if (value != null)
94 {
95 tmp = value.trim();
96
97 switch (fold)
98 {
99 case URL_CASE_FOLDING_NONE:
100 {
101 break;
102 }
103 case URL_CASE_FOLDING_LOWER:
104 {
105 tmp = tmp.toLowerCase();
106 break;
107 }
108 case URL_CASE_FOLDING_UPPER:
109 {
110 tmp = tmp.toUpperCase();
111 break;
112 }
113 default:
114 {
115 log.error("Passed " + fold + " as fold rule, which is illegal!");
116 break;
117 }
118 }
119 }
120 return tmp;
121 }
122
123 /***
124 * Gets the folding value from the properties
125 *
126 * @return The current Folding Value
127 */
128 public static int getUrlFolding()
129 {
130 if (folding == URL_CASE_FOLDING_UNSET)
131 {
132 Configuration conf = TurbineServices.getInstance().getConfiguration();
133 String foldString = conf.getString(URL_CASE_FOLDING_KEY,
134 URL_CASE_FOLDING_NONE_VALUE).toLowerCase();
135
136 folding = URL_CASE_FOLDING_NONE;
137
138 log.debug("Setting folding from " + foldString);
139 if (StringUtils.isNotEmpty(foldString))
140 {
141 if (foldString.equals(URL_CASE_FOLDING_NONE_VALUE))
142 {
143 folding = URL_CASE_FOLDING_NONE;
144 }
145 else if (foldString.equals(URL_CASE_FOLDING_LOWER_VALUE))
146 {
147 folding = URL_CASE_FOLDING_LOWER;
148 }
149 else if (foldString.equals(URL_CASE_FOLDING_UPPER_VALUE))
150 {
151 folding = URL_CASE_FOLDING_UPPER;
152 }
153 else
154 {
155 log.error("Got " + foldString + " from " + URL_CASE_FOLDING_KEY + " property, which is illegal!");
156 }
157 }
158 }
159 return folding;
160 }
161 }