2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
37 import javax.servlet.http.*;
38 import javax.servlet.*;
40 import freemarker.template.*;
44 import mir.misc.FileExtFilter;
48 * ServletModuleFileEdit -
49 * Allows one to do a basic edit of a file in a directory specified
52 * @author $Author: zapata $
53 * @version $Revision: 1.3 $ $Date: 2002/11/29 13:43:42 $
57 public class ServletModuleFileEdit extends ServletModule
60 // Singelton / Kontruktor
62 private static ServletModuleFileEdit instance =
63 new ServletModuleFileEdit();
64 public static ServletModule getInstance() { return instance; }
66 private String _dirName;
67 private String _extName;
69 private ServletModuleFileEdit() {
71 logger = new LoggerWrapper("ServletModule.FileEdit");
73 _dirName = MirConfig.getProp("ServletModule.FileEdit.FileDirectory");
74 _extName = MirConfig.getProp("ServletModule.FileEdit.ExtFilter");
77 MirConfig.getProp("ServletModule.FileEdit.ListTemplate");
78 templateObjektString =
79 MirConfig.getProp("ServletModule.FileEdit.ObjektTemplate");
80 templateConfirmString =
81 MirConfig.getProp("ServletModule.FileEdit.ConfirmTemplate");
84 public void list(HttpServletRequest req, HttpServletResponse res)
85 throws ServletModuleException
89 SimpleHash mergeData = new SimpleHash();
90 String offset = req.getParameter("offset");
91 if (offset==null || offset.equals("")) offset="0";
92 mergeData.put("offset",offset);
93 File dir = new File(_dirName);
94 System.out.println("DIRNAME: "+_dirName);
95 FileExtFilter extFilter = new FileExtFilter(_extName);
96 String[] dirEntries = dir.list(extFilter);
97 SimpleList theList = new SimpleList();
98 for ( int i = 0; i < dirEntries.length; ++i ) {
99 System.out.println(" FILE: "+dirEntries[i]);
100 theList.add(dirEntries[i]);
102 mergeData.put("filelist",theList);
104 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
106 catch (IOException e) {throw new ServletModuleException(e.toString());}
107 catch (Exception e) {throw new ServletModuleException(e.toString());}
110 public void edit(HttpServletRequest req, HttpServletResponse res)
111 throws ServletModuleException
113 String filename = req.getParameter("filename");
114 if (filename == null) throw new ServletModuleException("No filename specified");
117 File f = new File(_dirName, filename);
118 FileReader in = new FileReader(f);
119 StringWriter out = new StringWriter();
122 while ((c = in.read()) != -1)
126 SimpleHash withValues = new SimpleHash();
127 withValues.put("text", out.toString());
128 withValues.put("filename", filename);
131 deliver(req, res, withValues, null, templateObjektString);
132 } catch (Exception e) {
133 throw new ServletModuleException(e.toString());
137 public void update(HttpServletRequest req, HttpServletResponse res)
138 throws ServletModuleException
140 String filename = req.getParameter("filename");
141 if (filename == null) throw new ServletModuleException("No filename specified");
144 File f = new File(_dirName, filename);
145 StringReader in = new StringReader(req.getParameter("text"));
146 FileWriter out = new FileWriter(f);
149 while ((c = in.read()) != -1)
155 } catch (Exception e) {
156 throw new ServletModuleException(e.toString());