Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.bsq / src / main / java / org / gvsig / fmap / dal / fileutils / impl / DefaultPRJFile.java @ 43867

History | View | Annotate | Download (2.44 KB)

1

    
2
package org.gvsig.fmap.dal.fileutils.impl;
3

    
4
import org.gvsig.fmap.dal.fileutils.PRJFile;
5
import java.io.File;
6
import java.io.IOException;
7
import org.apache.commons.io.FileUtils;
8
import org.apache.commons.io.FilenameUtils;
9
import org.apache.commons.lang3.StringUtils;
10
import org.cresques.cts.ICRSFactory;
11
import org.cresques.cts.IProjection;
12
import org.gvsig.fmap.crs.CRSFactory;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15

    
16

    
17
public class DefaultPRJFile implements PRJFile {
18
            
19
    private static final Logger logger = LoggerFactory.getLogger(DefaultPRJFile.class);
20
    
21
        
22
    private File source;
23
    private IProjection crs;
24
    
25
    public DefaultPRJFile() {
26
        this.source = null;
27
        this.crs = null;        
28
    }
29
    
30
    @SuppressWarnings("OverridableMethodCallInConstructor")
31
    public DefaultPRJFile(File file) {
32
        this();
33
        this.read(file);
34
    }
35
    
36
    @Override
37
    public File getFile(File file) {
38
        File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION);
39
        return f;
40
    }
41
    
42
    @Override
43
    public File getFile() {
44
        return source;
45
    }
46
    
47
    @Override
48
    public IProjection getCRS() {
49
        return this.crs;
50
    }
51
    
52
    @Override
53
    public void setCRS(IProjection crs) {
54
        this.crs = crs;
55
    }
56
    
57
    @Override
58
    public void read(File file) {
59
        File f = this.getFile(file);
60
        if (f.exists()) {
61
            try {
62
                String theContents = FileUtils.readFileToString(f);
63
                if (StringUtils.isNotEmpty(theContents)){
64
                    IProjection theCrs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, theContents);
65
                    this.crs = theCrs;
66
                    this.source = f.getAbsoluteFile();
67
                }
68
            } catch (IOException e) {
69
                logger.warn("Couldn't read "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e);
70
            }
71
        }
72
            
73
    }
74
    
75
    @Override
76
    public void write(File file) throws IOException {
77
        File f = this.getFile(file);
78
        try {
79
            String export = crs.export(ICRSFactory.FORMAT_WKT_ESRI);
80
            if(export!=null){
81
                FileUtils.writeStringToFile(f, export);
82
                this.source = f;
83
            }
84
        } catch (Exception e) {
85
            logger.warn("Couldn't write "+FILE_EXTENSION+" file ("+f.getAbsolutePath()+").",e);
86
            throw e;
87
        }
88
    }
89
}