LOTS of changes. Biggies include proper support for relations, including
[cql-java-moved-to-github.git] / src / org / z3950 / zing / cql / CQLRelation.java
1 // $Id: CQLRelation.java,v 1.1 2002-10-30 09:19:26 mike Exp $
2
3 package org.z3950.zing.cql;
4 import java.util.Vector;
5 import java.lang.StringBuffer;
6
7 /**
8  * Represents a relation between a CQL qualifier and term.
9  * ###
10  *
11  * @version     $Id: CQLRelation.java,v 1.1 2002-10-30 09:19:26 mike Exp $
12  */
13 public class CQLRelation extends CQLNode {
14     String base;
15     Vector modifiers;
16
17     public CQLRelation(String base) {
18         this.base = base;
19         modifiers = new Vector();
20     }
21
22     public void addModifier(String modifier) {
23         modifiers.add(modifier);
24     }
25
26     public String[] getModifiers() {
27         int n = modifiers.size();
28         String[] res = new String[n];
29         for (int i = 0; i < n; i++) {
30             res[i] = (String) modifiers.get(i);
31         }
32
33         return res;
34     }
35
36     public String toXCQL(int level) {
37         StringBuffer buf = new StringBuffer();
38         buf.append (indent(level) + "<relation>\n" +
39                     indent(level+1) + "<value>" + xq(base) + "</value>\n");
40         String[] mods = getModifiers();
41         if (mods.length > 0) {
42             buf.append(indent(level+1) + "<modifiers>\n");
43             for (int i = 0; i < mods.length; i++)
44                 buf.append(indent(level+2)).
45                     append("<modifier><value>"). append(mods[i]).
46                     append("</value></modifier>\n");
47             buf.append(indent(level+1) + "</modifiers>\n");
48         }
49         buf.append(indent(level) + "</relation>\n");
50         return buf.toString();
51     }
52
53     public String toCQL() {
54         StringBuffer buf = new StringBuffer(base);
55         String[] mods = getModifiers();
56         for (int i = 0; i < mods.length; i++) {
57             buf.append("/").append(mods[i]);
58         }
59
60         return buf.toString();
61     }
62
63     public static void main(String[] args) {
64         if (args.length < 1) {
65             System.err.println("Usage: CQLRelation <base> <modifier>...");
66             System.exit(1);
67         }
68
69         CQLRelation res = new CQLRelation(args[0]);
70         for (int i = 1; i < args.length; i++) {
71             res.addModifier(args[i]);
72         }
73
74         System.out.println(res.toCQL());
75     }
76 }