xref: /aosp_15_r20/external/robolectric/resources/src/main/java/org/robolectric/res/RawResourceLoader.java (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1 package org.robolectric.res;
2 
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import org.robolectric.util.Logger;
6 
7 @SuppressWarnings("NewApi")
8 public class RawResourceLoader {
9   private final ResourcePath resourcePath;
10 
RawResourceLoader(ResourcePath resourcePath)11   public RawResourceLoader(ResourcePath resourcePath) {
12     this.resourcePath = resourcePath;
13   }
14 
loadTo(PackageResourceTable resourceTable)15   public void loadTo(PackageResourceTable resourceTable) throws IOException {
16     load(resourceTable, "raw");
17     load(resourceTable, "drawable");
18   }
19 
load(PackageResourceTable resourceTable, String folderBaseName)20   public void load(PackageResourceTable resourceTable, String folderBaseName) throws IOException {
21     Path resourceBase = resourcePath.getResourceBase();
22     for (Path dir : Fs.listFiles(resourceBase, new DirBaseNameFilter(folderBaseName))) {
23       loadRawFiles(resourceTable, folderBaseName, dir);
24     }
25   }
26 
loadRawFiles(PackageResourceTable resourceTable, String resourceType, Path rawDir)27   private void loadRawFiles(PackageResourceTable resourceTable, String resourceType, Path rawDir)
28       throws IOException {
29     Qualifiers qualifiers;
30     try {
31       qualifiers = Qualifiers.fromParentDir(rawDir);
32     } catch (IllegalArgumentException e) {
33       Logger.warn(rawDir + ": " + e.getMessage());
34       return;
35     }
36 
37     for (Path file : Fs.listFiles(rawDir)) {
38       String fileBaseName = Fs.baseNameFor(file);
39       resourceTable.addResource(
40           resourceType,
41           fileBaseName,
42           new FileTypedResource(
43               file,
44               ResType.FILE,
45               new XmlContext(resourceTable.getPackageName(), file, qualifiers)));
46     }
47   }
48 }
49