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

Last change on this file since 16688 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 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 * A program to take cgi args from the command line and return html
37 *
38 * @author <a href="mailto:[email protected]">Katherine Don</a>
39 * @version $Revision: 16688 $
40 */
41
42final public class Library2 {
43
44 protected XMLConverter converter = null;
45 protected Document doc = null;
46
47 protected HashMap saved_args = null;
48 protected GSParams params = null;
49 protected DefaultReceptionist recept = null;
50
51 public Library2 (){
52 this.converter = new XMLConverter();
53 this.doc = converter.newDOM();
54 this.saved_args = new HashMap();
55 this.params = new GSParams();
56 this.recept = new DefaultReceptionist();
57 }
58
59 public void configure(String site_name, String interface_name) {
60
61 HashMap config_params = new HashMap();
62 //config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
63 config_params.put(GSConstants.SITE_NAME, site_name);
64 config_params.put(GSConstants.INTERFACE_NAME, interface_name);
65 // new message router - create it and pass a handle to recept.
66 // the servlet wont use this directly
67 MessageRouter message_router = new MessageRouter();
68
69 message_router.setSiteName(site_name);
70 message_router.configure();
71 // new receptionist
72 recept.setConfigParams(config_params);
73 recept.setMessageRouter(message_router);
74 recept.setParams(params);
75 recept.configure();
76 }
77 public static void main(String args[]) {
78
79 if (args.length != 2) {
80 System.out.println("Usage: Library2 <site name> <interface name>");
81 System.exit(1);
82 }
83
84 Library2 library = new Library2();
85 library.configure(args[0], args[1]);
86
87
88 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
89 String query=null;
90 String result=null;
91 while (true) {
92 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
93 try {
94 query = br.readLine();
95 } catch (Exception e) {
96 System.err.println("Library2 exception:"+e.getMessage());
97 }
98 if (query.startsWith("exit")) {
99 System.exit(1);
100 }
101
102 result = library.process(query);
103
104 System.out.println(result);
105
106
107 }
108
109 }
110
111 protected String process(String query) {
112 Element xml_message = generateRequest(query);
113 System.out.println("*********************");
114 System.out.println(converter.getPrettyString(xml_message));
115 Node xml_result = recept.process(xml_message);
116 return this.converter.getPrettyString(xml_result);
117 }
118 protected Element generateRequest(String cgiargs) {
119
120 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
121 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "", "");
122 xml_message.appendChild(xml_request);
123 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
124 xml_request.appendChild(xml_param_list);
125
126 // the defaults
127 String action = "p";
128 String subaction = "home";
129 String lang = (String)saved_args.get(GSParams.LANGUAGE);
130 if (lang == null) {
131 lang = "en";
132 }
133 String output = "html";
134
135 String args [] = cgiargs.split("&");
136 for (int i=0; i<args.length; i++) {
137 String arg = args[i];
138 int pos = arg.indexOf('=');
139 if (pos == -1) continue;
140 String name = arg.substring(0, pos);
141 String value = arg.substring(pos+1);
142 if (name.equals(GSParams.ACTION)) {
143 action = value;
144 } else if (name.equals(GSParams.SUBACTION)) {
145 subaction = (value);
146 } else if (name.equals(GSParams.LANGUAGE)) {
147 lang = value;
148 saved_args.put(name, value);
149 } else if (name.equals(GSParams.OUTPUT)) {
150 output = value;
151 } else if (params.shouldSave(name)) {
152 saved_args.put(name, value);
153 } else {
154 // make a param now
155 Element param = doc.createElement(GSXML.PARAM_ELEM);
156 param.setAttribute(GSXML.NAME_ATT, name);
157 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
158 xml_param_list.appendChild(param);
159 }
160 }
161
162 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
163 xml_request.setAttribute(GSXML.ACTION_ATT, action);
164 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
165 xml_request.setAttribute(GSXML.LANG_ATT, lang);
166
167
168 // put in all the params from the session cache
169 Set params = saved_args.keySet();
170 Iterator i = params.iterator();
171 while (i.hasNext()) {
172 String name = (String)i.next();
173 if (name.equals(GSParams.LANGUAGE)) continue;
174 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
175 param.setAttribute(GSXML.NAME_ATT, name);
176 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name)));
177 xml_param_list.appendChild(param);
178 }
179
180 return xml_message;
181 }
182}
183
184
Note: See TracBrowser for help on using the repository browser.