KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > OrderBy


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/OrderBy.java,v 1.10 2004/07/28 09:35:01 ib Exp $
3  * $Revision: 1.10 $
4  * $Date: 2004/07/28 09:35:01 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.search.basic;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
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 /**
39  * Encapsulate an OrderBy expression. Also supplies a Comparator according
40  * to the specified parameters in the OrderBy expression,
41  *
42  * @version $Revision: 1.10 $
43  */

44 public class OrderBy {
45
46     /** the comparator according to this expression */
47     private Comparator JavaDoc theComparator = new _Comparator ();
48
49     /** all orderby elements of this expression */
50     protected List JavaDoc orderByElements = new ArrayList JavaDoc ();
51
52     /**
53      * initializes an OrderBy.
54      *
55      * @param orderByElement the JDOM element containing the
56      * orderBy expression
57      */

58     public void init (Element orderByElement) throws InvalidQueryException {
59         Namespace nameSpace = orderByElement.getNamespace ();
60         Iterator JavaDoc 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     /**
81      * Method getComparator
82      *
83      * @return the comparator according to the OrderBy expression
84      *
85      */

86     public Comparator JavaDoc getComparator () {
87         return theComparator;
88     }
89
90
91     /**
92      * Method isCaseSensitive
93      *
94      * @param order an order Element
95      *
96      * @return true if this order element shall regard case
97      *
98      */

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     /**
118      * Method isAscending
119      *
120      * @param order an Element
121      *
122      * @return a boolean
123      *
124      * @throws InvalidQueryException if ascending and descending is supplied
125      *
126      */

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     /**
142      * Method getPropName
143      *
144      * @param order an Element
145      *
146      * @return a String
147      *
148      * @throws InvalidQueryException
149      *
150      */

151     private PropertyName getProperty (Element orderElem) throws InvalidQueryException {
152
153         List JavaDoc 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     /**
166      * Method createProperty
167      *
168      * @param propElem an Element
169      *
170      * @return a _Property
171      *
172      */

173     protected PropertyName createProperty (Element propElem) throws InvalidQueryException {
174         String JavaDoc name = propElem.getName();
175         String JavaDoc nameSpace = propElem.getNamespace().getURI();
176         return new PropertyName (name, nameSpace);
177     }
178
179
180     /**
181      * a comparator that sorts according to the OrderBy element
182      */

183     private class _Comparator implements Comparator JavaDoc {
184
185         /**
186          * Compares two ComparableResources. Two resources are equal, if they
187          * have the same URI. Each <order> element within <orderby> generates
188          * a CompareHint object containing the property to compare and ascending
189          * / descending. If the two resources are not equal, they are compared
190          * according the first CompareHint. If this one returns "equal" (0),
191          * they are compared according the next CompareHint and so on. If the
192          * two resources are equal according all CompareHints, the first one is
193          * considered to be greater than the second.
194          *
195          * @param o1 an Object
196          * @param o2 an Object
197          *
198          * @return an int
199          *
200          */

201         public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc 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