View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.comparator;
17  
18  import org.apache.commons.collections.comparators.ComparableComparator;
19  import org.apache.commons.lang.StringUtils;
20  import org.displaytag.model.Cell;
21  import org.kuali.rice.core.api.util.type.TypeUtils;
22  import org.kuali.rice.krad.comparator.NumericValueComparator;
23  import org.kuali.rice.krad.comparator.StringValueComparator;
24  import org.kuali.rice.krad.comparator.TemporalValueComparator;
25  
26  import java.util.Comparator;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  /**
31   * This class...
32   *
33   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
34   */
35  @Deprecated
36  public class CellComparatorHelper {
37  
38      static private Pattern HREF_ENCLOSURE = Pattern.compile("<a [^>]+>([^<]*)</a>.*", Pattern.MULTILINE);
39  
40      /**
41       * This method is intended to be used in conjunction with displaytag.
42       * 
43       * @see #getSanitizedStaticValue(String)
44       * 
45       * @param cell
46       * @return a sanitized version of cell.getStaticValue().toString().
47       */
48      static public String getSanitizedStaticValue(Cell cell) {
49          if (null == cell) {
50              return null;
51          }
52  
53          return null == cell.getStaticValue() ? null : getSanitizedStaticValue(cell.getStaticValue().toString());
54      }
55  
56      /**
57       * Remove all end-of-line and tab characters (\r, \n, \t). If the value is enclosed in an html anchor tag, strip the html anchor
58       * tag. If the value ends in one or many "&nbsp;"s, strip them off. Return the modified String.
59       * 
60       * @param staticValue
61       * @return a sanitized version of staticValue
62       */
63      static public String getSanitizedStaticValue(String staticValue) {
64  
65          if (null == staticValue) {
66              return null;
67          }
68  
69          staticValue = StringUtils.replace(staticValue, "\r", "");
70          staticValue = StringUtils.replace(staticValue, "\n", "");
71          staticValue = StringUtils.replace(staticValue, "\t", "");
72  
73          String sanitizedValue = staticValue;
74  
75          // Extract the value if it's wrapped in an href.
76          Matcher matcher = HREF_ENCLOSURE.matcher(staticValue);
77          if (matcher.matches()) {
78  
79              sanitizedValue = matcher.group(1).trim();
80  
81          }
82  
83          // Strip off any "&nbsp;"s if they come at the end of the value.
84          while (sanitizedValue.endsWith("&nbsp;")) {
85  
86              sanitizedValue = sanitizedValue.substring(0, sanitizedValue.length() - 6).trim();
87  
88          }
89  
90          return sanitizedValue;
91  
92      }
93  
94      /**
95       * This method returns a comparator to be used for comparing the contents of cells, that is
96       * the compareTo method will be invoked w/ displaytag Cell objects
97       * @param propClass
98       * @return
99       */
100     public static Comparator getAppropriateComparatorForPropertyClass(Class propClass) {
101         // TODO, do we really need to create so many comparators (1 per each cell)?
102         if (propClass == null) {
103             return new NullCellComparator();
104         }
105         else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
106             return new NumericCellComparator();
107         }
108         else if (TypeUtils.isTemporalClass(propClass)) {
109             return new TemporalCellComparator();
110         }
111         else if (String.class.equals(propClass)) {
112             // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
113             return new StringCellComparator();
114         }
115         else {
116             return ComparableComparator.getInstance();
117         }
118     }
119     
120     /**
121      * This method returns a comparator to be used for comparing propertyValues (in String form)
122      * @param propClass
123      * @return
124      */
125     public static Comparator getAppropriateValueComparatorForPropertyClass(Class propClass) {
126         if (propClass == null) {
127             return NullValueComparator.getInstance();
128         }
129         else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
130             return NumericValueComparator.getInstance();
131         }
132         else if (TypeUtils.isTemporalClass(propClass)) {
133             return TemporalValueComparator.getInstance();
134         }
135         else if (String.class.equals(propClass)) {
136             // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
137             return StringValueComparator.getInstance();
138         }
139         else {
140             return ComparableComparator.getInstance();
141         }
142     }
143 }