1/*
2 * Copyright (C) 2011 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.dictionarypack;
18
19import android.text.TextUtils;
20import android.util.JsonReader;
21
22import java.io.IOException;
23import java.io.InputStreamReader;
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.List;
27import java.util.TreeMap;
28
29/**
30 * Helper class containing functions to parse the dictionary metadata.
31 */
32public class MetadataParser {
33
34    // Name of the fields in the JSON-formatted file.
35    private static final String ID_FIELD_NAME = MetadataDbHelper.WORDLISTID_COLUMN;
36    private static final String LOCALE_FIELD_NAME = "locale";
37    private static final String DESCRIPTION_FIELD_NAME = MetadataDbHelper.DESCRIPTION_COLUMN;
38    private static final String UPDATE_FIELD_NAME = "update";
39    private static final String FILESIZE_FIELD_NAME = MetadataDbHelper.FILESIZE_COLUMN;
40    private static final String RAW_CHECKSUM_FIELD_NAME = MetadataDbHelper.RAW_CHECKSUM_COLUMN;
41    private static final String CHECKSUM_FIELD_NAME = MetadataDbHelper.CHECKSUM_COLUMN;
42    private static final String REMOTE_FILENAME_FIELD_NAME =
43            MetadataDbHelper.REMOTE_FILENAME_COLUMN;
44    private static final String VERSION_FIELD_NAME = MetadataDbHelper.VERSION_COLUMN;
45    private static final String FORMATVERSION_FIELD_NAME = MetadataDbHelper.FORMATVERSION_COLUMN;
46
47    /**
48     * Parse one JSON-formatted word list metadata.
49     * @param reader the reader containing the data.
50     * @return a WordListMetadata object from the parsed data.
51     * @throws IOException if the underlying reader throws IOException during reading.
52     */
53    private static WordListMetadata parseOneWordList(final JsonReader reader)
54            throws IOException, BadFormatException {
55        final TreeMap<String, String> arguments = new TreeMap<>();
56        reader.beginObject();
57        while (reader.hasNext()) {
58            final String name = reader.nextName();
59            if (!TextUtils.isEmpty(name)) {
60                arguments.put(name, reader.nextString());
61            }
62        }
63        reader.endObject();
64        if (TextUtils.isEmpty(arguments.get(ID_FIELD_NAME))
65                || TextUtils.isEmpty(arguments.get(LOCALE_FIELD_NAME))
66                || TextUtils.isEmpty(arguments.get(DESCRIPTION_FIELD_NAME))
67                || TextUtils.isEmpty(arguments.get(UPDATE_FIELD_NAME))
68                || TextUtils.isEmpty(arguments.get(FILESIZE_FIELD_NAME))
69                || TextUtils.isEmpty(arguments.get(CHECKSUM_FIELD_NAME))
70                || TextUtils.isEmpty(arguments.get(REMOTE_FILENAME_FIELD_NAME))
71                || TextUtils.isEmpty(arguments.get(VERSION_FIELD_NAME))
72                || TextUtils.isEmpty(arguments.get(FORMATVERSION_FIELD_NAME))) {
73            throw new BadFormatException(arguments.toString());
74        }
75        // TODO: need to find out whether it's bulk or update
76        // The null argument is the local file name, which is not known at this time and will
77        // be decided later.
78        return new WordListMetadata(
79                arguments.get(ID_FIELD_NAME),
80                MetadataDbHelper.TYPE_BULK,
81                arguments.get(DESCRIPTION_FIELD_NAME),
82                Long.parseLong(arguments.get(UPDATE_FIELD_NAME)),
83                Long.parseLong(arguments.get(FILESIZE_FIELD_NAME)),
84                arguments.get(RAW_CHECKSUM_FIELD_NAME),
85                arguments.get(CHECKSUM_FIELD_NAME),
86                null,
87                arguments.get(REMOTE_FILENAME_FIELD_NAME),
88                Integer.parseInt(arguments.get(VERSION_FIELD_NAME)),
89                Integer.parseInt(arguments.get(FORMATVERSION_FIELD_NAME)),
90                0, arguments.get(LOCALE_FIELD_NAME));
91    }
92
93    /**
94     * Parses metadata in the JSON format.
95     * @param input a stream reader expected to contain JSON formatted metadata.
96     * @return dictionary metadata, as an array of WordListMetadata objects.
97     * @throws IOException if the underlying reader throws IOException during reading.
98     * @throws BadFormatException if the data was not in the expected format.
99     */
100    public static List<WordListMetadata> parseMetadata(final InputStreamReader input)
101            throws IOException, BadFormatException {
102        JsonReader reader = new JsonReader(input);
103        final ArrayList<WordListMetadata> readInfo = new ArrayList<>();
104        reader.beginArray();
105        while (reader.hasNext()) {
106            final WordListMetadata thisMetadata = parseOneWordList(reader);
107            if (!TextUtils.isEmpty(thisMetadata.mLocale))
108                readInfo.add(thisMetadata);
109        }
110        return Collections.unmodifiableList(readInfo);
111    }
112
113}
114