1 4 5 9 10 package org.openlaszlo.servlets; 11 12 import java.io.File ; 13 import java.net.URL ; 14 import java.io.FileWriter ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 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 34 public class LZViewer extends HttpServlet { 35 36 public void doGet(HttpServletRequest request, HttpServletResponse response) 37 throws ServletException, IOException 38 { 39 40 response.setHeader("Cache-control", "no-cache"); 42 response.setDateHeader("Expires", 0); 43 response.setContentType ("text/html"); 44 45 String url = (String )request.getAttribute("LZF_URL"); 46 if (url == null) { 47 throw new ServletException("No LZF_URL attribute set"); 48 } 49 String uri = (new URL (url)).getFile(); 50 String fileName = (String )request.getAttribute("LZF_FILENAME"); 51 if (fileName == null) { 52 throw new ServletException("No LZF_FILENAME attribute set"); 53 } 54 File file = new File (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 String lzx = request.getParameter("LZX"); 62 63 if (lzx != null) { 64 65 HttpSession session = request.getSession(); 66 67 File tempDir = new File (file.getParent() + File.separator); 69 tempDir.mkdirs(); 70 71 String tempFileName = "__tmp-" + session.getId() + ".lzx"; 74 String fullFileName = tempDir.getPath() + File.separator + tempFileName; 75 File tempFile = new File (fullFileName); 76 tempFile.deleteOnExit(); 77 FileWriter writer = new FileWriter (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 89 int slashIndex = uri.lastIndexOf('/'); 90 uri = uri.substring(0, slashIndex) + "/" + tempFileName; 91 fileName = fullFileName; 92 93 } else { 94 int length = (int)file.length(); 96 byte[] array = new byte[length]; 97 FileInputStream in = new FileInputStream (file); 98 in.read(array); 99 lzx = new String (array); 100 } 101 102 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 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 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, '<', "<"); 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 163 { 164 doGet(request, response); 165 } 166 } 167 | Popular Tags |