001: /******************************************************************************
002: * LZViewer.java
003: * ****************************************************************************/package org.openlaszlo.servlets;
004:
005: import java.io.File;
006: import java.net.URL;
007: import java.io.FileWriter;
008: import java.io.FileInputStream;
009: import java.io.IOException;
010: import javax.servlet.*;
011: import javax.servlet.http.*;
012:
013: import org.openlaszlo.cm.CompilationManager;
014: import org.openlaszlo.compiler.CompilationError;
015: import org.openlaszlo.compiler.Canvas;
016: import org.openlaszlo.utils.StringUtils;
017: import org.openlaszlo.xml.internal.XMLUtils;
018: import org.openlaszlo.servlets.LZBindingListener;
019:
020: /** Allows one to view and play with the source for an LZX file.
021: *
022: * A temporary LZX file with a unique session id will be created for each client
023: * session accessing this page. You may want to may want to occassionally remove
024: * these temp files.
025: */
026: public class LZViewer extends HttpServlet {
027:
028: public void doGet(HttpServletRequest request,
029: HttpServletResponse response) throws ServletException,
030: IOException {
031:
032: // Turn off client caching as best we can
033: response.setHeader("Cache-control", "no-cache");
034: response.setDateHeader("Expires", 0);
035: response.setContentType("text/html");
036:
037: String url = (String) request.getAttribute("LZF_URL");
038: if (url == null) {
039: throw new ServletException(
040: /* (non-Javadoc)
041: * @i18n.test
042: * @org-mes="No LZF_URL attribute set"
043: */
044: org.openlaszlo.i18n.LaszloMessages.getMessage(
045: LZViewer.class.getName(), "051018-52"));
046: }
047: String uri = (new URL(url)).getFile();
048: String fileName = (String) request.getAttribute("LZF_FILENAME");
049: if (fileName == null) {
050: throw new ServletException(
051: /* (non-Javadoc)
052: * @i18n.test
053: * @org-mes="No LZF_FILENAME attribute set"
054: */
055: org.openlaszlo.i18n.LaszloMessages.getMessage(
056: LZViewer.class.getName(), "051018-64"));
057: }
058: File file = new File(fileName);
059: CompilationManager compMgr = (CompilationManager) request
060: .getAttribute("LZF_COMPMGR");
061: if (compMgr == null) {
062: throw new ServletException(
063: /* (non-Javadoc)
064: * @i18n.test
065: * @org-mes="No LZF_COMPMGR attribute set"
066: */
067: org.openlaszlo.i18n.LaszloMessages.getMessage(
068: LZViewer.class.getName(), "051018-76"));
069: }
070:
071: // Grab the LZX property
072: String lzx = request.getParameter("LZX");
073:
074: if (lzx != null) {
075:
076: HttpSession session = request.getSession();
077:
078: // Construct a temporary file and copy LZX source to it
079: File tempDir = new File(file.getParent() + File.separator);
080: tempDir.mkdirs();
081:
082: // Associate a temporary filename to a session. "File.deleteOnExit()"
083: // method guarantees the file will be removed when the server exits.
084: String tempFileName = "__tmp-" + session.getId() + ".lzx";
085: String fullFileName = tempDir.getPath() + File.separator
086: + tempFileName;
087: File tempFile = new File(fullFileName);
088: tempFile.deleteOnExit();
089: FileWriter writer = new FileWriter(tempFile);
090: writer.write(lzx);
091: writer.close();
092:
093: LZBindingListener lz = (LZBindingListener) session
094: .getAttribute("laszlo");
095: if (lz == null) {
096: lz = new LZBindingListener(fullFileName);
097: session.setAttribute("laszlo", lz);
098: }
099:
100: // Adjust the URI
101:
102: int slashIndex = uri.lastIndexOf('/');
103: uri = uri.substring(0, slashIndex) + "/" + tempFileName;
104: fileName = fullFileName;
105:
106: } else {
107: // Silently truncate impossibly long source files
108: int length = (int) file.length();
109: byte[] array = new byte[length];
110: FileInputStream in = new FileInputStream(file);
111: in.read(array);
112: lzx = new String(array);
113: }
114:
115: // Get the canvas
116: int width = 500;
117: int height = 500;
118: CompilationError error = null;
119: try {
120: Canvas canvas = compMgr.getCanvas(fileName);
121: width = canvas.getWidth();
122: height = canvas.getHeight();
123: } catch (CompilationError e) {
124: error = e;
125: }
126:
127: // Display HTML
128: ServletOutputStream out = response.getOutputStream();
129: out.println("<html><head><title>LZViewer</title></head>");
130: if (error == null) {
131: out
132: .println("<body onload=\"openWin();\" onunload=\"closeWin();\">");
133: out.println("<!-- Pop-up a window with the app in it -->");
134: out.println("<script>");
135: out.println("viewerWin = null;");
136: out.println("function openWin() {");
137: out.println(" closeWin();");
138: out.print(" viewerWin = window.open('" + uri
139: + "', 'viewer',");
140: out.print("'width=" + width + ",height=" + height
141: + ",toolbar=no,location=no,");
142: out
143: .println("directories=no,status=no,menubars=no,scrollbars=no,resizable=no');");
144: out.println(" viewerWin.focus();");
145: out.println("}");
146: out.println("function closeWin() {");
147: out.println(" if (viewerWin && ! viewerWin.closed) {");
148: out.println(" viewerWin.close();");
149: out.println(" }");
150: out.println("}");
151: out.println("</script>");
152: } else {
153: String details = "";
154: if (error.getLineNumber() != null) {
155: details += ", line " + error.getLineNumber();
156: if (error.getColumnNumber() != null) {
157: details += ", column " + error.getColumnNumber();
158: }
159: }
160: out.println("<p><font color=#ff0000>Syntax Error" + details
161: + " </font>: ");
162: out.println(XMLUtils.escapeAmpersands(error
163: .getErrorMessage()));
164: }
165:
166: lzx = XMLUtils.escapeAmpersands(lzx);
167: lzx = StringUtils.replace(lzx, '<', "<");
168:
169: out.println("<!-- Display a form with the LZX text in it -->");
170: out.println("<form action=\"" + uri
171: + "?lzt=filter&filter=/LZViewer\" method=\"post\">");
172: out.println("<input type=\"submit\" value=\"Update\">");
173: out.println("<textarea name=\"LZX\" cols=\"80\" rows=\"36\">");
174: out.println(lzx);
175: out.println("</textarea>");
176: out.println("</form>");
177: out.println("</body></html>");
178: }
179:
180: public void doPost(HttpServletRequest request,
181: HttpServletResponse response) throws ServletException,
182: IOException {
183: doGet(request, response);
184: }
185: }
|