001 package railo.commons.io.res.util; 002 003 import java.io.IOException; 004 import java.io.OutputStream; 005 import java.util.ArrayList; 006 import java.util.List; 007 008 import railo.commons.io.IOUtil; 009 import railo.commons.io.res.Resource; 010 import railo.commons.io.res.filter.ResourceFilter; 011 import railo.commons.io.res.filter.ResourceNameFilter; 012 013 /** 014 * Helper class to build resources 015 */ 016 public abstract class ResourceSupport implements Resource { 017 018 @Override 019 public void copyFrom(Resource res,boolean append) throws IOException { 020 IOUtil.copy(res, this.getOutputStream(append),true); 021 } 022 023 @Override 024 public void copyTo(Resource res,boolean append) throws IOException { 025 IOUtil.copy(this, res.getOutputStream(append),true); 026 } 027 028 @Override 029 public Resource getAbsoluteResource() { 030 return this; 031 } 032 033 @Override 034 public String getAbsolutePath() { 035 return getPath(); 036 } 037 038 @Override 039 public OutputStream getOutputStream() throws IOException { 040 return getOutputStream(false); 041 } 042 043 @Override 044 public Resource getCanonicalResource() throws IOException { 045 return this; 046 } 047 048 @Override 049 public String getCanonicalPath() throws IOException { 050 return getPath(); 051 } 052 053 @Override 054 public void moveTo(Resource dest) throws IOException { 055 ResourceUtil.moveTo(this,dest); 056 } 057 058 @Override 059 public String[] list(ResourceFilter filter) { 060 String[] files = list(); 061 if(files==null) return null; 062 List list=new ArrayList(); 063 Resource res; 064 for(int i=0;i<files.length;i++) { 065 res=getRealResource(files[i]); 066 if(filter.accept(res))list.add(files[i]); 067 } 068 return (String[]) list.toArray(new String[list.size()]); 069 } 070 071 @Override 072 public String[] list(ResourceNameFilter filter) { 073 String[] lst=list(); 074 if(lst==null) return null; 075 076 List list=new ArrayList(); 077 for(int i=0;i<lst.length;i++) { 078 if(filter.accept(getParentResource(),lst[i]))list.add(lst[i]); 079 } 080 if(list.size()==0) return new String[0]; 081 if(list.size()==lst.length) return lst; 082 return (String[]) list.toArray(new String[list.size()]); 083 } 084 085 @Override 086 public Resource[] listResources(ResourceNameFilter filter) { 087 String[] files = list(); 088 if(files==null) return null; 089 090 List list=new ArrayList(); 091 for(int i=0;i<files.length;i++) { 092 if(filter.accept(this,files[i]))list.add(getRealResource(files[i])); 093 } 094 return (Resource[]) list.toArray(new Resource[list.size()]); 095 } 096 097 @Override 098 public Resource[] listResources(ResourceFilter filter) { 099 String[] files = list(); 100 if(files==null) return null; 101 102 List list=new ArrayList(); 103 Resource res; 104 for(int i=0;i<files.length;i++) { 105 res=this.getRealResource(files[i]); 106 if(filter.accept(res))list.add(res); 107 } 108 return (Resource[]) list.toArray(new Resource[list.size()]); 109 } 110 111 @Override 112 public String getReal(String realpath) { 113 return getRealResource(realpath).getPath(); 114 } 115 116 117 @Override 118 public String[] list() { 119 Resource[] children = listResources(); 120 if(children==null) return null; 121 String[] rtn=new String[children.length]; 122 for(int i=0;i<children.length;i++) { 123 rtn[i]=children[i].getName(); 124 } 125 return rtn; 126 } 127 128 129 @Override 130 public boolean canRead() { 131 return isReadable(); 132 } 133 134 @Override 135 public boolean canWrite() { 136 return isWriteable(); 137 } 138 139 @Override 140 public boolean renameTo(Resource dest) { 141 try { 142 moveTo(dest); 143 return true; 144 } 145 catch (IOException e) { 146 return false; 147 } 148 149 } 150 151 @Override 152 public boolean createNewFile() { 153 try { 154 createFile(false); 155 return true; 156 } 157 catch (IOException e) {} 158 return false; 159 } 160 161 @Override 162 public boolean mkdir() { 163 try { 164 createDirectory(false); 165 return true; 166 } 167 catch (IOException e) {} 168 return false; 169 } 170 171 @Override 172 public boolean mkdirs() { 173 try { 174 createDirectory(true); 175 return true; 176 } 177 catch (IOException e) { 178 return false; 179 } 180 } 181 182 183 @Override 184 public boolean delete() { 185 try { 186 remove(false); 187 return true; 188 } 189 catch (IOException e) {} 190 return false; 191 } 192 193 @Override 194 public boolean isArchive() { 195 return getAttribute(Resource.ATTRIBUTE_ARCHIVE); 196 } 197 198 @Override 199 public boolean isSystem() { 200 return getAttribute(Resource.ATTRIBUTE_SYSTEM); 201 } 202 203 @Override 204 public boolean isHidden() { 205 return getAttribute(Resource.ATTRIBUTE_HIDDEN); 206 } 207 208 @Override 209 public void setArchive(boolean value) throws IOException { 210 setAttribute(ATTRIBUTE_ARCHIVE, value); 211 } 212 213 @Override 214 public void setHidden(boolean value) throws IOException { 215 setAttribute(ATTRIBUTE_HIDDEN, value); 216 } 217 218 @Override 219 public boolean setReadOnly() { 220 return setWritable(false); 221 } 222 223 @Override 224 public void setSystem(boolean value) throws IOException { 225 setAttribute(ATTRIBUTE_SYSTEM, value); 226 } 227 228 @Override 229 public boolean equals(Object obj) { 230 if(this==obj) return true; 231 if(!(obj instanceof Resource)) return false; 232 Resource other=(Resource) obj; 233 234 if(getResourceProvider()!=other.getResourceProvider()) return false; 235 236 if(getResourceProvider().isCaseSensitive()) { 237 if(getPath().equals(other.getPath())) return true; 238 return ResourceUtil.getCanonicalPathEL(this).equals(ResourceUtil.getCanonicalPathEL(other)); 239 } 240 if(getPath().equalsIgnoreCase(other.getPath())) return true; 241 return ResourceUtil.getCanonicalPathEL(this).equalsIgnoreCase(ResourceUtil.getCanonicalPathEL(other)); 242 243 } 244 245 @Override 246 public String toString() { 247 return getPath(); 248 } 249 250 @Override 251 public boolean getAttribute(short attribute) { 252 return false; 253 } 254 255 @Override 256 public void setAttribute(short attribute, boolean value) throws IOException { 257 throw new IOException("the resource ["+getPath()+"] does not support attributes"); 258 } 259 }