1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 /* 24 * the main Class 25 * 26 * Command Line arguments are reviewed 27 * and a Converter is constructed 28 */ 29 package com.sun.star.tooling.converter; 30 31 import java.io.BufferedWriter; 32 import java.io.File; 33 import java.io.FileWriter; 34 import java.io.IOException; 35 import java.util.Calendar; 36 37 import com.sun.star.tooling.DirtyTags.DirtyTagWrapper; 38 39 /** 40 * The main class of the converter tool 41 * 42 * The converter tool is command line based. 43 * Its classes allow the converting between the 44 * file formats sdf, gsi and <a href="http://www.oasis-open.org/committees/xliff/documents/cs-xliff-core-1.1-20031031.htm">xliff</a>. 45 * 46 * Those file formats are used in localization 47 * of Star-Office and Open-Office. 48 * 49 * Information about the whole localization process can be found in 50 * <a href="http://ded-1.germany.sun.com/webcontent/guidelines/pdf/L10NSO8.pdf">http://ded-1.germany.sun.com/webcontent/guidelines/pdf/L10NSO8.pdf</a> 51 * 52 * @author Christian Schmidt 2005 53 * 54 */ 55 public class Convert { 56 57 private static Calendar cal; 58 59 private static final String EMPTY = ""; 60 61 /** 62 * The name of the file containing the debug information 63 * that where found while converting (every output goes here too) 64 */ 65 private static String dbgName = EMPTY; 66 67 /** 68 * the character that separates the extension from the file name 69 */ 70 private static final char extensionSeperator = '.'; 71 72 /** 73 * the log File 74 */ 75 private static File log; 76 77 /** 78 * the name of the log file 79 */ 80 private static String logString = EMPTY; 81 82 /** 83 * indicates whether existing sources should be overwritten 84 * without asking 85 */ 86 private static boolean overwrite = false; 87 88 /** 89 * A second Source File needed for GSI Merging 90 */ 91 private static File secondSource; 92 93 //private static final char pathSeperator = '\\'; 94 95 /** 96 * The language that should be the source language 97 * that means the language to translate from 98 */ 99 private static String sourceLanguage = "en-US"; 100 101 /** 102 * the name of the source file 103 */ 104 private static String sourceName = EMPTY; 105 106 /** 107 * the type of the source file (sdf,xliff,...) 108 */ 109 private static String sourceType = EMPTY; 110 111 /** 112 * The time when converting started 113 */ 114 private static String startTime = EMPTY; 115 /** 116 * The language that should be the target language 117 * that means the language to translate to 118 */ 119 private static String TargetLanguage = EMPTY; 120 121 /** 122 * the name of the target file 123 */ 124 private static String targetName = EMPTY; 125 126 /** 127 * the type of the target file (sdf,xliff,...) 128 */ 129 private static String targetType = EMPTY; 130 131 /** 132 * Store the current version ID and number of this tool 133 */ 134 final static String version = " Prod.20050710:1255 "; 135 136 /** 137 * The name of the (original) sdf file used as second source for gsi->sdf merging 138 */ 139 private static String secondSourceName=EMPTY; 140 /** 141 * Indicate whether strings in xliff files should 142 * be wrapped with ept/bpt or sub tags to enable translation tools 143 * to synchronize source language string with there translation 144 * 145 * @see <a href="http://ded-1.germany.sun.com/webcontent/guidelines/pdf/L10NSO8.pdf">http://ded-1.germany.sun.com/webcontent/guidelines/pdf/L10NSO8.pdf</a> 146 */ 147 private static boolean doWrap=true; 148 149 150 main(String[] args)151 public static void main(String[] args) throws IOException, Exception { 152 try{ 153 154 //go, parse and check the command line parameters 155 ParameterChecker.checkClParameters(args); 156 ParameterChecker.createContentOfClParameters(); 157 //Initialize the tagWrapper 158 DirtyTagWrapper.setWrapping(doWrap); 159 //create an instance of converter 160 Converter conv = new Converter(sourceType, sourceName, sourceLanguage, 161 targetType, targetName, TargetLanguage,secondSourceName, overwrite); 162 // get aktual time 163 cal = Calendar.getInstance(); 164 startTime = cal.getTime().toString(); 165 //show infos 166 printPreamble(); 167 //do the job 168 conv.convert(); 169 170 showStatistic(); 171 172 //close log, debug... 173 OutputHandler.closeAll(); 174 175 } catch(Exception e){ 176 System.out.print("An EXCEPTION occured, please check your commad line settings \n"+e.getMessage()); 177 System.exit(-1); 178 }catch(Throwable t){ 179 System.out.print("A FATAL ERROR occured, please check your commad line settings \n"+t.getMessage()); 180 System.exit(-1); 181 } 182 183 } 184 185 /** 186 * show the command line help 187 */ printHelp()188 private static void printHelp() { 189 190 final String ls = System.getProperty("line.separator"); 191 System.out 192 .println( 193 194 "File Converting Tool 'converter' Version " 195 + Convert.version 196 + ls 197 + "Converts SDF files to wellformed XLIFF or GSI files and vice versa" + ls 198 + ls 199 + "Use: " + ls 200 + "convert [-h]|[SourcePath [TargetPath] [-T Type] [[-S Type [secondSourcePath]] " + ls 201 + " [-s LanguageID] [-t LanguageID] [-l [LogPath]] [-o]]" + ls 202 + ls 203 + "-h show this help." + ls 204 + "SourcePath path of the file to convert." + ls 205 + "secondSourcePath path of the SDF file to merge to (GSI -> SDF only!)." + ls 206 + "TargetPath path where to store the result." + ls 207 + "LogPath path of the log file" + ls 208 + "-T Type the type of the target file (xliff,sdf,gsi)" + ls 209 + "-S Type the type of the source file (xliff,sdf,gsi)" + ls 210 + "-s LanguageID the ISO language code of the source language (de, fr...)." + ls 211 + " Default is 'en-US' " + ls 212 + "-t LanguageID the language code of the target language (de, fr...)." + ls 213 + " Default is first found Language other than source language." + ls 214 + "-l [LogPath] write a log file, you can name the file." + ls 215 + "-o overwrite existing files without asking." + ls 216 + "-nw disable the wrapping with ept/bpt tags." + ls 217 + ls 218 + "The created files were stored in the SourceFile Path if nothing else is given. " + ls 219 + "The extension is '.xliff','.sdf' depending on the source file and '.log' " + ls 220 + "for the logfile." + ls); 221 } 222 223 /** 224 * show the parameters the converter starts with 225 * 226 * @throws IOException 227 */ printPreamble()228 final private static void printPreamble() throws IOException { 229 OutputHandler.out(EMPTY); 230 OutputHandler.out("Source File is: " + sourceName); 231 OutputHandler.out("Target File is: " + targetName); 232 if (OutputHandler.doLog) { 233 OutputHandler.out("Log File is: " + logString); 234 } else { 235 OutputHandler.out("Log File is: disabled"); 236 } 237 238 OutputHandler.out(EMPTY); 239 240 } 241 242 /** 243 * show some statistic data 244 * 245 * @throws IOException 246 */ showStatistic()247 final private static void showStatistic() throws IOException { 248 OutputHandler.dbg(EMPTY); 249 OutputHandler 250 .out((targetType.equalsIgnoreCase("xliff") ? "TransUnits written: " 251 : "Lines written : ") 252 + Converter.getLineCounter()); 253 OutputHandler.dbg(EMPTY); 254 OutputHandler.out("Started at : " + Convert.startTime); 255 Convert.cal = Calendar.getInstance(); 256 OutputHandler.out("Finished at : " + Convert.cal.getTime()); 257 OutputHandler.dbg(EMPTY); 258 259 } 260 261 /** 262 * Get the extensiion of a file name 263 * (sdf,xliff,gsi) 264 * 265 * @param sourceString the file name 266 * @return the extension 267 */ extractExtension(String sourceString)268 static protected String extractExtension(String sourceString) { 269 String ext = sourceString.substring(sourceString 270 .lastIndexOf(Convert.extensionSeperator) + 1); 271 return ext; 272 } 273 274 // static protected String extractFileName(String sourceString) { 275 // String sName = EMPTY; 276 // sName = (sourceString.substring(sourceString 277 // .lastIndexOf(File.separator) + 1, sourceString 278 // .lastIndexOf(Convert.extensionSeperator))); 279 // 280 // return sName; 281 // } 282 // 283 // static protected String extractPath(String sourceString) { 284 // String sPath = sourceString.substring(0, sourceString 285 // .lastIndexOf(File.separator) + 1); 286 // return sPath; 287 // } 288 289 /** 290 * 291 */ Convert()292 public Convert() { 293 } 294 295 /** 296 * 297 * Verify a parameter array and create content useable by the programm 298 * from the switches and attributes set at command line 299 * 300 * @author Christian Schmidt 2005 301 */ 302 private static class ParameterChecker { 303 304 /** 305 * Holds the path of the source file 306 */ 307 private static String filePath; 308 /** 309 * Holds the name of the source file 310 */ 311 private static String fileName; 312 313 /** 314 * Create a new Instance of ParameterChecker 315 * 316 * 317 */ ParameterChecker()318 public ParameterChecker(){}; 319 /** 320 * Checks the command line parameters 321 * 322 * @param args the parameters to check and to parse 323 * @throws IOException 324 */ checkClParameters(String[] args)325 private static void checkClParameters(String[] args) throws IOException { 326 try { 327 //show help if no attrributes... 328 if (args.length == 0) { 329 printHelp(); 330 System.exit(-1); 331 } 332 //...or attribute is -h 333 if (args[0].equals("-h")) { 334 printHelp(); 335 System.exit(0); 336 } 337 if (args[0].equals("-ver")) { 338 System.out.println("File Converting Tool Version "+version); 339 System.exit(0); 340 } 341 //source file Location and path is always first attribute 342 sourceName = new String(args[0]); 343 344 File source = new File(sourceName); 345 //break if there is no source to convert 346 if (!source.exists()) 347 throw new IOException("ERROR:Can not find Source File '" 348 + sourceName + "'. Aborting..."); 349 350 // String name=source.getName(); 351 // String parent=source.getParent(); 352 // String path=source.getPath(); 353 354 filePath = (source.getParent()==null)?"":source.getParent()+File.separator; //extractPath(sourceName); 355 fileName = source.getName().substring(0,source.getName().lastIndexOf(extensionSeperator)); 356 357 for (int i = 1; i < args.length; i++) { 358 359 360 if ("-s".equals(args[i])) { 361 if (args.length > i + 1) { 362 sourceLanguage = args[++i]; 363 364 } else { 365 throw new ConverterException( 366 "missing argument for -s source language"); 367 } 368 continue; 369 } 370 371 if ("-S".equals(args[i])) { 372 if (args.length > i + 1) { 373 sourceType = args[++i]; 374 if (args.length > i +1 &&!args[i+1].startsWith("-")) { 375 secondSourceName = args[++i]; 376 } 377 378 } else { 379 throw new ConverterException( 380 "missing argument for -S source type"); 381 } 382 continue; 383 } 384 385 if ("-T".equals(args[i])) { 386 if (args.length > i + 1) { 387 targetType = args[++i]; 388 389 390 } else { 391 throw new ConverterException( 392 "missing argument for -T target type"); 393 } 394 continue; 395 } 396 397 if ("-l".equals(args[i])) { 398 OutputHandler.doLog = true; 399 if (args.length > i + 1 400 && (!args[i + 1].startsWith("-"))) { 401 logString = args[++i]; 402 } else { 403 logString = EMPTY; 404 } 405 continue; 406 } 407 408 if ("-o".equals(args[i])) { 409 overwrite = true; 410 continue; 411 } 412 413 if ("-nw".equals(args[i])) { 414 doWrap = false; 415 continue; 416 } 417 418 if ("-h".equals(args[i])) { 419 printHelp(); 420 System.exit(0); 421 } 422 423 if ("-dbg".equals(args[i])) { 424 OutputHandler.doDebug = true; 425 continue; 426 } 427 428 if ("-t".equals(args[i])) { 429 if (args.length > i + 1) { 430 TargetLanguage = args[++i]; 431 } else { 432 throw new ConverterException( 433 "missing argument for -t target language"); 434 } 435 continue; 436 } 437 438 if (i == 1 && !args[i].startsWith("-")) { //target file 439 // found 440 targetName = args[i]; 441 continue; 442 } 443 //if we come here we 444 //can not match the Attribute 445 throw new ConverterException("unknown Attribute: " 446 + args[i]); 447 448 449 }//end for 450 } catch (ConverterException e) { 451 OutputHandler.out("ERROR: "+e.getMessage()); 452 System.exit(-1); 453 } catch (Throwable t){ 454 System.out.print("An Error occured while parsing the command line,\n please check your commad line settings.\n "+t.getMessage()); 455 System.exit(-1); 456 } 457 458 }//end checkClParameters 459 460 /** 461 * Creates the appropriate content of what ever data 462 * we found in the command line 463 * 464 * @throws IOException 465 */ createContentOfClParameters()466 private static void createContentOfClParameters() throws IOException { 467 468 try { 469 if (OutputHandler.doDebug) { 470 // if the -dbg switch is set, we 471 // create 472 // a file that gets all information 473 // produced by this tool 474 475 OutputHandler.dbgFile = new BufferedWriter(new FileWriter( 476 new File(new String(filePath + fileName + ".dbg")))); 477 } 478 479 if (OutputHandler.doLog) {// create a logfile? 480 //given at command line? 481 if (EMPTY.equals(logString) || logString == null) { 482 logString = new String(filePath + fileName + ".log"); 483 } 484 log = FileMaker.newFile(logString, overwrite); 485 OutputHandler.logFile = (new BufferedWriter(new FileWriter( 486 log))); 487 } 488 489 if (EMPTY.equals(sourceType) || sourceType == null) { 490 // not given at command line? 491 if (!(EMPTY.equals(sourceName) || sourceName == null)) { 492 sourceType = extractExtension(sourceName); 493 } else { 494 throw new ConverterException("Source type is missing"); 495 } 496 } 497 498 if(sourceType.equalsIgnoreCase("gsi")){ 499 500 if(EMPTY.equals(Convert.secondSourceName)){ 501 Convert.secondSourceName=filePath+fileName+".sdf"; 502 } 503 //secondSource=new File(Convert.secondSourceName); 504 505 } 506 507 if (EMPTY.equals(sourceName)) { 508 sourceName = filePath + fileName + "." + sourceType; 509 } 510 //no target type given at command line? 511 if (EMPTY.equals(targetType) || targetType == null) { 512 if (!(EMPTY.equals(targetName) || targetName == null)) { 513 targetType = extractExtension(targetName); 514 } else { 515 throw new ConverterException("Target type is missing"); 516 517 } 518 } 519 //no target File specified at command line 520 if (EMPTY.equals(targetName) || targetName == null) { 521 targetName = filePath + fileName + "." + targetType; 522 if (targetName.equals(Convert.secondSourceName)){ 523 OutputHandler.out("ERROR: \nSource '"+Convert.secondSourceName+"' and \nTarget'"+targetName+"' are the same"); 524 System.exit(0); 525 }else if (targetName.equals(Convert.sourceName)){ 526 OutputHandler.out("ERROR: \nSource '"+Convert.sourceName+"' and \nTarget'"+targetName+"' are the same"); 527 System.exit(0); 528 } 529 }else if (targetName.equals(Convert.secondSourceName)){ 530 OutputHandler.out("ERROR: \nSource '"+Convert.secondSourceName+"' and \nTarget'"+targetName+"' are the same"); 531 System.exit(0); 532 }else if (targetName.equals(Convert.sourceName)){ 533 OutputHandler.out("ERROR: \nSource '"+Convert.sourceName+"' and \nTarget'"+targetName+"' are the same"); 534 System.exit(0); 535 } 536 537 538 } catch (ConverterException e) { 539 OutputHandler.out(e.getMessage()); 540 } 541 542 } 543 544 } 545 546 }