001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.res.util;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.util.ArrayList;
024import java.util.List;
025
026import lucee.commons.io.IOUtil;
027import lucee.commons.io.res.Resource;
028import lucee.commons.io.res.filter.ResourceFilter;
029import lucee.commons.io.res.filter.ResourceNameFilter;
030
031/**
032 * Helper class to build resources
033 */
034public abstract class ResourceSupport implements Resource {
035
036        @Override
037        public void copyFrom(Resource res,boolean append) throws IOException {
038                IOUtil.copy(res, this.getOutputStream(append),true);
039        }
040
041        @Override
042        public void copyTo(Resource res,boolean append) throws IOException {
043                IOUtil.copy(this, res.getOutputStream(append),true);
044        }
045
046        @Override
047        public Resource getAbsoluteResource() {
048                return this;
049        }
050
051        @Override
052        public String getAbsolutePath() {
053                return getPath();
054        }
055
056        @Override
057        public OutputStream getOutputStream() throws IOException {
058                return getOutputStream(false);
059        }
060
061        @Override
062        public Resource getCanonicalResource() throws IOException {
063                return this;
064        }
065
066        @Override
067        public String getCanonicalPath() throws IOException {
068                return getPath();
069        }
070
071        @Override
072        public void moveTo(Resource dest) throws IOException {
073                ResourceUtil.moveTo(this, dest,false);
074        }
075        
076        @Override
077        public String[] list(ResourceFilter filter) {
078                String[] files = list();
079                if(files==null) return null;
080                List list=new ArrayList();
081                Resource res;
082                for(int i=0;i<files.length;i++) {
083                        res=getRealResource(files[i]);
084                        if(filter.accept(res))list.add(files[i]);
085                }
086                return (String[]) list.toArray(new String[list.size()]);
087        }
088
089        @Override
090        public String[] list(ResourceNameFilter filter) {
091                String[] lst=list();
092                if(lst==null) return null;
093                
094                List list=new ArrayList();
095                for(int i=0;i<lst.length;i++) {
096                        if(filter.accept(getParentResource(),lst[i]))list.add(lst[i]);
097                }
098                if(list.size()==0) return new String[0];
099                if(list.size()==lst.length) return lst;
100                return (String[]) list.toArray(new String[list.size()]);
101        }
102
103        @Override
104        public Resource[] listResources(ResourceNameFilter filter) {
105                String[] files = list();
106                if(files==null) return null;
107                
108                List list=new ArrayList();
109                for(int i=0;i<files.length;i++) {
110                        if(filter.accept(this,files[i]))list.add(getRealResource(files[i]));
111                }
112                return (Resource[]) list.toArray(new Resource[list.size()]);
113        }
114
115        @Override
116        public Resource[] listResources(ResourceFilter filter) {
117                String[] files = list();
118                if(files==null) return null;
119                
120                List list=new ArrayList();
121                Resource res;
122                for(int i=0;i<files.length;i++) {
123                        res=this.getRealResource(files[i]);
124                        if(filter.accept(res))list.add(res);
125                }
126                return (Resource[]) list.toArray(new Resource[list.size()]);
127        }
128
129        @Override
130        public String getReal(String relpath) {
131                return getRealResource(relpath).getPath();
132        }
133        
134
135        @Override
136        public String[] list() {
137                Resource[] children = listResources();
138                if(children==null) return null;
139                String[] rtn=new String[children.length];
140                for(int i=0;i<children.length;i++) {
141                        rtn[i]=children[i].getName();
142                }
143                return rtn;
144        }
145        
146
147        @Override
148        public boolean canRead() {
149                return isReadable();
150        }
151
152        @Override
153        public boolean canWrite() {
154                return isWriteable();
155        }
156
157        @Override
158        public boolean renameTo(Resource dest) {
159                try {
160                        moveTo(dest);
161                        return true;
162                }
163                catch (IOException e) {
164                        return false;
165                }
166                
167        }
168
169        @Override
170        public boolean createNewFile() {
171                try {
172                        createFile(false);
173                        return true;
174                } 
175                catch (IOException e) {}
176                return false;
177        }
178
179        @Override
180        public boolean mkdir() {
181                try {
182                        createDirectory(false);
183                        return true;
184                }
185                catch (IOException e) {}
186                return false;
187        }
188
189        @Override
190        public boolean mkdirs() {
191                try {
192                        createDirectory(true);
193                        return true;
194                }
195                catch (IOException e) {
196                        return false;
197                }
198        }
199        
200
201        @Override
202        public boolean delete() {
203                try {
204                        remove(false);
205                        return true;
206                } 
207                catch (IOException e) {}
208                return false;
209        }
210
211        @Override
212        public boolean isArchive() {
213                return getAttribute(Resource.ATTRIBUTE_ARCHIVE);
214        }
215
216        @Override
217        public boolean isSystem() {
218                return getAttribute(Resource.ATTRIBUTE_SYSTEM);
219        }
220
221        @Override
222        public boolean isHidden() {
223                return getAttribute(Resource.ATTRIBUTE_HIDDEN);
224        }
225
226        @Override
227        public void setArchive(boolean value) throws IOException {
228                setAttribute(ATTRIBUTE_ARCHIVE, value);
229        }
230
231        @Override
232        public void setHidden(boolean value) throws IOException {
233                setAttribute(ATTRIBUTE_HIDDEN, value);
234        }
235
236        @Override
237        public boolean setReadOnly() {
238                return setWritable(false);
239        }
240
241        @Override
242        public void setSystem(boolean value) throws IOException {
243                setAttribute(ATTRIBUTE_SYSTEM, value);
244        }
245        
246        @Override
247        public boolean equals(Object obj) {
248                if(this==obj) return true;
249                if(!(obj instanceof Resource)) return false;
250                Resource other=(Resource) obj;
251                
252                if(getResourceProvider()!=other.getResourceProvider()) return false;
253                
254                if(getResourceProvider().isCaseSensitive()) {
255                        if(getPath().equals(other.getPath())) return true;
256                        return ResourceUtil.getCanonicalPathEL(this).equals(ResourceUtil.getCanonicalPathEL(other));
257                }
258                if(getPath().equalsIgnoreCase(other.getPath())) return true;
259                return ResourceUtil.getCanonicalPathEL(this).equalsIgnoreCase(ResourceUtil.getCanonicalPathEL(other));
260                
261        }
262        
263        @Override
264        public String toString() {
265                return getPath();
266        }
267
268        @Override
269        public boolean getAttribute(short attribute) {
270                return false;
271        }
272
273        @Override
274        public void setAttribute(short attribute, boolean value) throws IOException {
275                throw new IOException("the resource ["+getPath()+"] does not support attributes");
276        }
277}