1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33 public class CellComparatorHelper {
34
35 static private Pattern HREF_ENCLOSURE = Pattern.compile("<a [^>]+>([^<]*)</a>.*", Pattern.MULTILINE);
36
37
38
39
40
41
42
43
44
45 static public String getSanitizedStaticValue(Cell cell) {
46 if (null == cell) {
47 return null;
48 }
49
50 return null == cell.getStaticValue() ? null : getSanitizedStaticValue(cell.getStaticValue().toString());
51 }
52
53
54
55
56
57
58
59
60 static public String getSanitizedStaticValue(String staticValue) {
61
62 if (null == staticValue) {
63 return null;
64 }
65
66 staticValue = StringUtils.replace(staticValue, "\r", "");
67 staticValue = StringUtils.replace(staticValue, "\n", "");
68 staticValue = StringUtils.replace(staticValue, "\t", "");
69
70 String sanitizedValue = staticValue;
71
72
73 Matcher matcher = HREF_ENCLOSURE.matcher(staticValue);
74 if (matcher.matches()) {
75
76 sanitizedValue = matcher.group(1).trim();
77
78 }
79
80
81 while (sanitizedValue.endsWith(" ")) {
82
83 sanitizedValue = sanitizedValue.substring(0, sanitizedValue.length() - 6).trim();
84
85 }
86
87 return sanitizedValue;
88
89 }
90
91
92
93
94
95
96
97 public static Comparator getAppropriateComparatorForPropertyClass(Class propClass) {
98
99 if (propClass == null) {
100 return new NullCellComparator();
101 }
102 else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
103 return new NumericCellComparator();
104 }
105 else if (TypeUtils.isTemporalClass(propClass)) {
106 return new TemporalCellComparator();
107 }
108 else if (String.class.equals(propClass)) {
109
110 return new StringCellComparator();
111 }
112 else {
113 return ComparableComparator.getInstance();
114 }
115 }
116
117
118
119
120
121
122 public static Comparator getAppropriateValueComparatorForPropertyClass(Class propClass) {
123 if (propClass == null) {
124 return NullValueComparator.getInstance();
125 }
126 else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
127 return NumericValueComparator.getInstance();
128 }
129 else if (TypeUtils.isTemporalClass(propClass)) {
130 return TemporalValueComparator.getInstance();
131 }
132 else if (String.class.equals(propClass)) {
133
134 return StringValueComparator.getInstance();
135 }
136 else {
137 return ComparableComparator.getInstance();
138 }
139 }
140 }