~inf5750-charting/dhis2-academy/charting2

« back to all changes in this revision

Viewing changes to tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/FileUtils.java

  • Committer: Saptarshi
  • Date: 2009-03-09 11:13:57 UTC
  • Revision ID: sunbiz@gmail.com-20090309111357-hxeg44u2d9i09i6u
Adding 18n Resource Editor to launchpad trunk with support for Unicode languages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.i18nresourceeditor.persistence;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2005, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the <ORGANIZATION> nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
import org.hisp.dhis.i18nresourceeditor.exception.I18nResourceException;
 
30
import org.hisp.dhis.i18nresourceeditor.exception.I18nInvalidLocaleException;
 
31
 
 
32
import java.util.Hashtable;
 
33
import java.util.Properties;
 
34
import java.util.Locale;
 
35
import java.io.File;
 
36
import java.io.FileNotFoundException;
 
37
import java.io.IOException;
 
38
import java.io.FileInputStream;
 
39
import java.io.FileOutputStream;
 
40
 
 
41
/**
 
42
 * @author Oyvind Brucker
 
43
 */
 
44
public class FileUtils {
 
45
 
 
46
    public static Hashtable<String, String> loadResource(String path)
 
47
            throws I18nResourceException {
 
48
        File file = new File(path);
 
49
 
 
50
        Properties prop = new Properties();
 
51
 
 
52
        try {
 
53
            prop.load(new FileInputStream(file));
 
54
        } catch (FileNotFoundException fnfe) {
 
55
            throw new I18nResourceException("Error while loading resource, file not found", fnfe);
 
56
        } catch (IOException ioe) {
 
57
            throw new I18nResourceException("I/O Error while loading resource", ioe);
 
58
        } catch (IllegalArgumentException iae) {
 
59
            throw new I18nResourceException("Encoding related error while loading resource", iae);
 
60
        }
 
61
        Hashtable<String, String> hash = new Hashtable<String, String>();
 
62
 
 
63
        for (Object key : prop.keySet()) {
 
64
            String k = (String) key;
 
65
            String v = prop.getProperty(k);
 
66
            hash.put(k, v);
 
67
        }
 
68
 
 
69
        return hash;
 
70
    }
 
71
 
 
72
    /**
 
73
     * @param content key/value pairs to save
 
74
     * @param path    full path for the resource including complete filename with locale information
 
75
     * @throws I18nResourceException
 
76
     */
 
77
    public static void saveResource(Hashtable content, String path)
 
78
            throws I18nResourceException {
 
79
        File file = new File(path);
 
80
 
 
81
        Properties prop = new Properties();
 
82
 
 
83
        prop.putAll(content);
 
84
 
 
85
        try {
 
86
            prop.store(new FileOutputStream(file, true), "Resource generated by I18n Resource Editor");
 
87
        } catch (FileNotFoundException fnfe) {
 
88
            throw new I18nResourceException("Error while saving resource, file not found", fnfe);
 
89
        } catch (IOException ioe) {
 
90
            throw new I18nResourceException("I/O Error while saving resource", ioe);
 
91
        }
 
92
 
 
93
    }
 
94
 
 
95
    /**
 
96
     * Converts a string from a filename convention (en_GB) to a locale object
 
97
     * Examples of valid input: "no", "no_NO", "no_NO_NY", "no_NO_B", "no_NO_POSIX"
 
98
     *
 
99
     * @param name String to convert
 
100
     * @return locale
 
101
     */
 
102
    public static Locale getLocaleFromFilename(String name)
 
103
            throws I18nInvalidLocaleException {
 
104
 
 
105
        if (name.startsWith("_")) {
 
106
            name = name.replaceFirst("_", "");
 
107
        }
 
108
 
 
109
        String[] tmp = name.split("_");
 
110
 
 
111
        if (tmp.length == 1) {
 
112
            if (tmp[0].length() == 2) {
 
113
                return new Locale(tmp[0]);
 
114
            }
 
115
        } else if (tmp.length == 2) {
 
116
            if (tmp[0].length() == 2 && tmp[1].length() == 2) {
 
117
                return new Locale(tmp[0], tmp[1]);
 
118
            }
 
119
        } else if (tmp.length == 3) {
 
120
            if (tmp[0].length() == 2 && tmp[1].length() == 2) {
 
121
                return new Locale(tmp[0], tmp[1], tmp[2]);
 
122
            }
 
123
        }
 
124
 
 
125
        throw new I18nInvalidLocaleException("Unable to determine Locale for string: " + name);
 
126
    }
 
127
}