=== modified file 'tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/FileUtils.java' --- tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/FileUtils.java 2009-11-18 19:01:08 +0000 +++ tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/FileUtils.java 2010-10-15 03:41:37 +0000 @@ -26,44 +26,54 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import org.hisp.dhis.i18nresourceeditor.exception.I18nResourceException; -import org.hisp.dhis.i18nresourceeditor.exception.I18nInvalidLocaleException; - -import java.util.Hashtable; -import java.util.Properties; -import java.util.Locale; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.util.Hashtable; +import java.util.Locale; +import java.util.Properties; + +import org.hisp.dhis.i18nresourceeditor.exception.I18nInvalidLocaleException; +import org.hisp.dhis.i18nresourceeditor.exception.I18nResourceException; /** * @author Oyvind Brucker */ -public class FileUtils { +public class FileUtils +{ - public static Hashtable loadResource(String path) - throws I18nResourceException { - File file = new File(path); + public static Hashtable loadResource( String path ) + throws I18nResourceException + { + File file = new File( path ); Properties prop = new Properties(); - try { - prop.load(new FileInputStream(file)); - } catch (FileNotFoundException fnfe) { - throw new I18nResourceException("Error while loading resource, file not found", fnfe); - } catch (IOException ioe) { - throw new I18nResourceException("I/O Error while loading resource", ioe); - } catch (IllegalArgumentException iae) { - throw new I18nResourceException("Encoding related error while loading resource", iae); + try + { + prop.load( new FileInputStream( file ) ); + } + catch ( FileNotFoundException fnfe ) + { + throw new I18nResourceException( "Error while loading resource, file not found", fnfe ); + } + catch ( IOException ioe ) + { + throw new I18nResourceException( "I/O Error while loading resource", ioe ); + } + catch ( IllegalArgumentException iae ) + { + throw new I18nResourceException( "Encoding related error while loading resource", iae ); } Hashtable hash = new Hashtable(); - for (Object key : prop.keySet()) { + for ( Object key : prop.keySet() ) + { String k = (String) key; - String v = prop.getProperty(k); - hash.put(k, v); + String v = prop.getProperty( k ); + hash.put( k, v ); } return hash; @@ -71,57 +81,104 @@ /** * @param content key/value pairs to save - * @param path full path for the resource including complete filename with locale information + * @param path full path for the resource including complete filename with + * locale information * @throws I18nResourceException */ - public static void saveResource(Hashtable content, String path) - throws I18nResourceException { - File file = new File(path); + public static void saveResource( Hashtable content, String path ) + throws I18nResourceException + { + File file = new File( path ); Properties prop = new Properties(); - prop.putAll(content); - - try { - prop.store(new FileOutputStream(file, true), "Resource generated by I18n Resource Editor"); - } catch (FileNotFoundException fnfe) { - throw new I18nResourceException("Error while saving resource, file not found", fnfe); - } catch (IOException ioe) { - throw new I18nResourceException("I/O Error while saving resource", ioe); + prop.putAll( content ); + + try + { + clearFile( file ); + + prop.store( new FileOutputStream( file, true ), "Resource generated by I18n Resource Editor" ); + } + catch ( FileNotFoundException fnfe ) + { + throw new I18nResourceException( "Error while saving resource, file not found", fnfe ); + } + catch ( IOException ioe ) + { + throw new I18nResourceException( "I/O Error while saving resource", ioe ); } } /** * Converts a string from a filename convention (en_GB) to a locale object - * Examples of valid input: "no", "no_NO", "no_NO_NY", "no_NO_B", "no_NO_POSIX" - * + * Examples of valid input: "no", "no_NO", "no_NO_NY", "no_NO_B", + * "no_NO_POSIX" + * * @param name String to convert * @return locale */ - public static Locale getLocaleFromFilename(String name) - throws I18nInvalidLocaleException { - - if (name.startsWith("_")) { - name = name.replaceFirst("_", ""); - } - - String[] tmp = name.split("_"); - - if (tmp.length == 1) { - if (tmp[0].length() == 2) { - return new Locale(tmp[0]); - } - } else if (tmp.length == 2) { - if (tmp[0].length() == 2 && tmp[1].length() == 2) { - return new Locale(tmp[0], tmp[1]); - } - } else if (tmp.length == 3) { - if (tmp[0].length() == 2 && tmp[1].length() == 2) { - return new Locale(tmp[0], tmp[1], tmp[2]); - } - } - - throw new I18nInvalidLocaleException("Unable to determine Locale for string: " + name); + public static Locale getLocaleFromFilename( String name ) + throws I18nInvalidLocaleException + { + + if ( name.startsWith( "_" ) ) + { + name = name.replaceFirst( "_", "" ); + } + + String[] tmp = name.split( "_" ); + + if ( tmp.length == 1 ) + { + if ( tmp[0].length() == 2 ) + { + return new Locale( tmp[0] ); + } + } + else if ( tmp.length == 2 ) + { + if ( tmp[0].length() == 2 && tmp[1].length() == 2 ) + { + return new Locale( tmp[0], tmp[1] ); + } + } + else if ( tmp.length == 3 ) + { + if ( tmp[0].length() == 2 && tmp[1].length() == 2 ) + { + return new Locale( tmp[0], tmp[1], tmp[2] ); + } + } + + throw new I18nInvalidLocaleException( "Unable to determine Locale for string: " + name ); + } + + /** + * This method is used to clear/empty a specified file, if exists. + * + * @param file the given file + * @return void + * @throws I18nResourceException + */ + private static void clearFile( File file ) + throws I18nResourceException + { + try + { + FileOutputStream erasor = new FileOutputStream( file ); + erasor.write( (new String()).getBytes() ); + erasor.close(); + } + catch ( FileNotFoundException fnfe ) + { + throw new I18nResourceException( "Error while saving resource, file not found", fnfe ); + } + catch ( IOException ioe ) + { + throw new I18nResourceException( "I/O Error while clearing/emptying file's name" + file.getAbsolutePath(), + ioe ); + } } } === modified file 'tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/ResourceManager.java' --- tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/ResourceManager.java 2009-03-09 11:13:57 +0000 +++ tools/dhis-i18n-resourceeditor/src/main/java/org/hisp/dhis/i18nresourceeditor/persistence/ResourceManager.java 2010-10-15 03:41:37 +0000 @@ -162,7 +162,7 @@ if (module.isModified(locale)) { String completeResourcePath = module.getResourcePath().getAbsolutePath() + File.separator + module.getBasename() + "_" + locale + ".properties"; - + FileUtils.saveResource(translationMap.get(locale), completeResourcePath); log.outInfo("Saved resource: " + completeResourcePath);