001/* 002 * #%L 003 * Netarchivesuite - common 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023package dk.netarkivet.common.tools; 024 025import java.io.BufferedReader; 026import java.io.BufferedWriter; 027import java.io.File; 028import java.io.FileInputStream; 029import java.io.FileOutputStream; 030import java.io.FileReader; 031import java.io.IOException; 032import java.io.OutputStreamWriter; 033import java.util.Properties; 034 035/** 036 * Program to reformat a a Translation file. This executable takes three parameters : 037 * <ol> 038 * <li>base properties file, that defines the key ordering</li> 039 * <li>properties file to reformat, sorting keys in the order defined by the first file</li> 040 * <li>character encoding for reformat and output file</li> 041 * </ol> 042 * <p> 043 * The second file is overwritten 044 */ 045public class ReformatTranslationFile { 046 047 /** 048 * The main program. 049 * 050 * @param args the 3 arguments 051 * @throws IOException if unable to read or write the files. 052 */ 053 public static void main(String[] args) throws IOException { 054 055 if (args.length != 3) { 056 System.out.println("Usage: java " + ReformatTranslationFile.class.getName() 057 + "\n\t<properties file defining key order>" + "\n\t<properties file to fetch values from>" 058 + "\n\t<second and output file encoding>"); 059 System.exit(1); 060 } 061 062 File order = new File(args[0]); 063 File reformat = new File(args[1]); 064 String encoding = args[2]; 065 066 Properties defaultProps = new Properties(); 067 defaultProps.load(new FileInputStream(order)); 068 069 Properties sourceProps = new Properties(); 070 sourceProps.load(new FileInputStream(reformat)); 071 072 if (reformat.delete()) { 073 reformat.createNewFile(); 074 } 075 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(reformat), encoding)); 076 077 BufferedReader in = new BufferedReader(new FileReader(order)); 078 String line = null; 079 while ((line = in.readLine()) != null) { 080 if (line.indexOf("=") == -1) { 081 out.write(line); 082 out.newLine(); 083 } else { 084 String[] parts = line.split("="); 085 String key = parts[0].trim(); 086 out.write(key + "="); 087 for (char c : parts[1].toCharArray()) { 088 if (Character.isWhitespace(c)) { 089 out.write(c); 090 } else { 091 break; 092 } 093 } 094 String propVal = sourceProps.getProperty(key); 095 if (propVal == null) { 096 System.out.println("No value for key '" + key + "' in right file"); 097 out.write(defaultProps.getProperty(key)); 098 } else { 099 out.write(propVal); 100 } 101 out.newLine(); 102 103 } 104 } 105 106 in.close(); 107 out.close(); 108 109 System.out.println("Successfully reformatted " + reformat.getAbsolutePath()); 110 System.exit(0); 111 } 112 113}