Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / FilesResourcesStorage.java @ 2195

History | View | Annotate | Download (5.96 KB)

1
package org.gvsig.tools.resourcesstorage;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.net.MalformedURLException;
10
import java.net.URL;
11
import java.util.ArrayList;
12
import java.util.List;
13
import org.apache.commons.io.FileUtils;
14
import org.apache.commons.io.IOUtils;
15
import org.apache.commons.lang3.StringUtils;
16
import org.gvsig.tools.util.HasAFile;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
public class FilesResourcesStorage extends AbstractResourcesStorage {
23

    
24
    public static class FileResource
25
            extends AbstractResourcesStorage.AbstractResource
26
            implements ResourcesStorage.Resource, HasAFile {
27

    
28
        private class ResourceInputStream extends InputStream {
29

    
30
            @Override
31
            public int read() throws IOException {
32
                return in.read();
33
            }
34

    
35
            @Override
36
            public void close() throws IOException {
37
                FileResource.this.close();
38
            }
39
        }
40

    
41
        private class ResourceOutputStream extends OutputStream {
42

    
43
            @Override
44
            public void write(int b) throws IOException {
45
                out.write(b);
46
            }
47

    
48
            @Override
49
            public void flush() throws IOException {
50
                out.flush();
51
            }
52

    
53
            @Override
54
            public void close() throws IOException {
55
                FileResource.this.close();
56
            }
57
        }
58

    
59
        private final File resource;
60
        private FileInputStream in;
61
        private FileOutputStream out;
62

    
63
        public FileResource(String name, File resource) {
64
            super(name);
65
            this.resource = resource;
66
        }
67

    
68
        public String getName() {
69
            return name;
70
        }
71

    
72
        @Override
73
        public boolean isReadOnly() {
74
            return false;
75
        }
76

    
77
        @Override
78
        public URL getURL() {
79
            try {
80
                return this.resource.toURI().toURL();
81
            } catch (MalformedURLException ex) {
82
                return null;
83
            }
84
        }
85

    
86
        @Override
87
        public File getFile() {
88
            return this.resource;
89
        }
90

    
91
        @Override
92
        public boolean exists() {
93
            return this.resource.exists();
94
        }
95

    
96
        @Override
97
        public InputStream asInputStream() throws java.io.FileNotFoundException {
98
            if (this.out != null || this.in != null) {
99
                throw new IllegalStateException("Input already open");
100
            }
101
            this.in = new FileInputStream(this.resource);
102
            return new ResourceInputStream();
103
        }
104

    
105
        @Override
106
        public OutputStream asOutputStream() throws java.io.FileNotFoundException {
107
            if (this.out != null || this.in != null) {
108
                throw new IllegalStateException("Input already open");
109
            }
110
            this.out = new FileOutputStream(this.resource);
111
            return new ResourceOutputStream();
112
        }
113

    
114
        @Override
115
        public void close() {
116
            IOUtils.closeQuietly(out);
117
            IOUtils.closeQuietly(in);
118
            this.in = null;
119
            this.out = null;
120
        }
121

    
122
        @Override
123
        public void setFile(File file) {
124
            throw new UnsupportedOperationException("Not supported yet.");
125
        }
126

    
127
    }
128

    
129
    private final List<String> resourcesPaths;
130

    
131
    public FilesResourcesStorage(String fullPathName) {
132
        this.resourcesPaths = new ArrayList<>();
133
        this.resourcesPaths.add(fullPathName);
134
    }
135

    
136
    public FilesResourcesStorage(List<String> fullPathNames) {
137
        this.resourcesPaths = new ArrayList<>();
138
        this.resourcesPaths.addAll(fullPathNames);
139
    }
140

    
141
    @Override
142
    public Resource getResource(String resourceName) {
143
        return new FileResource(resourceName, getResourceFile(resourceName));
144
    }
145

    
146
    public List<String> getPaths() {
147
        return this.resourcesPaths;
148
    }
149

    
150
    @Override
151
    public List<String> getResourceNames() {
152
        List<String> names = new ArrayList<>();
153

    
154
        for (String pathName : resourcesPaths) {
155
            File folder = new File(pathName);
156
            if (folder.isDirectory()) {
157
                for (File f : folder.listFiles()) {
158
                    if (f.isFile()) {
159
                        names.add(f.getName());
160
                    }
161
                }
162
            } else {
163
                File parent = folder.getParentFile();
164
                String prefix = pathName + this.getSeparator();
165
                int prefix_len = prefix.length();
166
                for (File f : parent.listFiles()) {
167
                    if (f.isFile()) {
168
                        String fname = f.getName();
169
                        if (StringUtils.startsWithIgnoreCase(fname, prefix)) {
170
                            names.add(fname.substring(prefix_len));
171
                        }
172
                    }
173
                }
174
            }
175
        }
176
        return names;
177
    }
178

    
179
    @Override
180
    public boolean remove(String resourceName) {
181
        File fileToRemove = this.getResourceFile(resourceName);
182
        return FileUtils.deleteQuietly(fileToRemove);
183
    }
184

    
185
    @Override
186
    public boolean allowRemove() {
187
        return true;
188
    }
189

    
190
    private File getResourceFile(String resourceName) {
191
        File fres;
192
        for (String pathName : resourcesPaths) {
193
            File folder = new File(pathName);
194
            if (folder.isDirectory()) {
195
                fres = new File(folder, resourceName);
196
            } else {
197
                fres = new File(pathName + this.getSeparator() + resourceName);
198
            }
199
            if (fres.exists()) {
200
                return fres;
201
            }
202
        }
203
        String pathName = this.resourcesPaths.get(0);
204
        File folder = new File(pathName);
205
        if (folder.isDirectory()) {
206
            fres = new File(folder, resourceName);
207
        } else {
208
            fres = new File(pathName + this.getSeparator() + resourceName);
209
        }
210
        return fres;
211
    }
212

    
213
}