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 getMymeType(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 getMymeType(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                 */
134                public String getExtension(Resource res);// FUTURE set deprecated
135             // FUTURE public String getExtension(Resource res, String defaultValue);
136    
137                /**
138                 * get the Extension of a file
139                 * @param strFile
140                 * @return extension of file
141                 */
142                public String getExtension(String strFile);// FUTURE set deprecated
143                // FUTURE public String getExtension(String strFile, String defaultValue);
144                
145    
146                /**
147                 * copy a file or directory recursive (with his content)
148                 * @param file file or directory to delete
149                 * @throws IOException 
150                 * @throws FileNotFoundException 
151                 */
152                public void copyRecursive(Resource src,Resource trg) throws IOException;
153                
154                
155                /**
156                 * copy a file or directory recursive (with his content)
157                 * @param src
158                 * @param trg
159                 * @param filter
160                 * @throws IOException 
161                 * @throws FileNotFoundException 
162                 */
163                public void copyRecursive(Resource src,Resource trg,ResourceFilter filter) throws IOException;
164                
165                public void removeChildren(Resource res) throws IOException;
166    
167                    public void removeChildren(Resource res,ResourceNameFilter filter) throws IOException;
168                    
169                    public void removeChildren(Resource res,ResourceFilter filter) throws IOException;
170                    
171                    public void moveTo(Resource src, Resource dest) throws IOException;
172    
173    
174                    /**
175                     * return if Resource is empty, means is directory and has no children or a empty file,
176                     * if not exists return false.
177                     * @param res
178                     */
179                    public boolean isEmpty(Resource res);
180    
181                    public boolean isEmptyDirectory(Resource res);
182                    
183                    public boolean isEmptyFile(Resource res);
184                    
185                    public String translatePath(String path, boolean slashAdBegin, boolean slashAddEnd);
186                    
187                    public String[] translatePathName(String path);
188                    
189                    public String merge(String parent, String child);
190                    
191                    public String removeScheme(String scheme, String path);
192                    
193                    /**
194                     * check if directory creation is ok with the rules for the Resource interface, to not change this rules.
195                     * @param resource
196                     * @param createParentWhenNotExists
197                     * @throws IOException
198                     */
199                    public void checkCreateDirectoryOK(Resource resource, boolean createParentWhenNotExists) throws IOException;
200    
201    
202                    /**
203                     * check if file creating is ok with the rules for the Resource interface, to not change this rules.
204                     * @param resource
205                     * @param createParentWhenNotExists
206                     * @throws IOException
207                     */
208                    public void checkCreateFileOK(Resource resource, boolean createParentWhenNotExists) throws IOException;
209    
210                    /**
211                     * check if copying a file is ok with the rules for the Resource interface, to not change this rules.
212                     * @param source
213                     * @param target
214                     * @throws IOException
215                     */
216                    public void checkCopyToOK(Resource source, Resource target) throws IOException;
217    
218                    /**
219                     * check if moveing a file is ok with the rules for the Resource interface, to not change this rules.
220                     * @param source
221                     * @param target
222                     * @throws IOException
223                     */
224                    public void checkMoveToOK(Resource source, Resource target) throws IOException;
225    
226                    /**
227                     * check if getting a inputstream of the file is ok with the rules for the Resource interface, to not change this rules.
228                     * @param resource
229                     * @throws IOException
230                     */
231                    public void checkGetInputStreamOK(Resource resource) throws IOException;
232    
233                    /**
234                     * check if getting a outputstream of the file is ok with the rules for the Resource interface, to not change this rules.
235                     * @param resource
236                     * @throws IOException
237                     */
238                    public void checkGetOutputStreamOK(Resource resource) throws IOException;
239    
240                    /**
241                     * check if removing 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 checkRemoveOK(Resource resource) throws IOException;
246    
247    
248                    public String toString(Resource r, String charset) throws IOException;
249    
250    }