1/**
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin.dicttool;
18
19import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
20import com.android.inputmethod.latin.makedict.DictionaryHeader;
21import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
22
23import java.io.File;
24import java.util.Arrays;
25
26public class Header extends Dicttool.Command {
27    public static final String COMMAND = "header";
28
29    public Header() {
30    }
31
32    @Override
33    public String getHelp() {
34        return COMMAND + " <filename>: prints the header contents of a dictionary file";
35    }
36
37    @Override
38    public void run() throws UnsupportedFormatException {
39        final boolean plumbing;
40        if (mArgs.length > 0 && "-p".equals(mArgs[0])) {
41            plumbing = true;
42            mArgs = Arrays.copyOfRange(mArgs, 1, mArgs.length);
43        } else {
44            plumbing = false;
45        }
46        if (mArgs.length < 1) {
47            throw new RuntimeException("Not enough arguments for command " + COMMAND);
48        }
49        final String filename = mArgs[0];
50        final File dictFile = new File(filename);
51        final DecoderChainSpec<DictionaryHeader> spec =
52                BinaryDictOffdeviceUtils.decodeDictionaryForProcess(dictFile,
53                        new BinaryDictOffdeviceUtils.HeaderReaderProcessor());
54        if (null == spec) {
55            throw new UnsupportedFormatException(filename
56                    + " doesn't seem to be a valid version 2 dictionary file");
57        }
58
59        final DictionaryHeader header = spec.mResult;
60        System.out.println("Dictionary : " + dictFile.getAbsolutePath());
61        System.out.println("Size : " + dictFile.length() + " bytes");
62        System.out.println("Format : Binary dictionary format");
63        System.out.println("Format version : " + header.mFormatOptions.mVersion);
64        System.out.println("Packaging : " + spec.describeChain());
65        System.out.println("Header attributes :");
66        System.out.print(header.mDictionaryOptions.toString(2 /* indentCount */, plumbing));
67    }
68}
69