001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.helpers;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.status.StatusLogger;
021    
022    import java.io.File;
023    import java.io.UnsupportedEncodingException;
024    import java.net.MalformedURLException;
025    import java.net.URI;
026    import java.net.URL;
027    import java.net.URLDecoder;
028    
029    /**
030     * File utilities.
031     */
032    public final class FileUtils {
033    
034        /** Constant for the file URL protocol.*/
035        private static final String PROTOCOL_FILE = "file";
036    
037        private static final String JBOSS_FILE = "vfsfile";
038    
039        private static final Logger LOGGER = StatusLogger.getLogger();
040    
041        private FileUtils() {
042        }
043    
044          /**
045         * Tries to convert the specified URL to a file object. If this fails,
046         * <b>null</b> is returned.
047         *
048         * @param uri the URI
049         * @return the resulting file object
050         */
051        public static File fileFromURI(URI uri) {
052            if (uri == null || (uri.getScheme() != null &&
053                (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri.getScheme())))) {
054                return null;
055            } else {
056                if (uri.getScheme() == null) {
057                    try {
058                        uri = new File(uri.getPath()).toURI();
059                    } catch (final Exception ex) {
060                        LOGGER.warn("Invalid URI " + uri);
061                        return null;
062                    }
063                }
064                try {
065                    return new File(URLDecoder.decode(uri.toURL().getFile(), "UTF8"));
066                } catch (final MalformedURLException ex) {
067                    LOGGER.warn("Invalid URL " + uri, ex);
068                } catch (final UnsupportedEncodingException uee) {
069                    LOGGER.warn("Invalid encoding: UTF8", uee);
070                }
071                return null;
072            }
073        }
074    
075        public static boolean isFile(final URL url) {
076            return url != null && (url.getProtocol().equals(PROTOCOL_FILE) || url.getProtocol().equals(JBOSS_FILE));
077        }
078    }