1
package org.hisp.dhis.i18nresourceeditor.persistence;
4
* Copyright (c) 2004-2005, University of Oslo
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.
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.
29
import org.hisp.dhis.i18nresourceeditor.exception.I18nResourceException;
30
import org.hisp.dhis.i18nresourceeditor.exception.I18nInvalidLocaleException;
32
import java.util.Hashtable;
33
import java.util.Properties;
34
import java.util.Locale;
36
import java.io.FileNotFoundException;
37
import java.io.IOException;
38
import java.io.FileInputStream;
39
import java.io.FileOutputStream;
42
* @author Oyvind Brucker
44
public class FileUtils {
46
public static Hashtable<String, String> loadResource(String path)
47
throws I18nResourceException {
48
File file = new File(path);
50
Properties prop = new Properties();
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);
61
Hashtable<String, String> hash = new Hashtable<String, String>();
63
for (Object key : prop.keySet()) {
64
String k = (String) key;
65
String v = prop.getProperty(k);
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
77
public static void saveResource(Hashtable content, String path)
78
throws I18nResourceException {
79
File file = new File(path);
81
Properties prop = new Properties();
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);
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"
99
* @param name String to convert
102
public static Locale getLocaleFromFilename(String name)
103
throws I18nInvalidLocaleException {
105
if (name.startsWith("_")) {
106
name = name.replaceFirst("_", "");
109
String[] tmp = name.split("_");
111
if (tmp.length == 1) {
112
if (tmp[0].length() == 2) {
113
return new Locale(tmp[0]);
115
} else if (tmp.length == 2) {
116
if (tmp[0].length() == 2 && tmp[1].length() == 2) {
117
return new Locale(tmp[0], tmp[1]);
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]);
125
throw new I18nInvalidLocaleException("Unable to determine Locale for string: " + name);