You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileResourceManager.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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. package com.dmdirc.util.resourcemanager;
  23. import com.dmdirc.util.io.StreamUtils;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.net.MalformedURLException;
  30. import java.net.URL;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * Provides an easy way to access files inside a project.
  38. */
  39. public final class FileResourceManager extends ResourceManager {
  40. /** Base path for the project. */
  41. private final String basePath;
  42. /**
  43. * Creates a new instance of FileResourceManager.
  44. *
  45. * @param basePath Base path for the resource manager
  46. */
  47. protected FileResourceManager(final String basePath) {
  48. super();
  49. this.basePath = basePath;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public boolean resourceExists(final String resource) {
  54. final File file;
  55. if (resource.startsWith(basePath)) {
  56. file = new File(resource);
  57. } else {
  58. file = new File(basePath, resource);
  59. }
  60. return file.exists() && !file.isDirectory();
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public byte[] getResourceBytes(final String resource) {
  65. FileInputStream inputStream = null;
  66. final File file;
  67. if (resource.startsWith(basePath)) {
  68. file = new File(resource);
  69. } else {
  70. file = new File(basePath, resource);
  71. }
  72. if (!file.exists()) {
  73. return new byte[0];
  74. }
  75. if (file.isDirectory()) {
  76. return new byte[0];
  77. }
  78. final byte[] bytes = new byte[(int) file.length()];
  79. try {
  80. inputStream = new FileInputStream(file);
  81. inputStream.read(bytes);
  82. } catch (IOException ex) {
  83. return new byte[0];
  84. } finally {
  85. StreamUtils.close(inputStream);
  86. }
  87. return bytes;
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public InputStream getResourceInputStream(final String resource) {
  92. final File file;
  93. if (resource.startsWith(basePath)) {
  94. file = new File(resource);
  95. } else {
  96. file = new File(basePath, resource);
  97. }
  98. if (!file.exists()) {
  99. return null;
  100. }
  101. if (file.isDirectory()) {
  102. return null;
  103. }
  104. try {
  105. return new FileInputStream(file);
  106. } catch (FileNotFoundException ex) {
  107. return null;
  108. }
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public URL getResourceURL(final String resource) throws MalformedURLException {
  113. if (resourceExists(resource)) {
  114. return new File(basePath, resource).toURI().toURL();
  115. } else {
  116. return null;
  117. }
  118. }
  119. /** {@inheritDoc} */
  120. @Override
  121. public Map<String, byte[]> getResourcesEndingWithAsBytes(
  122. final String resourcesSuffix) {
  123. final List<File> files = getFileListing(new File(basePath));
  124. final Map<String, byte[]> resources = new HashMap<String, byte[]>();
  125. for (File file : files) {
  126. final String path = file.getPath().substring(basePath.length(),
  127. file.getPath().length());
  128. if (path.endsWith(resourcesSuffix)) {
  129. resources.put(path, getResourceBytes(path));
  130. }
  131. }
  132. return resources;
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public Map<String, byte[]> getResourcesStartingWithAsBytes(
  137. final String resourcesPrefix) {
  138. final List<File> files = getFileListing(new File(basePath));
  139. final Map<String, byte[]> resources = new HashMap<String, byte[]>();
  140. for (File file : files) {
  141. final String path = file.getPath().substring(basePath.length(),
  142. file.getPath().length());
  143. if (path.startsWith(resourcesPrefix)) {
  144. resources.put(path, getResourceBytes(path));
  145. }
  146. }
  147. return resources;
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public Map<String, InputStream> getResourcesStartingWithAsInputStreams(
  152. final String resourcesPrefix) {
  153. final List<File> files = getFileListing(new File(basePath));
  154. final Map<String, InputStream> resources = new HashMap<String, InputStream>();
  155. for (File file : files) {
  156. final String path = file.getPath().substring(basePath.length(),
  157. file.getPath().length());
  158. if (path.startsWith(resourcesPrefix)) {
  159. resources.put(path, getResourceInputStream(path));
  160. }
  161. }
  162. return resources;
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. public List<String> getResourcesStartingWith(final String resourcesPrefix) {
  167. final List<File> files = getFileListing(new File(basePath));
  168. final List<String> resources = new ArrayList<String>();
  169. for (File file : files) {
  170. final String path = file.getPath().substring(basePath.length(),
  171. file.getPath().length());
  172. if (path.startsWith(resourcesPrefix)) {
  173. resources.add(path);
  174. }
  175. }
  176. return resources;
  177. }
  178. /**
  179. * Returns a resursive listing of a directory tree.
  180. *
  181. * @param startingDirectory Starting directory for the file listing
  182. *
  183. * @return Recursive directory listing
  184. */
  185. private static List<File> getFileListing(final File startingDirectory) {
  186. final List<File> result = new ArrayList<File>();
  187. if (startingDirectory.listFiles() == null) {
  188. return result;
  189. }
  190. final List<File> files = Arrays.asList(startingDirectory.listFiles());
  191. for (File file : files) {
  192. if (file.isFile()) {
  193. result.add(file);
  194. } else {
  195. result.addAll(getFileListing(file));
  196. }
  197. }
  198. return result;
  199. }
  200. }