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 |
|
---|
37 | short_map_.put("c", "collection");
|
---|
38 | long_map_.put("collection", "c");
|
---|
39 |
|
---|
40 | short_map_.put("a", "action");
|
---|
41 | long_map_.put("action", "a");
|
---|
42 |
|
---|
43 | short_map_.put("sa", "subaction");
|
---|
44 | long_map_.put("subaction", "sa");
|
---|
45 |
|
---|
46 | short_map_.put("sn", "serviceName");
|
---|
47 | long_map_.put("serviceName", "sn");
|
---|
48 |
|
---|
49 | short_map_.put("sc", "serviceCluster");
|
---|
50 | long_map_.put("serviceCluster", "sc");
|
---|
51 |
|
---|
52 | short_map_.put("l", "lang");
|
---|
53 | long_map_.put("lang", "l");
|
---|
54 |
|
---|
55 | long_map_.put("query", "q");
|
---|
56 | short_map_.put("q", "query");
|
---|
57 |
|
---|
58 | long_map_.put("resource", "r");
|
---|
59 | short_map_.put("r", "resource");
|
---|
60 |
|
---|
61 | short_map_.put("cl", "classifier");
|
---|
62 | long_map_.put("classifier", "cl");
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean toLong(Element param_list) {
|
---|
66 |
|
---|
67 | if (!param_list.getNodeName().equals("paramList")) {
|
---|
68 | System.out.println("wrong element to CGIArgConverter:toLong");
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 |
|
---|
72 | Element param = (Element)param_list.getFirstChild();
|
---|
73 | while (param!=null) {
|
---|
74 | String name = param.getAttribute("name");
|
---|
75 | param.setAttribute("name", toLong(name));
|
---|
76 | param = (Element)param.getNextSibling();
|
---|
77 | }
|
---|
78 | return true;
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | public boolean addShortNames(Element param_list) {
|
---|
83 | if (!param_list.getNodeName().equals("paramList")) {
|
---|
84 | System.out.println("wrong element to CGIArgConverter:toShort");
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Element param = (Element)param_list.getFirstChild();
|
---|
89 | while (param!=null) {
|
---|
90 | String name = param.getAttribute("name");
|
---|
91 | param.setAttribute("shortname", toShort(name));
|
---|
92 | param = (Element)param.getNextSibling();
|
---|
93 | }
|
---|
94 | return true;
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | public String toLong(String short_name) {
|
---|
100 |
|
---|
101 |
|
---|
102 | if (short_map_.containsKey(short_name)) {
|
---|
103 | return (String)short_map_.get(short_name);
|
---|
104 | }
|
---|
105 | System.err.println("CGIArgConverter: short name '"+short_name+"' not found in map!");
|
---|
106 | return short_name; // just return the original
|
---|
107 | }
|
---|
108 |
|
---|
109 | public String toShort(String long_name) {
|
---|
110 |
|
---|
111 | if (long_map_.containsKey(long_name)) {
|
---|
112 | return (String)long_map_.get(long_name);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // else need to create a new mapping
|
---|
116 | String short_name = newShortName(long_name);
|
---|
117 | long_map_.put(long_name, short_name);
|
---|
118 | short_map_.put(short_name, long_name);
|
---|
119 | return short_name;
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected String newShortName(String long_name) {
|
---|
123 |
|
---|
124 | // for now, try the first letter, then first two etc.
|
---|
125 | int i=1;
|
---|
126 | String short_try= long_name.substring(0,1);
|
---|
127 | while(short_map_.containsKey(short_try)&& i<long_name.length()) {
|
---|
128 | i++;
|
---|
129 | short_try = long_name.substring(0, i);
|
---|
130 | }
|
---|
131 | if (i==long_name.length()) {
|
---|
132 | return ""; // couldn't find one
|
---|
133 | }
|
---|
134 | return short_try;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|