KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > Repository


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components;
17
18 import java.io.File JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26
27 import org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.servlet.multipart.Part;
30
31 /**
32  * @author stefano
33  */

34 public class Repository {
35     
36     public static final String JavaDoc FILE_NAME = "document";
37     
38     private static Repository instance;
39     
40     private Repository() {
41         // do nothing;
42
}
43     
44     public static Repository getInstance() {
45         if (instance == null) {
46             instance = new Repository();
47         }
48         return instance;
49     }
50
51     private static File JavaDoc getDir(String JavaDoc dirName) {
52         File JavaDoc dir = new File JavaDoc(dirName);
53         if (!dir.isDirectory()) throw new RuntimeException JavaDoc("'"+ dirName + "' is not a directory!");
54         return dir;
55     }
56
57     public static void save(Request request, String JavaDoc dirName) throws Exception JavaDoc {
58         File JavaDoc dir = getDir(dirName);
59         
60         Enumeration JavaDoc params = request.getParameterNames();
61         while (params.hasMoreElements()) {
62             String JavaDoc name = (String JavaDoc) params.nextElement();
63             if (name.indexOf("..") > -1) throw new Exception JavaDoc("We are under attack!!");
64 //System.out.println("[param] " + name);
65
if (name.startsWith("save:")) {
66                 Part part = (Part) request.get(name);
67                 String JavaDoc code = name.substring(5);
68                 File JavaDoc file = new File JavaDoc(dir, code);
69                 save(part,file);
70             } else if (name.startsWith("delete:")) {
71                 String JavaDoc value = request.getParameter(name);
72                 if (value.length() > 0) {
73                     String JavaDoc code = name.substring(7);
74                     File JavaDoc file = new File JavaDoc(dir, code);
75 //System.out.println("[delete] " + file);
76
remove(file);
77                 }
78             }
79         }
80     }
81
82     public static void fomSave(FOM_Cocoon cocoon, String JavaDoc dirName) throws Exception JavaDoc {
83         save(cocoon.getRequest(), dirName);
84     }
85     
86     public static void save(Request request, String JavaDoc param, String JavaDoc file) throws Exception JavaDoc {
87         Part part = (Part) request.get(param);
88         save(part,new File JavaDoc(file));
89     }
90     
91     public static void save(Part part, File JavaDoc file) throws Exception JavaDoc {
92 //System.out.println("[upload] " + part.getFileName() + " -> " + file);
93
InputStream JavaDoc in = null;
94         FileOutputStream JavaDoc out = null;
95         try {
96             in = part.getInputStream();
97             out = new FileOutputStream JavaDoc(file);
98             copy(in, out);
99         } finally {
100             if (out != null) {
101                 out.close();
102             }
103             if (in != null) {
104                 in.close();
105             }
106         }
107     }
108     
109     public static OutputStream JavaDoc getOutputStream(String JavaDoc dir) throws IOException JavaDoc {
110         String JavaDoc mainFile = dir + "/" + FILE_NAME + ".xml";
111         String JavaDoc versionedFile = dir + "/" + FILE_NAME + "." + getVersionID(dir) + ".xml";
112         copy(mainFile, versionedFile);
113         return new FileOutputStream JavaDoc(mainFile);
114     }
115
116     public static void revertFrom(String JavaDoc dir, int version) throws IOException JavaDoc {
117         String JavaDoc mainFile = dir + "/" + FILE_NAME + ".xml";
118         String JavaDoc versionedFile = dir + "/" + FILE_NAME + "." + version + ".xml";
119         copy(versionedFile,mainFile);
120     }
121     
122     /**
123      * Returns the highest version id of the files included in the given
124      * directory.
125      */

126     public static int getVersionID(String JavaDoc dirName) {
127         File JavaDoc dir = getDir(dirName);
128         
129         File JavaDoc[] content = dir.listFiles();
130         int id = 0;
131         for (int i = 0; i < content.length; i++) {
132             if (content[i].isFile()) {
133                 try {
134                     int localid = getVersion(content[i].getName());
135                     if (localid > id) id = localid;
136                 } catch (Exception JavaDoc e) {}
137             }
138         }
139
140         return ++id;
141     }
142
143     public static Object JavaDoc[] getVersions(String JavaDoc dirName) {
144         File JavaDoc dir = getDir(dirName);
145         ArrayList JavaDoc versions = new ArrayList JavaDoc();
146
147         File JavaDoc[] content = dir.listFiles();
148         for (int i = 0; i < content.length; i++) {
149             if (content[i].isFile()) {
150                 try {
151                     int version = getVersion(content[i].getName());
152                     if (version > 0) {
153                         versions.add(new Integer JavaDoc(version));
154                     }
155                 } catch (Exception JavaDoc e) {}
156             }
157         }
158
159         return versions.toArray();
160     }
161         
162     /**
163      * Return the version encoded into the name as a numeric subextension of
164      * an .xml extension.
165      *
166      * Example:
167      * anything.123.xml -> 123
168      * document.3.xml -> 3
169      * document.0.xml -> 0
170      * document.xml -> -1
171      * image.0.jpg -> -1
172      */

173     private static int getVersion(String JavaDoc name) {
174         int extIndex = name.lastIndexOf(".xml");
175         if (extIndex > 0) {
176             String JavaDoc nameWithoutExtension = name.substring(0,extIndex);
177             int dotIndex = nameWithoutExtension.lastIndexOf('.');
178             if (dotIndex > 0) {
179                 String JavaDoc localidString = nameWithoutExtension.substring(dotIndex + 1);
180                 return Integer.parseInt(localidString);
181             }
182         }
183         return -1;
184     }
185     
186     public static int getID(String JavaDoc dirName) {
187         File JavaDoc dir = getDir(dirName);
188
189         File JavaDoc[] content = dir.listFiles();
190         int id = 0;
191         for (int i = 0; i < content.length; i++) {
192             if (content[i].isDirectory()) {
193                 try {
194                     String JavaDoc name = content[i].getName();
195                     int localid = Integer.parseInt(name);
196                     if (localid > id) id = localid;
197                 } catch (Exception JavaDoc e) {}
198             }
199         }
200
201         return ++id;
202     }
203     
204     public static boolean remove(String JavaDoc fileName) {
205         return remove(new File JavaDoc(fileName));
206     }
207     
208     public static boolean remove(File JavaDoc file) {
209         boolean success = true;
210         
211         if (file.isDirectory()) {
212             File JavaDoc[] content = file.listFiles();
213             for (int i = 0; i < content.length; i++) {
214                 success = remove(content[i]);
215             }
216             
217         }
218         
219 //System.out.println("[delete] " + file);
220
success = file.delete();
221         
222         return success;
223     }
224     
225     public static void copy(String JavaDoc from, String JavaDoc to) throws IOException JavaDoc {
226         copy(new File JavaDoc(from),new File JavaDoc(to));
227     }
228
229     public static void copy(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
230
231 //System.out.println("[copy] " + from + " -> " + to);
232

233         if (!from.exists()) {
234             throw new IOException JavaDoc("Cannot find source file/folder");
235         }
236         
237         if (from.isDirectory()) {
238             to.mkdirs();
239             File JavaDoc[] content = from.listFiles();
240             for (int i = 0; i < content.length; i++) {
241                 File JavaDoc src = content[i];
242                 copy(src,new File JavaDoc(to, src.getName()));
243             }
244         } else {
245             to.createNewFile();
246             FileInputStream JavaDoc in = null;
247             FileOutputStream JavaDoc out = null;
248             try {
249                 in = new FileInputStream JavaDoc(from);
250                 out = new FileOutputStream JavaDoc(to);
251                 copy(in,out);
252             } finally {
253                 if (out != null) out.close();
254                 if (in != null) in.close();
255             }
256         }
257     }
258     
259     public static void copy(InputStream JavaDoc from, OutputStream JavaDoc to) throws IOException JavaDoc {
260         byte[] buffer = new byte[64 * 1024];
261         int count = 0;
262         do {
263             to.write(buffer, 0, count);
264             count = from.read(buffer, 0, buffer.length);
265         } while (count != -1);
266     }
267 }
268
Popular Tags