001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io;
020
021import java.io.File;
022import java.net.MalformedURLException;
023import java.net.URL;
024
025/**
026 * Helper methods for file objects
027 */
028public final class FileUtil {
029    /**
030     * Field <code>FILE_SEPERATOR</code>
031     */
032    public static final char FILE_SEPERATOR=File.separatorChar; 
033    /**
034     * Field <code>FILE_ANTI_SEPERATOR</code>
035     */
036    public static final char FILE_ANTI_SEPERATOR=(FILE_SEPERATOR=='/')?'\\':'/';
037    
038    /**
039     * Field <code>TYPE_DIR</code>
040     */
041    public static final short TYPE_DIR=0;
042    
043    /**
044     * Field <code>TYPE_FILE</code>
045     */
046    public static final short TYPE_FILE=1;
047
048    /**
049     * Field <code>LEVEL_FILE</code>
050     */
051    public static final short LEVEL_FILE=0;
052    /**
053     * Field <code>LEVEL_PARENT_FILE</code>
054     */
055    public static final short LEVEL_PARENT_FILE=1;
056    /**
057     * Field <code>LEVEL_GRAND_PARENT_FILE</code>
058     */
059    public static final short LEVEL_GRAND_PARENT_FILE=2;
060    
061    /** 
062     * create a file from path
063     * @param path
064     * @return new File Object
065     */
066    public static File toFile(String path) { 
067        return new File(path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR)); 
068    } 
069
070    /**
071     * create a File from parent file and string
072     * @param parent 
073     * @param path 
074     * @return new File Object
075     */
076    public static File toFile(File parent, String path) {
077        return new File(parent,path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR));
078    }
079
080    /**
081     * create a File from parent file and string
082     * @param parent 
083     * @param path 
084     * @return new File Object
085     */
086    public static File toFile(String parent, String path) {
087        return new File(
088                        parent.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR),
089                        path.replace(FILE_ANTI_SEPERATOR,FILE_SEPERATOR));
090    }
091
092        /**
093         * translate a URL to a File Object
094         * @param url
095         * @return matching file object
096         * @throws MalformedURLException
097         */
098        public static final File URLToFile(URL url) throws MalformedURLException {
099            if (!"file".equals(url.getProtocol()))
100                throw new MalformedURLException("URL protocol must be 'file'.");
101            return new File(URIToFilename(url.getFile()));
102        }
103        
104          /**
105           * Fixes a platform dependent filename to standard URI form.
106           * @param str The string to fix.
107           * @return Returns the fixed URI string.
108           */
109          public static final String URIToFilename(String str) {
110            // Windows fix
111            if (str.length() >= 3) {
112              if (str.charAt(0) == '/' && str.charAt(2) == ':') {
113                char ch1 = Character.toUpperCase(str.charAt(1));
114                if (ch1 >= 'A' && ch1 <= 'Z') str = str.substring(1);
115              }
116            }
117            // handle platform dependent strings
118            str = str.replace('/', java.io.File.separatorChar);
119            return str;
120          }
121
122
123
124
125}