OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Rev 65 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 25 Rev 61
Line 16... Line 16...
16
import java.io.File;
16
import java.io.File;
17
import java.io.FileInputStream;
17
import java.io.FileInputStream;
18
import java.io.FileNotFoundException;
18
import java.io.FileNotFoundException;
19
import java.io.InputStream;
19
import java.io.InputStream;
20
 
20
 
-
 
21
import javax.swing.JFrame;
-
 
22
import javax.swing.JOptionPane;
-
 
23
 
21
public abstract class AbstractLocalTemplateProvider implements TemplateProvider {
24
public abstract class AbstractLocalTemplateProvider implements TemplateProvider {
22
 
25
 
23
    @Override
26
    @Override
24
    public InputStream getTemplate(String templateId, String langage, String type) {
27
    public InputStream getTemplate(String templateId, String language, String type) {
25
        try {
-
 
26
            final File templateFile = getTemplateFile(templateId, langage, type);
28
        final File file = getFileTemplate(templateId, language, type);
27
            if (templateFile == null || !templateFile.exists()) {
-
 
28
                return null;
-
 
29
            }
-
 
30
            return new FileInputStream(templateFile);
29
        return (file == null ? null : getInputStream(file));
31
        } catch (FileNotFoundException e) {
-
 
32
            return null;
-
 
33
        }
-
 
34
    }
30
    }
35
 
31
 
36
    @Override
-
 
37
    public InputStream getTemplatePrintConfiguration(String templateId, String langage, String type) {
-
 
38
        final File t = getTemplateFile(templateId, langage, type);
-
 
39
        final String name = t.getName();
-
 
40
        if (name.toLowerCase().endsWith(".ods")) {
32
    private FileInputStream getInputStream(File f) {
41
            final File file = new File(t.getParent(), name.substring(0, name.length() - 4) + ".odsp");
-
 
42
            try {
33
        try {
43
                return new FileInputStream(file);
34
            return new FileInputStream(f);
44
            } catch (FileNotFoundException e) {
35
        } catch (FileNotFoundException e) {
45
                System.err.println("No print configuration " + file.getAbsolutePath() + " for template id: " + templateId);
36
            System.err.println("Error: no file:" + f.getAbsolutePath());
46
                e.printStackTrace();
37
            e.printStackTrace();
47
            }
38
        }
-
 
39
        return null;
-
 
40
    }
-
 
41
 
-
 
42
    public File getFileTemplate(String templateId, String language, String type) {
-
 
43
        File templateFile = getTemplateFromLocalFile(templateId, language, type);
-
 
44
        if (templateFile != null && templateFile.exists()) {
-
 
45
            return templateFile;
-
 
46
        }
-
 
47
        templateFile = getTemplateFromLocalFile(templateId + ".ods", language, type);
-
 
48
        if (templateFile != null && templateFile.exists()) {
-
 
49
            return templateFile;
-
 
50
        }
-
 
51
        templateFile = getTemplateFromLocalFile(templateId + ".odt", language, type);
-
 
52
        if (templateFile != null && templateFile.exists()) {
-
 
53
            return templateFile;
48
        }
54
        }
49
        return null;
55
        return null;
50
    }
56
    }
51
 
57
 
52
    @Override
58
    @Override
53
    public InputStream getTemplateConfiguration(String templateId, String langage, String type) {
59
    public InputStream getTemplatePrintConfiguration(String templateId, String language, String type) {
54
        final File t = getTemplateFile(templateId, langage, type);
60
        final File file = getFileTemplatePrintConfiguration(templateId, language, type);
55
        final String name = t.getName();
-
 
56
        if (name.toLowerCase().endsWith(".ods")) {
-
 
57
            final File file = new File(t.getParent(), name.substring(0, name.length() - 4) + ".xml");
-
 
58
            try {
-
 
59
                return new FileInputStream(file);
61
        return getInputStream(file);
60
            } catch (FileNotFoundException e) {
-
 
61
                System.err.println("No template configuration " + file.getAbsolutePath() + " for template id: " + templateId);
-
 
62
                e.printStackTrace();
-
 
63
            }
62
    }
-
 
63
 
-
 
64
    public File getFileTemplatePrintConfiguration(String templateId, String language, String type) {
-
 
65
        final File file = getTemplateFromLocalFile(templateId + ".odsp", language, type);
-
 
66
        return file;
64
        }
67
    }
-
 
68
 
-
 
69
    @Override
-
 
70
    public InputStream getTemplateConfiguration(String templateId, String language, String type) {
-
 
71
        final File file = getFileTemplateConfiguration(templateId, language, type);
65
        return null;
72
        return getInputStream(file);
66
    }
73
    }
67
 
74
 
68
    public abstract File getTemplateFile(String templateId, String langage, String type);
75
    public File getFileTemplateConfiguration(String templateId, String language, String type) {
-
 
76
        final File file = getTemplateFromLocalFile(templateId + ".xml", language, type);
-
 
77
        return file;
-
 
78
    }
-
 
79
 
-
 
80
    protected abstract File getTemplateFromLocalFile(String templateIdWithExtension, String language, String type);
69
 
81
 
70
    @Override
82
    @Override
71
    public abstract String getTemplatePath(String templateId, String langage, String type);
83
    public abstract String getTemplatePath(String templateId, String language, String type);
-
 
84
 
-
 
85
    public static String insertBeforeExtenstion(String fileName, String text) {
-
 
86
        final int index = fileName.lastIndexOf('.');
-
 
87
        if (index < 0) {
-
 
88
            throw new IllegalArgumentException("No extension found in fileName '" + fileName + "'");
-
 
89
        }
-
 
90
        if (index == 0) {
-
 
91
            return fileName + text;
-
 
92
        }
-
 
93
        final String name = fileName.substring(0, index);
-
 
94
        final String ext = fileName.substring(index);
-
 
95
        return name + text + ext;
-
 
96
    }
72
 
97
 
-
 
98
    void ensureDelete(File f) {
-
 
99
        for (int i = 0; i < 2; i++) {
-
 
100
            if (f.delete()) {
-
 
101
                return;
-
 
102
            }
-
 
103
            JOptionPane.showMessageDialog(new JFrame(), "Fichier " + f.getAbsolutePath() + " vérrouillé.\nMerci de fermer tout programme pouvant y accéder (OpenOffice).");
-
 
104
        }
-
 
105
        f.deleteOnExit();
-
 
106
 
-
 
107
    }
73
}
108
}