source: trunk/gsdl3/src/java/org/greenstone/gsdl3/Library2.java@ 5798

Last change on this file since 5798 was 4994, checked in by kjdon, 21 years ago

Library2 now takes cgi args as input, and returns the page. you can copy and paste urls from hte output back in as input, and get the next page.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/*
2 * Library2.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3;
20
21import org.greenstone.gsdl3.core.*;
22import org.greenstone.gsdl3.util.*;
23
24import org.w3c.dom.Document;
25import org.w3c.dom.Element;
26
27import java.io.BufferedReader;
28import java.io.InputStreamReader;
29import java.io.File;
30import java.io.IOException;
31import java.util.HashMap;
32import java.util.Set;
33import java.util.Iterator;
34/**
35 * A program to take cgi args from the command line and return html
36 *
37 * @author <a href="mailto:[email protected]">Katherine Don</a>
38 * @version $Revision: 4994 $
39 */
40
41final public class Library2 {
42
43 protected XMLConverter converter = null;
44 protected Document doc = null;
45
46 protected HashMap saved_args = null;
47 protected GSParams params = null;
48 protected DefaultReceptionist recept = null;
49
50 public Library2 (){
51 this.converter = new XMLConverter();
52 this.doc = converter.newDOM();
53 this.saved_args = new HashMap();
54 this.params = new GSParams();
55 this.recept = new DefaultReceptionist();
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.setParams(params);
74 recept.configure();
75 }
76 public static void main(String args[]) {
77
78 if (args.length != 3) {
79 System.out.println("Usage: Library2 <gsdl3 home> <site name> <interface name>");
80 System.exit(1);
81 }
82
83 Library2 library = new Library2();
84 library.configure(args[0], args[1], args[2]);
85
86
87 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
88 String query=null;
89 String result=null;
90 while (true) {
91 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
92 try {
93 query = br.readLine();
94 } catch (Exception e) {
95 System.err.println("Library2 exception:"+e.getMessage());
96 }
97 if (query.startsWith("exit")) {
98 System.exit(1);
99 }
100
101 result = library.process(query);
102
103 System.out.println(result);
104
105
106 }
107
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(GSParams.LANGUAGE);
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(GSParams.ACTION)) {
142 action = value;
143 } else if (name.equals(GSParams.SUBACTION)) {
144 subaction = (value);
145 } else if (name.equals(GSParams.LANGUAGE)) {
146 lang = value;
147 saved_args.put(name, value);
148 } else if (name.equals(GSParams.OUTPUT)) {
149 output = value;
150 } else if (params.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 xml_param_list.appendChild(param);
158 }
159 }
160
161 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
162 xml_request.setAttribute(GSXML.ACTION_ATT, action);
163 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
164 xml_request.setAttribute(GSXML.LANG_ATT, lang);
165
166
167 // put in all the params from the session cache
168 Set params = saved_args.keySet();
169 Iterator i = params.iterator();
170 while (i.hasNext()) {
171 String name = (String)i.next();
172 if (name.equals(GSParams.LANGUAGE)) continue;
173 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
174 param.setAttribute(GSXML.NAME_ATT, name);
175 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name)));
176 xml_param_list.appendChild(param);
177 }
178
179 return xml_message;
180 }
181}
182
183
Note: See TracBrowser for help on using the repository browser.