summaryrefslogtreecommitdiff
path: root/l10ntools/java/l10nconv/java/com/sun/star/tooling/converter/Convert.java
blob: 5b1fb6c62b6180a5f957a1bab1a12cf6666dc0c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: Convert.java,v $
 * $Revision: 1.4 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
/*
 * the main Class
 *
 * Command Line arguments are reviewed
 * and a Converter is constructed
 */
package com.sun.star.tooling.converter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import com.sun.star.tooling.DirtyTags.DirtyTagWrapper;

/**
 * The main class of the converter tool
 *
 * The converter tool is command line based.
 * Its classes allow the converting between the
 * file formats sdf, gsi and <a href="http://www.oasis-open.org/committees/xliff/documents/cs-xliff-core-1.1-20031031.htm">xliff</a>.
 *
 * Those file formats are used in localization
 * of Star-Office and Open-Office.
 *
 * Information about the whole localization process can be found in
 * <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>
 *
 * @author Christian Schmidt 2005
 *
 */
public class Convert {

    private static Calendar     cal;

    private static final String EMPTY              = "";

    /**
     * The name of the  file containing the debug information
     * that where found while converting (every output goes here too)
     */
    private static String       dbgName            = EMPTY;

    /**
     * the character that separates the extension from the file name
     */
    private static final char   extensionSeperator = '.';

    /**
     * the log File
     */
    private static File         log;

    /**
     * the name of the log file
     */
    private static String       logString          = EMPTY;

    /**
     * indicates whether existing sources should be overwritten
     * without asking
     */
    private static boolean      overwrite          = false;

    /**
     * A second Source File needed for GSI Merging
     */
    private static File secondSource;

    //private static final char   pathSeperator      = '\\';

    /**
     * The language that should be the source language
     * that means the language to translate from
     */
    private static String       sourceLanguage     = "en-US";

    /**
     * the name of the source file
     */
    private static String       sourceName         = EMPTY;

    /**
     * the type of the source file (sdf,xliff,...)
     */
    private static String       sourceType         = EMPTY;

    /**
     * The time when converting started
     */
    private static String       startTime          = EMPTY;
    /**
    * The language that should be the target language
    * that means the language to translate to
    */
    private static String       TargetLanguage     = EMPTY;

    /**
     * the name of the target file
     */
    private static String       targetName         = EMPTY;

    /**
     * the type of the target file (sdf,xliff,...)
     */
    private static String       targetType         = EMPTY;

    /**
     * Store the current version ID and number of this tool
     */
    final static String         version            = " Prod.20050710:1255 ";

    /**
     * The name of the (original) sdf file used as second source for gsi->sdf merging
     */
    private static String secondSourceName=EMPTY;
    /**
     * Indicate whether strings in xliff files should
     * be wrapped with ept/bpt or sub tags to enable translation tools
     * to synchronize source language string with there translation
     *
     * @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>
     */
    private static boolean doWrap=true;



            public static void main(String[] args) throws IOException, Exception {
                try{

                    //go, parse and check the command line parameters
                    ParameterChecker.checkClParameters(args);
                    ParameterChecker.createContentOfClParameters();
                    //Initialize the tagWrapper
                    DirtyTagWrapper.setWrapping(doWrap);
                    //create an instance of converter
                    Converter conv = new Converter(sourceType, sourceName, sourceLanguage,
                            targetType, targetName, TargetLanguage,secondSourceName, overwrite);
                    // get aktual time
                    cal = Calendar.getInstance();
                    startTime = cal.getTime().toString();
                    //show infos
                    printPreamble();
                    //do the job
                    conv.convert();

                    showStatistic();

                    //close log, debug...
                    OutputHandler.closeAll();

                } catch(Exception e){
                    System.out.print("An EXCEPTION occured, please check your commad line settings \n"+e.getMessage());
                    System.exit(-1);
                }catch(Throwable t){
                    System.out.print("A FATAL ERROR occured, please check your commad line settings \n"+t.getMessage());
                    System.exit(-1);
                }

    }

    /**
     * show the command line help
     */
    private static void printHelp() {

        final String ls = System.getProperty("line.separator");
        System.out
                .println(

                "File Converting Tool 'converter' Version "
                        + Convert.version
                        + ls
                        + "Converts SDF files to wellformed XLIFF or GSI files and vice versa" + ls
                        + ls
                        + "Use: " + ls
                        + "convert [-h]|[SourcePath [TargetPath] [-T Type] [[-S Type [secondSourcePath]]  " + ls
                        + "        [-s LanguageID] [-t LanguageID] [-l [LogPath]] [-o]]" + ls
                        + ls
                        + "-h               show this help." + ls
                        + "SourcePath       path of the file to convert." + ls
                        + "secondSourcePath path of the SDF file to merge to (GSI -> SDF only!)." + ls
                        + "TargetPath       path where to store the result." + ls
                        + "LogPath          path of the log file" + ls
                        + "-T Type          the type of the target file (xliff,sdf,gsi)" + ls
                        + "-S Type          the type of the source file (xliff,sdf,gsi)" + ls
                        + "-s LanguageID    the ISO language code of the source language (de, fr...)." + ls
                        + "                 Default is 'en-US' " + ls
                        + "-t LanguageID    the language code of the target language (de, fr...)." + ls
                        + "                 Default is first found Language other than source language." + ls
                        + "-l [LogPath]     write a log file,  you can name the file." + ls
                        + "-o               overwrite existing files without asking." + ls
                        + "-nw              disable the wrapping with ept/bpt tags." + ls
                        + ls
                        + "The created files were stored in the SourceFile Path if nothing else is given. " + ls
                        + "The extension is '.xliff','.sdf' depending on the source file and '.log' " + ls
                        + "for the logfile." + ls);
    }

    /**
     * show the parameters the converter starts with
     *
     * @throws IOException
     */
    final private static void printPreamble() throws IOException {
        OutputHandler.out(EMPTY);
        OutputHandler.out("Source File is: " + sourceName);
        OutputHandler.out("Target File is: " + targetName);
        if (OutputHandler.doLog) {
            OutputHandler.out("Log File    is: " + logString);
        } else {
            OutputHandler.out("Log File    is: disabled");
        }

        OutputHandler.out(EMPTY);

    }

    /**
     * show some statistic data
     *
     * @throws IOException
     */
    final private static void showStatistic() throws IOException {
        OutputHandler.dbg(EMPTY);
        OutputHandler
                .out((targetType.equalsIgnoreCase("xliff") ? "TransUnits written:   "
                        : "Lines written :         ")
                        + Converter.getLineCounter());
        OutputHandler.dbg(EMPTY);
        OutputHandler.out("Started    at :         " + Convert.startTime);
        Convert.cal = Calendar.getInstance();
        OutputHandler.out("Finished   at :         " + Convert.cal.getTime());
        OutputHandler.dbg(EMPTY);

    }

    /**
     * Get the extensiion of a file name
     * (sdf,xliff,gsi)
     *
     * @param sourceString     the file name
     * @return                 the extension
     */
    static protected String extractExtension(String sourceString) {
        String ext = sourceString.substring(sourceString
                .lastIndexOf(Convert.extensionSeperator) + 1);
        return ext;
    }

//    static protected String extractFileName(String sourceString) {
//        String sName = EMPTY;
//        sName = (sourceString.substring(sourceString
//                .lastIndexOf(File.separator) + 1, sourceString
//                .lastIndexOf(Convert.extensionSeperator)));
//
//        return sName;
//    }
//
//    static protected String extractPath(String sourceString) {
//        String sPath = sourceString.substring(0, sourceString
//                .lastIndexOf(File.separator) + 1);
//        return sPath;
//    }

    /**
     *
     */
    public Convert() {
    }

    /**
     *
     * Verify a parameter array and create content useable by the programm
     * from the switches and attributes set at command line
     *
     * @author Christian Schmidt 2005
     */
    private static class ParameterChecker {

        /**
         * Holds the path of the source file
         */
        private static String filePath;
        /**
         * Holds the name of the source file
         */
        private static String fileName;

        /**
         * Create a new Instance of ParameterChecker
         *
         *
         */
        public ParameterChecker(){};
        /**
         * Checks the command line parameters
         *
         * @param args          the parameters to check and to parse
         * @throws IOException
         */
        private static void checkClParameters(String[] args) throws IOException {
            try {
                //show help if no attrributes...
                if (args.length == 0) {
                    printHelp();
                    System.exit(-1);
                }
                //...or attribute is -h
                if (args[0].equals("-h")) {
                    printHelp();
                    System.exit(0);
                }
                if (args[0].equals("-ver")) {
                    System.out.println("File Converting Tool Version "+version);
                    System.exit(0);
                }
                //source file Location and path is always first attribute
                sourceName = new String(args[0]);

                File source = new File(sourceName);
                //break if there is no source to convert
                if (!source.exists())
                    throw new IOException("ERROR:Can not find Source File '"
                            + sourceName + "'. Aborting...");

    //            String name=source.getName();
    //            String parent=source.getParent();
    //            String path=source.getPath();

                filePath = (source.getParent()==null)?"":source.getParent()+File.separator; //extractPath(sourceName);
                fileName = source.getName().substring(0,source.getName().lastIndexOf(extensionSeperator));

                for (int i = 1; i < args.length; i++) {


                        if ("-s".equals(args[i])) {
                            if (args.length > i + 1) {
                                sourceLanguage = args[++i];

                            } else {
                                throw new ConverterException(
                                        "missing argument for -s source language");
                            }
                            continue;
                        }

                        if ("-S".equals(args[i])) {
                            if (args.length > i + 1) {
                                sourceType = args[++i];
                                if (args.length > i +1 &&!args[i+1].startsWith("-")) {
                                    secondSourceName = args[++i];
                                }

                            } else {
                                throw new ConverterException(
                                        "missing argument for -S  source type");
                            }
                            continue;
                        }

                        if ("-T".equals(args[i])) {
                            if (args.length > i + 1) {
                                targetType = args[++i];


                            } else {
                                throw new ConverterException(
                                        "missing argument for -T  target type");
                            }
                            continue;
                        }

                        if ("-l".equals(args[i])) {
                            OutputHandler.doLog = true;
                            if (args.length > i + 1
                                    && (!args[i + 1].startsWith("-"))) {
                                logString = args[++i];
                            } else {
                                logString = EMPTY;
                            }
                            continue;
                        }

                        if ("-o".equals(args[i])) {
                            overwrite = true;
                            continue;
                        }

                        if ("-nw".equals(args[i])) {
                            doWrap = false;
                            continue;
                        }

                        if ("-h".equals(args[i])) {
                            printHelp();
                            System.exit(0);
                        }

                        if ("-dbg".equals(args[i])) {
                            OutputHandler.doDebug = true;
                            continue;
                        }

                        if ("-t".equals(args[i])) {
                            if (args.length > i + 1) {
                                TargetLanguage = args[++i];
                            } else {
                                throw new ConverterException(
                                        "missing argument for -t  target language");
                            }
                            continue;
                        }

                        if (i == 1 && !args[i].startsWith("-")) { //target file
                            // found
                            targetName = args[i];
                            continue;
                        }
                        //if we come here we
                        //can not match the Attribute
                        throw new ConverterException("unknown Attribute: "
                                + args[i]);


                }//end for
            } catch (ConverterException e) {
                OutputHandler.out("ERROR: "+e.getMessage());
                System.exit(-1);
            } catch (Throwable t){
                System.out.print("An Error occured while parsing the command line,\n please check your commad line settings.\n "+t.getMessage());
                System.exit(-1);
            }

        }//end checkClParameters

        /**
         * Creates the appropriate content of what ever data
         * we found in the command line
         *
         * @throws IOException
         */
        private static void createContentOfClParameters() throws IOException {

            try {
                if (OutputHandler.doDebug) {
                    // if the -dbg switch is set, we
                    // create
                    // a file that gets all information
                    // produced by this tool

                    OutputHandler.dbgFile = new BufferedWriter(new FileWriter(
                            new File(new String(filePath + fileName + ".dbg"))));
                }

                if (OutputHandler.doLog) {// create a logfile?
                    //given at command line?
                    if (EMPTY.equals(logString) || logString == null) {
                        logString = new String(filePath + fileName + ".log");
                    }
                    log = FileMaker.newFile(logString, overwrite);
                    OutputHandler.logFile = (new BufferedWriter(new FileWriter(
                            log)));
                }

                if (EMPTY.equals(sourceType) || sourceType == null) {
                    // not given at command line?
                    if (!(EMPTY.equals(sourceName) || sourceName == null)) {
                        sourceType = extractExtension(sourceName);
                    } else {
                        throw new ConverterException("Source type is missing");
                    }
                }

                if(sourceType.equalsIgnoreCase("gsi")){

                    if(EMPTY.equals(Convert.secondSourceName)){
                        Convert.secondSourceName=filePath+fileName+".sdf";
                    }
                    //secondSource=new File(Convert.secondSourceName);

                }

                if (EMPTY.equals(sourceName)) {
                    sourceName = filePath + fileName + "." + sourceType;
                }
                //no target type given at command line?
                if (EMPTY.equals(targetType) || targetType == null) {
                    if (!(EMPTY.equals(targetName) || targetName == null)) {
                        targetType = extractExtension(targetName);
                    } else {
                        throw new ConverterException("Target type is missing");

                    }
                }
                //no target File specified at command line
                if (EMPTY.equals(targetName) || targetName == null) {
                    targetName = filePath + fileName + "." + targetType;
                    if (targetName.equals(Convert.secondSourceName)){
                        OutputHandler.out("ERROR: \nSource '"+Convert.secondSourceName+"' and \nTarget'"+targetName+"' are the same");
                        System.exit(0);
                    }else if (targetName.equals(Convert.sourceName)){
                        OutputHandler.out("ERROR: \nSource '"+Convert.sourceName+"' and \nTarget'"+targetName+"' are the same");
                        System.exit(0);
                    }
                }else if (targetName.equals(Convert.secondSourceName)){
                    OutputHandler.out("ERROR: \nSource '"+Convert.secondSourceName+"' and \nTarget'"+targetName+"' are the same");
                    System.exit(0);
                }else if (targetName.equals(Convert.sourceName)){
                    OutputHandler.out("ERROR: \nSource '"+Convert.sourceName+"' and \nTarget'"+targetName+"' are the same");
                    System.exit(0);
                }


            } catch (ConverterException e) {
                OutputHandler.out(e.getMessage());
            }

        }

    }

}