01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings.resource;
14:
15: import org.wings.StaticResource;
16:
17: import java.io.File;
18: import java.io.FileInputStream;
19: import java.io.FileNotFoundException;
20: import java.io.InputStream;
21:
22: /**
23: * For externalizing a file as resource.
24: *
25: * @author Holger Engels
26: */
27: public class FileResource extends StaticResource {
28:
29: private final File file;
30:
31: public FileResource(String name) {
32: this (new File(name));
33: }
34:
35: public FileResource(File file) {
36: this (file, null, "unknown");
37: }
38:
39: public FileResource(File file, String ext, String mt) {
40: super (ext, mt);
41: this .file = file;
42: if (extension == null) {
43: int dotIndex = file.getName().lastIndexOf('.');
44: if (dotIndex > -1) {
45: extension = file.getName().substring(dotIndex + 1);
46: }
47: }
48: try {
49: size = (int) file.length();
50: } catch (SecurityException ignore) {
51: }
52: }
53:
54: @Override
55: public String toString() {
56: return getId() + (file != null ? " " + file.getName() : "");
57: }
58:
59: public final File getFile() {
60: return file;
61: }
62:
63: @Override
64: protected final InputStream getResourceStream()
65: throws ResourceNotFoundException {
66: try {
67: return new FileInputStream(file);
68: } catch (FileNotFoundException e) {
69: throw new ResourceNotFoundException(
70: "Unable to open resource file: " + file);
71: }
72: }
73: }
|