001    package railo.runtime.util;
002    
003    import java.io.File;
004    import java.io.FileNotFoundException;
005    import java.io.IOException;
006    
007    import railo.commons.io.res.Resource;
008    import railo.commons.io.res.filter.ResourceFilter;
009    import railo.commons.io.res.filter.ResourceNameFilter;
010    import railo.runtime.PageContext;
011    import railo.runtime.exp.PageException;
012    
013    public interface ResourceUtil {
014            
015                    
016                    /**
017                 * Field <code>FILE_SEPERATOR</code>
018                 */
019                public static final char FILE_SEPERATOR=File.separatorChar; 
020                /**
021                 * Field <code>FILE_ANTI_SEPERATOR</code>
022                 */
023                public static final char FILE_ANTI_SEPERATOR=(FILE_SEPERATOR=='/')?'\\':'/';
024                
025                /**
026                 * Field <code>TYPE_DIR</code>
027                 */
028                public static final short TYPE_DIR=0;
029                
030                /**
031                 * Field <code>TYPE_FILE</code>
032                 */
033                public static final short TYPE_FILE=1;
034    
035                /**
036                 * Field <code>LEVEL_FILE</code>
037                 */
038                public static final short LEVEL_FILE=0;
039                /**
040                 * Field <code>LEVEL_PARENT_FILE</code>
041                 */
042                public static final short LEVEL_PARENT_FILE=1;
043                /**
044                 * Field <code>LEVEL_GRAND_PARENT_FILE</code>
045                 */
046                public static final short LEVEL_GRAND_PARENT_FILE=2;
047                    
048                /**
049                 * cast a String (argument destination) to a File Object, 
050                 * if destination is not a absolute, file object will be relative to current position (get from PageContext)
051                 * file must exist otherwise throw exception
052                 * @param pc Page Context to et actuell position in filesystem
053                 * @param path relative or absolute path for file object
054                 * @return file object from destination
055                 * @throws ExpressionException
056                 */
057                public Resource toResourceExisting(PageContext pc ,String path) throws PageException;
058                
059    
060                /**
061                 * cast a String (argument destination) to a File Object, 
062                 * if destination is not a absolute, file object will be relative to current position (get from PageContext)
063                 * at least parent must exist
064                 * @param pc Page Context to et actuell position in filesystem
065                 * @param destination relative or absolute path for file object
066                 * @return file object from destination
067                 * @throws ExpressionException
068                 */
069                public Resource toResourceExistingParent(PageContext pc ,String destination) throws PageException;
070                
071                /**
072                 * cast a String (argument destination) to a File Object, 
073                 * if destination is not a absolute, file object will be relative to current position (get from PageContext)
074                 * existing file is prefered but dont must exist
075                 * @param pc Page Context to et actuell position in filesystem
076                 * @param destination relative or absolute path for file object
077                 * @return file object from destination
078                 */
079                public Resource toResourceNotExisting(PageContext pc ,String destination);
080                            
081                /**
082                 * create a file if possible, return file if ok, otherwise return null 
083                 * @param res file to touch 
084                 * @param level touch also parent and grand parent
085                 * @param type is file or directory
086                 * @return file if exists, otherwise null
087                 */
088                public Resource createResource(Resource res, short level, short type);
089                
090                    /**
091                     * sets a attribute to the resource
092                     * @param res
093                     * @param attributes
094                     * @throws IOException
095                     */
096                    public void setAttribute(Resource res,String attributes) throws IOException;
097                    
098                /**
099                 * return the mime type of a file, does not check the extension of the file, it checks the header
100                 * @param res
101                 * @param defaultValue 
102                 * @return mime type of the file
103                 */
104                public String getMimeType(Resource res, String defaultValue);
105    
106                /**
107                 * return the mime type of a byte array
108                 * @param barr
109                 * @param defaultValue 
110                 * @return mime type of the file
111                 */
112                public String getMimeType(byte[] barr, String defaultValue);
113                
114                    /**
115                     * check if file is a child of given directory
116                     * @param file file to search
117                     * @param dir directory to search
118                     * @return is inside or not
119                     */
120                    public boolean isChildOf(Resource file, Resource dir);
121                    
122                    /**
123                     * return diffrents of one file to a other if first is child of second otherwise return null
124                     * @param file file to search
125                     * @param dir directory to search
126                     */
127                    public String getPathToChild(Resource file, Resource dir);
128                    
129                /**
130                 * get the Extension of a file resource
131                 * @param res
132                 * @return extension of file
133                 * @deprecated use instead <code>getExtension(Resource res, String defaultValue);</code>
134                 */
135                public String getExtension(Resource res);
136    
137                /**
138                 * get the Extension of a file resource
139                 * @param res
140                 * @return extension of file
141                 */
142                public String getExtension(Resource res, String defaultValue);
143    
144                /**
145                 * get the Extension of a file
146                 * @param strFile
147                 * @return extension of file
148                 * @deprecated use instead <code>getExtension(String strFile, String defaultValue);</code>
149                 */
150                public String getExtension(String strFile);
151    
152                /**
153                 * get the Extension of a file resource
154                 * @param res
155                 * @return extension of file
156                 */
157                public String getExtension(String strFile, String defaultValue);
158                
159    
160                /**
161                 * copy a file or directory recursive (with his content)
162                 * @param file file or directory to delete
163                 * @throws IOException 
164                 * @throws FileNotFoundException 
165                 */
166                public void copyRecursive(Resource src,Resource trg) throws IOException;
167                
168                
169                /**
170                 * copy a file or directory recursive (with his content)
171                 * @param src
172                 * @param trg
173                 * @param filter
174                 * @throws IOException 
175                 * @throws FileNotFoundException 
176                 */
177                public void copyRecursive(Resource src,Resource trg,ResourceFilter filter) throws IOException;
178                
179                public void removeChildren(Resource res) throws IOException;
180    
181                    public void removeChildren(Resource res,ResourceNameFilter filter) throws IOException;
182                    
183                    public void removeChildren(Resource res,ResourceFilter filter) throws IOException;
184                    
185                    public void moveTo(Resource src, Resource dest) throws IOException;
186    
187    
188                    /**
189                     * return if Resource is empty, means is directory and has no children or a empty file,
190                     * if not exist return false.
191                     * @param res
192                     */
193                    public boolean isEmpty(Resource res);
194    
195                    public boolean isEmptyDirectory(Resource res);
196                    
197                    public boolean isEmptyFile(Resource res);
198                    
199                    public String translatePath(String path, boolean slashAdBegin, boolean slashAddEnd);
200                    
201                    public String[] translatePathName(String path);
202                    
203                    public String merge(String parent, String child);
204                    
205                    public String removeScheme(String scheme, String path);
206                    
207                    /**
208                     * check if directory creation is ok with the rules for the Resource interface, to not change this rules.
209                     * @param resource
210                     * @param createParentWhenNotExists
211                     * @throws IOException
212                     */
213                    public void checkCreateDirectoryOK(Resource resource, boolean createParentWhenNotExists) throws IOException;
214    
215    
216                    /**
217                     * check if file creating is ok with the rules for the Resource interface, to not change this rules.
218                     * @param resource
219                     * @param createParentWhenNotExists
220                     * @throws IOException
221                     */
222                    public void checkCreateFileOK(Resource resource, boolean createParentWhenNotExists) throws IOException;
223    
224                    /**
225                     * check if copying a file is ok with the rules for the Resource interface, to not change this rules.
226                     * @param source
227                     * @param target
228                     * @throws IOException
229                     */
230                    public void checkCopyToOK(Resource source, Resource target) throws IOException;
231    
232                    /**
233                     * check if moveing a file is ok with the rules for the Resource interface, to not change this rules.
234                     * @param source
235                     * @param target
236                     * @throws IOException
237                     */
238                    public void checkMoveToOK(Resource source, Resource target) throws IOException;
239    
240                    /**
241                     * check if getting a inputstream of the file is ok with the rules for the Resource interface, to not change this rules.
242                     * @param resource
243                     * @throws IOException
244                     */
245                    public void checkGetInputStreamOK(Resource resource) throws IOException;
246    
247                    /**
248                     * check if getting a outputstream of the file is ok with the rules for the Resource interface, to not change this rules.
249                     * @param resource
250                     * @throws IOException
251                     */
252                    public void checkGetOutputStreamOK(Resource resource) throws IOException;
253    
254                    /**
255                     * check if removing the file is ok with the rules for the Resource interface, to not change this rules.
256                     * @param resource
257                     * @throws IOException
258                     */
259                    public void checkRemoveOK(Resource resource) throws IOException;
260    
261    
262                    public String toString(Resource r, String charset) throws IOException;
263    
264    
265                    public String contractPath(PageContext pc, String path);
266    
267    }