/*
* Library2.java
* Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.greenstone.gsdl3;
import org.greenstone.gsdl3.core.*;
import org.greenstone.gsdl3.util.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
/**
* A program to take cgi args from the command line and return html
*
* @author Katherine Don
* @version $Revision: 16688 $
*/
final public class Library2 {
protected XMLConverter converter = null;
protected Document doc = null;
protected HashMap saved_args = null;
protected GSParams params = null;
protected DefaultReceptionist recept = null;
public Library2 (){
this.converter = new XMLConverter();
this.doc = converter.newDOM();
this.saved_args = new HashMap();
this.params = new GSParams();
this.recept = new DefaultReceptionist();
}
public void configure(String site_name, String interface_name) {
HashMap config_params = new HashMap();
//config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
config_params.put(GSConstants.SITE_NAME, site_name);
config_params.put(GSConstants.INTERFACE_NAME, interface_name);
// new message router - create it and pass a handle to recept.
// the servlet wont use this directly
MessageRouter message_router = new MessageRouter();
message_router.setSiteName(site_name);
message_router.configure();
// new receptionist
recept.setConfigParams(config_params);
recept.setMessageRouter(message_router);
recept.setParams(params);
recept.configure();
}
public static void main(String args[]) {
if (args.length != 2) {
System.out.println("Usage: Library2 ");
System.exit(1);
}
Library2 library = new Library2();
library.configure(args[0], args[1]);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String query=null;
String result=null;
while (true) {
System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
try {
query = br.readLine();
} catch (Exception e) {
System.err.println("Library2 exception:"+e.getMessage());
}
if (query.startsWith("exit")) {
System.exit(1);
}
result = library.process(query);
System.out.println(result);
}
}
protected String process(String query) {
Element xml_message = generateRequest(query);
System.out.println("*********************");
System.out.println(converter.getPrettyString(xml_message));
Node xml_result = recept.process(xml_message);
return this.converter.getPrettyString(xml_result);
}
protected Element generateRequest(String cgiargs) {
Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "", "");
xml_message.appendChild(xml_request);
Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
xml_request.appendChild(xml_param_list);
// the defaults
String action = "p";
String subaction = "home";
String lang = (String)saved_args.get(GSParams.LANGUAGE);
if (lang == null) {
lang = "en";
}
String output = "html";
String args [] = cgiargs.split("&");
for (int i=0; i