source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryCommandline.java@ 33256

Last change on this file since 33256 was 30477, checked in by davidb, 8 years ago

Changes in the Java code to support the new approach taken to client-side XSLT (using Saxon-CE JS library in the browser -- see next commit). Also some better error reporting when processing XSLT files

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
RevLine 
[3235]1/*
[26497]2 * LibraryCommandline.java
[3235]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 */
[3222]19package org.greenstone.gsdl3;
20
21import org.greenstone.gsdl3.core.*;
22import org.greenstone.gsdl3.util.*;
[26453]23import org.greenstone.util.GlobalProperties;
[3222]24
[4904]25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
[16688]27import org.w3c.dom.Node;
[4904]28
[3222]29import java.io.BufferedReader;
30import java.io.InputStreamReader;
31import java.io.File;
32import java.io.IOException;
[4706]33import java.util.HashMap;
[4904]34import java.util.Set;
35import java.util.Iterator;
[24982]36
[3235]37/**
[4904]38 * A program to take cgi args from the command line and return html
[24982]39 *
[3235]40 */
[3363]41
[26497]42final public class LibraryCommandline
[24982]43{
[25635]44 protected HashMap<String, String> saved_args = null;
[24982]45 protected GSParams params = null;
46 protected DefaultReceptionist recept = null;
47
[26497]48 public LibraryCommandline()
[24982]49 {
[25635]50 this.saved_args = new HashMap<String, String>();
[24982]51 this.params = new GSParams();
52 this.recept = new DefaultReceptionist();
[3222]53 }
54
[24982]55 public void configure(String site_name, String interface_name)
56 {
[4904]57
[27617]58 HashMap<String, Object> config_params = new HashMap<String, Object>();
[24982]59 //config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
60 config_params.put(GSConstants.SITE_NAME, site_name);
61 config_params.put(GSConstants.INTERFACE_NAME, interface_name);
[30477]62 config_params.put(GSConstants.USE_CLIENT_SIDE_XSLT, false);
[26453]63
[24982]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();
[3292]67
[24982]68 message_router.setSiteName(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();
[3363]75 }
76
[26453]77 /*
78 * On Linux, run as:
[29713]79 * GS3> source ./gs3-setup.sh
80 * GS3> java -classpath "web/WEB-INF/lib/*":"lib/jni/*":"web/WEB-INF/classes/." org.greenstone.gsdl3.LibraryCommandline localsite default
[26453]81 * Press enter to accept the default cgi-args to pass in.
82 *
[26454]83 * For how to include all jars in a folder into the classpath to run a java program, see
84 * http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath
[26453]85 * http://stackoverflow.com/questions/6780678/run-class-in-jar-file
86 */
[24982]87 public static void main(String args[])
88 {
[4904]89
[26494]90 if (System.getenv("GSDL3SRCHOME") == null) {
91 System.out.println("Before calling this script, run: source gs3-setup.sh");
92 System.exit(1);
93 }
94
[24982]95 if (args.length != 2)
96 {
[26497]97 System.out.println("Usage: LibraryCommandline <site name> <interface name>");
[24982]98 System.exit(1);
99 }
[4904]100
[26453]101 // force GlobalProperties to default GSDL3HOME to GSDL3SRCHOME/web if not already set
102 GlobalProperties.loadGlobalProperties("");
103
[26497]104 LibraryCommandline library = new LibraryCommandline();
[24982]105 library.configure(args[0], args[1]);
[4904]106
[24982]107 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
108 String query = null;
109 String result = null;
110 while (true)
111 {
112 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
113 try
114 {
115 query = br.readLine();
116 }
117 catch (Exception e)
118 {
[26497]119 System.err.println("LibraryCommandline exception:" + e.getMessage());
[24982]120 }
121 if (query.startsWith("exit"))
122 {
123 System.exit(1);
124 }
125
126 result = library.process(query);
127
128 System.out.println(result);
129
130 }
131
[4904]132 }
[24982]133
134 protected String process(String query)
135 {
136 Element xml_message = generateRequest(query);
137 System.out.println("*********************");
[28972]138 System.out.println(XMLConverter.getPrettyString(xml_message));
[24982]139 Node xml_result = recept.process(xml_message);
[28972]140 return XMLConverter.getPrettyString(xml_result);
[4904]141 }
142
[24982]143 protected Element generateRequest(String cgiargs)
144 {
[4904]145
[28972]146 Document msg_doc = XMLConverter.newDOM();
147 Element xml_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
148 Element xml_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PAGE, "", new UserContext());
[24982]149 xml_message.appendChild(xml_request);
[28972]150 Element xml_param_list = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
[24982]151 xml_request.appendChild(xml_param_list);
[4904]152
[24982]153 // the defaults
154 String action = "p";
155 String subaction = "home";
[25635]156 String lang = saved_args.get(GSParams.LANGUAGE);
[24982]157 if (lang == null)
158 {
159 lang = "en";
160 }
161 String output = "html";
162
163 String args[] = cgiargs.split("&");
164 for (int i = 0; i < args.length; i++)
165 {
166 String arg = args[i];
167 int pos = arg.indexOf('=');
168 if (pos == -1)
169 continue;
170 String name = arg.substring(0, pos);
171 String value = arg.substring(pos + 1);
172 if (name.equals(GSParams.ACTION))
173 {
174 action = value;
175 }
176 else if (name.equals(GSParams.SUBACTION))
177 {
178 subaction = (value);
179 }
180 else if (name.equals(GSParams.LANGUAGE))
181 {
182 lang = value;
183 saved_args.put(name, value);
184 }
185 else if (name.equals(GSParams.OUTPUT))
186 {
187 output = value;
188 }
189 else if (params.shouldSave(name))
190 {
191 saved_args.put(name, value);
192 }
193 else
194 {
195 // make a param now
[28972]196 Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
[24982]197 param.setAttribute(GSXML.NAME_ATT, name);
198 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
199 xml_param_list.appendChild(param);
200 }
201 }
202
203 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
204 xml_request.setAttribute(GSXML.ACTION_ATT, action);
205 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
206 xml_request.setAttribute(GSXML.LANG_ATT, lang);
207
208 // put in all the params from the session cache
[25635]209 Set<String> params = saved_args.keySet();
210 Iterator<String> i = params.iterator();
[24982]211 while (i.hasNext())
212 {
[25635]213 String name = i.next();
[24982]214 if (name.equals(GSParams.LANGUAGE))
215 continue;
[28972]216 Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
[24982]217 param.setAttribute(GSXML.NAME_ATT, name);
[25635]218 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(saved_args.get(name)));
[24982]219 xml_param_list.appendChild(param);
220 }
221
222 return xml_message;
[4904]223 }
[3222]224}
Note: See TracBrowser for help on using the repository browser.