6e7f21d98a9431e4c92afdf663d13d63fd00a69b
[cql-java-moved-to-github.git] / src / main / java / org / z3950 / zing / cql / CQLSortNode.java
1 // $Id: CQLSortNode.java,v 1.2 2008-04-11 12:05:15 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.2 2008-04-11 12:05:15 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 Vector<ModifierSet> getSortIndexes() {
37         return keys;
38     }
39
40     public String toXCQL(int level, Vector<CQLPrefix> prefixes,
41                          Vector<ModifierSet> sortkeys) {
42         if (sortkeys != null)
43             throw new Error("CQLSortNode.toXCQL() called with sortkeys");
44         return subtree.toXCQL(level, prefixes, keys);
45     }
46
47     public String toCQL() {
48         StringBuffer buf = new StringBuffer(subtree.toCQL());
49
50         if (keys != null) {
51             buf.append(" sortby");
52             for (int i = 0; i < keys.size(); i++) {
53                 ModifierSet key = keys.get(i);
54                 buf.append(" " + key.toCQL());
55             }
56         }
57
58         return buf.toString();
59     }
60
61     public String toPQF(Properties config) throws PQFTranslationException {
62         return "@attr 1=oops \"###\"";
63     }
64
65     public byte[] toType1BER(Properties config)
66         throws PQFTranslationException {
67         // There is no way to represent sorting in a standard Z39.50
68         // Type-1 query, so the best we can do is return the
69         // underlying query and ignore the sort-specification.
70         return subtree.toType1BER(config);
71     }
72 }