Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / resource / file / FileResource.java @ 23900

History | View | Annotate | Download (3.1 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.data.resource.file;
32

    
33
import java.io.File;
34
import java.io.FileInputStream;
35
import java.io.FileNotFoundException;
36
import java.text.MessageFormat;
37

    
38
import org.gvsig.fmap.data.exceptions.InitializeException;
39
import org.gvsig.fmap.data.resource.ResourceParameters;
40
import org.gvsig.fmap.data.resource.exception.AccessResourceException;
41
import org.gvsig.fmap.data.resource.exception.PrepareResourceException;
42
import org.gvsig.fmap.data.resource.exception.ResourceException;
43
import org.gvsig.fmap.data.resource.exception.ResourceNotifyOpenException;
44
import org.gvsig.fmap.data.resource.spi.AbstractResource;
45

    
46
/**
47
 * @author jmvivo
48
 *
49
 */
50
public class FileResource extends AbstractResource {
51

    
52
        final public static String RESOURCE_TYPE_NAME = "file";
53
        final public static String RESOURCE_TYPE_DESCRIPTION = "File of filesystem";
54

    
55
        private File file;
56

    
57
        public FileResource(FileResourceParameters params)
58
                        throws InitializeException {
59
                super(params);
60
                file = null;
61
        }
62

    
63
        public Object get() throws AccessResourceException {
64
                try {
65
                        prepare();
66
                } catch (PrepareResourceException e) {
67
                        throw new AccessResourceException(this, e);
68
                }
69
                return getParameters().get("fileName");
70
        }
71

    
72
        public String getFileName() throws AccessResourceException {
73
                return (String) get();
74
        }
75

    
76
        public File getFile() throws AccessResourceException {
77
                if (file == null) {
78
                        file = new File(getFileName());
79
                }
80
                return file;
81
        }
82

    
83
        public FileInputStream getFileInputStream() throws AccessResourceException,
84
                        FileNotFoundException {
85
                FileInputStream fis = new FileInputStream(getFile());
86
                try {
87
                        notifyOpen();
88
                } catch (ResourceNotifyOpenException e) {
89
                        throw new AccessResourceException(this, e);
90
                }
91
                return fis;
92
        }
93

    
94
        public String getName() throws AccessResourceException {
95
                return MessageFormat.format("FileResource({1})", new Object[] { getFileName() });
96
        }
97

    
98
        public boolean isThis(ResourceParameters parameters)
99
                        throws ResourceException {
100
                if (!(parameters instanceof FileResourceParameters)) {
101
                        return false;
102
                }
103
                FileResourceParameters params;
104
                params = (FileResourceParameters) parameters.getCopy();
105
                prepare(params);
106
                return params.getFileName().equals(this.getFileName());
107

    
108
        }
109

    
110
}
111