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,v 1.2.2.2 2004/05/20 03:33:43 seade Exp $
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 = 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 /***
119 * Gets the folding value from the properties
120 *
121 * @return The current Folding Value
122 */
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 }