KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > transformer > FolderFileContainer


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.transformer;
33
34 import java.io.*;
35 import java.util.*;
36
37 /**
38  * @author Taras Puchko
39  */

40 class FolderFileContainer extends FileContainer {
41
42     private Map<String JavaDoc, FolderFileEntry> entries;
43
44     public FolderFileContainer(File location) {
45         super(location);
46     }
47
48     public FolderFileContainer(File location, List<String JavaDoc> fileNames) {
49         super(location);
50         entries = new LinkedHashMap<String JavaDoc, FolderFileEntry>();
51         for (String JavaDoc fileName : fileNames) {
52             String JavaDoc name = fileName.replace(File.separatorChar, '/');
53             entries.put(name, new FolderFileEntry(name, new File(location, name)));
54         }
55     }
56
57     public Collection<? extends FileEntry> getEntries() {
58         if (entries == null) {
59             entries = new LinkedHashMap<String JavaDoc, FolderFileEntry>();
60             scanFolder(location, location.getPath().length() + 1);
61         }
62         return new ArrayList<FolderFileEntry>(entries.values());
63     }
64
65     public void removeEntry(String JavaDoc name) {
66         if (entries != null) {
67             FolderFileEntry entry = entries.remove(name);
68             if (entry != null) entry.file.delete();
69         } else {
70             new File(location, name).delete();
71         }
72     }
73
74     private void scanFolder(File folder, int prefixLength) {
75         for (File file : folder.listFiles()) {
76             String JavaDoc name = file.getPath().substring(prefixLength).replace(File.separatorChar, '/');
77             if (file.isDirectory()) {
78                 scanFolder(file, prefixLength);
79             } else {
80                 entries.put(name, new FolderFileEntry(name, file));
81             }
82         }
83     }
84
85     public void putEntry(String JavaDoc name, byte[] contents) {
86         File file = new File(location, name);
87         if (entries == null) entries = new LinkedHashMap<String JavaDoc, FolderFileEntry>();
88         entries.put(name, new FolderFileEntry(name, file));
89         file.getParentFile().mkdirs();
90         try {
91             FileOutputStream stream = new FileOutputStream(file);
92             try {
93                 stream.write(contents);
94             } finally {
95                 stream.close();
96             }
97         } catch (IOException e) {
98             throw new RuntimeException JavaDoc(e);
99         }
100     }
101
102     public void flush(SystemLogger logger) {
103         //no-op
104
}
105
106     private static class FolderFileEntry extends FileEntry {
107
108         private File file;
109
110         public FolderFileEntry(String JavaDoc name, File file) {
111             super(name);
112             this.file = file;
113         }
114
115         public byte[] getContent() {
116             try {
117                 FileInputStream stream = new FileInputStream(file);
118                 try {
119                     return readFully(stream, (int) file.length());
120                 } finally {
121                     stream.close();
122                 }
123             } catch (IOException e) {
124                 throw new RuntimeException JavaDoc(e);
125             }
126         }
127     }
128 }
129
Popular Tags