]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/ConvertToV6.java
Minor automated code simplifications.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / ConvertToV6.java
1 // Copyright 2020 Reimar Döffinger. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.RandomAccessFile;
21
22 public class ConvertToV6 {
23     public static void main(final String[] args) throws IOException {
24         if (args.length != 2 && args.length != 3) {
25             System.out.println("Usage: ConvertToV6 <input.v007> <output.v006> [skipHtml]");
26             System.out.println("If the option third argument is given as 'skipHtml'");
27             System.out.println("the v6 dictionary will be without all HTML entries to reduce its size");
28             return;
29         }
30         boolean skipHtml = false;
31         boolean skipHtmlOpt = false;
32         if (args.length == 3) {
33             if (!args[2].equals("skipHtml") && !args[2].equals("skipHtmlOpt")) {
34                 System.out.println("Unknown extra argument '" + args[2] + "'");
35                 return;
36             }
37             skipHtml = true;
38             skipHtmlOpt = args[2].equals("skipHtmlOpt");
39         }
40         final String inname = args[0];
41         final String outname = args[1];
42         FileInputStream in;
43         try {
44             in = new FileInputStream(inname);
45         } catch (FileNotFoundException e) {
46             System.out.println("Could not open input file '" + inname + "'");
47             System.out.println(e);
48             return;
49         }
50         final Dictionary dictionary = new Dictionary(in.getChannel());
51         if (dictionary.dictFileVersion <= 6) {
52             System.out.println("Input dictionary is already v6 or older!");
53             return;
54         }
55         if (skipHtmlOpt && dictionary.htmlEntries.size() == 0) {
56             System.exit(3);
57         }
58         RandomAccessFile out;
59         try {
60             out = new RandomAccessFile(outname, "rw");
61         } catch (FileNotFoundException e) {
62             System.out.println("Could not open output file '" + outname + "'");
63             System.out.println(e);
64             return;
65         }
66         if (out.length() > 0) {
67             System.out.println("Output file '" + outname + "' already exists, aborting!");
68             return;
69         }
70         new DictionaryV6Writer(dictionary).writev6(out, skipHtml);
71         out.close();
72         in.close();
73     }
74 }