KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > servlet > viewhandler > XMLViewHandler


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.servlet.viewhandler;
66
67 import com.jcorporate.expresso.core.controller.ControllerException;
68 import com.jcorporate.expresso.core.controller.ControllerResponse;
69 import com.jcorporate.expresso.core.db.DBException;
70 import com.jcorporate.expresso.core.misc.StringUtil;
71 import com.jcorporate.expresso.ext.xml.dbobj.ControllerXSLMap;
72 import com.jcorporate.expresso.ext.xml.dbobj.UserAgent;
73 import com.jcorporate.expresso.services.dbobj.Setup;
74 import org.apache.log4j.Logger;
75
76 import javax.servlet.ServletConfig JavaDoc;
77 import javax.servlet.ServletException JavaDoc;
78 import javax.servlet.http.HttpServletRequest JavaDoc;
79 import javax.servlet.http.HttpServletResponse JavaDoc;
80 import javax.xml.transform.Transformer JavaDoc;
81 import javax.xml.transform.TransformerConfigurationException JavaDoc;
82 import javax.xml.transform.TransformerException JavaDoc;
83 import javax.xml.transform.TransformerFactory JavaDoc;
84 import javax.xml.transform.stream.StreamResult JavaDoc;
85 import javax.xml.transform.stream.StreamSource JavaDoc;
86 import java.io.File JavaDoc;
87 import java.io.IOException JavaDoc;
88 import java.io.PrintWriter JavaDoc;
89 import java.io.StringReader JavaDoc;
90
91
92 /**
93  * Special servlet designed to interact with server-side Controller objects,
94  * transforming xml
95  *
96  * @author Michael Nash, Larry Hamel
97  */

98 public class XMLViewHandler extends ViewHandler {
99     protected static final String JavaDoc thisClass = XMLViewHandler.class.getName() + ".";
100     private static Logger log = Logger.getLogger(XMLViewHandler.class);
101     private String JavaDoc mRootPath;
102
103     public XMLViewHandler() {
104     }
105
106     /**
107      * Handle the Controller by producing XML output, transforming via a specified
108      * XSL stylesheet
109      * Creation date: (8/3/00 5:22:58 PM)
110      */

111     public void handleView(ControllerResponse con,
112                            HttpServletRequest JavaDoc req,
113                            HttpServletResponse JavaDoc res)
114             throws ControllerException, IOException JavaDoc, ServletException JavaDoc {
115
116
117         try {
118             String JavaDoc xsltName = StringUtil.notNull(con.getParameter("xsl"));
119
120             /* If the xslt name is not defined, use the default name */
121             if (xsltName.equals("")) {
122                 xsltName = Setup.getValueRequired(con.getDBName(),
123                         "DefaultXSL");
124
125                 String JavaDoc userAgent = StringUtil.notNull(req.getHeader("User-agent"));
126                 UserAgent ua = new UserAgent();
127                 ua.setDataContext(con.getDBName());
128
129                 /* wlo: changed 'String' to 'int'
130                    the changes here are due to the parameter type change
131                    of the getMatch() method in the ControllerXSLMap class */

132                 int uaId = 0;
133
134                 if (ua.getMatch(userAgent)) {
135                     uaId = ua.getFieldInt("UserAgent"); //wlo: changed 'getField' to 'getFieldInt'
136

137                     ControllerXSLMap myMap = new ControllerXSLMap();
138                     myMap.setDataContext(con.getDBName());
139
140                     if (myMap.getMatch(con.getUser(), con.getControllerClass(),
141                             uaId)) {
142                         xsltName = myMap.getField("XSLFileName");
143                     } /* if mapping found for this controller */
144
145                 } /* if user-agent match for this user-agent string */
146
147             }
148
149
150             // set content type
151
String JavaDoc mime = con.getParameter("mime");
152             if (mime != null) {
153                 res.setContentType(mime);
154             } else if (xsltName.equalsIgnoreCase("none")) {
155                 res.setContentType("text/xml");
156             } else {
157                 // assume html
158

159                 String JavaDoc charset = con.getString("charset");
160
161                 if (charset != null) {
162                     res.setContentType("text/html;charset=" + charset);
163                 } else {
164                     res.setContentType("text/html;charset=ISO-8859-1");
165                 }
166             }
167
168             PrintWriter JavaDoc out = res.getWriter();
169             String JavaDoc responseString = con.toXML();
170
171             if (xsltName.equalsIgnoreCase("none")) {
172                 // raw xml from controllers
173
out.println("<?xml version=\"1.0\"?>");
174                 out.println(responseString);
175             } else {
176
177                 // determine whether the xsl path is relative
178
File JavaDoc xslFile = new File JavaDoc(xsltName);
179                 if (!xslFile.exists()) {
180                     xslFile = new File JavaDoc(mRootPath, xsltName);
181                     if (!xslFile.exists()) {
182                         log.error("cannot find xsl file: " + xsltName);
183                     }
184                 }
185
186                 // Have the XSLTProcessorFactory obtain a interface to a
187
// new XSLTProcessor object.
188
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
189                 Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc(xslFile));
190
191                 // Have the transformer object transform the output of the controller
192
transformer.transform(new StreamSource JavaDoc(new StringReader JavaDoc(responseString)),
193                         new StreamResult JavaDoc(out));
194             }
195
196             //out.flush();
197
} catch (DBException de) {
198             res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
199             throw new ControllerException(de);
200         } catch (TransformerConfigurationException JavaDoc te1) {
201             res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
202             throw new ControllerException(te1);
203         } catch (TransformerException JavaDoc te2) {
204             res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
205             throw new ControllerException(te2);
206         }
207     } /* handleView() */
208
209     /**
210      * Standard Servlet init() function
211      *
212      * @param sc The servlet Configuration passed by the web.xml file
213      */

214     public void init(ServletConfig JavaDoc sc)
215             throws ServletException JavaDoc {
216         super.init(sc);
217         mRootPath = sc.getServletContext().getRealPath("/");
218     } /* init(ServletConfig) */
219
220 }
Popular Tags