Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / exportto / impl / DefaultExporttoManager.java @ 34981

History | View | Annotate | Download (4.83 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
package org.gvsig.exportto.impl;
23

    
24
import java.io.File;
25
import java.io.FileInputStream;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.Map;
33
import java.util.Properties;
34

    
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.exportto.ExporttoManager;
39
import org.gvsig.exportto.ExporttoService;
40
import org.gvsig.fmap.dal.DataServerExplorer;
41
import org.gvsig.fmap.dal.NewDataStoreParameters;
42

    
43
/**
44
 * Default {@link ExporttoManager} implementation.
45
 * 
46
 * @author gvSIG Team
47
 * @version $Id$
48
 */
49
public class DefaultExporttoManager implements ExporttoManager {
50
    private static final Logger LOG =
51
        LoggerFactory.getLogger(DefaultExporttoManager.class);
52

    
53
    @SuppressWarnings("rawtypes")
54
    private Map filters = null;
55
    private static final String EXPORTTOFILTERS_FILENAME = "exporttofilters.properties";  
56
    private static final String GVSIGPREFERENCES_FOLDERNAME = "gvSIG";  
57

    
58
    @SuppressWarnings("rawtypes")
59
    public DefaultExporttoManager() {
60
        super(); 
61
        filters = new HashMap();
62
        try {
63
            initializeFilters();
64
        } catch (IOException e) {
65
            LOG.error("Error reading the exporttofilters.properties file");
66
        }       
67
    }
68

    
69
    @SuppressWarnings({ "rawtypes", "unchecked" })
70
    private void initializeFilters() throws IOException {        
71
        String path = System.getProperty("user.home");
72
        path = path + File.separatorChar + GVSIGPREFERENCES_FOLDERNAME;
73
        File directory = new File(path);
74
        if (!directory.exists()){
75
            directory.mkdirs();
76
        }
77
        path = path + File.separatorChar + EXPORTTOFILTERS_FILENAME;
78
        File exporttoFiltersFile = new File(path);
79
        if (!exporttoFiltersFile.exists()){
80
            File templateFile = new File(getClass().getClassLoader().getResource("org/gvsig/exportto/lib/impl/exporttofilters.properties").getFile());
81
            if (!templateFile.exists()){
82
                return;
83
            }            
84
            copy(templateFile, exporttoFiltersFile);           
85
        }
86
        Properties properties = new Properties();
87
        properties.load(new FileInputStream(exporttoFiltersFile));
88
        Iterator it = properties.keySet().iterator();
89
        while (it.hasNext()){
90
            String key = (String)it.next();
91
            filters.put(key, properties.get(key));
92
        }
93
    }
94

    
95
    private void copy(File sourceLocation, File targetLocation)
96
    throws IOException {
97
        if (sourceLocation.isDirectory()) {
98
            if (!targetLocation.exists()) {
99
                targetLocation.mkdir();
100
            }
101

    
102
            String[] children = sourceLocation.list();
103
            for (int i = 0; i < children.length; i++) {
104
                copy(new File(sourceLocation, children[i]), new File(
105
                    targetLocation, children[i]));
106
            }
107
        } else {
108
            targetLocation.getParentFile().mkdirs();
109

    
110
            InputStream in = new FileInputStream(sourceLocation);
111
            OutputStream out = new FileOutputStream(targetLocation);
112

    
113
            // Copy the bits from instream to outstream
114
            byte[] buf = new byte[1024];
115
            int len;
116
            while ((len = in.read(buf)) > 0) {
117
                out.write(buf, 0, len);
118
            }
119
            in.close();
120
            out.close();
121
        }
122
    }
123

    
124
    public ExporttoService getExporttoService(DataServerExplorer dataServerExplorer, NewDataStoreParameters newDataStoreParameters) {
125
        return new DefaultExporttoService(dataServerExplorer, newDataStoreParameters);
126
    }
127

    
128
    @SuppressWarnings("rawtypes")
129
    public Iterator getPredefinedFilters() {        
130
        return filters.keySet().iterator();
131
    }
132

    
133
    public String getFilter(String filterName) {
134
        if (filters.containsKey(filterName)){
135
            return (String)filters.get(filterName);
136
        }
137
        return null;
138
    }
139

    
140
}