1 package org.robolectric.res;
2 
3 import org.robolectric.util.Logger;
4 
5 public class RawResourceLoader {
6   private final ResourcePath resourcePath;
7 
RawResourceLoader(ResourcePath resourcePath)8   public RawResourceLoader(ResourcePath resourcePath) {
9     this.resourcePath = resourcePath;
10   }
11 
loadTo(PackageResourceTable resourceTable)12   public void loadTo(PackageResourceTable resourceTable) {
13     load(resourceTable, "raw");
14     load(resourceTable, "drawable");
15   }
16 
load(PackageResourceTable resourceTable, String folderBaseName)17   public void load(PackageResourceTable resourceTable, String folderBaseName) {
18     FsFile resourceBase = resourcePath.getResourceBase();
19     FsFile[] files = resourceBase.listFiles(new StartsWithFilter(folderBaseName));
20     if (files == null) {
21       throw new RuntimeException(resourceBase.join(folderBaseName) + " is not a directory");
22     }
23     for (FsFile dir : files) {
24       loadRawFiles(resourceTable, folderBaseName, dir);
25     }
26   }
27 
loadRawFiles(PackageResourceTable resourceTable, String resourceType, FsFile rawDir)28   private void loadRawFiles(PackageResourceTable resourceTable, String resourceType, FsFile rawDir) {
29     FsFile[] files = rawDir.listFiles();
30     if (files != null) {
31       Qualifiers qualifiers;
32       try {
33         qualifiers = Qualifiers.fromParentDir(rawDir);
34       } catch (IllegalArgumentException e) {
35         Logger.warn(rawDir + ": " + e.getMessage());
36         return;
37       }
38 
39       for (FsFile file : files) {
40         String fileBaseName = file.getBaseName();
41         resourceTable.addResource(resourceType, fileBaseName,
42             new FileTypedResource(file, ResType.FILE,
43                 new XmlContext(resourceTable.getPackageName(), file, qualifiers)));
44       }
45     }
46   }
47 }
48