KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > xpath > XPathComparator


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.apache.commons.jelly.xpath;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.commons.jelly.util.NestedRuntimeException;
22 import org.dom4j.Node;
23 import org.jaxen.JaxenException;
24 import org.jaxen.XPath;
25
26 /**
27  * Compares xml nodes by extracting the value at xpath and
28  * comparing it.
29  *
30  * @author <a HREF="mailto:jason@jhorman.org">Jason Horman</a>
31  * @version $Id: XPathComparator.java 155420 2005-02-26 13:06:03Z dirkv $
32  */

33
34 public class XPathComparator implements Comparator JavaDoc {
35
36     /** The xpath to use to extract value from nodes to compare */
37     private XPath xpath = null;
38
39     /** Sort descending or ascending */
40     private boolean descending = false;
41
42     public XPathComparator() {
43
44     }
45
46     public XPathComparator(XPath xpath, boolean descending) {
47         this.xpath = xpath;
48         this.descending = descending;
49     }
50
51     public void setXpath(XPath xpath) {
52         this.xpath = xpath;
53     }
54
55     public XPath getXpath() {
56         return xpath;
57     }
58
59     public void setDescending(boolean descending) {
60         this.descending = descending;
61     }
62
63     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
64         return compare((Node)o1, (Node)o2);
65     }
66
67     public int compare(Node n1, Node n2) {
68         try {
69
70             // apply the xpaths. not using stringValueOf since I don't
71
// want all of the child nodes appended to the strings
72
Object JavaDoc val1 = xpath.evaluate(n1);
73             Object JavaDoc val2 = xpath.evaluate(n2);
74
75             // return if null
76
if (val1 == null || val2 == null) {
77                 return val1 == null ? (val2 == null ? 1 : -1) : 1;
78             }
79
80             Comparable JavaDoc c1 = getComparableValue(val1);
81             Comparable JavaDoc c2 = getComparableValue(val2);
82
83             // compare descending or ascending
84
if (!descending) {
85                 return c1.compareTo(c2);
86             } else {
87                 return c2.compareTo(c1);
88             }
89
90         } catch (JaxenException e) {
91
92             throw new XPathSortException("error sorting nodes", e);
93
94         }
95     }
96
97     /**
98      * Turns the XPath result value into a Comparable object.
99      */

100     protected Comparable JavaDoc getComparableValue(Object JavaDoc value) {
101         if (value instanceof List JavaDoc) {
102             List JavaDoc list = (List JavaDoc) value;
103             if (list.isEmpty()) {
104                 value = "";
105             }
106             value = list.get(0);
107             if (value == null) {
108                 value = "";
109             }
110         }
111         if (value instanceof Comparable JavaDoc) {
112             return (Comparable JavaDoc) value;
113         }
114         else if (value instanceof Node) {
115             Node node = (Node) value;
116             return node.getStringValue();
117         }
118         return value.toString();
119     }
120
121     /**
122      * My own runtime exception in case something goes wrong with sort.
123      */

124     public static class XPathSortException extends NestedRuntimeException {
125         public XPathSortException(String JavaDoc message, Throwable JavaDoc cause) {
126             super(message, cause);
127         }
128     }
129 }
130
Popular Tags