001: /*
002: * AbstractDSpaceTransformer.java
003: *
004: * Version: $Revision: 1.14 $
005: *
006: * Date: $Date: 2006/05/02 05:30:55 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.cocoon;
042:
043: import java.io.IOException;
044: import java.io.UnsupportedEncodingException;
045: import java.net.URLDecoder;
046: import java.net.URLEncoder;
047: import java.sql.SQLException;
048: import java.util.Map;
049:
050: import org.apache.avalon.framework.parameters.Parameters;
051: import org.apache.cocoon.ProcessingException;
052: import org.apache.cocoon.components.flow.FlowHelper;
053: import org.apache.cocoon.components.flow.WebContinuation;
054: import org.apache.cocoon.environment.ObjectModelHelper;
055: import org.apache.cocoon.environment.Request;
056: import org.apache.cocoon.environment.SourceResolver;
057: import org.dspace.app.xmlui.objectmanager.DSpaceObjectManager;
058: import org.dspace.app.xmlui.utils.ContextUtil;
059: import org.dspace.app.xmlui.utils.UIException;
060: import org.dspace.app.xmlui.wing.AbstractWingTransformer;
061: import org.dspace.app.xmlui.wing.WingException;
062: import org.dspace.app.xmlui.wing.element.Body;
063: import org.dspace.app.xmlui.wing.element.Options;
064: import org.dspace.app.xmlui.wing.element.PageMeta;
065: import org.dspace.app.xmlui.wing.element.UserMeta;
066: import org.dspace.app.xmlui.wing.ObjectManager;
067: import org.dspace.authorize.AuthorizeException;
068: import org.dspace.core.Constants;
069: import org.dspace.core.Context;
070: import org.dspace.eperson.EPerson;
071: import org.xml.sax.SAXException;
072:
073: /**
074: * @author Scott Phillips
075: */
076: public abstract class AbstractDSpaceTransformer extends
077: AbstractWingTransformer implements DSpaceTransformer {
078:
079: private static final String NAME_TRIM = "org.dspace.app.xmlui.";
080:
081: protected Map objectModel;
082:
083: protected Context context;
084:
085: protected String contextPath;
086:
087: protected String servletPath;
088:
089: protected String sitemapURI;
090:
091: protected String url;
092:
093: protected Parameters parameters;
094:
095: protected EPerson eperson;
096:
097: protected WebContinuation knot;
098:
099: // Only access this through getObjectManager, so that we don't have to create one if we don't want too.
100: private ObjectManager objectManager;
101:
102: public void setup(SourceResolver resolver, Map objectModel,
103: String src, Parameters parameters)
104: throws ProcessingException, SAXException, IOException {
105: this .objectModel = objectModel;
106: this .parameters = parameters;
107: try {
108: this .context = ContextUtil.obtainContext(objectModel);
109: this .eperson = context.getCurrentUser();
110: Request request = ObjectModelHelper.getRequest(objectModel);
111: this .contextPath = request.getContextPath();
112: if (contextPath == null)
113: contextPath = "/";
114:
115: this .servletPath = request.getServletPath();
116: this .sitemapURI = request.getSitemapURI();
117: this .knot = FlowHelper.getWebContinuation(objectModel);
118: } catch (SQLException sqle) {
119: handleException(sqle);
120: }
121:
122: // Initialize the Wing framework.
123: try {
124: this .setupWing();
125: } catch (WingException we) {
126: throw new ProcessingException(we);
127: }
128: }
129:
130: protected void handleException(Exception e) throws SAXException {
131: throw new SAXException(
132: "An error was encountered while processing the '"
133: + this .getComponentName()
134: + "' Wing based component: "
135: + this .getClass().getName(), e);
136: }
137:
138: /** What to add at the end of the body */
139: public void addBody(Body body) throws SAXException, WingException,
140: UIException, SQLException, IOException, AuthorizeException {
141: // Do nothing
142: }
143:
144: /** What to add to the options list */
145: public void addOptions(Options options) throws SAXException,
146: WingException, UIException, SQLException, IOException,
147: AuthorizeException {
148: // Do nothing
149: }
150:
151: /** What user metadata to add to the document */
152: public void addUserMeta(UserMeta userMeta) throws SAXException,
153: WingException, UIException, SQLException, IOException,
154: AuthorizeException {
155: // Do nothing
156: }
157:
158: /** What page metadata to add to the document */
159: public void addPageMeta(PageMeta pageMeta) throws SAXException,
160: WingException, UIException, SQLException, IOException,
161: AuthorizeException {
162: // Do nothing
163: }
164:
165: public ObjectManager getObjectManager() {
166: if (this .objectManager == null)
167: this .objectManager = new DSpaceObjectManager();
168: return this .objectManager;
169: }
170:
171: /** What is a unique name for this component? */
172: public String getComponentName() {
173: String name = this .getClass().getName();
174: if (name.startsWith(NAME_TRIM))
175: name = name.substring(NAME_TRIM.length());
176: return name;
177: }
178:
179: /**
180: * Encode the given string for URL transmission.
181: *
182: * @param unencodedString
183: * The unencoded string.
184: * @return The encoded string
185: */
186: public static String URLEncode(String unencodedString)
187: throws UIException {
188: if (unencodedString == null)
189: return "";
190:
191: try {
192: return URLEncoder.encode(unencodedString,
193: Constants.DEFAULT_ENCODING);
194: } catch (UnsupportedEncodingException uee) {
195: throw new UIException(uee);
196: }
197:
198: }
199:
200: /**
201: * Decode the given string from URL transmission.
202: *
203: * @param encodedString
204: * The encoded string.
205: * @return The unencoded string
206: */
207: public static String URLDecode(String encodedString)
208: throws UIException {
209: try {
210: return URLDecoder.decode(encodedString,
211: Constants.DEFAULT_ENCODING);
212: } catch (UnsupportedEncodingException uee) {
213: throw new UIException(uee);
214: }
215:
216: }
217:
218: /**
219: * Generate a URL for the given base URL with the given parameters. This is
220: * a convenance method to make it easier to generate URL refrences with
221: * parameters.
222: *
223: * Example
224: * Map<String,String> parameters = new Map<String,String>();
225: * parameters.put("arg1","value1");
226: * parameters.put("arg2","value2");
227: * parameters.put("arg3","value3");
228: * String url = genrateURL("/my/url",parameters);
229: *
230: * would result in the string:
231: * url == "/my/url?arg1=value1&arg2=value2&arg3=value3"
232: *
233: * @param baseURL The baseURL without any parameters.
234: * @param parameters The parameters to be encoded on in the URL.
235: * @return The parameterized Post URL.
236: */
237: public static String generateURL(String baseURL,
238: Map<String, String> parameters) {
239: boolean first = true;
240: for (String key : parameters.keySet()) {
241: if (first) {
242: baseURL += "?";
243: first = false;
244: } else {
245: baseURL += "&";
246: }
247:
248: baseURL += key + "=" + parameters.get(key);
249: }
250:
251: return baseURL;
252: }
253:
254: /**
255: * Recyle
256: */
257: public void recycle() {
258: this .objectModel = null;
259: this .context = null;
260: this .contextPath = null;
261: this .servletPath = null;
262: this .sitemapURI = null;
263: this .url = null;
264: this .parameters = null;
265: this .eperson = null;
266: this .knot = null;
267: this .objectManager = null;
268: super .recycle();
269: }
270:
271: /**
272: * Dispose
273: */
274: public void dispose() {
275: this.objectModel = null;
276: this.context = null;
277: this.contextPath = null;
278: this.servletPath = null;
279: this.sitemapURI = null;
280: this.url = null;
281: this.parameters = null;
282: this.eperson = null;
283: this.knot = null;
284: this.objectManager = null;
285: super.dispose();
286: }
287:
288: }
|