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;
36 import javax.servlet.http.*;
37 import javax.servlet.*;
38 import freemarker.template.*;
41 import mir.misc.FileExtFilter;
44 * ServletModuleFileEdit -
45 * Allows one to do a basic edit of a file in a directory specified
48 * @author $Author: mh $
49 * @version $Revision: 1.2 $ $Date: 2002/09/01 22:05:57 $
53 public class ServletModuleFileEdit extends ServletModule
56 // Singelton / Kontruktor
58 private static ServletModuleFileEdit instance =
59 new ServletModuleFileEdit();
60 public static ServletModule getInstance() { return instance; }
62 private String _dirName;
63 private String _extName;
65 private ServletModuleFileEdit() {
67 theLog = Logfile.getInstance(
68 MirConfig.getPropWithHome("ServletModule.FileEdit.Logfile"));
69 _dirName = MirConfig.getProp("ServletModule.FileEdit.FileDirectory");
70 _extName = MirConfig.getProp("ServletModule.FileEdit.ExtFilter");
73 MirConfig.getProp("ServletModule.FileEdit.ListTemplate");
74 templateObjektString =
75 MirConfig.getProp("ServletModule.FileEdit.ObjektTemplate");
76 templateConfirmString =
77 MirConfig.getProp("ServletModule.FileEdit.ConfirmTemplate");
80 public void list(HttpServletRequest req, HttpServletResponse res)
81 throws ServletModuleException
85 SimpleHash mergeData = new SimpleHash();
86 String offset = req.getParameter("offset");
87 if (offset==null || offset.equals("")) offset="0";
88 mergeData.put("offset",offset);
89 File dir = new File(_dirName);
90 System.out.println("DIRNAME: "+_dirName);
91 FileExtFilter extFilter = new FileExtFilter(_extName);
92 String[] dirEntries = dir.list(extFilter);
93 SimpleList theList = new SimpleList();
94 for ( int i = 0; i < dirEntries.length; ++i ) {
95 System.out.println(" FILE: "+dirEntries[i]);
96 theList.add(dirEntries[i]);
98 mergeData.put("filelist",theList);
101 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
103 catch (IOException e) {throw new ServletModuleException(e.toString());}
104 catch (Exception e) {throw new ServletModuleException(e.toString());}
107 public void edit(HttpServletRequest req, HttpServletResponse res)
108 throws ServletModuleException
110 String filename = req.getParameter("filename");
111 if (filename == null) throw new ServletModuleException("No filename specified");
114 File f = new File(_dirName, filename);
115 FileReader in = new FileReader(f);
116 StringWriter out = new StringWriter();
119 while ((c = in.read()) != -1)
123 SimpleHash withValues = new SimpleHash();
124 withValues.put("text", out.toString());
125 withValues.put("filename", filename);
128 deliver(req, res, withValues, null, templateObjektString);
129 } catch (Exception e) {
130 throw new ServletModuleException(e.toString());
134 public void update(HttpServletRequest req, HttpServletResponse res)
135 throws ServletModuleException
137 String filename = req.getParameter("filename");
138 if (filename == null) throw new ServletModuleException("No filename specified");
141 File f = new File(_dirName, filename);
142 StringReader in = new StringReader(req.getParameter("text"));
143 FileWriter out = new FileWriter(f);
146 while ((c = in.read()) != -1)
152 } catch (Exception e) {
153 throw new ServletModuleException(e.toString());