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 /** 019 * @see railo.commons.io.res.Resource#copyFrom(railo.commons.io.res.Resource,boolean) 020 */ 021 public void copyFrom(Resource res,boolean append) throws IOException { 022 IOUtil.copy(res, this.getOutputStream(append),true); 023 } 024 025 /** 026 * @see railo.commons.io.res.Resource#copyTo(railo.commons.io.res.Resource,boolean) 027 */ 028 public void copyTo(Resource res,boolean append) throws IOException { 029 IOUtil.copy(this, res.getOutputStream(append),true); 030 } 031 032 /** 033 * @see res.Resource#getAbsoluteResource() 034 */ 035 public Resource getAbsoluteResource() { 036 return this; 037 } 038 039 /** 040 * @see res.Resource#getAbsolutePath() 041 */ 042 public String getAbsolutePath() { 043 return getPath(); 044 } 045 046 /** 047 * @see res.Resource#getOutputStream() 048 */ 049 public OutputStream getOutputStream() throws IOException { 050 return getOutputStream(false); 051 } 052 053 /** 054 * @see res.Resource#getCanonicalResource() 055 */ 056 public Resource getCanonicalResource() throws IOException { 057 return this; 058 } 059 060 /** 061 * @see res.Resource#getCanonicalPath() 062 */ 063 public String getCanonicalPath() throws IOException { 064 return getPath(); 065 } 066 067 /** 068 * @see res.Resource#moveTo(res.Resource) 069 */ 070 public void moveTo(Resource dest) throws IOException { 071 ResourceUtil.moveTo(this,dest); 072 } 073 074 /** 075 * @see res.Resource#list(res.filter.ResourceFilter) 076 */ 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 /** 090 * @see res.Resource#list(res.filter.ResourceNameFilter) 091 */ 092 public String[] list(ResourceNameFilter filter) { 093 String[] lst=list(); 094 if(lst==null) return null; 095 096 List list=new ArrayList(); 097 for(int i=0;i<lst.length;i++) { 098 if(filter.accept(getParentResource(),lst[i]))list.add(lst[i]); 099 } 100 if(list.size()==0) return new String[0]; 101 if(list.size()==lst.length) return lst; 102 return (String[]) list.toArray(new String[list.size()]); 103 } 104 105 /** 106 * @see res.Resource#listResources(res.filter.ResourceNameFilter) 107 */ 108 public Resource[] listResources(ResourceNameFilter filter) { 109 String[] files = list(); 110 if(files==null) return null; 111 112 List list=new ArrayList(); 113 for(int i=0;i<files.length;i++) { 114 if(filter.accept(this,files[i]))list.add(getRealResource(files[i])); 115 } 116 return (Resource[]) list.toArray(new Resource[list.size()]); 117 } 118 119 /** 120 * @see res.Resource#listResources(res.filter.ResourceFilter) 121 */ 122 public Resource[] listResources(ResourceFilter filter) { 123 String[] files = list(); 124 if(files==null) return null; 125 126 List list=new ArrayList(); 127 Resource res; 128 for(int i=0;i<files.length;i++) { 129 res=this.getRealResource(files[i]); 130 if(filter.accept(res))list.add(res); 131 } 132 return (Resource[]) list.toArray(new Resource[list.size()]); 133 } 134 135 /** 136 * @see res.Resource#getReal(java.lang.String) 137 */ 138 public String getReal(String realpath) { 139 return getRealResource(realpath).getPath(); 140 } 141 142 143 /** 144 * @see res.Resource#list() 145 */ 146 public String[] list() { 147 Resource[] children = listResources(); 148 if(children==null) return null; 149 String[] rtn=new String[children.length]; 150 for(int i=0;i<children.length;i++) { 151 rtn[i]=children[i].getName(); 152 } 153 return rtn; 154 } 155 156 157 /** 158 * @see res.Resource#canRead() 159 */ 160 public boolean canRead() { 161 return isReadable(); 162 } 163 164 /** 165 * @see res.Resource#canWrite() 166 */ 167 public boolean canWrite() { 168 return isWriteable(); 169 } 170 171 /** 172 * @see res.Resource#renameTo(res.Resource) 173 */ 174 public boolean renameTo(Resource dest) { 175 try { 176 moveTo(dest); 177 return true; 178 } 179 catch (IOException e) { 180 return false; 181 } 182 183 } 184 185 /** 186 * @see res.Resource#createNewFile() 187 */ 188 public boolean createNewFile() { 189 try { 190 createFile(false); 191 return true; 192 } 193 catch (IOException e) {} 194 return false; 195 } 196 197 /** 198 * @see res.Resource#mkdir() 199 */ 200 public boolean mkdir() { 201 try { 202 createDirectory(false); 203 return true; 204 } 205 catch (IOException e) {} 206 return false; 207 } 208 209 /** 210 * @see res.Resource#mkdirs() 211 */ 212 public boolean mkdirs() { 213 try { 214 createDirectory(true); 215 return true; 216 } 217 catch (IOException e) { 218 return false; 219 } 220 } 221 222 223 /** 224 * 225 * @see railo.commons.io.res.Resource#delete() 226 */ 227 public boolean delete() { 228 try { 229 remove(false); 230 return true; 231 } 232 catch (IOException e) {} 233 return false; 234 } 235 236 /** 237 * 238 * @see railo.commons.io.res.Resource#isArchive() 239 */ 240 public boolean isArchive() { 241 return getAttribute(Resource.ATTRIBUTE_ARCHIVE); 242 } 243 244 /** 245 * 246 * @see railo.commons.io.res.Resource#isSystem() 247 */ 248 public boolean isSystem() { 249 return getAttribute(Resource.ATTRIBUTE_SYSTEM); 250 } 251 252 /** 253 * 254 * @see railo.commons.io.res.Resource#isHidden() 255 */ 256 public boolean isHidden() { 257 return getAttribute(Resource.ATTRIBUTE_HIDDEN); 258 } 259 260 /** 261 * 262 * @see railo.commons.io.res.Resource#setArchive(boolean) 263 */ 264 public void setArchive(boolean value) throws IOException { 265 setAttribute(ATTRIBUTE_ARCHIVE, value); 266 } 267 268 /** 269 * 270 * @see railo.commons.io.res.Resource#setHidden(boolean) 271 */ 272 public void setHidden(boolean value) throws IOException { 273 setAttribute(ATTRIBUTE_HIDDEN, value); 274 } 275 276 /** 277 * 278 * @see railo.commons.io.res.Resource#setReadOnly() 279 */ 280 public boolean setReadOnly() { 281 return setWritable(false); 282 } 283 284 /** 285 * 286 * @see railo.commons.io.res.Resource#setSystem(boolean) 287 */ 288 public void setSystem(boolean value) throws IOException { 289 setAttribute(ATTRIBUTE_SYSTEM, value); 290 } 291 292 /** 293 * 294 * @see java.lang.Object#equals(java.lang.Object) 295 */ 296 public boolean equals(Object obj) { 297 if(this==obj) return true; 298 if(!(obj instanceof Resource)) return false; 299 Resource other=(Resource) obj; 300 301 if(getResourceProvider()!=other.getResourceProvider()) return false; 302 303 if(getResourceProvider().isCaseSensitive()) { 304 if(getPath().equals(other.getPath())) return true; 305 return ResourceUtil.getCanonicalPathEL(this).equals(ResourceUtil.getCanonicalPathEL(other)); 306 } 307 if(getPath().equalsIgnoreCase(other.getPath())) return true; 308 return ResourceUtil.getCanonicalPathEL(this).equalsIgnoreCase(ResourceUtil.getCanonicalPathEL(other)); 309 310 } 311 312 /** 313 * 314 * @see java.lang.Object#toString() 315 */ 316 public String toString() { 317 return getPath(); 318 } 319 320 /** 321 * 322 * @see railo.commons.io.res.Resource#getAttribute(short) 323 */ 324 public boolean getAttribute(short attribute) { 325 return false; 326 } 327 328 /** 329 * 330 * @see railo.commons.io.res.Resource#setAttribute(short, boolean) 331 */ 332 public void setAttribute(short attribute, boolean value) throws IOException { 333 throw new IOException("the resource ["+getPath()+"] does not support attributes"); 334 } 335 }