1 23 24 package org.apache.slide.search.basic; 25 26 import java.util.ArrayList ; 27 import java.util.Comparator ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import org.apache.slide.common.PropertyName; 31 import org.apache.slide.search.CompareHint; 32 import org.apache.slide.search.InvalidQueryException; 33 import org.jdom.Attribute; 34 import org.jdom.Element; 35 import org.jdom.Namespace; 36 37 38 44 public class OrderBy { 45 46 47 private Comparator theComparator = new _Comparator (); 48 49 50 protected List orderByElements = new ArrayList (); 51 52 58 public void init (Element orderByElement) throws InvalidQueryException { 59 Namespace nameSpace = orderByElement.getNamespace (); 60 Iterator it = 61 orderByElement.getChildren (Literals.ORDER, nameSpace).iterator(); 62 63 while (it.hasNext()) { 64 Element order = (Element) it.next(); 65 PropertyName p = getProperty (order); 66 boolean isAscending = isAscending (order); 67 boolean isCaseSensitive = isCaseSensitive (order); 68 69 orderByElements.add 70 (createCompareHint (p, isAscending, isCaseSensitive)); 71 } 72 } 73 74 protected CompareHint createCompareHint (PropertyName prop, 75 boolean isAscending, 76 boolean isCaseSensitive) { 77 return new CompareHint (prop, isAscending, isCaseSensitive) ; 78 } 79 80 86 public Comparator getComparator () { 87 return theComparator; 88 } 89 90 91 99 private boolean isCaseSensitive (Element order) { 100 boolean result = true; 101 Attribute caseSens = 102 order.getAttribute (Literals.CASESENSITIVE, order.getNamespace()); 103 104 if (caseSens != null) { 105 try { 106 result = caseSens.getBooleanValue(); 107 } 108 catch (org.jdom.DataConversionException e) { 109 e.printStackTrace(); 110 } 111 } 112 return result; 113 } 114 115 116 117 127 private boolean isAscending (Element order) throws InvalidQueryException { 128 Element asc = order.getChild (Literals.ASCENDING, order.getNamespace()); 129 Element desc = order.getChild (Literals.DESCENDING, order.getNamespace()); 130 boolean result = true; 131 132 if (asc != null && desc != null) 133 throw new InvalidQueryException ("either ascending or descending may be supplied"); 134 135 if (desc != null) 136 result = false; 137 138 return result; 139 } 140 141 151 private PropertyName getProperty (Element orderElem) throws InvalidQueryException { 152 153 List propList = 154 orderElem.getChild (Literals.PROP, orderElem.getNamespace()).getChildren(); 155 156 if (propList.size() != 1) 157 throw new InvalidQueryException 158 ("Expected exactly 1 prop element, found " + propList.size()); 159 160 Element propElem = (Element)propList.get(0); 161 162 return createProperty (propElem); 163 } 164 165 173 protected PropertyName createProperty (Element propElem) throws InvalidQueryException { 174 String name = propElem.getName(); 175 String nameSpace = propElem.getNamespace().getURI(); 176 return new PropertyName (name, nameSpace); 177 } 178 179 180 183 private class _Comparator implements Comparator { 184 185 201 public int compare(Object o1, Object o2) { 202 ComparableResource r1 = (ComparableResource)o1; 203 ComparableResource r2 = (ComparableResource)o2; 204 205 int result = 0; 206 if (r1.getInternalHref().equals (r2.getInternalHref())) { 207 result = 0; 208 } 209 else { 210 211 Iterator it = orderByElements.iterator(); 212 while (it.hasNext() && result == 0) { 213 CompareHint obe = (CompareHint)it.next(); 214 result = r1.compareTo (r2, obe); 215 } 216 217 if (result == 0) 218 result = 1; 219 } 220 221 return result; 222 } 223 } 224 225 } 226 227 | Popular Tags |