- Timestamp:
- 2012-01-26T11:14:42+13:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/Library2.java
r16688 r24982 33 33 import java.util.Set; 34 34 import java.util.Iterator; 35 35 36 /** 36 37 * A program to take cgi args from the command line and return html 37 * 38 * 38 39 * @author <a href="mailto:[email protected]">Katherine Don</a> 39 40 * @version $Revision$ 40 41 */ 41 42 42 final public class Library2 { 43 44 protected XMLConverter converter = null; 45 protected Document doc = null; 46 47 protected HashMap saved_args = null; 48 protected GSParams params = null; 49 protected DefaultReceptionist recept = null; 50 51 public Library2 (){ 52 this.converter = new XMLConverter(); 53 this.doc = converter.newDOM(); 54 this.saved_args = new HashMap(); 55 this.params = new GSParams(); 56 this.recept = new DefaultReceptionist(); 57 } 58 59 public void configure(String site_name, String interface_name) { 60 61 HashMap config_params = new HashMap(); 62 //config_params.put(GSConstants.GSDL3_HOME, gsdl_home); 63 config_params.put(GSConstants.SITE_NAME, site_name); 64 config_params.put(GSConstants.INTERFACE_NAME, interface_name); 65 // new message router - create it and pass a handle to recept. 66 // the servlet wont use this directly 67 MessageRouter message_router = new MessageRouter(); 68 69 message_router.setSiteName(site_name); 70 message_router.configure(); 71 // new receptionist 72 recept.setConfigParams(config_params); 73 recept.setMessageRouter(message_router); 74 recept.setParams(params); 75 recept.configure(); 76 } 77 public static void main(String args[]) { 78 79 if (args.length != 2) { 80 System.out.println("Usage: Library2 <site name> <interface name>"); 81 System.exit(1); 82 } 83 84 Library2 library = new Library2(); 85 library.configure(args[0], args[1]); 86 87 88 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 89 String query=null; 90 String result=null; 91 while (true) { 92 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)"); 93 try { 94 query = br.readLine(); 95 } catch (Exception e) { 96 System.err.println("Library2 exception:"+e.getMessage()); 97 } 98 if (query.startsWith("exit")) { 99 System.exit(1); 100 } 101 102 result = library.process(query); 103 104 System.out.println(result); 105 106 107 } 108 109 } 110 111 protected String process(String query) { 112 Element xml_message = generateRequest(query); 113 System.out.println("*********************"); 114 System.out.println(converter.getPrettyString(xml_message)); 115 Node xml_result = recept.process(xml_message); 116 return this.converter.getPrettyString(xml_result); 117 } 118 protected Element generateRequest(String cgiargs) { 119 120 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 121 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "", ""); 122 xml_message.appendChild(xml_request); 123 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 124 xml_request.appendChild(xml_param_list); 125 126 // the defaults 127 String action = "p"; 128 String subaction = "home"; 129 String lang = (String)saved_args.get(GSParams.LANGUAGE); 130 if (lang == null) { 131 lang = "en"; 132 } 133 String output = "html"; 134 135 String args [] = cgiargs.split("&"); 136 for (int i=0; i<args.length; i++) { 137 String arg = args[i]; 138 int pos = arg.indexOf('='); 139 if (pos == -1) continue; 140 String name = arg.substring(0, pos); 141 String value = arg.substring(pos+1); 142 if (name.equals(GSParams.ACTION)) { 143 action = value; 144 } else if (name.equals(GSParams.SUBACTION)) { 145 subaction = (value); 146 } else if (name.equals(GSParams.LANGUAGE)) { 147 lang = value; 148 saved_args.put(name, value); 149 } else if (name.equals(GSParams.OUTPUT)) { 150 output = value; 151 } else if (params.shouldSave(name)) { 152 saved_args.put(name, value); 153 } else { 154 // make a param now 155 Element param = doc.createElement(GSXML.PARAM_ELEM); 156 param.setAttribute(GSXML.NAME_ATT, name); 157 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value)); 158 xml_param_list.appendChild(param); 159 } 160 } 161 162 xml_request.setAttribute(GSXML.OUTPUT_ATT, output); 163 xml_request.setAttribute(GSXML.ACTION_ATT, action); 164 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 165 xml_request.setAttribute(GSXML.LANG_ATT, lang); 166 167 168 // put in all the params from the session cache 169 Set params = saved_args.keySet(); 170 Iterator i = params.iterator(); 171 while (i.hasNext()) { 172 String name = (String)i.next(); 173 if (name.equals(GSParams.LANGUAGE)) continue; 174 Element param = this.doc.createElement(GSXML.PARAM_ELEM); 175 param.setAttribute(GSXML.NAME_ATT, name); 176 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name))); 177 xml_param_list.appendChild(param); 178 } 179 180 return xml_message; 181 } 43 final public class Library2 44 { 45 46 protected XMLConverter converter = null; 47 protected Document doc = null; 48 49 protected HashMap saved_args = null; 50 protected GSParams params = null; 51 protected DefaultReceptionist recept = null; 52 53 public Library2() 54 { 55 this.converter = new XMLConverter(); 56 this.doc = converter.newDOM(); 57 this.saved_args = new HashMap(); 58 this.params = new GSParams(); 59 this.recept = new DefaultReceptionist(); 60 } 61 62 public void configure(String site_name, String interface_name) 63 { 64 65 HashMap config_params = new HashMap(); 66 //config_params.put(GSConstants.GSDL3_HOME, gsdl_home); 67 config_params.put(GSConstants.SITE_NAME, site_name); 68 config_params.put(GSConstants.INTERFACE_NAME, interface_name); 69 // new message router - create it and pass a handle to recept. 70 // the servlet wont use this directly 71 MessageRouter message_router = new MessageRouter(); 72 73 message_router.setSiteName(site_name); 74 message_router.configure(); 75 // new receptionist 76 recept.setConfigParams(config_params); 77 recept.setMessageRouter(message_router); 78 recept.setParams(params); 79 recept.configure(); 80 } 81 82 public static void main(String args[]) 83 { 84 85 if (args.length != 2) 86 { 87 System.out.println("Usage: Library2 <site name> <interface name>"); 88 System.exit(1); 89 } 90 91 Library2 library = new Library2(); 92 library.configure(args[0], args[1]); 93 94 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 95 String query = null; 96 String result = null; 97 while (true) 98 { 99 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)"); 100 try 101 { 102 query = br.readLine(); 103 } 104 catch (Exception e) 105 { 106 System.err.println("Library2 exception:" + e.getMessage()); 107 } 108 if (query.startsWith("exit")) 109 { 110 System.exit(1); 111 } 112 113 result = library.process(query); 114 115 System.out.println(result); 116 117 } 118 119 } 120 121 protected String process(String query) 122 { 123 Element xml_message = generateRequest(query); 124 System.out.println("*********************"); 125 System.out.println(converter.getPrettyString(xml_message)); 126 Node xml_result = recept.process(xml_message); 127 return this.converter.getPrettyString(xml_result); 128 } 129 130 protected Element generateRequest(String cgiargs) 131 { 132 133 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 134 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "", ""); 135 xml_message.appendChild(xml_request); 136 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 137 xml_request.appendChild(xml_param_list); 138 139 // the defaults 140 String action = "p"; 141 String subaction = "home"; 142 String lang = (String) saved_args.get(GSParams.LANGUAGE); 143 if (lang == null) 144 { 145 lang = "en"; 146 } 147 String output = "html"; 148 149 String args[] = cgiargs.split("&"); 150 for (int i = 0; i < args.length; i++) 151 { 152 String arg = args[i]; 153 int pos = arg.indexOf('='); 154 if (pos == -1) 155 continue; 156 String name = arg.substring(0, pos); 157 String value = arg.substring(pos + 1); 158 if (name.equals(GSParams.ACTION)) 159 { 160 action = value; 161 } 162 else if (name.equals(GSParams.SUBACTION)) 163 { 164 subaction = (value); 165 } 166 else if (name.equals(GSParams.LANGUAGE)) 167 { 168 lang = value; 169 saved_args.put(name, value); 170 } 171 else if (name.equals(GSParams.OUTPUT)) 172 { 173 output = value; 174 } 175 else if (params.shouldSave(name)) 176 { 177 saved_args.put(name, value); 178 } 179 else 180 { 181 // make a param now 182 Element param = doc.createElement(GSXML.PARAM_ELEM); 183 param.setAttribute(GSXML.NAME_ATT, name); 184 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value)); 185 xml_param_list.appendChild(param); 186 } 187 } 188 189 xml_request.setAttribute(GSXML.OUTPUT_ATT, output); 190 xml_request.setAttribute(GSXML.ACTION_ATT, action); 191 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 192 xml_request.setAttribute(GSXML.LANG_ATT, lang); 193 194 // put in all the params from the session cache 195 Set params = saved_args.keySet(); 196 Iterator i = params.iterator(); 197 while (i.hasNext()) 198 { 199 String name = (String) i.next(); 200 if (name.equals(GSParams.LANGUAGE)) 201 continue; 202 Element param = this.doc.createElement(GSXML.PARAM_ELEM); 203 param.setAttribute(GSXML.NAME_ATT, name); 204 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String) saved_args.get(name))); 205 xml_param_list.appendChild(param); 206 } 207 208 return xml_message; 209 } 182 210 } 183 184
Note:
See TracChangeset
for help on using the changeset viewer.