1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 import java.io.FileFilter ; 24 25 import org.apache.lenya.util.FileUtil; 26 27 30 public class ResourcesManager { 31 32 private Document document; 33 34 public static final String RESOURCES_PREFIX = "resources"; 35 36 public static final String RESOURCES_META_SUFFIX = ".meta"; 37 38 43 public ResourcesManager(Document document) { 44 this.document = document; 45 } 46 47 52 public String getPathFromPublication() { 53 return RESOURCES_PREFIX + "/" + getDocument().getArea() + getDocument().getId(); 54 } 55 56 61 public File getPath() { 62 File publicationPath = getDocument().getPublication().getDirectory(); 63 File resourcesPath = new File (publicationPath, getPathFromPublication().replace('/', 64 File.separatorChar)); 65 return resourcesPath; 66 } 67 68 72 public String getResourceUrl(File resource) { 73 return 74 getDocument().getPublication().getId() 75 + "/" 76 + getDocument().getArea() 77 + getDocument().getId() 78 + "/" 79 + resource.getName(); 80 } 81 82 87 public File [] getResources() { 88 89 FileFilter filter = new FileFilter () { 91 92 public boolean accept(File file) { 93 return file.isFile() && !file.getName().endsWith(RESOURCES_META_SUFFIX); 94 } 95 }; 96 97 return getFiles(filter); 98 } 99 100 104 public File [] getImageResources() { 105 final String [] IMAGE_FILE_EXTENSIONS = {".jpg", ".png", ".bmp", ".gif", ".svg"}; 106 return getFiles( new FileFilter () { 107 public boolean accept(File file) { 108 for(int i=0; i<IMAGE_FILE_EXTENSIONS.length; i++) 109 if (file.getName().toLowerCase().endsWith(IMAGE_FILE_EXTENSIONS[i])) 110 return true; 111 return false; 112 } 113 }); 114 } 115 116 121 protected File [] getFiles(FileFilter filter) { 122 File [] files = new File [0]; 123 if (getPath().isDirectory()) { 124 files = getPath().listFiles(filter); 125 } 126 127 return files; 128 } 129 130 135 public File [] getMetaFiles() { 136 FileFilter filter = new FileFilter () { 137 138 public boolean accept(File file) { 139 return file.isFile() && file.getName().endsWith(RESOURCES_META_SUFFIX); 140 } 141 }; 142 return getFiles(filter); 143 } 144 145 152 public File getMetaFile(final File resource) throws IllegalArgumentException { 153 if(resource.getName().endsWith(RESOURCES_META_SUFFIX)) 154 throw new IllegalArgumentException ("File is itself a meta file."); 155 156 final FileFilter filter = new FileFilter () { 157 public boolean accept(File file) { 158 return file.isFile() && 159 file.getName().equals(resource.getName().concat(RESOURCES_META_SUFFIX)); 160 } 161 }; 162 163 final File [] metaFiles = getFiles(filter); 164 assert(metaFiles.length == 0); 165 return metaFiles[0]; 166 } 167 168 171 public void deleteResources() { 172 173 File stopDirectory = new File (getDocument().getPublication().getDirectory(), RESOURCES_PREFIX); 174 175 File [] resources = getResources(); 176 for (int i = 0; i < resources.length; i++) { 177 resources[i].delete(); 178 FileUtil.deleteParentDirs(resources[i], stopDirectory); 179 } 180 181 File [] metas = getMetaFiles(); 182 for (int i = 0; i < metas.length; i++) { 183 metas[i].delete(); 184 FileUtil.deleteParentDirs(metas[i], stopDirectory); 185 } 186 } 187 188 public Document getDocument() { 189 return document; 190 } 191 } | Popular Tags |