Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / SQLiteUtils.java @ 47579

History | View | Annotate | Download (3.42 KB)

1
package org.gvsig.sqlite.dal;
2

    
3
import org.gvsig.sqlite.dal.geopackage.GeopackageUtils;
4
import java.io.File;
5
import org.apache.commons.io.FileUtils;
6
import org.apache.commons.io.FilenameUtils;
7
import org.apache.commons.lang3.StringUtils;
8
import static org.gvsig.sqlite.dal.SQLiteHelper.LOGGER;
9

    
10
/**
11
 *
12
 * @author gvSIG Team
13
 */
14
public class SQLiteUtils {
15
    
16
    public static final String SQLITE_JDBC_DRIVER = "org.sqlite.JDBC";
17
    
18
    public static final String EXTENSION_SQLITE = "sqlite";
19
    public static final String EXTENSION_SQLITE3 = "sqlite3";
20
    public static final String EXTENSION_DB = "db";
21
    public static final String EXTENSION_DB3 = "db3";
22
    public static final String EXTENSION_S3DB = "s3db";
23
    public static final String EXTENSION_SL3 = "sl3";
24
        
25
    public static File[] getFiles(File f) {
26
        if( f==null ) {
27
            return null;
28
        }
29
        String s = removeFileNameExtension(f.getName()) + ".";
30
        File[] files = new File[] {
31
            new File(f.getParentFile(), s+GeopackageUtils.EXTENSION),
32
            new File(f.getParentFile(), s+GeopackageUtils.EXTENDED_EXTENSION),            
33
            new File(f.getParentFile(), s+EXTENSION_DB),
34
            new File(f.getParentFile(), s+EXTENSION_DB3),
35
            new File(f.getParentFile(), s+EXTENSION_SQLITE),
36
            new File(f.getParentFile(), s+EXTENSION_SQLITE3),
37
            new File(f.getParentFile(), s+EXTENSION_S3DB),
38
            new File(f.getParentFile(), s+EXTENSION_SL3)
39
        };
40
        return files;
41
    }
42
    
43
    public static boolean existsdb(File f) {
44
        File[] files = getFiles(f);
45
        for (File file : files) {
46
            if( file.exists() ) {
47
                return true;
48
            }
49
        }
50
        return false;
51
    }
52
    
53
    public static boolean removedb(File f) {
54
        try {
55
            File[] files = getFiles(f);
56
            for (File file : files) {
57
                if( file.exists() ) {
58
                    FileUtils.delete(file);
59
                }
60
            }
61
        } catch(Throwable t) {
62
            return false;
63
        }
64
        return true;
65
    }
66
    
67
    public static File normalizeFile(File f) {
68
        if( f==null ) {
69
            return null;
70
        }
71
        f = new File(f.getParentFile(), normalizeFileName(f.getName()));
72
        return f;
73
    }
74

    
75
    public static String normalizeFileName(String name) {
76
        String s = removeFileNameExtension(name);
77
        if( s == null ) {
78
            return null;
79
        }
80
        return s+"."+GeopackageUtils.EXTENSION;
81
    }
82
    
83
    public static String removeFileNameExtension(String name) {
84
        if( StringUtils.isBlank(name) ) {
85
            return null;
86
        }
87
        return FilenameUtils.removeExtension(name);
88
    }
89

    
90
    public static File getLocalFile(SQLiteConnectionParameters params) {
91
        File f = params.getFile();
92
        return normalizeFile(f);
93
    }
94
        
95
    public static String getConnectionURL(SQLiteConnectionParameters params) {
96
        String fname = params.getFile().getAbsolutePath().replace("\\","/");
97
        if( StringUtils.isBlank(FilenameUtils.getExtension(fname)) ) {
98
            if(fname.endsWith(".")) {
99
                fname = fname + GeopackageUtils.EXTENSION;
100
            } else {
101
                fname = fname + "." +GeopackageUtils.EXTENSION;
102
            }
103
            params.setFile(new File(fname));
104
        }
105
        String connectionURL = "jdbc:sqlite:" + fname;
106
        return connectionURL;        
107
    }
108
    
109
}