1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 |
|
---|
6 | /** converts long names for parameters into short names for cgi args, and vice versa */
|
---|
7 | public class CGIArgConverter {
|
---|
8 |
|
---|
9 | /** long to short mapping */
|
---|
10 | protected HashMap long_map_=null;
|
---|
11 | /** short to long mapping */
|
---|
12 | protected HashMap short_map_=null;
|
---|
13 |
|
---|
14 | /** initialises the maps with known parameters */
|
---|
15 | public CGIArgConverter() {
|
---|
16 | long_map_ = new HashMap();
|
---|
17 | short_map_ = new HashMap();
|
---|
18 |
|
---|
19 | // initialize with the ones we know about
|
---|
20 | short_map_.put("a", "action");
|
---|
21 | long_map_.put("action", "a");
|
---|
22 |
|
---|
23 | short_map_.put("sa", "subaction");
|
---|
24 | long_map_.put("subaction", "sa");
|
---|
25 |
|
---|
26 | short_map_.put("sn", "serviceName");
|
---|
27 | long_map_.put("serviceName", "sn");
|
---|
28 |
|
---|
29 | short_map_.put("sc", "serviceCluster");
|
---|
30 | long_map_.put("serviceCluster", "sc");
|
---|
31 |
|
---|
32 | short_map_.put("l", "lang");
|
---|
33 | long_map_.put("lang", "l");
|
---|
34 |
|
---|
35 | short_map_.put("c", "collection");
|
---|
36 | long_map_.put("collection", "c");
|
---|
37 |
|
---|
38 | long_map_.put("resource", "r");
|
---|
39 | short_map_.put("r", "resource");
|
---|
40 |
|
---|
41 | // these are service specific - should we have these here??
|
---|
42 | long_map_.put("case", "k");
|
---|
43 | short_map_.put("k", "case");
|
---|
44 |
|
---|
45 | long_map_.put("stem", "s");
|
---|
46 | short_map_.put("s", "stem");
|
---|
47 |
|
---|
48 | long_map_.put("queryLevel", "ql");
|
---|
49 | short_map_.put("ql", "queryLevel");
|
---|
50 |
|
---|
51 | long_map_.put("index", "i");
|
---|
52 | short_map_.put("i", "index");
|
---|
53 |
|
---|
54 | long_map_.put("matchMode", "mm");
|
---|
55 | short_map_.put("mm", "matchMode");
|
---|
56 |
|
---|
57 | long_map_.put("sortBy", "sb");
|
---|
58 | short_map_.put("sb", "sortBy");
|
---|
59 |
|
---|
60 | long_map_.put("maxDocs", "md");
|
---|
61 | short_map_.put("md", "maxDocs");
|
---|
62 |
|
---|
63 | long_map_.put("query", "q");
|
---|
64 | short_map_.put("q", "query");
|
---|
65 |
|
---|
66 | short_map_.put("cl", "classifier");
|
---|
67 | long_map_.put("classifier", "cl");
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** takes a paramList element - contains <param name="x"> elements
|
---|
71 | * converts all the short names to long names
|
---|
72 | * returns true if successful, false otherwise */
|
---|
73 | public boolean toLong(Element param_list) {
|
---|
74 |
|
---|
75 | if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
76 | System.err.println("CGIArgConverter Error: wrong element passed to toLong");
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Element param = (Element)param_list.getFirstChild();
|
---|
81 | while (param!=null) {
|
---|
82 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
83 | param.setAttribute(GSXML.NAME_ATT, toLong(name));
|
---|
84 | param = (Element)param.getNextSibling();
|
---|
85 | }
|
---|
86 | return true;
|
---|
87 |
|
---|
88 | }
|
---|
89 | /** adds shortnames to the params in the list */
|
---|
90 | public boolean addShortNames(Element param_list) {
|
---|
91 | if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
92 | System.err.println("CGIArgConverter Error: wrong element passed to addShortNames");
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | Element param = (Element)param_list.getFirstChild();
|
---|
97 | while (param!=null) {
|
---|
98 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
99 | param.setAttribute(GSXML.PARAM_SHORTNAME_ATT, toShort(name));
|
---|
100 | param = (Element)param.getNextSibling();
|
---|
101 | }
|
---|
102 | return true;
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** returns the long form of a short arg -
|
---|
107 | * returns the original if it doesn't know about this short arg */
|
---|
108 | public String toLong(String short_name) {
|
---|
109 |
|
---|
110 | if (short_map_.containsKey(short_name)) {
|
---|
111 | return (String)short_map_.get(short_name);
|
---|
112 | }
|
---|
113 | return short_name; // just return the original
|
---|
114 | }
|
---|
115 | /** returns the short form of a long arg */
|
---|
116 | public String toShort(String long_name) {
|
---|
117 |
|
---|
118 | if (long_map_.containsKey(long_name)) {
|
---|
119 | return (String)long_map_.get(long_name);
|
---|
120 | }
|
---|
121 |
|
---|
122 | // else need to create a new mapping
|
---|
123 | String short_name = newShortName(long_name);
|
---|
124 | long_map_.put(long_name, short_name);
|
---|
125 | short_map_.put(short_name, long_name);
|
---|
126 | return short_name;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** tries to create a new short form of the long name -
|
---|
130 | * if it cant, it just returns the original name */
|
---|
131 | protected String newShortName(String long_name) {
|
---|
132 |
|
---|
133 | // for now, try the first letter, then first two etc.
|
---|
134 | int i=1;
|
---|
135 | String short_try= long_name.substring(0,1);
|
---|
136 | while(short_map_.containsKey(short_try)&& i<long_name.length()) {
|
---|
137 | i++;
|
---|
138 | short_try = long_name.substring(0, i);
|
---|
139 | }
|
---|
140 | return short_try;
|
---|
141 |
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|