FileLocator.java

  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.commons.configuration2.io;

  18. import java.net.URL;
  19. import java.util.Objects;

  20. /**
  21.  * <p>
  22.  * A class describing the location of a file.
  23.  * </p>
  24.  * <p>
  25.  * An instance of this class provides information for locating and accessing a file. The file location can be defined
  26.  * </p>
  27.  * <ul>
  28.  * <li>as a URL; this identifies a file in a unique way</li>
  29.  * <li>as a combination of base path and file name; if this variant is used, there may be an additional location step
  30.  * required in order to identify the referenced file (for instance, the file name may be interpreted as the name of a
  31.  * resource to be loaded from class path).</li>
  32.  * </ul>
  33.  * <p>
  34.  * In addition, other properties are available which are also needed for loading or saving a file, like the underlying
  35.  * {@link FileSystem}. The encoding to be used when accessing the represented data is also part of the data contained in
  36.  * an instance; if no encoding is set explicitly, the platform's default encoding is used.
  37.  * <p>
  38.  * Instances of this class are immutable and thus can be safely shared between arbitrary components. {@link FileHandler}
  39.  * also uses an instance to reference the associated file. Instances are created using a <em>builder</em>.
  40.  * {@link FileLocatorUtils} offers convenience methods for obtaining such a builder.
  41.  * </p>
  42.  *
  43.  * @since 2.0
  44.  */
  45. public final class FileLocator {
  46.     /**
  47.      * A typical <em>builder</em> implementation for creating {@code FileLocator} objects. An instance of this class is
  48.      * returned by the {@code fileLocator()} method of {link FileLocatorUtils}. It can be used to define the various
  49.      * components of the {@code FileLocator} object. By calling {@code create()} the new immutable {@code FileLocator}
  50.      * instance is created.
  51.      */
  52.     public static final class FileLocatorBuilder {
  53.         /** The base path. */
  54.         private String basePath;

  55.         /** The encoding. */
  56.         private String encoding;

  57.         /** The file name. */
  58.         private String fileName;

  59.         /** The file system. */
  60.         private FileSystem fileSystem;

  61.         /** The location strategy. */
  62.         private FileLocationStrategy locationStrategy;

  63.         /** The URL. */
  64.         private URL sourceURL;

  65.         /** The URL connection options. */
  66.         private URLConnectionOptions urlConnectionOptions;

  67.         /**
  68.          * Creates a new instance of {@code FileLocatorBuilder} and initializes the builder's properties from the passed in
  69.          * {@code FileLocator} object.
  70.          *
  71.          * @param src the source {@code FileLocator} (may be <strong>null</strong>)
  72.          */
  73.         FileLocatorBuilder(final FileLocator src) {
  74.             if (src != null) {
  75.                 initBuilder(src);
  76.             }
  77.         }

  78.         /**
  79.          * Specifies the base path of the new {@code FileLocator}.
  80.          *
  81.          * @param path the base path
  82.          * @return a reference to this builder for method chaining
  83.          */
  84.         public FileLocatorBuilder basePath(final String path) {
  85.             basePath = path;
  86.             return this;
  87.         }

  88.         /**
  89.          * Creates a new immutable {@code FileLocatorImpl} object based on the properties set so far for this builder.
  90.          *
  91.          * @return the newly created {@code FileLocator} object, never null.
  92.          */
  93.         public FileLocator create() {
  94.             return new FileLocator(this);
  95.         }

  96.         /**
  97.          * Specifies the encoding of the new {@code FileLocator}.
  98.          *
  99.          * @param enc the encoding
  100.          * @return a reference to this builder for method chaining
  101.          */
  102.         public FileLocatorBuilder encoding(final String enc) {
  103.             encoding = enc;
  104.             return this;
  105.         }

  106.         /**
  107.          * Specifies the file name of the new {@code FileLocator}.
  108.          *
  109.          * @param name the file name
  110.          * @return a reference to this builder for method chaining
  111.          */
  112.         public FileLocatorBuilder fileName(final String name) {
  113.             fileName = name;
  114.             return this;
  115.         }

  116.         /**
  117.          * Specifies the {@code FileSystem} of the new {@code FileLocator}.
  118.          *
  119.          * @param fs the {@code FileSystem}
  120.          * @return a reference to this builder for method chaining
  121.          */
  122.         public FileLocatorBuilder fileSystem(final FileSystem fs) {
  123.             fileSystem = fs;
  124.             return this;
  125.         }

  126.         /**
  127.          * Initializes the properties of this builder from the passed in locator object.
  128.          *
  129.          * @param src the source {@code FileLocator}
  130.          */
  131.         private void initBuilder(final FileLocator src) {
  132.             basePath = src.getBasePath();
  133.             fileName = src.getFileName();
  134.             sourceURL = src.getSourceURL();
  135.             urlConnectionOptions = src.getURLConnectionOptions();
  136.             encoding = src.getEncoding();
  137.             fileSystem = src.getFileSystem();
  138.             locationStrategy = src.getLocationStrategy();
  139.         }

  140.         /**
  141.          * Specifies the {@code FileLocationStrategy} to be used when the referenced file is to be located.
  142.          *
  143.          * @param strategy the {@code FileLocationStrategy}
  144.          * @return a reference to this builder for method chaining
  145.          */
  146.         public FileLocatorBuilder locationStrategy(final FileLocationStrategy strategy) {
  147.             locationStrategy = strategy;
  148.             return this;
  149.         }

  150.         /**
  151.          * Specifies the source URL of the new {@code FileLocator}.
  152.          *
  153.          * @param url the source URL
  154.          * @return a reference to this builder for method chaining
  155.          */
  156.         public FileLocatorBuilder sourceURL(final URL url) {
  157.             this.sourceURL = url;
  158.             return this;
  159.         }

  160.         /**
  161.          * Specifies the source URL connection options of the new {@code FileLocator}.
  162.          *
  163.          * @param urlConnectionOptions the source URL connection options.
  164.          * @return a reference to this builder for method chaining
  165.          */
  166.         public FileLocatorBuilder urlConnectionOptions(final URLConnectionOptions urlConnectionOptions) {
  167.             this.urlConnectionOptions = urlConnectionOptions;
  168.             return this;

  169.         }
  170.     }

  171.     /** The base path. */
  172.     private final String basePath;

  173.     /** The encoding. */
  174.     private final String encoding;

  175.     /** The file name. */
  176.     private final String fileName;

  177.     /** The file system. */
  178.     private final FileSystem fileSystem;

  179.     /** The file location strategy. */
  180.     private final FileLocationStrategy locationStrategy;

  181.     /** The source URL. */
  182.     private final URL sourceURL;

  183.     /** The source URL connection options. */
  184.     private final URLConnectionOptions urlConnectionOptions;

  185.     /**
  186.      * Creates a new instance of {@code FileLocatorImpl} and initializes it from the given builder instance
  187.      *
  188.      * @param builder the builder
  189.      */
  190.     public FileLocator(final FileLocatorBuilder builder) {
  191.         fileName = builder.fileName;
  192.         basePath = builder.basePath;
  193.         sourceURL = builder.sourceURL;
  194.         urlConnectionOptions = builder.urlConnectionOptions;
  195.         encoding = builder.encoding;
  196.         fileSystem = builder.fileSystem;
  197.         locationStrategy = builder.locationStrategy;
  198.     }

  199.     /**
  200.      * Compares this object with another one. Two instances of {@code FileLocatorImpl} are considered equal if all of their
  201.      * properties are equal.
  202.      *
  203.      * @param obj the object to compare to
  204.      * @return a flag whether these objects are equal
  205.      */
  206.     @Override
  207.     public boolean equals(final Object obj) {
  208.         if (this == obj) {
  209.             return true;
  210.         }
  211.         if (!(obj instanceof FileLocator)) {
  212.             return false;
  213.         }
  214.         final FileLocator other = (FileLocator) obj;
  215.         return Objects.equals(basePath, other.basePath) && Objects.equals(encoding, other.encoding) && Objects.equals(fileName, other.fileName)
  216.             && Objects.equals(fileSystem, other.fileSystem) && Objects.equals(locationStrategy, other.locationStrategy)
  217.             && Objects.equals(sourceURL, other.sourceURL) && Objects.equals(urlConnectionOptions, other.urlConnectionOptions);
  218.     }

  219.     /**
  220.      * Gets the base path stored in this locator or <strong>null</strong> if it is undefined.
  221.      *
  222.      * @return the base path
  223.      */
  224.     public String getBasePath() {
  225.         return basePath;
  226.     }

  227.     /**
  228.      * Gets the encoding stored in this locator or <strong>null</strong> if it is undefined.
  229.      *
  230.      * @return the encoding
  231.      */
  232.     public String getEncoding() {
  233.         return encoding;
  234.     }

  235.     /**
  236.      * Gets the file name stored in this locator or <strong>null</strong> if it is undefined.
  237.      *
  238.      * @return the file name
  239.      */
  240.     public String getFileName() {
  241.         return fileName;
  242.     }

  243.     /**
  244.      * Gets the {@code FileSystem} to be used for accessing the file referenced by this locator or <strong>null</strong> if it is
  245.      * undefined.
  246.      *
  247.      * @return the {@code FileSystem}
  248.      */
  249.     public FileSystem getFileSystem() {
  250.         return fileSystem;
  251.     }

  252.     /**
  253.      * Gets the {@code FileLocationStrategy} to be used for locating the referenced file. If no specific
  254.      * {@code FileLocationStrategy} has been set, result is <strong>null</strong>. This means that the default strategy should be
  255.      * used.
  256.      *
  257.      * @return the {@code FileLocationStrategy} to be used
  258.      */
  259.     public FileLocationStrategy getLocationStrategy() {
  260.         return locationStrategy;
  261.     }

  262.     /**
  263.      * Gets the URL pointing to the referenced source file or <strong>null</strong> if it is undefined.
  264.      *
  265.      * @return the source URL
  266.      */
  267.     public URL getSourceURL() {
  268.         return sourceURL;
  269.     }

  270.     /**
  271.      * Gets the URLConnectionOptions
  272.      *
  273.      * @return the URLConnectionOptions
  274.      */
  275.     public URLConnectionOptions getURLConnectionOptions() {
  276.         return urlConnectionOptions;
  277.     }

  278.     /**
  279.      * Returns a hash code for this object.
  280.      *
  281.      * @return a hash code for this object
  282.      */
  283.     @Override
  284.     public int hashCode() {
  285.         return Objects.hash(basePath, encoding, fileName, fileSystem, locationStrategy, sourceURL, urlConnectionOptions);
  286.     }

  287.     @Override
  288.     public String toString() {
  289.         return "FileLocator [basePath=" + basePath + ", encoding=" + encoding + ", fileName=" + fileName + ", fileSystem=" + fileSystem + ", locationStrategy="
  290.             + locationStrategy + ", sourceURL=" + sourceURL + ", urlConnectionOptions=" + urlConnectionOptions + "]";
  291.     }
  292. }