Changeset 4904 for trunk/gsdl3/src/java/org/greenstone/gsdl3/Library2.java
- Timestamp:
- 2003-07-11T16:30:27+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/Library2.java
r4706 r4904 22 22 import org.greenstone.gsdl3.util.*; 23 23 24 import org.w3c.dom.Document; 25 import org.w3c.dom.Element; 26 24 27 import java.io.BufferedReader; 25 28 import java.io.InputStreamReader; … … 27 30 import java.io.IOException; 28 31 import java.util.HashMap; 32 import java.util.Set; 33 import java.util.Iterator; 29 34 /** 30 * A program to take XML cgi-inputfrom the command line and return html35 * A program to take cgi args from the command line and return html 31 36 * 32 37 * @author <a href="mailto:[email protected]">Katherine Don</a> … … 36 41 final public class Library2 { 37 42 43 protected XMLConverter converter = null; 44 protected Document doc = null; 45 46 protected HashMap saved_args = null; 47 protected GSCGI cgi = null; 48 protected WebReceptionist recept = null; 49 50 public Library2 (){ 51 this.converter = new XMLConverter(); 52 this.doc = converter.newDOM(); 53 this.saved_args = new HashMap(); 54 this.cgi = new GSCGI(); 55 this.recept = new WebReceptionist(); 56 } 57 58 public void configure(String gsdl_home, String site_name, String interface_name) { 59 60 HashMap config_params = new HashMap(); 61 config_params.put(GSConstants.GSDL3_HOME, gsdl_home); 62 config_params.put(GSConstants.SITE_NAME, site_name); 63 config_params.put(GSConstants.INTERFACE_NAME, interface_name); 64 // new message router - create it and pass a handle to recept. 65 // the servlet wont use this directly 66 MessageRouter message_router = new MessageRouter(); 67 68 message_router.setSiteHome(GSFile.siteHome(gsdl_home, site_name)); 69 message_router.configure(); 70 // new receptionist 71 recept.setConfigParams(config_params); 72 recept.setMessageRouter(message_router); 73 recept.configure(); 74 recept.setCGI(cgi); 75 } 38 76 public static void main(String args[]) { 39 77 … … 42 80 System.exit(1); 43 81 } 44 45 HashMap config_params = new HashMap();46 config_params.put(GSConstants.GSDL3_HOME, args[0]);47 config_params.put(GSConstants.SITE_NAME, args[1]);48 config_params.put(GSConstants.INTERFACE_NAME, args[2]);49 // new message router - create it and pass a handle to recept.50 // the servlet wont use this directly51 MessageRouter message_router_ = new MessageRouter();52 82 53 message_router_.setSiteHome(GSFile.siteHome(args[0], args[1])); 54 message_router_.configure(); 55 // new receptionist 56 Receptionist recept_ = new Receptionist(); 57 recept_.setConfigParams(config_params); 58 recept_.setMessageRouter(message_router_); 59 recept_.configure(); 83 Library2 library = new Library2(); 84 library.configure(args[0], args[1], args[2]); 85 60 86 61 87 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); … … 63 89 String result=null; 64 90 while (true) { 65 System.out.println("Please enter an XML representation of cgi params (all on one line), or 'exit' to quit");91 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)"); 66 92 try { 67 93 query = br.readLine(); … … 72 98 System.exit(1); 73 99 } 100 101 result = library.process(query); 74 102 75 result = recept_.process(query);76 77 103 System.out.println(result); 78 104 … … 80 106 } 81 107 82 } 108 } 109 110 protected String process(String query) { 111 Element xml_message = generateRequest(query); 112 System.out.println("*********************"); 113 System.out.println(converter.getPrettyString(xml_message)); 114 Element xml_result = recept.process(xml_message); 115 return this.converter.getPrettyString(xml_result); 116 } 117 protected Element generateRequest(String cgiargs) { 118 119 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 120 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", ""); 121 xml_message.appendChild(xml_request); 122 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 123 xml_request.appendChild(xml_param_list); 124 125 // the defaults 126 String action = "p"; 127 String subaction = "home"; 128 String lang = (String)saved_args.get(GSCGI.LANGUAGE_ARG); 129 if (lang == null) { 130 lang = "en"; 131 } 132 String output = "html"; 133 134 String args [] = cgiargs.split("&"); 135 for (int i=0; i<args.length; i++) { 136 String arg = args[i]; 137 int pos = arg.indexOf('='); 138 if (pos == -1) continue; 139 String name = arg.substring(0, pos); 140 String value = arg.substring(pos+1); 141 if (name.equals(GSCGI.ACTION_ARG)) { 142 action = value; 143 } else if (name.equals(GSCGI.SUBACTION_ARG)) { 144 subaction = (value); 145 } else if (name.equals(GSCGI.LANGUAGE_ARG)) { 146 lang = value; 147 saved_args.put(name, value); 148 } else if (name.equals(GSCGI.OUTPUT_ARG)) { 149 output = value; 150 } else if ( cgi.shouldSave(name)) { 151 saved_args.put(name, value); 152 } else { 153 // make a param now 154 Element param = doc.createElement(GSXML.PARAM_ELEM); 155 param.setAttribute(GSXML.NAME_ATT, name); 156 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value)); 157 } 158 } 159 160 xml_request.setAttribute(GSXML.OUTPUT_ATT, output); 161 xml_request.setAttribute(GSXML.ACTION_ATT, action); 162 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 163 xml_request.setAttribute(GSXML.LANG_ATT, lang); 164 165 166 // put in all the params from the session cache 167 Set params = saved_args.keySet(); 168 Iterator i = params.iterator(); 169 while (i.hasNext()) { 170 String name = (String)i.next(); 171 if (name.equals(GSCGI.LANGUAGE_ARG)) continue; 172 Element param = this.doc.createElement(GSXML.PARAM_ELEM); 173 param.setAttribute(GSXML.NAME_ATT, name); 174 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name))); 175 xml_param_list.appendChild(param); 176 } 177 178 return xml_message; 179 } 83 180 } 84 181
Note:
See TracChangeset
for help on using the changeset viewer.