Class FreeColDataFile

  • Direct Known Subclasses:
    FreeColModFile, FreeColSavegameFile

    public class FreeColDataFile
    extends java.lang.Object
    Support for reading a FreeCol data file. The data file is either a ZIP-file or a directory containing certain files.
    • Field Detail

      • logger

        private static final java.util.logging.Logger logger
      • ZIP_FILE_EXTENSION

        protected static final java.lang.String ZIP_FILE_EXTENSION
        See Also:
        Constant Field Values
      • resourceScheme

        private static final java.lang.String resourceScheme
        A fake URI scheme for resources delegating to other resources.
        See Also:
        Constant Field Values
      • file

        private final java.io.File file
        The file this object represents.
      • jarDirectory

        private final java.lang.String jarDirectory
        A prefix string for the jar-entries (only if file is a ZIP-file).
    • Constructor Detail

      • FreeColDataFile

        public FreeColDataFile​(java.io.File file)
                        throws java.io.IOException
        Opens the given file for reading.
        Parameters:
        file - The file to be read.
        Throws:
        java.io.IOException - if the file does not exist.
    • Method Detail

      • findJarDirectory

        private static java.lang.String findJarDirectory​(java.io.File file)
        Finds the directory within the zip-file in case the data file has been renamed.
        Parameters:
        file - The zip-file.
        Returns:
        The name of the base directory in the zip-file or null on error.
      • getURI

        protected java.net.URI getURI​(java.lang.String name)
        Get a URI to access a resource through.
        Parameters:
        name - A name with special prefixes to convert to the URI.
        Returns:
        A URI, or null if none found.
      • getInputStream

        public java.io.BufferedInputStream getInputStream​(java.lang.String filename)
                                                   throws java.io.IOException
        Gets an input stream for the specified resource.
        Parameters:
        filename - The filename of a resource within this collection of data. If this object represents a directory then the provided filename should be relative towards the path of the directory. In case of a compressed archive it should be the path within the archive.
        Returns:
        An InputStream to read the resource with.
        Throws:
        java.io.IOException - if an error occurs
      • getResourceMapping

        public ResourceMapping getResourceMapping()
        Creates a ResourceMapping from the available resource files.
        Returns:
        A ResourceMapping or null there is no resource mapping file.
      • handleResources

        private java.util.List<java.lang.String> handleResources​(java.util.Properties properties,
                                                                 ResourceMapping rc)
        Handles loading of all resources defined in properties. This method also handles alternate variations and sizes.
        Parameters:
        properties - The resources to be loaded.
        rc - The output object where resolved resources should be placed.
        Returns:
        A list of keys that are virtual resources, that is a link to another resource. Virtual resources are used by mods when the author wants to reuse graphics that are defined in other FreeCol data files.
      • handleNormalResource

        private void handleNormalResource​(ResourceFactory resourceFactory,
                                          ResourceMapping rc,
                                          java.lang.String key,
                                          java.lang.String value)
      • handleVirtualResources

        private void handleVirtualResources​(java.util.List<java.lang.String> virtualResources,
                                            LogBuilder lb,
                                            java.util.Properties properties,
                                            ResourceMapping rc)
      • readResourcesProperties

        private java.util.Properties readResourcesProperties​(LogBuilder lb)
        Reads "resources.properties". We support localized versions of this file, so that localized images can be provided.
        Parameters:
        lb - Just used for logging.
        Returns:
        The read properties, or null if an error occured while reading the file(s).
      • stripEnding

        private java.lang.String stripEnding​(java.lang.String key,
                                             java.lang.String ending)
      • extendWithAdditionalSizesAndVariations

        private void extendWithAdditionalSizesAndVariations​(ResourceFactory resourceFactory,
                                                            ImageResource imageResource,
                                                            java.lang.String value)
      • findVariationsWithAlternateSizes

        private java.util.Map<java.net.URI,​java.util.List<java.net.URI>> findVariationsWithAlternateSizes​(java.lang.String name)
      • findFilesWithVariationOrAlternativeSizeAsUri

        private java.util.List<java.net.URI> findFilesWithVariationOrAlternativeSizeAsUri​(java.nio.file.Path filePath,
                                                                                          boolean findVariation)
                                                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • findFilesWithVariationOrAlternativeSize

        private java.util.List<java.nio.file.Path> findFilesWithVariationOrAlternativeSize​(java.nio.file.Path filePath,
                                                                                           boolean findVariation)
                                                                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getPath

        public java.lang.String getPath()
        Get the path to the underlying file. Useful for error messages.
        Returns:
        The path to the file.
      • getFileFilter

        public static javax.swing.filechooser.FileFilter getFileFilter​(java.lang.String extension)
        Get a file filter for a given extension.
        Parameters:
        extension - The file extension to filter on.
        Returns:
        File filters for the extension.