View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.helpers;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.status.StatusLogger;
21  
22  import java.io.File;
23  import java.io.UnsupportedEncodingException;
24  import java.net.MalformedURLException;
25  import java.net.URI;
26  import java.net.URL;
27  import java.net.URLDecoder;
28  
29  /**
30   * File utilities.
31   */
32  public final class FileUtils {
33  
34      /** Constant for the file URL protocol.*/
35      private static final String PROTOCOL_FILE = "file";
36  
37      private static final String JBOSS_FILE = "vfsfile";
38  
39      private static final Logger logger = StatusLogger.getLogger();
40  
41      private FileUtils() {
42      }
43  
44        /**
45       * Tries to convert the specified URL to a file object. If this fails,
46       * <b>null</b> is returned.
47       *
48       * @param uri the URI
49       * @return the resulting file object
50       */
51      public static File fileFromURI(URI uri) {
52          if (uri == null || (uri.getScheme() != null &&
53              (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri.getScheme())))) {
54              return null;
55          } else {
56              if (uri.getScheme() == null) {
57                  try {
58                      uri = new File(uri.getPath()).toURI();
59                  } catch (Exception ex) {
60                      logger.warn("Invalid URI " + uri);
61                      return null;
62                  }
63              }
64              try {
65                  return new File(URLDecoder.decode(uri.toURL().getFile(), "UTF8"));
66              } catch (MalformedURLException ex) {
67                  logger.warn("Invalid URL " + uri, ex);
68              } catch (UnsupportedEncodingException uee) {
69                  logger.warn("Invalid encoding: UTF8", uee);
70              }
71              return null;
72          }
73      }
74  
75      public static boolean isFile(URL url) {
76          return url != null && (url.getProtocol().equals(PROTOCOL_FILE) || url.getProtocol().equals(JBOSS_FILE));
77      }
78  }