gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / FilesResourcesStorage.java @ 2142
History | View | Annotate | Download (5.39 KB)
1 | 1893 | jjdelcerro | 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 | 1991 | jjdelcerro | import java.util.ArrayList; |
12 | import java.util.List; |
||
13 | 1893 | jjdelcerro | import org.apache.commons.io.IOUtils; |
14 | 2142 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
15 | 1893 | jjdelcerro | import org.gvsig.tools.util.HasAFile; |
16 | |||
17 | /**
|
||
18 | *
|
||
19 | * @author jjdelcerro
|
||
20 | */
|
||
21 | public class FilesResourcesStorage extends AbstractResourcesStorage { |
||
22 | |||
23 | 2107 | jjdelcerro | public static class FileResource |
24 | extends AbstractResourcesStorage.AbstractResource
|
||
25 | implements ResourcesStorage.Resource, HasAFile
|
||
26 | { |
||
27 | 1893 | jjdelcerro | |
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 | 2107 | jjdelcerro | public FileResource(String name, File resource) { |
64 | super(name);
|
||
65 | 1893 | jjdelcerro | this.resource = resource;
|
66 | } |
||
67 | 2107 | jjdelcerro | |
68 | public String getName() { |
||
69 | return name;
|
||
70 | } |
||
71 | 1893 | jjdelcerro | |
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 | 1991 | jjdelcerro | private final List<String> resourcesPaths; |
130 | 1893 | jjdelcerro | |
131 | public FilesResourcesStorage(String fullPathName) { |
||
132 | 1991 | jjdelcerro | this.resourcesPaths = new ArrayList<>(); |
133 | this.resourcesPaths.add(fullPathName);
|
||
134 | 1893 | jjdelcerro | } |
135 | |||
136 | 2104 | jjdelcerro | public FilesResourcesStorage(List<String> fullPathNames) { |
137 | this.resourcesPaths = new ArrayList<>(); |
||
138 | this.resourcesPaths.addAll(fullPathNames);
|
||
139 | } |
||
140 | |||
141 | 1893 | jjdelcerro | @Override
|
142 | public Resource getResource(String resourceName) { |
||
143 | 1991 | jjdelcerro | File fres;
|
144 | for (String pathName : resourcesPaths) { |
||
145 | File folder = new File(pathName); |
||
146 | if( folder.isDirectory() ) {
|
||
147 | fres = new File(folder,resourceName); |
||
148 | } else {
|
||
149 | fres = new File(pathName + this.getSeparator() + resourceName); |
||
150 | } |
||
151 | if( fres.exists() ) {
|
||
152 | 2107 | jjdelcerro | return new FileResource(resourceName, fres); |
153 | 1991 | jjdelcerro | } |
154 | 1961 | jjdelcerro | } |
155 | 1991 | jjdelcerro | String pathName = this.resourcesPaths.get(0); |
156 | fres = new File(pathName + this.getSeparator() + resourceName); |
||
157 | 2107 | jjdelcerro | return new FileResource(resourceName, fres); |
158 | 1893 | jjdelcerro | } |
159 | |||
160 | 1991 | jjdelcerro | public List<String> getPaths() { |
161 | return this.resourcesPaths; |
||
162 | } |
||
163 | 2142 | jjdelcerro | |
164 | @Override
|
||
165 | public List<String> getResourceNames() { |
||
166 | List<String> names = new ArrayList<>(); |
||
167 | |||
168 | for (String pathName : resourcesPaths) { |
||
169 | File folder = new File(pathName); |
||
170 | if( folder.isDirectory() ) {
|
||
171 | for (File f: folder.listFiles()) { |
||
172 | if( f.isFile() ) {
|
||
173 | names.add(f.getName()); |
||
174 | } |
||
175 | } |
||
176 | } else {
|
||
177 | File parent = folder.getParentFile();
|
||
178 | String prefix = pathName + this.getSeparator(); |
||
179 | int prefix_len = prefix.length();
|
||
180 | for (File f: parent.listFiles()) { |
||
181 | if( f.isFile() ) {
|
||
182 | String fname = f.getName();
|
||
183 | if( StringUtils.startsWithIgnoreCase(fname, prefix) ) {
|
||
184 | names.add(fname.substring(prefix_len)); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | return names;
|
||
191 | } |
||
192 | |||
193 | 1893 | jjdelcerro | } |