1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 | import org.w3c.dom.Element;
|
---|
5 | import org.w3c.dom.NodeList;
|
---|
6 |
|
---|
7 | /** GSCGI holds the cgi args / xml parameters fro cgi type requests to the receptionist
|
---|
8 | * it includes methods to convert long parameter names to short cgi arg names
|
---|
9 | */
|
---|
10 | public class GSCGI {
|
---|
11 |
|
---|
12 | // the args that are used in the interface
|
---|
13 | public static final String ACTION_ARG = "a"; // the major type of action- eg query or browse or process
|
---|
14 | public static final String SUBACTION_ARG = "sa"; // subtype of action if we want different processing than the default
|
---|
15 | public static final String REQUEST_TYPE_ARG = "rt"; // whether the request is just to display the service form, or to actually do a request to teh service
|
---|
16 | public static final String REQUEST_ONLY_ARG = "ro"; // if == 1 do the request and pass back the xml - no page formatting
|
---|
17 | public static final String OUTPUT_ARG = "o"; // if processing is to be done, what type of output - html/xml/other??
|
---|
18 | public static final String SERVICE_ARG = "s"; // the name of the service
|
---|
19 | public static final String CLUSTER_ARG = "c"; // these two are the same
|
---|
20 | public static final String COLLECTION_ARG = "c";
|
---|
21 | public static final String LANGUAGE_ARG = "l";
|
---|
22 | public static final String DOCUMENT_ARG = "d";
|
---|
23 | public static final String RESOURCE_ARG = "r";
|
---|
24 | public static final String PROCESS_ID_ARG = "pid"; // if a request wasn't completed, this identifies the request - used when asking for a status update
|
---|
25 |
|
---|
26 | /** long to short mapping */
|
---|
27 | protected HashMap long_map_=null;
|
---|
28 | /** short to long mapping */
|
---|
29 | protected HashMap short_map_=null;
|
---|
30 |
|
---|
31 | /** initialises the maps with known parameters */
|
---|
32 | public GSCGI() {
|
---|
33 | long_map_ = new HashMap();
|
---|
34 | short_map_ = new HashMap();
|
---|
35 |
|
---|
36 | // initialize with the ones we know about
|
---|
37 | addStaticParam(ACTION_ARG);
|
---|
38 | addStaticParam(SUBACTION_ARG);
|
---|
39 | addStaticParam(REQUEST_TYPE_ARG);
|
---|
40 | addStaticParam(REQUEST_ONLY_ARG);
|
---|
41 | addStaticParam(CLUSTER_ARG);
|
---|
42 | addStaticParam(COLLECTION_ARG);
|
---|
43 | addStaticParam(LANGUAGE_ARG);
|
---|
44 | addStaticParam(DOCUMENT_ARG);
|
---|
45 | addStaticParam(RESOURCE_ARG);
|
---|
46 | addStaticParam(OUTPUT_ARG);
|
---|
47 | addStaticParam(SERVICE_ARG);
|
---|
48 | addStaticParam(PROCESS_ID_ARG);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /* static params - have only one form */
|
---|
52 | public boolean addStaticParam(String param) {
|
---|
53 | return addParam(param, param);
|
---|
54 | }
|
---|
55 | /** adds another param to teh list
|
---|
56 | * should check that its not already present? */
|
---|
57 | public boolean addParam(String long_name, String short_name) {
|
---|
58 | long_map_.put(long_name, short_name);
|
---|
59 | short_map_.put(short_name, long_name);
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** takes a paramList element - contains <param name="x"> elements
|
---|
64 | * converts all the short names to long names
|
---|
65 | * returns true if successful, false otherwise */
|
---|
66 | public boolean paramListToLongNames(Element param_list) {
|
---|
67 |
|
---|
68 | if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
69 | System.err.println("GSCGI Error: wrong element passed to toLong");
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
73 | for (int i=0; i< params.getLength(); i++) {
|
---|
74 | Element param = (Element)params.item(i);
|
---|
75 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
76 | param.setAttribute(GSXML.NAME_ATT, toLong(name));
|
---|
77 | }
|
---|
78 | return true;
|
---|
79 |
|
---|
80 | }
|
---|
81 | /** adds shortnames to the params in the list */
|
---|
82 | public boolean paramListAddShortNames(Element param_list) {
|
---|
83 | if (!param_list.getNodeName().equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
|
---|
84 | System.err.println("GSCGI Error: wrong element passed to addShortNames");
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 |
|
---|
88 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
89 | for (int i=0; i< params.getLength(); i++) {
|
---|
90 | Element param = (Element)params.item(i);
|
---|
91 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
92 | param.setAttribute(GSXML.PARAM_SHORTNAME_ATT, toShort(name));
|
---|
93 | }
|
---|
94 | return true;
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** returns the long form of a short arg -
|
---|
99 | * returns the original if it doesn't know about this short arg */
|
---|
100 | public String toLong(String short_name) {
|
---|
101 |
|
---|
102 | if (short_map_.containsKey(short_name)) {
|
---|
103 | return (String)short_map_.get(short_name);
|
---|
104 | }
|
---|
105 | return short_name; // just return the original
|
---|
106 | }
|
---|
107 | /** returns the short form of a long arg */
|
---|
108 | public String toShort(String long_name) {
|
---|
109 |
|
---|
110 | if (long_map_.containsKey(long_name)) {
|
---|
111 | return (String)long_map_.get(long_name);
|
---|
112 | }
|
---|
113 |
|
---|
114 | // else need to create a new mapping
|
---|
115 | String short_name = newShortName(long_name);
|
---|
116 | long_map_.put(long_name, short_name);
|
---|
117 | short_map_.put(short_name, long_name);
|
---|
118 | return short_name;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** tries to create a new short form of the long name -
|
---|
122 | * if it cant, it just returns the original name */
|
---|
123 | protected String newShortName(String long_name) {
|
---|
124 |
|
---|
125 | // for now, try the first letter, then first two etc.
|
---|
126 | int i=1;
|
---|
127 | String short_try= long_name.substring(0,1);
|
---|
128 | while(short_map_.containsKey(short_try)&& i<long_name.length()) {
|
---|
129 | i++;
|
---|
130 | short_try = long_name.substring(0, i);
|
---|
131 | }
|
---|
132 | return short_try;
|
---|
133 |
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|