View Javadoc

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