From 015832c06c5256f88ae0761f47c03cc9c92c8843 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Mon, 13 Apr 2020 16:35:44 +0200 Subject: [PATCH] Add code to convert a Dictionary to the old v6 format. --- convert_to_v6.sh | 3 + genv6.sh | 20 +++++ .../dictionary/engine/ConvertToV6.java | 75 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100755 convert_to_v6.sh create mode 100755 genv6.sh create mode 100644 src/com/hughes/android/dictionary/engine/ConvertToV6.java diff --git a/convert_to_v6.sh b/convert_to_v6.sh new file mode 100755 index 0000000..3b6bd5c --- /dev/null +++ b/convert_to_v6.sh @@ -0,0 +1,3 @@ +JAVA=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java +test -x "$JAVA" || JAVA=java +"$JAVA" -classpath bin/ com.hughes.android.dictionary.engine.ConvertToV6 "$@" diff --git a/genv6.sh b/genv6.sh new file mode 100755 index 0000000..9bb5147 --- /dev/null +++ b/genv6.sh @@ -0,0 +1,20 @@ +set -e +rm -rf data/outputsv6 +mkdir data/outputsv6 +for i in data/outputs/*.quickdic ; do + o=data/outputsv6/$(basename "$i") + ./convert_to_v6.sh "$i" "$o" + 7z a -mx=9 "$o".v006.zip "$o" + rm "$o" + # skipHtml makes no sense for single-language dictionaries + if echo "$o" | grep -q '-' ; then + if ./convert_to_v6.sh "$i" "$o" skipHtmlOpt ; then + 7z a -mx=9 "$o".small.v006.zip "$o" + rm "$o" + elif [ $? -ne 3 ] ; then + # Check for magic 3 indicating "no HTML entries in dictionary" + echo "Converting dictionary failed!" + exit 1 + fi + fi +done diff --git a/src/com/hughes/android/dictionary/engine/ConvertToV6.java b/src/com/hughes/android/dictionary/engine/ConvertToV6.java new file mode 100644 index 0000000..444a7c2 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/ConvertToV6.java @@ -0,0 +1,75 @@ +// Copyright 2020 Reimar Döffinger. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.android.dictionary.engine; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + +public class ConvertToV6 { + public static void main(final String[] args) throws IOException { + if (args.length != 2 && args.length != 3) { + System.out.println("Usage: ConvertToV6 [skipHtml]"); + System.out.println("If the option third argument is given as 'skipHtml'"); + System.out.println("the v6 dictionary will be without all HTML entries to reduce its size"); + return; + } + boolean skipHtml = false; + boolean skipHtmlOpt = false; + if (args.length == 3) { + if (!args[2].equals("skipHtml") && !args[2].equals("skipHtmlOpt")) { + System.out.println("Unknown extra argument '" + args[2] + "'"); + return; + } + skipHtml = true; + skipHtmlOpt = args[2].equals("skipHtmlOpt"); + } + final String inname = args[0]; + final String outname = args[1]; + FileInputStream in; + try { + in = new FileInputStream(inname); + } catch (FileNotFoundException e) { + System.out.println("Could not open input file '" + inname + "'"); + System.out.println(e); + return; + } + final Dictionary dictionary = new Dictionary(in.getChannel()); + if (dictionary.dictFileVersion <= 6) { + System.out.println("Input dictionary is already v6 or older!"); + return; + } + if (skipHtmlOpt && dictionary.htmlEntries.size() == 0) { + System.exit(3); + } + RandomAccessFile out; + try { + out = new RandomAccessFile(outname, "rw"); + } catch (FileNotFoundException e) { + System.out.println("Could not open output file '" + outname + "'"); + System.out.println(e); + return; + } + if (out.length() > 0) { + System.out.println("Output file '" + outname + "' already exists, aborting!"); + return; + } + dictionary.writev6(out, skipHtml); + out.close(); + in.close(); + } +} -- 2.43.0