Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CellComparatorHelper |
|
| 7.25;7.25 |
1 | /* | |
2 | * Copyright 2006-2007 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 java.util.Comparator; | |
19 | import java.util.regex.Matcher; | |
20 | import java.util.regex.Pattern; | |
21 | ||
22 | import org.apache.commons.collections.comparators.ComparableComparator; | |
23 | import org.apache.commons.lang.StringUtils; | |
24 | import org.displaytag.model.Cell; | |
25 | import org.kuali.rice.core.util.type.TypeUtils; | |
26 | ||
27 | /** | |
28 | * This class... | |
29 | */ | |
30 | 0 | public class CellComparatorHelper { |
31 | ||
32 | 0 | static private Pattern HREF_ENCLOSURE = Pattern.compile("<a [^>]+>([^<]*)</a>.*", Pattern.MULTILINE); |
33 | ||
34 | /** | |
35 | * This method is intended to be used in conjunction with displaytag. | |
36 | * | |
37 | * @see #getSanitizedStaticValue(String) | |
38 | * | |
39 | * @param cell | |
40 | * @return a sanitized version of cell.getStaticValue().toString(). | |
41 | */ | |
42 | static public String getSanitizedStaticValue(Cell cell) { | |
43 | 0 | if (null == cell) { |
44 | 0 | return null; |
45 | } | |
46 | ||
47 | 0 | return null == cell.getStaticValue() ? null : getSanitizedStaticValue(cell.getStaticValue().toString()); |
48 | } | |
49 | ||
50 | /** | |
51 | * 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 | |
52 | * tag. If the value ends in one or many " "s, strip them off. Return the modified String. | |
53 | * | |
54 | * @param staticValue | |
55 | * @return a sanitized version of staticValue | |
56 | */ | |
57 | static public String getSanitizedStaticValue(String staticValue) { | |
58 | ||
59 | 0 | if (null == staticValue) { |
60 | 0 | return null; |
61 | } | |
62 | ||
63 | 0 | staticValue = StringUtils.replace(staticValue, "\r", ""); |
64 | 0 | staticValue = StringUtils.replace(staticValue, "\n", ""); |
65 | 0 | staticValue = StringUtils.replace(staticValue, "\t", ""); |
66 | ||
67 | 0 | String sanitizedValue = staticValue; |
68 | ||
69 | // Extract the value if it's wrapped in an href. | |
70 | 0 | Matcher matcher = HREF_ENCLOSURE.matcher(staticValue); |
71 | 0 | if (matcher.matches()) { |
72 | ||
73 | 0 | sanitizedValue = matcher.group(1).trim(); |
74 | ||
75 | } | |
76 | ||
77 | // Strip off any " "s if they come at the end of the value. | |
78 | 0 | while (sanitizedValue.endsWith(" ")) { |
79 | ||
80 | 0 | sanitizedValue = sanitizedValue.substring(0, sanitizedValue.length() - 6).trim(); |
81 | ||
82 | } | |
83 | ||
84 | 0 | return sanitizedValue; |
85 | ||
86 | } | |
87 | ||
88 | /** | |
89 | * This method returns a comparator to be used for comparing the contents of cells, that is | |
90 | * the compareTo method will be invoked w/ displaytag Cell objects | |
91 | * @param propClass | |
92 | * @return | |
93 | */ | |
94 | public static Comparator getAppropriateComparatorForPropertyClass(Class propClass) { | |
95 | // TODO, do we really need to create so many comparators (1 per each cell)? | |
96 | 0 | if (propClass == null) { |
97 | 0 | return new NullCellComparator(); |
98 | } | |
99 | 0 | else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) { |
100 | 0 | return new NumericCellComparator(); |
101 | } | |
102 | 0 | else if (TypeUtils.isTemporalClass(propClass)) { |
103 | 0 | return new TemporalCellComparator(); |
104 | } | |
105 | 0 | else if (String.class.equals(propClass)) { |
106 | // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER | |
107 | 0 | return new StringCellComparator(); |
108 | } | |
109 | else { | |
110 | 0 | return ComparableComparator.getInstance(); |
111 | } | |
112 | } | |
113 | ||
114 | /** | |
115 | * This method returns a comparator to be used for comparing propertyValues (in String form) | |
116 | * @param propClass | |
117 | * @return | |
118 | */ | |
119 | public static Comparator getAppropriateValueComparatorForPropertyClass(Class propClass) { | |
120 | 0 | if (propClass == null) { |
121 | 0 | return NullValueComparator.getInstance(); |
122 | } | |
123 | 0 | else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) { |
124 | 0 | return NumericValueComparator.getInstance(); |
125 | } | |
126 | 0 | else if (TypeUtils.isTemporalClass(propClass)) { |
127 | 0 | return TemporalValueComparator.getInstance(); |
128 | } | |
129 | 0 | else if (String.class.equals(propClass)) { |
130 | // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER | |
131 | 0 | return StringValueComparator.getInstance(); |
132 | } | |
133 | else { | |
134 | 0 | return ComparableComparator.getInstance(); |
135 | } | |
136 | } | |
137 | } |