KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > LZViewer


1 /******************************************************************************
2  * LZViewer.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets;
11
12 import java.io.File JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.io.FileWriter JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import javax.servlet.*;
18 import javax.servlet.http.*;
19
20 import org.openlaszlo.cm.CompilationManager;
21 import org.openlaszlo.compiler.CompilationError;
22 import org.openlaszlo.compiler.Canvas;
23 import org.openlaszlo.utils.StringUtils;
24 import org.openlaszlo.xml.internal.XMLUtils;
25 import org.openlaszlo.servlets.LZBindingListener;
26
27
28 /** Allows one to view and play with the source for an LZX file.
29  *
30  * A temporary LZX file with a unique session id will be created for each client
31  * session accessing this page. You may want to may want to occassionally remove
32  * these temp files.
33  */

34 public class LZViewer extends HttpServlet {
35
36     public void doGet(HttpServletRequest request, HttpServletResponse response)
37         throws ServletException, IOException JavaDoc
38     {
39
40         // Turn off client caching as best we can
41
response.setHeader("Cache-control", "no-cache");
42         response.setDateHeader("Expires", 0);
43         response.setContentType ("text/html");
44
45         String JavaDoc url = (String JavaDoc)request.getAttribute("LZF_URL");
46         if (url == null) {
47             throw new ServletException("No LZF_URL attribute set");
48         }
49         String JavaDoc uri = (new URL JavaDoc(url)).getFile();
50         String JavaDoc fileName = (String JavaDoc)request.getAttribute("LZF_FILENAME");
51         if (fileName == null) {
52             throw new ServletException("No LZF_FILENAME attribute set");
53         }
54         File JavaDoc file = new File JavaDoc(fileName);
55         CompilationManager compMgr = (CompilationManager)request.getAttribute("LZF_COMPMGR");
56         if (compMgr == null) {
57             throw new ServletException("No LZF_COMPMGR attribute set");
58         }
59  
60         // Grab the LZX property
61
String JavaDoc lzx = request.getParameter("LZX");
62
63         if (lzx != null) {
64
65             HttpSession session = request.getSession();
66
67             // Construct a temporary file and copy LZX source to it
68
File JavaDoc tempDir = new File JavaDoc(file.getParent() + File.separator);
69             tempDir.mkdirs();
70
71             // Associate a temporary filename to a session. "File.deleteOnExit()"
72
// method guarantees the file will be removed when the server exits.
73
String JavaDoc tempFileName = "__tmp-" + session.getId() + ".lzx";
74             String JavaDoc fullFileName = tempDir.getPath() + File.separator + tempFileName;
75             File JavaDoc tempFile = new File JavaDoc(fullFileName);
76             tempFile.deleteOnExit();
77             FileWriter JavaDoc writer = new FileWriter JavaDoc(tempFile);
78             writer.write(lzx);
79             writer.close();
80
81             LZBindingListener lz = (LZBindingListener)session.getAttribute("laszlo");
82             if (lz == null) {
83                 lz = new LZBindingListener(fullFileName);
84                 session.setAttribute("laszlo", lz);
85             }
86
87             // Adjust the URI
88

89             int slashIndex = uri.lastIndexOf('/');
90             uri = uri.substring(0, slashIndex) + "/" + tempFileName;
91             fileName = fullFileName;
92
93         } else {
94             // Silently truncate impossibly long source files
95
int length = (int)file.length();
96             byte[] array = new byte[length];
97             FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
98             in.read(array);
99             lzx = new String JavaDoc(array);
100         }
101
102         // Get the canvas
103
int width = 500;
104         int height = 500;
105         CompilationError error = null;
106         try {
107             Canvas canvas = compMgr.getCanvas(fileName);
108             width = canvas.getWidth();
109             height = canvas.getHeight();
110         }
111         catch (CompilationError e) {
112             error = e;
113         }
114
115         // Display HTML
116
ServletOutputStream out = response.getOutputStream();
117         out.println("<html><head><title>LZViewer</title></head>");
118         if (error == null) {
119             out.println("<body onload=\"openWin();\" onunload=\"closeWin();\">");
120             out.println("<!-- Pop-up a window with the app in it -->");
121             out.println("<script>");
122             out.println("viewerWin = null;");
123             out.println("function openWin() {");
124             out.println(" closeWin();");
125             out.print (" viewerWin = window.open('" + uri + "', 'viewer',");
126             out.print ("'width=" + width + ",height=" + height + ",toolbar=no,location=no,");
127             out.println("directories=no,status=no,menubars=no,scrollbars=no,resizable=no');");
128             out.println(" viewerWin.focus();");
129             out.println("}");
130             out.println("function closeWin() {");
131             out.println(" if (viewerWin && ! viewerWin.closed) {");
132             out.println(" viewerWin.close();");
133             out.println(" }");
134             out.println("}");
135             out.println("</script>");
136         } else {
137             String JavaDoc details = "";
138             if (error.getLineNumber() != null) {
139                 details += ", line " + error.getLineNumber();
140                 if (error.getColumnNumber() != null) {
141                     details += ", column " + error.getColumnNumber();
142                 }
143             }
144             out.println("<p><font color=#ff0000>Syntax Error" + details + " </font>: ");
145             out.println(XMLUtils.escapeAmpersands(error.getErrorMessage()));
146         }
147
148         lzx = XMLUtils.escapeAmpersands(lzx);
149         lzx = StringUtils.replace(lzx, '<', "&lt;");
150
151         out.println("<!-- Display a form with the LZX text in it -->");
152         out.println("<form action=\"" + uri + "?lzt=filter&filter=/LZViewer\" method=\"post\">");
153         out.println("<input type=\"submit\" value=\"Update\">");
154         out.println("<textarea name=\"LZX\" cols=\"80\" rows=\"36\">");
155         out.println(lzx);
156         out.println("</textarea>");
157         out.println("</form>");
158         out.println("</body></html>");
159     }
160
161     public void doPost(HttpServletRequest request, HttpServletResponse response)
162         throws ServletException, IOException JavaDoc
163     {
164         doGet(request, response);
165     }
166 }
167
Popular Tags