00001
00010 package dochelper;
00011
00012 import java.util.HashMap;
00013
00014 import dochelper.exceptions.IllegalParamterTypeException;
00015 import dochelper.exceptions.InitializationException;
00016 import dochelper.exceptions.InvalidValueException;
00017 import dochelper.exceptions.MissingArgumentException;
00018 import dochelper.exceptions.ResourceUnavailableException;
00019 import java.util.Collections;
00020 import java.util.Comparator;
00021
00026 public class MatchOrderProcessor implements Processor {
00027
00031 protected static final String DIRECTION = "direction";
00032
00033
00037 protected HashMap<String, Object> params = new HashMap<String, Object>();
00038
00049 public ResultNode operate(ResultNode doc) {
00050
00051
00052
00053 orderTree(doc);
00054 return doc;
00055 }
00056
00057
00058 private void orderTree(ResultNode doc) {
00059
00060 if (doc.isLeaf()) return;
00061
00062
00063 for(ResultNode child : doc.getChildren()) {
00064 orderTree(child);
00065 }
00066
00067 Collections.sort(doc.getChildren(), new ResultNodeComparator());
00068 }
00069
00070 public class ResultNodeComparator implements Comparator {
00071
00072 public int compare(Object o1, Object o2) {
00073
00074
00075
00076 ResultNode r1 = (ResultNode)o1;
00077 ResultNode r2 = (ResultNode)o2;
00078
00079
00080 if(r1.getValue()==null ^ r2.getValue() == null) {
00081 return (r1.getValue()==null)?-1:1;
00082 }
00083
00084 else if (r1.getValue()==null && r2.getValue() == null) {
00085 return 0;
00086 }
00087
00088 int base = r1.getValue().compareTo(r2.getValue());
00089 int direction = ((String)(params.get(DIRECTION))).equalsIgnoreCase("desc")?-1:1;
00090
00091 return base*direction;
00092 }
00093
00094 }
00095
00099 public void checkParams() throws InvalidValueException, MissingArgumentException, ResourceUnavailableException {
00100 }
00101
00105 public void init() throws InitializationException, ResourceUnavailableException {
00106
00107 if (!params.containsKey(DIRECTION)) {
00108 params.put(DIRECTION, "asc");
00109 }
00110 }
00111
00115 public void setParam(String paramName, Object value)
00116 throws IllegalParamterTypeException {
00117 params.put(paramName, value);
00118 }
00119 }