File Source: FileResourceManager.java

         /* 
    P/P   *  Method: com.dmdirc.util.resourcemanager.FileResourceManager__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.util.resourcemanager;
    24  
    25  import com.dmdirc.logger.ErrorLevel;
    26  import com.dmdirc.logger.Logger;
    27  
    28  import java.io.File;
    29  import java.io.FileInputStream;
    30  import java.io.FileNotFoundException;
    31  import java.io.IOException;
    32  import java.io.InputStream;
    33  import java.util.ArrayList;
    34  import java.util.Arrays;
    35  import java.util.HashMap;
    36  import java.util.List;
    37  import java.util.Map;
    38  
    39  /**
    40   * Provides an easy way to access files inside a project.
    41   */
    42  public final class FileResourceManager extends ResourceManager {
    43      
    44      /** Base path for the project. */
    45      private final String basePath;
    46      
    47      /**
    48       * Creates a new instance of FileResourceManager.
    49       * 
    50       * @param basePath Base path for the resource manager
    51       */
    52      protected FileResourceManager(final String basePath) {
                 /* 
    P/P           *  Method: void com.dmdirc.util.resourcemanager.FileResourceManager(String)
                  * 
                  *  Postconditions:
                  *    this.basePath == basePath
                  *    init'ed(this.basePath)
                  */
    53          super();
    54          
    55          this.basePath = basePath;
    56      }
    57      
    58      /** {@inheritDoc} */
    59      @Override
    60      public boolean resourceExists(final String resource) {
    61          final File file;
    62          
                 /* 
    P/P           *  Method: bool resourceExists(String)
                  * 
                  *  Preconditions:
                  *    resource != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@63: {0}, {1}
                  */
    63          if (resource.startsWith(basePath)) {
    64              file = new File(resource);
    65          } else {
    66              file = new File(basePath, resource);
    67          }
    68          
    69          return file.exists() && !file.isDirectory();
    70      }
    71      
    72      /** {@inheritDoc} */
    73      @Override
    74      public byte[] getResourceBytes(final String resource) {
    75          FileInputStream inputStream;
    76          final File file;
    77          
                 /* 
    P/P           *  Method: byte[] getResourceBytes(String)
                  * 
                  *  Preconditions:
                  *    resource != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.logger.ErrorLevel.LOW)
                  *    java.io.File:length(...)@92 >= 0
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&new byte[](getResourceBytes#5),&new byte[](getResourceBytes#8),&new byte[](getResourceBytes#7),&new byte[](getResourceBytes#4),&new byte[](getResourceBytes#3)}
                  *    new byte[](getResourceBytes#3) num objects <= 1
                  *    new byte[](getResourceBytes#3).length == 0
                  *    new byte[](getResourceBytes#4) num objects <= 1
                  *    new byte[](getResourceBytes#4).length == 0
                  *    new byte[](getResourceBytes#5) num objects <= 1
                  *    new byte[](getResourceBytes#5).length <= 264-1
                  *    new byte[](getResourceBytes#7) num objects <= 1
                  *    new byte[](getResourceBytes#7).length == 0
                  *    new byte[](getResourceBytes#8) num objects <= 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.io.File:exists(...)@84: {1}, {0}
                  *    java.io.File:isDirectory(...)@88: {0}, {1}
                  *    java.lang.String:startsWith(...)@78: {0}, {1}
                  */
    78          if (resource.startsWith(basePath)) {
    79              file = new File(resource);
    80          } else {
    81              file = new File(basePath, resource);
    82          }
    83          
    84          if (!file.exists()) {
    85              return new byte[0];
    86          }
    87          
    88          if (file.isDirectory()) {
    89              return new byte[0];
    90          }
    91          
    92          final byte[] bytes = new byte[(int) file.length()];
    93          
    94          try {
    95              inputStream = new FileInputStream(file);
    96          } catch (FileNotFoundException ex) {
    97              return new byte[0];
    98          }
    99          
   100          try {
   101              inputStream.read(bytes);
   102          } catch (IOException ex) {
   103              return new byte[0];
   104          }
   105          
   106          try {
   107              inputStream.close();
   108          } catch (IOException ex) {
   109              Logger.userError(ErrorLevel.LOW, "Unable to close stream");
   110          }
   111          
   112          return bytes;
   113      }
   114      
   115      /** {@inheritDoc} */
   116      @Override
   117      public InputStream getResourceInputStream(final String resource) {
   118          final File file;
   119          
                 /* 
    P/P           *  Method: InputStream getResourceInputStream(String)
                  * 
                  *  Preconditions:
                  *    resource != null
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&amp;new FileInputStream(getResourceInputStream#3)}
                  *    new FileInputStream(getResourceInputStream#3) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    java.io.File:exists(...)@126: {1}, {0}
                  *    java.io.File:isDirectory(...)@130: {0}, {1}
                  *    java.lang.String:startsWith(...)@120: {0}, {1}
                  */
   120          if (resource.startsWith(basePath)) {
   121              file = new File(resource);
   122          } else {
   123              file = new File(basePath, resource);
   124          }
   125          
   126          if (!file.exists()) {
   127              return null;
   128          }
   129          
   130          if (file.isDirectory()) {
   131              return null;
   132          }
   133          
   134          try {
   135              return new FileInputStream(file);
   136          } catch (FileNotFoundException ex) {
   137              return null;
   138          }
   139      }
   140      
   141      /** {@inheritDoc} */
   142      @Override
   143      public Map<String, byte[]> getResourcesEndingWithAsBytes(
   144              final String resourcesSuffix) {
                 /* 
    P/P           *  Method: Map getResourcesEndingWithAsBytes(String)
                  * 
                  *  Preconditions:
                  *    (soft) this.basePath != null
                  * 
                  *  Presumptions:
                  *    java.io.File:getPath(...)@149 != null
                  *    java.util.Iterator:next(...)@148 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesEndingWithAsBytes#2)
                  *    new HashMap(getResourcesEndingWithAsBytes#2) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:endsWith(...)@151: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@148: {0}, {1}
                  */
   145          final List<File> files = getFileListing(new File(basePath));
   146          final Map<String, byte[]> resources = new HashMap<String, byte[]>();
   147          
   148          for (File file : files) {
   149              final String path = file.getPath().substring(basePath.length(),
   150                      file.getPath().length());
   151              if (path.endsWith(resourcesSuffix)) {
   152                  resources.put(path, getResourceBytes(path));
   153              }
   154          }
   155          
   156          return resources;
   157      }
   158      
   159      /** {@inheritDoc} */
   160      @Override
   161      public Map<String, byte[]> getResourcesStartingWithAsBytes(
   162              final String resourcesPrefix) {
                 /* 
    P/P           *  Method: Map getResourcesStartingWithAsBytes(String)
                  * 
                  *  Preconditions:
                  *    (soft) this.basePath != null
                  * 
                  *  Presumptions:
                  *    java.io.File:getPath(...)@167 != null
                  *    java.util.Iterator:next(...)@166 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesStartingWithAsBytes#2)
                  *    new HashMap(getResourcesStartingWithAsBytes#2) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@169: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@166: {0}, {1}
                  */
   163          final List<File> files = getFileListing(new File(basePath));
   164          final Map<String, byte[]> resources = new HashMap<String, byte[]>();
   165          
   166          for (File file : files) {
   167              final String path = file.getPath().substring(basePath.length(),
   168                      file.getPath().length());
   169              if (path.startsWith(resourcesPrefix)) {
   170                  resources.put(path, getResourceBytes(path));
   171              }
   172          }
   173          
   174          return resources;
   175      }
   176      
   177      /** {@inheritDoc} */
   178      @Override
   179      public Map<String, InputStream> getResourcesStartingWithAsInputStreams(
   180              final String resourcesPrefix) {
                 /* 
    P/P           *  Method: Map getResourcesStartingWithAsInputStreams(String)
                  * 
                  *  Preconditions:
                  *    (soft) this.basePath != null
                  * 
                  *  Presumptions:
                  *    java.io.File:getPath(...)@185 != null
                  *    java.util.Iterator:next(...)@184 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesStartingWithAsInputStreams#2)
                  *    new HashMap(getResourcesStartingWithAsInputStreams#2) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@187: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@184: {0}, {1}
                  */
   181          final List<File> files = getFileListing(new File(basePath));
   182          final Map<String, InputStream> resources = new HashMap<String, InputStream>();
   183          
   184          for (File file : files) {
   185              final String path = file.getPath().substring(basePath.length(),
   186                      file.getPath().length());
   187              if (path.startsWith(resourcesPrefix)) {
   188                  resources.put(path, getResourceInputStream(path));
   189              }
   190          }
   191          
   192          return resources;
   193      }
   194      
   195      /** {@inheritDoc} */
   196      @Override
   197      public List<String> getResourcesStartingWith(final String resourcesPrefix) {
                 /* 
    P/P           *  Method: List getResourcesStartingWith(String)
                  * 
                  *  Preconditions:
                  *    (soft) this.basePath != null
                  * 
                  *  Presumptions:
                  *    java.io.File:getPath(...)@202 != null
                  *    java.util.Iterator:next(...)@201 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getResourcesStartingWith#2)
                  *    new ArrayList(getResourcesStartingWith#2) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@204: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@201: {0}, {1}
                  */
   198          final List<File> files = getFileListing(new File(basePath));
   199          final List<String> resources = new ArrayList<String>();
   200          
   201          for (File file : files) {
   202              final String path = file.getPath().substring(basePath.length(),
   203                      file.getPath().length());
   204              if (path.startsWith(resourcesPrefix)) {
   205                  resources.add(path);
   206              }
   207          }
   208          
   209          return resources;
   210      }
   211      
   212      /**
   213       * Returns a resursive listing of a directory tree.
   214       *
   215       * @param startingDirectory Starting directory for the file listing
   216       *
   217       * @return Recursive directory listing
   218       */
   219      private static List<File> getFileListing(final File startingDirectory) {
                 /* 
    P/P           *  Method: List getFileListing(File)
                  * 
                  *  Preconditions:
                  *    startingDirectory != null
                  * 
                  *  Presumptions:
                  *    java.util.Arrays:asList(...)@226 != null
                  *    java.util.Iterator:next(...)@227 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getFileListing#1)
                  *    new ArrayList(getFileListing#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.io.File:isFile(...)@228: {0}, {1}
                  *    java.io.File:listFiles(...)@222: Inverse{null}, Addr_Set{null}
                  *    java.util.Iterator:hasNext(...)@227: {0}, {1}
                  */
   220          final List<File> result = new ArrayList<File>();
   221          
   222          if (startingDirectory.listFiles() == null) {
   223              return result;
   224          }
   225          
   226          final List<File> files = Arrays.asList(startingDirectory.listFiles());
   227          for (File file : files) {
   228              if (file.isFile()) {
   229                  result.add(file);
   230              } else {
   231                  result.addAll(getFileListing(file));
   232              }
   233          }
   234          return result;
   235      }
   236  }








SofCheck Inspector Build Version : 2.17854
FileResourceManager.java 2009-Jun-25 01:54:24
FileResourceManager.class 2009-Sep-02 17:04:17