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