Path: blob/master/sourcetools/j9nls/com/ibm/oti/NLSTool/J9ReverseNLS.java
6004 views
/*******************************************************************************1* Copyright (c) 2004, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.oti.NLSTool;2223import java.io.BufferedReader;24import java.io.ByteArrayOutputStream;25import java.io.File;26import java.io.FileInputStream;27import java.io.FileWriter;28import java.io.IOException;29import java.io.InputStream;30import java.io.OutputStream;31import java.io.StringReader;32import java.io.Writer;33import java.util.Hashtable;34import java.util.Iterator;35import java.util.Properties;36import java.util.Vector;3738public class J9ReverseNLS implements NLSConstants {3940public static void main(String[] args) throws IOException {4142if (args.length != 2 || args[0].compareToIgnoreCase("-help") == 0) {43printUsage();44System.exit(0);45}4647String propertiesFile = args[0];48String locale = args[1];4950J9NLS nlsTool = new J9NLS();51Hashtable nlsFiles = nlsTool.searchNLSFiles();5253J9NLS.dp("Reading " + propertiesFile + "...");54Properties javaProperties = new Properties();55javaProperties.load(new FileInputStream(propertiesFile));5657Vector baseNLSFiles = (Vector)nlsFiles.get("");58for (Iterator iter = baseNLSFiles.iterator(); iter.hasNext();) {59File nlsInputFile = (File) iter.next();60Properties nlsProperties = new Properties();61InputStream is = new FileInputStream(nlsInputFile);62nlsProperties.load(is);63is.close();6465File nlsOutputFile = localeSpecificNLSFile(nlsInputFile, locale);66J9NLS.dp("Writing " + nlsOutputFile + "...");67Writer out = new FileWriter(nlsOutputFile);68out.write("# This file was automatically reverse engineered from " + nlsInputFile.getName() + " and " + propertiesFile + "\n");69out.write(MODULE_KEY + "=" + nlsProperties.getProperty(MODULE_KEY) + "\n");70out.write(HEADER_KEY + "=" + nlsProperties.getProperty(HEADER_KEY) + "\n");71out.write("\n");7273Vector orderedKeys = nlsTool.getOrderedKeys(nlsProperties, nlsInputFile);74for (int index = 0; index < orderedKeys.size(); index++) {75String key = (String) orderedKeys.get(index);76String smallKey = nlsProperties.getProperty(MODULE_KEY) + nlsTool.formatMsgNumber(index);77String value = javaProperties.getProperty(smallKey);7879if (value != null) {80out.write(encode(key, value));81} else {82System.err.println("Warning: no translated value found for " + key);83out.write("\n#translation required for the following key: \n");84out.write("#" + encode(key, nlsProperties.getProperty(key)) + "\n");85}86}8788out.close();89}90}919293private static void printUsage() {94System.out.println("Reverse engineers .NLS files from a .properties file.");95System.out.println("Usage: J9ReverseNLS java<locale>.properties locale");96System.out.println("e.g. J9ReverseNLS java_fr.properties _fr");97}9899private static File localeSpecificNLSFile(File baseFile, String locale) {100String baseName = baseFile.getName();101String extension = baseName.substring(baseName.indexOf('.'), baseName.length());102baseName = baseName.substring(0, baseName.indexOf('.'));103return new File(baseFile.getParentFile(), baseName + locale + extension);104}105106private static String encode(String key, String plainText) throws IOException {107Properties props = new Properties();108OutputStream os = new ByteArrayOutputStream();109String result = "";110111props.setProperty(key, plainText);112props.store(os, null);113114BufferedReader reader = new BufferedReader(new StringReader( os.toString() ) );115String line = reader.readLine();116/* skip leading comments */117while (line != null && line.length() > 0 && line.charAt(0) == '#') {118line = reader.readLine();119}120/* read the rest */121while (line != null) {122result += line;123result += "\n";124line = reader.readLine();125}126127return result;128}129}130131132