KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > document > AbstractJExcelView


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.document;
18
19 import java.io.OutputStream JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import jxl.Workbook;
27 import jxl.write.WritableWorkbook;
28
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.support.LocalizedResourceHelper;
31 import org.springframework.web.servlet.support.RequestContextUtils;
32 import org.springframework.web.servlet.view.AbstractView;
33
34 /**
35  * Convenient superclass for Excel document views.
36  *
37  * <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>. More
38  * information on <i>JExcelAPI</i> can be found on their <a
39  * HREF="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
40  *
41  * <p>Properties:
42  * <ul>
43  * <li>url (optional): The url of an existing Excel document to pick as a
44  * starting point. It is done without localization part nor the .xls extension.
45  * </ul>
46  *
47  * <p>The file will be searched with locations in the following order:
48  * <ul>
49  * <li>[url]_[language]_[country].xls
50  * <li>[url]_[language].xls
51  * <li>[url].xls
52  * </ul>
53  *
54  * <p>For working with the workbook in the subclass, see <a
55  * HREF="http://www.andykhan.com/jexcelapi/">Java Excel API site</a>
56  *
57  * <p>As an example, you can try this snippet:
58  *
59  * <pre>
60  * protected void buildExcelDocument(Map model, WritableWorkbook workbook, HttpServletRequest request,
61  * HttpServletResponse response) {
62  * if (workbook.getNumberOfSheets() == 0) {
63  * workbook.createSheet(&quot;Spring&quot;, 0);
64  * }
65  *
66  * WritableSheet sheet = workbook.getSheet(&quot;Spring&quot;);
67  * Label label = new Label(0, 0, &quot;This is a nice label&quot;);
68  * sheet.addCell(label);
69  * }</pre>
70  *
71  * The use of this view is close to the AbstractExcelView class,
72  * just using the JExcel API instead of the Apache POI API.
73  *
74  * @author Bram Smeets
75  * @author Alef Arendsen
76  * @author Juergen Hoeller
77  * @since 1.2.5
78  * @see AbstractExcelView
79  * @see AbstractPdfView
80  */

81 public abstract class AbstractJExcelView extends AbstractView {
82
83     /** The content type for an Excel response */
84     private static final String JavaDoc CONTENT_TYPE = "application/vnd.ms-excel";
85
86     /** The extension to look for existing templates */
87     private static final String JavaDoc EXTENSION = ".xls";
88
89
90     /** The url at which the template to use is located */
91     private String JavaDoc url;
92
93
94     /**
95      * Default Constructor.
96      * Sets the content type of the view to "application/vnd.ms-excel".
97      */

98     public AbstractJExcelView() {
99         setContentType(CONTENT_TYPE);
100     }
101
102     /**
103      * Set the URL of the Excel workbook source, without localization part nor extension.
104      */

105     public void setUrl(String JavaDoc url) {
106         this.url = url;
107     }
108
109
110     /**
111      * Renders the Excel view, given the specified model.
112      */

113     protected final void renderMergedOutputModel(
114             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
115         
116         // Set the content type and get the output stream.
117
response.setContentType(getContentType());
118         OutputStream JavaDoc out = response.getOutputStream();
119
120         WritableWorkbook workbook;
121         if (this.url != null) {
122             Workbook template = getTemplateSource(this.url, request);
123             workbook = Workbook.createWorkbook(out, template);
124         }
125         else {
126             logger.debug("Creating Excel Workbook from scratch");
127             workbook = Workbook.createWorkbook(out);
128         }
129
130         buildExcelDocument(model, workbook, request, response);
131
132         // Should we set the content length here?
133
// response.setContentLength(workbook.getBytes().length);
134

135         workbook.write();
136         out.flush();
137         workbook.close();
138     }
139
140     /**
141      * Create the workbook from an existing XLS document.
142      * @param url the URL of the Excel template without localization part nor extension
143      * @param request current HTTP request
144      * @return the template workbook
145      * @throws Exception in case of failure
146      */

147     protected Workbook getTemplateSource(String JavaDoc url, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
148         LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext());
149         Locale JavaDoc userLocale = RequestContextUtils.getLocale(request);
150         Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale);
151
152         // Create the Excel document from the source.
153
if (logger.isDebugEnabled()) {
154             logger.debug("Loading Excel workbook from " + inputFile);
155         }
156         return Workbook.getWorkbook(inputFile.getInputStream());
157     }
158
159     /**
160      * Subclasses must implement this method to create an Excel Workbook
161      * document, given the model.
162      * @param model the model Map
163      * @param workbook the Excel workbook to complete
164      * @param request in case we need locale etc. Shouldn't look at attributes.
165      * @param response in case we need to set cookies. Shouldn't write to it.
166      * @throws Exception in case of failure
167      */

168     protected abstract void buildExcelDocument(
169             Map JavaDoc model, WritableWorkbook workbook, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
170             throws Exception JavaDoc;
171
172 }
173
Popular Tags