1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 |
|
---|
6 | public class CGIArgConverter {
|
---|
7 |
|
---|
8 | protected HashMap long_map_=null;
|
---|
9 | protected HashMap short_map_=null;
|
---|
10 |
|
---|
11 | public CGIArgConverter() {
|
---|
12 | long_map_ = new HashMap();
|
---|
13 | short_map_ = new HashMap();
|
---|
14 |
|
---|
15 | // initialize with the ones we know about
|
---|
16 | long_map_.put("case", "k");
|
---|
17 | short_map_.put("k", "case");
|
---|
18 |
|
---|
19 | long_map_.put("stem", "s");
|
---|
20 | short_map_.put("s", "stem");
|
---|
21 |
|
---|
22 | long_map_.put("queryLevel", "ql");
|
---|
23 | short_map_.put("ql", "queryLevel");
|
---|
24 |
|
---|
25 | long_map_.put("index", "i");
|
---|
26 | short_map_.put("i", "index");
|
---|
27 |
|
---|
28 | long_map_.put("matchMode", "mm");
|
---|
29 | short_map_.put("mm", "matchMode");
|
---|
30 |
|
---|
31 | long_map_.put("sortBy", "sb");
|
---|
32 | short_map_.put("sb", "sortBy");
|
---|
33 |
|
---|
34 | long_map_.put("maxDocs", "md");
|
---|
35 | short_map_.put("md", "maxDocs");
|
---|
36 | // add in short names for the ones that are used, but dont need logn names
|
---|
37 | short_map_.put("a", "a"); // action
|
---|
38 | short_map_.put("sa", "sa"); // subaction
|
---|
39 | short_map_.put("sn", "sn"); // service name
|
---|
40 | short_map_.put("c", "c"); // collection
|
---|
41 | short_map_.put("l", "l"); // lang
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | public boolean toLong(Element param_list) {
|
---|
46 |
|
---|
47 | if (!param_list.getNodeName().equals("paramList")) {
|
---|
48 | System.out.println("wrong element to CGIArgConverter:toLong");
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 |
|
---|
52 | Element param = (Element)param_list.getFirstChild();
|
---|
53 | while (param!=null) {
|
---|
54 | String name = param.getAttribute("name");
|
---|
55 | param.setAttribute("name", toLong(name));
|
---|
56 | param = (Element)param.getNextSibling();
|
---|
57 | }
|
---|
58 | return true;
|
---|
59 |
|
---|
60 | }
|
---|
61 | public boolean addShortNames(Element param_list) {
|
---|
62 | if (!param_list.getNodeName().equals("paramList")) {
|
---|
63 | System.out.println("wrong element to CGIArgConverter:toShort");
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | Element param = (Element)param_list.getFirstChild();
|
---|
68 | while (param!=null) {
|
---|
69 | String name = param.getAttribute("name");
|
---|
70 | param.setAttribute("shortname", toShort(name));
|
---|
71 | param = (Element)param.getNextSibling();
|
---|
72 | }
|
---|
73 | return true;
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | public String toLong(String short_name) {
|
---|
79 |
|
---|
80 |
|
---|
81 | if (short_map_.containsKey(short_name)) {
|
---|
82 | return (String)short_map_.get(short_name);
|
---|
83 | }
|
---|
84 | System.err.println("CGIArgConverter: short name '"+short_name+"' not found in map!");
|
---|
85 | return short_name; // just return the original
|
---|
86 | }
|
---|
87 |
|
---|
88 | public String toShort(String long_name) {
|
---|
89 |
|
---|
90 | if (long_map_.containsKey(long_name)) {
|
---|
91 | return (String)long_map_.get(long_name);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // else need to create a new mapping
|
---|
95 | String short_name = newShortName(long_name);
|
---|
96 | long_map_.put(long_name, short_name);
|
---|
97 | short_map_.put(short_name, long_name);
|
---|
98 | return short_name;
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected String newShortName(String long_name) {
|
---|
102 |
|
---|
103 | // for now, try the first letter, then first two etc.
|
---|
104 | int i=1;
|
---|
105 | String short_try= long_name.substring(0,1);
|
---|
106 | while(short_map_.containsKey(short_try)&& i<long_name.length()) {
|
---|
107 | i++;
|
---|
108 | short_try = long_name.substring(0, i);
|
---|
109 | }
|
---|
110 | if (i==long_name.length()) {
|
---|
111 | return ""; // couldn't find one
|
---|
112 | }
|
---|
113 | return short_try;
|
---|
114 | }
|
---|
115 | }
|
---|