|
From: <tr...@us...> - 2003-06-27 00:49:51
|
Update of /cvsroot/babeldoc/babeldoc/modules/babelfish/src/com/babeldoc/babelfish In directory sc8-pr-cvs1:/tmp/cvs-serv22842/src/com/babeldoc/babelfish Modified Files: BabelfishTranslator.java Log Message: Updated the formatting and license header in the babelfish module Index: BabelfishTranslator.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/babelfish/src/com/babeldoc/babelfish/BabelfishTranslator.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** BabelfishTranslator.java 22 May 2003 03:19:48 -0000 1.6 --- BabelfishTranslator.java 27 Jun 2003 00:47:26 -0000 1.7 *************** *** 1,29 **** ! /* ! * $Header$ ! * $DateTime$ * * ! * babeldoc: universal document processor * ! * This program is free software; you can redistribute it and/or ! * modify it under the terms of the GNU General Public License ! * as published by the Free Software Foundation; either version 2 ! * of the License, or (at your option) any later version. * ! * This program is distributed in the hope that it will be useful, ! * but WITHOUT ANY WARRANTY; without even the implied warranty of ! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.babeldoc.babelfish; import com.Ostermiller.util.StringTokenizer; import com.babeldoc.core.BabeldocCommand; import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; --- 1,75 ---- ! /* ==================================================================== ! * The Apache Software License, Version 1.1 * + * Copyright (c) 2000 The Apache Software Foundation. All rights + * reserved. * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted provided that the following conditions ! * are met: * ! * 1. Redistributions of source code must retain the above copyright ! * notice, this list of conditions and the following disclaimer. * ! * 2. Redistributions in binary form must reproduce the above copyright ! * notice, this list of conditions and the following disclaimer in ! * the documentation and/or other materials provided with the ! * distribution. ! * ! * 3. The end-user documentation included with the redistribution, ! * if any, must include the following acknowledgment: ! * "This product includes software developed by the ! * Apache Software Foundation (http://www.apache.org/)." ! * Alternately, this acknowledgment may appear in the software itself, ! * if and wherever such third-party acknowledgments normally appear. ! * ! * 4. The names "Apache" and "Apache Software Foundation" must ! * not be used to endorse or promote products derived from this ! * software without prior written permission. For written ! * permission, please contact ap...@ap.... ! * ! * 5. Products derived from this software may not be called "Apache", ! * nor may "Apache" appear in their name, without prior written ! * permission of the Apache Software Foundation. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ! * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ! * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ! * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ! * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ! * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ! * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ! * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! * SUCH DAMAGE. ! * ==================================================================== ! * ! * This software consists of voluntary contributions made by many ! * individuals on behalf of the Apache Software Foundation. For more ! * information on the Apache Software Foundation, please see ! * <http://www.apache.org/>. ! * ! * Portions of this software are based upon public domain software ! * originally written at the National Center for Supercomputing Applications, ! * University of Illinois, Urbana-Champaign. ! * ==================================================================== ! * ! * Babeldoc: The Universal Document Processor ! * ! * $Header$ ! * $DateTime$ ! * $Author$ * */ package com.babeldoc.babelfish; import com.Ostermiller.util.StringTokenizer; + import com.babeldoc.core.BabeldocCommand; import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; + import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.OptionBuilder; *************** *** 31,43 **** import java.io.*; import java.net.*; import java.util.Enumeration; import java.util.Properties; /** ! * Convert the resource bundle from english to a specified language. This is a rework ! * of the jBabel. http://alumni.cse.iitk.ac.in/~debajyoti.bera/jbabel.html. This tool ! * reads in the destination property file (if it exists) then it will merge the translated ! * values with the existing values. * * @author bmcdonald --- 77,93 ---- import java.io.*; + import java.net.*; + import java.util.Enumeration; import java.util.Properties; + /** ! * Convert the resource bundle from english to a specified language. This is a ! * rework of the jBabel. ! * http://alumni.cse.iitk.ac.in/~debajyoti.bera/jbabel.html. This tool reads ! * in the destination property file (if it exists) then it will merge the ! * translated values with the existing values. * * @author bmcdonald *************** *** 49,54 **** --- 99,108 ---- public static final String jTranslateEnd = "</div>"; + /** + * Creates a new BabelfishTranslator object. + */ public BabelfishTranslator() { } + /** * Process the commands on the command line *************** *** 61,74 **** /** ! * Print the usage instructions */ ! public void printUsage() { ! super.printUsage(); } /** ! * No finishing up processing here - not applicable. */ ! public void finishUp() { } --- 115,140 ---- /** ! * No finishing up processing here - not applicable. */ ! public void finishUp() { } /** ! * Make the from / to string ! * ! * @param from ! * @param to ! * ! * @return */ ! public static String makeFromTo(String from, String to) { ! return from + "_" + to; ! } ! ! /** ! * Print the usage instructions ! */ ! public void printUsage() { ! super.printUsage(); } *************** *** 81,182 **** super.setupCommandLine(options); ! options.addOption(OptionBuilder.isRequired().hasArg(). ! withDescription(I18n.get("babelfish.002")).withLongOpt("output").create('o')); ! options.addOption(OptionBuilder.isRequired().hasArg(). ! withDescription(I18n.get("babelfish.003")).withLongOpt("property").create('p')); ! options.addOption(OptionBuilder.isRequired().hasArg(). ! withDescription(I18n.get("babelfish.004")).withLongOpt("to").create('t')); ! options.addOption(OptionBuilder.isRequired(false).hasArg(false). ! withDescription(I18n.get("babelfish.005")).withLongOpt("merge").create('m')); } /** * * @param text * @param fromTo * @return * @throws IOException */ ! protected static String getQueryText(String text, String fromTo) throws IOException { ! //get the query string ready ! String encodedtext = URLEncoder.encode(text, "ISO-8859-1"); ! String requestString = new String("doit=done&tt=urltext&" + "urltext=" + encodedtext + "&url=" + URLEncoder.encode("http://", "ISO-8859-1") + "&" + "lp=" + fromTo); ! return requestString; } /** * * @return */ ! protected static URL getUrl() { ! try { ! return new URL(BABELFISH_URL); ! } catch (MalformedURLException e) { ! return null; } } /** ! * Translate the text from one language to another * ! * @param text ! * @param fromTo ! * @return ! * @throws IOException */ ! public static String translateText(String text, String fromTo) throws IOException { ! HttpURLConnection urlConn = getUrlConnection(getUrl()); ! BufferedReader reader = getPostResults(text, fromTo, urlConn); ! String resultString = getPostString(urlConn, reader); ! return extractTranslationResult(resultString); } /** ! * Make the from / to string * - * @param from - * @param to * @return */ ! public static String makeFromTo(String from, String to) { ! return from+"_"+to; } /** * - * @param urlConn - * @param reader * @return * @throws IOException */ ! private static String getPostString(HttpURLConnection urlConn, BufferedReader reader) throws IOException { ! int responseCode = urlConn.getResponseCode(); ! if (responseCode != HttpURLConnection.HTTP_OK) { ! System.out.println(I18n.get("babelfish.007", new Integer(responseCode))); ! throw new ConnectException("while translating"); ! } ! String resultString = new String(), inputLine; ! //Reads data ! while ((inputLine = reader.readLine()) != null) { ! resultString += inputLine; } - - reader.close(); - urlConn.disconnect(); - return resultString; } /** * * @param value * @param fromTo * @param urlConn * @return * @throws IOException */ ! private static BufferedReader getPostResults(String value, String fromTo, HttpURLConnection urlConn) throws IOException { String reqStr = getQueryText(value, fromTo); String reqLen = Integer.toString(reqStr.length()); --- 147,336 ---- super.setupCommandLine(options); ! options.addOption(OptionBuilder.isRequired().hasArg() ! .withDescription(I18n.get("babelfish.002")) ! .withLongOpt("output").create('o')); ! options.addOption(OptionBuilder.isRequired().hasArg() ! .withDescription(I18n.get("babelfish.003")) ! .withLongOpt("property").create('p')); ! options.addOption(OptionBuilder.isRequired().hasArg() ! .withDescription(I18n.get("babelfish.004")) ! .withLongOpt("to").create('t')); ! options.addOption(OptionBuilder.isRequired(false).hasArg(false) ! .withDescription(I18n.get("babelfish.005")) ! .withLongOpt("merge").create('m')); } /** + * Translate the entire properties file + * + * @param to + * @param propsFile + * @param output DOCUMENT ME! + * @param merge DOCUMENT ME! + * + * @throws IOException DOCUMENT ME! + */ + public static void translate(String to, String propsFile, String output, + boolean merge) throws IOException { + Properties props = new Properties(); + props.load(new FileInputStream(propsFile)); + + String fromTo = makeFromTo("en", to); + Properties translated = new Properties(); + File outputFile = new File(output); + loadExistingTranlations(merge, outputFile, output, translated); + translateAllValues(props, fromTo, translated); + writeTranslations(outputFile, translated); + } + + /** + * Translate the text from one language to another * * @param text * @param fromTo + * * @return + * * @throws IOException */ ! public static String translateText(String text, String fromTo) ! throws IOException { ! HttpURLConnection urlConn = getUrlConnection(getUrl()); ! ! BufferedReader reader = getPostResults(text, fromTo, urlConn); ! String resultString = getPostString(urlConn, reader); ! ! return extractTranslationResult(resultString); } /** + * Translate the possible + * + * @param value + * @param fromTo * * @return + * + * @throws IOException */ ! public static String translateTextLines(String value, String fromTo) ! throws IOException { ! String transValue = new String(); ! ! if (value.indexOf("\n") > 0) { ! StringTokenizer st = new StringTokenizer(value, "\n"); ! ! while (st.hasMoreTokens()) { ! String line = (String) st.next(); ! transValue += translateText(line, fromTo); ! ! if (st.hasMoreTokens()) { ! transValue += "\\n"; ! } ! } ! } else { ! transValue = translateText(value, fromTo); } + + return transValue; } /** ! * Execute the commandline * ! * @param commandLine */ ! public void execute(CommandLine commandLine) { ! String toLang = commandLine.getOptionValue('t'); ! String propsFile = commandLine.getOptionValue('p'); ! String output = commandLine.getOptionValue('o'); ! boolean merge = commandLine.hasOption('m'); ! try { ! translate(toLang, propsFile, output, merge); ! } catch (IOException e) { ! LogService.getInstance().logError("<<|>>", e); ! } } /** ! * Main routine ! * ! * @param args ! */ ! public static void main(String[] args) { ! new BabelfishTranslator(args); ! System.exit(0); ! } ! ! /** ! * Translate the natural language text from language to another language. ! * The from and to languages are given as the ISO codes, french = fr, etc. ! * ! * @param fromLang ! * @param toLang ! * @param text * * @return */ ! public static String translate(String fromLang, String toLang, String text) { ! String fromTo = BabelfishTranslator.makeFromTo(fromLang, toLang); ! ! try { ! return BabelfishTranslator.translateTextLines(text, fromTo); ! } catch (IOException e) { ! LogService.getInstance().logError(e); ! ! return ""; ! } } /** + * DOCUMENT ME! + * + * @param text + * @param fromTo * * @return + * * @throws IOException */ ! protected static String getQueryText(String text, String fromTo) ! throws IOException { ! //get the query string ready ! String encodedtext = URLEncoder.encode(text, "ISO-8859-1"); ! String requestString = new String("doit=done&tt=urltext&" + "urltext=" + ! encodedtext + "&url=" + URLEncoder.encode("http://", "ISO-8859-1") + ! "&" + "lp=" + fromTo); ! return requestString; ! } ! /** ! * DOCUMENT ME! ! * ! * @return ! */ ! protected static URL getUrl() { ! try { ! return new URL(BABELFISH_URL); ! } catch (MalformedURLException e) { ! return null; } } /** + * DOCUMENT ME! * * @param value * @param fromTo * @param urlConn + * * @return + * * @throws IOException */ ! private static BufferedReader getPostResults(String value, String fromTo, ! HttpURLConnection urlConn) throws IOException { String reqStr = getQueryText(value, fromTo); String reqLen = Integer.toString(reqStr.length()); *************** *** 190,204 **** POSTWriter.close(); ! BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); return reader; } /** * * @param url * @return * @throws IOException */ ! private static HttpURLConnection getUrlConnection(URL url) throws IOException { HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); --- 344,398 ---- POSTWriter.close(); ! BufferedReader reader = new BufferedReader(new InputStreamReader( ! urlConn.getInputStream())); ! return reader; } /** + * DOCUMENT ME! + * + * @param urlConn + * @param reader + * + * @return + * + * @throws IOException + * @throws ConnectException DOCUMENT ME! + */ + private static String getPostString(HttpURLConnection urlConn, + BufferedReader reader) throws IOException { + int responseCode = urlConn.getResponseCode(); + + if (responseCode != HttpURLConnection.HTTP_OK) { + System.out.println(I18n.get("babelfish.007", new Integer(responseCode))); + throw new ConnectException("while translating"); + } + + String resultString = new String(); + String inputLine; + + //Reads data + while ((inputLine = reader.readLine()) != null) { + resultString += inputLine; + } + + reader.close(); + urlConn.disconnect(); + + return resultString; + } + + /** + * DOCUMENT ME! * * @param url + * * @return + * * @throws IOException */ ! private static HttpURLConnection getUrlConnection(URL url) ! throws IOException { HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); *************** *** 210,264 **** //set the content type etc. helped by Java Networking Tutorial urlConn.setRequestProperty("Accept-Language", "en-us"); ! urlConn.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); urlConn.setRequestProperty("User-Agent", "BabelDoc0.9"); urlConn.setRequestProperty("Host", "world.altavista.com"); return urlConn; } /** * * @param resultString * @return */ ! private static String extractTranslationResult(String resultString) throws UnsupportedEncodingException { int firstIndex = resultString.indexOf(jTranslateStart); ! if (firstIndex == -1) return null; firstIndex = resultString.indexOf('>', firstIndex); int lastIndex = resultString.indexOf(jTranslateEnd, firstIndex); resultString = resultString.substring(firstIndex + 1, lastIndex); return URLDecoder.decode(resultString, "ISO-8859-1"); } /** ! * Translate the entire properties file * ! * @param to ! * @param propsFile */ ! public static void translate(String to, String propsFile, String output, boolean merge) ! throws IOException { ! Properties props = new Properties(); ! props.load(new FileInputStream(propsFile)); ! ! String fromTo = makeFromTo("en", to); ! Properties translated = new Properties(); ! File outputFile = new File(output); ! loadExistingTranlations(merge, outputFile, output, translated); ! translateAllValues(props, fromTo, translated); ! writeTranslations(outputFile, translated); ! } ! ! private static void writeTranslations(File outputFile, Properties translated) throws IOException { ! if(outputFile.exists()) { ! outputFile.delete(); ! } ! FileOutputStream fos = new FileOutputStream(outputFile); ! translated.store(fos, "Generated by: " + BabelfishTranslator.class.toString()); ! fos.close(); ! } ! ! private static void loadExistingTranlations(boolean merge, File outputFile, String output, Properties translated) throws IOException { ! if (merge&&outputFile.exists()) { FileInputStream fin = new FileInputStream(output); translated.load(fin); --- 404,453 ---- //set the content type etc. helped by Java Networking Tutorial urlConn.setRequestProperty("Accept-Language", "en-us"); ! urlConn.setRequestProperty("Content-type", ! "application/x-www-form-urlencoded"); urlConn.setRequestProperty("User-Agent", "BabelDoc0.9"); urlConn.setRequestProperty("Host", "world.altavista.com"); + return urlConn; } /** + * DOCUMENT ME! * * @param resultString + * * @return + * + * @throws UnsupportedEncodingException DOCUMENT ME! */ ! private static String extractTranslationResult(String resultString) ! throws UnsupportedEncodingException { int firstIndex = resultString.indexOf(jTranslateStart); ! ! if (firstIndex == -1) { return null; + } + firstIndex = resultString.indexOf('>', firstIndex); + int lastIndex = resultString.indexOf(jTranslateEnd, firstIndex); resultString = resultString.substring(firstIndex + 1, lastIndex); + return URLDecoder.decode(resultString, "ISO-8859-1"); } /** ! * Used to merge existing translations into new file with new translations. * ! * @param merge DOCUMENT ME! ! * @param outputFile DOCUMENT ME! ! * @param output DOCUMENT ME! ! * @param translated DOCUMENT ME! ! * ! * @throws IOException DOCUMENT ME! */ ! private static void loadExistingTranlations(boolean merge, File outputFile, ! String output, Properties translated) throws IOException { ! if (merge && outputFile.exists()) { FileInputStream fin = new FileInputStream(output); translated.load(fin); *************** *** 267,271 **** } ! private static void translateAllValues(Properties props, String fromTo, Properties translated) throws IOException { for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); --- 456,470 ---- } ! /** ! * Do the translation ! * ! * @param props DOCUMENT ME! ! * @param fromTo DOCUMENT ME! ! * @param translated DOCUMENT ME! ! * ! * @throws IOException DOCUMENT ME! ! */ ! private static void translateAllValues(Properties props, String fromTo, ! Properties translated) throws IOException { for (Enumeration e = props.keys(); e.hasMoreElements();) { String name = (String) e.nextElement(); *************** *** 273,279 **** // Only get the value if it does not exist already - do not overwrite the values ! if(!translated.containsKey(name)) { String transValue = translateTextLines(value, fromTo); System.out.println(name + "=" + transValue); if (transValue != null) { translated.setProperty(name, transValue); --- 472,479 ---- // Only get the value if it does not exist already - do not overwrite the values ! if (!translated.containsKey(name)) { String transValue = translateTextLines(value, fromTo); System.out.println(name + "=" + transValue); + if (transValue != null) { translated.setProperty(name, transValue); *************** *** 286,356 **** /** ! * Translate the possible ! * @param value ! * @param fromTo ! * @return ! * @throws IOException ! */ ! public static String translateTextLines(String value, String fromTo) throws IOException { ! String transValue = new String(); ! if (value.indexOf("\n") > 0) { ! StringTokenizer st = new StringTokenizer(value, "\n"); ! while (st.hasMoreTokens()) { ! String line = (String) st.next(); ! transValue += translateText(line, fromTo); ! if (st.hasMoreTokens()) { ! transValue += "\\n"; ! } ! } ! } else { ! transValue = translateText(value, fromTo); ! } ! return transValue; ! } ! ! /** ! * Execute the commandline * ! * @param commandLine ! */ ! public void execute(CommandLine commandLine) { ! String toLang = commandLine.getOptionValue('t'); ! String propsFile = commandLine.getOptionValue('p'); ! String output = commandLine.getOptionValue('o'); ! boolean merge = commandLine.hasOption('m'); ! ! try { ! translate(toLang, propsFile, output, merge); ! } catch (IOException e) { ! LogService.getInstance().logError("<<|>>", e); ! } ! } ! ! /** ! * Translate the natural language text from language to another language. The ! * from and to languages are given as the ISO codes, french = fr, etc. * ! * @param fromLang ! * @param toLang ! * @param text ! * @return */ ! public static String translate(String fromLang, String toLang, String text) { ! String fromTo = BabelfishTranslator.makeFromTo(fromLang, toLang); ! try { ! return BabelfishTranslator.translateTextLines(text, fromTo); ! } catch (IOException e) { ! LogService.getInstance().logError(e); ! return ""; } - } ! /** ! * Main routine ! * @param args ! */ ! public static void main(String[] args) { ! new BabelfishTranslator(args); ! System.exit(0); } } --- 486,506 ---- /** ! * Write the translations to disk * ! * @param outputFile DOCUMENT ME! ! * @param translated DOCUMENT ME! * ! * @throws IOException DOCUMENT ME! */ ! private static void writeTranslations(File outputFile, Properties translated) ! throws IOException { ! if (outputFile.exists()) { ! outputFile.delete(); } ! FileOutputStream fos = new FileOutputStream(outputFile); ! translated.store(fos, ! "Generated by: " + BabelfishTranslator.class.toString()); ! fos.close(); } } |