Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.jelly.xpath.XPathComparator |
0% |
0% |

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;
19 |  |
     import java.util.List;
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 {
35 |  |
36 |  |
         /** The xpath to use to extract value from nodes to compare */
37 | 0 |
         private XPath xpath = null;
38 |  |
39 |  |
         /** Sort descending or ascending */
40 | 0 |
         private boolean descending = false;
41 |  |
42 | 0 |
         public XPathComparator() {
43 |  |
44 | 0 |
         }
45 |  |
46 | 0 |
         public XPathComparator(XPath xpath, boolean descending) {
47 | 0 |
             this.xpath = xpath;
48 | 0 |
             this.descending = descending;
49 | 0 |
         }
50 |  |
51 |  |
         public void setXpath(XPath xpath) {
52 | 0 |
             this.xpath = xpath;
53 | 0 |
         }
54 |  |
55 |  |
         public XPath getXpath() {
56 | 0 |
             return xpath;
57 |  |
         }
58 |  |
59 |  |
         public void setDescending(boolean descending) {
60 | 0 |
             this.descending = descending;
61 | 0 |
         }
62 |  |
63 |  |
         public int compare(Object o1, Object o2) {
64 | 0 |
             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 | 0 |
                 Object val1 = xpath.evaluate(n1);
73 | 0 |
                 Object val2 = xpath.evaluate(n2);
74 |  |
75 |  |
                 // return if null
76 | 0 |
                 if (val1 == null || val2 == class="keyword">null) {
77 | 0 |
                     return val1 == null ? (val2 == class="keyword">null ? 1 : -1) : 1;
78 |  |
                 }
79 |  |
80 | 0 |
                 Comparable c1 = getComparableValue(val1);
81 | 0 |
                 Comparable c2 = getComparableValue(val2);
82 |  |
83 |  |
                 // compare descending or ascending
84 | 0 |
                 if (!descending) {
85 | 0 |
                     return c1.compareTo(c2);
86 |  |
                 } else {
87 | 0 |
                     return c2.compareTo(c1);
88 |  |
                 }
89 |  |
90 | 0 |
             } catch (JaxenException e) {
91 |  |
92 | 0 |
                 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 getComparableValue(Object value) {
101 | 0 |
             if (value instanceof List) {
102 | 0 |
                 List list = (List) value;
103 | 0 |
                 if (list.isEmpty()) {
104 | 0 |
                     value = "";
105 |  |
                 }
106 | 0 |
                 value = list.get(0);
107 | 0 |
                 if (value == null) {
108 | 0 |
                     value = "";
109 |  |
                 }
110 |  |
             }
111 | 0 |
             if (value instanceof Comparable) {
112 | 0 |
                 return (Comparable) value;
113 |  |
             }
114 | 0 |
             else if (value instanceof Node) {
115 | 0 |
                 Node node = (Node) value;
116 | 0 |
                 return node.getStringValue();
117 |  |
             }
118 | 0 |
             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 message, Throwable cause) {
126 |  |
                 super(message, cause);
127 |  |
             }
128 |  |
         }
129 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.