bdcc7d22500a1d65b56414c29d15034944d0fc0d
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLSortNode.java
1 // $Id: CQLSortNode.java,v 1.1 2007-07-03 12:55:56 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Properties;
5 import java.util.Vector;
6
7
8 /**
9  * Represents a sort node in a CQL parse-tree.
10  *
11  * @version     $Id: CQLSortNode.java,v 1.1 2007-07-03 12:55:56 mike Exp $
12  */
13 public class CQLSortNode extends CQLNode {
14     /**
15      * The root of a subtree representing the query whose result is to
16      * be sorted.
17      */ 
18     public CQLNode subtree;
19
20     /**
21      * The set of sort keys by which results are to be sorted,
22      * each expressed as an index together with zero or more
23      * modifiers.
24      */ 
25     Vector<ModifierSet> keys;
26
27     public CQLSortNode(CQLNode subtree) {
28         this.subtree = subtree;
29         keys = new Vector<ModifierSet>();
30     }
31
32     public void addSortIndex(ModifierSet key) {
33         keys.add(key);
34     }
35
36     public String toXCQL(int level, Vector<CQLPrefix> prefixes,
37                          Vector<ModifierSet> sortkeys) {
38         if (sortkeys != null)
39             throw new Error("CQLSortNode.toXCQL() called with sortkeys");
40         return subtree.toXCQL(level, prefixes, keys);
41     }
42
43     public String toCQL() {
44         StringBuffer buf = new StringBuffer(subtree.toCQL());
45
46         if (keys != null) {
47             buf.append(" sortby");
48             for (int i = 0; i < keys.size(); i++) {
49                 ModifierSet key = keys.get(i);
50                 buf.append(" " + key.toCQL());
51             }
52         }
53
54         return buf.toString();
55     }
56
57     public String toPQF(Properties config) throws PQFTranslationException {
58         return "@attr 1=oops \"###\"";
59     }
60
61     public byte[] toType1BER(Properties config)
62         throws PQFTranslationException {
63         // There is no way to represent sorting in a standard Z39.50
64         // Type-1 query, so the best we can do is return the
65         // underlying query and ignore the sort-specification.
66         return subtree.toType1BER(config);
67     }
68 }