KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > ResourcesManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: ResourcesManager.java 43284 2004-08-26 22:18:52Z roku $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24
25 import org.apache.lenya.util.FileUtil;
26
27 /**
28  * Manager for resources of a CMS document.
29  */

30 public class ResourcesManager {
31
32     private Document document;
33
34     public static final String JavaDoc RESOURCES_PREFIX = "resources";
35
36     public static final String JavaDoc RESOURCES_META_SUFFIX = ".meta";
37
38     /**
39      * Create a new instance of Resources.
40      *
41      * @param document the document for which the resources are managed
42      */

43     public ResourcesManager(Document document) {
44         this.document = document;
45     }
46
47     /**
48      * Get the path to the resources.
49      *
50      * @return the path to the resources
51      */

52     public String JavaDoc getPathFromPublication() {
53         return RESOURCES_PREFIX + "/" + getDocument().getArea() + getDocument().getId();
54     }
55
56     /**
57      * Get the path to the resources.
58      *
59      * @return the path to the resources
60      */

61     public File JavaDoc getPath() {
62         File JavaDoc publicationPath = getDocument().getPublication().getDirectory();
63         File JavaDoc resourcesPath = new File JavaDoc(publicationPath, getPathFromPublication().replace('/',
64                 File.separatorChar));
65         return resourcesPath;
66     }
67
68     /**
69      * Returns the path of a resource relative to the context prefix.
70      * @return The path of a resource relative to the context prefix.
71      */

72     public String JavaDoc getResourceUrl(File JavaDoc resource) {
73         return
74             getDocument().getPublication().getId()
75             + "/"
76             + getDocument().getArea()
77             + getDocument().getId()
78             + "/"
79             + resource.getName();
80     }
81     
82     /**
83      * Get all resources for the associated document.
84      *
85      * @return all resources of the associated document
86      */

87     public File JavaDoc[] getResources() {
88
89         // filter the meta files out. We only want to see the "real" resources.
90
FileFilter JavaDoc filter = new FileFilter JavaDoc() {
91
92             public boolean accept(File JavaDoc file) {
93                 return file.isFile() && !file.getName().endsWith(RESOURCES_META_SUFFIX);
94             }
95         };
96
97         return getFiles(filter);
98     }
99
100     /**
101      * Return all resources which are images.
102      * @return All image resources.
103      */

104     public File JavaDoc[] getImageResources() {
105         final String JavaDoc[] IMAGE_FILE_EXTENSIONS = {".jpg", ".png", ".bmp", ".gif", ".svg"};
106         return getFiles( new FileFilter JavaDoc() {
107                 public boolean accept(File JavaDoc 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     /**
117      * Returns the resources that are matched by a certain file filter.
118      * @param filter A file filter.
119      * @return A file array.
120      */

121     protected File JavaDoc[] getFiles(FileFilter JavaDoc filter) {
122         File JavaDoc[] files = new File JavaDoc[0];
123         if (getPath().isDirectory()) {
124             files = getPath().listFiles(filter);
125         }
126
127         return files;
128     }
129
130     /**
131      * Get the meta data for all resources for the associated document.
132      *
133      * @return all meta data files for the resources for the associated document.
134      */

135     public File JavaDoc[] getMetaFiles() {
136         FileFilter JavaDoc filter = new FileFilter JavaDoc() {
137
138             public boolean accept(File JavaDoc file) {
139                 return file.isFile() && file.getName().endsWith(RESOURCES_META_SUFFIX);
140             }
141         };
142         return getFiles(filter);
143     }
144
145     /**
146      * Returns a meta file for a given resource.
147      * @param resource A resource the meta file should be returned for.
148      * @return A file containing meta information about a resource.
149      * Returns null if no meta file was found.
150      * @throws IllegalArgumentException If resource is a meta file itself.
151      */

152     public File JavaDoc getMetaFile(final File JavaDoc resource) throws IllegalArgumentException JavaDoc {
153         if(resource.getName().endsWith(RESOURCES_META_SUFFIX))
154             throw new IllegalArgumentException JavaDoc("File is itself a meta file.");
155         
156         final FileFilter JavaDoc filter = new FileFilter JavaDoc() {
157             public boolean accept(File JavaDoc file) {
158                 return file.isFile() &&
159                     file.getName().equals(resource.getName().concat(RESOURCES_META_SUFFIX));
160             }
161         };
162         
163         final File JavaDoc[] metaFiles = getFiles(filter);
164         assert(metaFiles.length == 0);
165         return metaFiles[0];
166     }
167     
168     /**
169      * Deletes all resources.
170      */

171     public void deleteResources() {
172
173         File JavaDoc stopDirectory = new File JavaDoc(getDocument().getPublication().getDirectory(), RESOURCES_PREFIX);
174
175         File JavaDoc[] 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 JavaDoc[] 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