source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/Library2.java@ 25635

Last change on this file since 25635 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

  • 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;
26import org.w3c.dom.Node;
27
28import java.io.BufferedReader;
29import java.io.InputStreamReader;
30import java.io.File;
31import java.io.IOException;
32import java.util.HashMap;
33import java.util.Set;
34import java.util.Iterator;
35
36/**
37 * A program to take cgi args from the command line and return html
38 *
39 * @author <a href="mailto:[email protected]">Katherine Don</a>
40 * @version $Revision: 25635 $
41 */
42
43final public class Library2
44{
45
46 protected XMLConverter converter = null;
47 protected Document doc = null;
48
49 protected HashMap<String, String> 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<String, String>();
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<String, Comparable> config_params = new HashMap<String, Comparable>();
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, "", new UserContext());
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 = 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<String> params = saved_args.keySet();
196 Iterator<String> i = params.iterator();
197 while (i.hasNext())
198 {
199 String name = 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(saved_args.get(name)));
205 xml_param_list.appendChild(param);
206 }
207
208 return xml_message;
209 }
210}
Note: See TracBrowser for help on using the repository browser.