source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/CGIArgConverter.java@ 3618

Last change on this file since 3618 was 3601, checked in by kjdon, 22 years ago

now handles multiparams

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1package org.greenstone.gsdl3.util;
2
3import java.util.HashMap;
4import org.w3c.dom.Element;
5import org.w3c.dom.NodeList;
6
7/** converts long names for parameters into short names for cgi args, and vice versa */
8public class CGIArgConverter {
9
10 /** long to short mapping */
11 protected HashMap long_map_=null;
12 /** short to long mapping */
13 protected HashMap short_map_=null;
14
15 /** initialises the maps with known parameters */
16 public CGIArgConverter() {
17 long_map_ = new HashMap();
18 short_map_ = new HashMap();
19
20 // initialize with the ones we know about
21 short_map_.put("a", "action");
22 long_map_.put("action", "a");
23
24 short_map_.put("sa", "subaction");
25 long_map_.put("subaction", "sa");
26
27 short_map_.put("sn", "serviceName");
28 long_map_.put("serviceName", "sn");
29
30 short_map_.put("sc", "serviceCluster");
31 long_map_.put("serviceCluster", "sc");
32
33 short_map_.put("l", "lang");
34 long_map_.put("lang", "l");
35
36 short_map_.put("c", "collection");
37 long_map_.put("collection", "c");
38
39 long_map_.put("resource", "r");
40 short_map_.put("r", "resource");
41
42 // these are service specific - should we have these here??
43 long_map_.put("case", "k");
44 short_map_.put("k", "case");
45
46 long_map_.put("stem", "s");
47 short_map_.put("s", "stem");
48
49 long_map_.put("queryLevel", "ql");
50 short_map_.put("ql", "queryLevel");
51
52 long_map_.put("index", "i");
53 short_map_.put("i", "index");
54
55 long_map_.put("matchMode", "mm");
56 short_map_.put("mm", "matchMode");
57
58 long_map_.put("sortBy", "sb");
59 short_map_.put("sb", "sortBy");
60
61 long_map_.put("maxDocs", "md");
62 short_map_.put("md", "maxDocs");
63
64 long_map_.put("query", "q");
65 short_map_.put("q", "query");
66
67 short_map_.put("cl", "classifier");
68 long_map_.put("classifier", "cl");
69 }
70
71 /** takes a paramList element - contains <param name="x"> elements
72 * converts all the short names to long names
73 * returns true if successful, false otherwise */
74 public boolean toLong(Element param_list) {
75
76 if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
77 System.err.println("CGIArgConverter Error: wrong element passed to toLong");
78 return false;
79 }
80 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
81 for (int i=0; i< params.getLength(); i++) {
82 Element param = (Element)params.item(i);
83 String name = param.getAttribute(GSXML.NAME_ATT);
84 param.setAttribute(GSXML.NAME_ATT, toLong(name));
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 NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
97 for (int i=0; i< params.getLength(); i++) {
98 Element param = (Element)params.item(i);
99 String name = param.getAttribute(GSXML.NAME_ATT);
100 param.setAttribute(GSXML.PARAM_SHORTNAME_ATT, toShort(name));
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
Note: See TracBrowser for help on using the repository browser.