1 10 11 package org.mmbase.bridge.jsp.taglib.util; 12 13 import javax.servlet.jsp.JspTagException ; 14 import org.mmbase.bridge.jsp.taglib.TaglibException; 15 import org.mmbase.bridge.jsp.taglib.ContextReferrerTag; 16 import javax.servlet.jsp.PageContext ; 17 import java.util.*; 18 import java.text.Collator ; 19 20 27 public class ListSorter { 28 29 30 public static List sort(List list, String comparator, ContextReferrerTag tag) throws JspTagException { 31 if (comparator != null) { 32 if (comparator.equals("SHUFFLE")) { 33 Collections.shuffle(list); 34 } else if (comparator.equals("REVERSE")) { 35 Collections.reverse(list); 36 } else if (comparator.equals("NATURAL")) { 37 Collections.sort(list); 38 } else if (comparator.equals("CASE_INSENSITIVE")) { 39 Collator col = Collator.getInstance(tag.getLocale()); 40 col.setStrength(Collator.PRIMARY); 41 Collections.sort(list, col); 42 } else { 43 try { 44 PageContext pageContext = tag.getPageContext(); 45 Class claz = null; 46 boolean pageClass = false; 47 if (comparator.indexOf(".") == -1) { 48 Class [] classes = pageContext.getPage().getClass().getDeclaredClasses(); 49 for (int i = 0; i < classes.length; i++) { 50 if (classes[i].toString().endsWith(comparator)) { 51 claz = classes[i]; 52 pageClass = true; 53 break; 54 } 55 } 56 } 57 if (claz == null) { 58 claz = Class.forName(comparator); 59 } 60 61 if (pageClass && ! java.lang.reflect.Modifier.isStatic(claz.getModifiers())) { 62 throw new TaglibException("Don't know how to instantiate non-static inner class: " + comparator + " (make it static please)"); 63 } 64 Comparator comp = (Comparator) claz.newInstance(); 65 init(comp, pageContext); 66 Collections.sort(list, comp); 67 } catch (Exception e) { 68 throw new TaglibException(e); 69 } 70 } 71 } 72 return list; 73 74 } 75 76 private static final Class [] PAGE_CONTEXT_ARRAY = { PageContext .class }; 77 78 private static void init(Comparator comp, PageContext pageContext) { 79 try { 80 java.lang.reflect.Method initMethod = comp.getClass().getMethod("init" , PAGE_CONTEXT_ARRAY); 81 initMethod.invoke(comp, new Object [] { pageContext }); 82 } catch(Exception e){ 83 } 85 } 86 87 88 } 89 | Popular Tags |