12ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/* GENERATED SOURCE. DO NOT MODIFY. */
2f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// © 2016 and later: Unicode, Inc. and others.
3f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html#License
42ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller/*
52ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
6f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert * Copyright (C) 2012-2016, International Business Machines Corporation and
72ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller * others. All Rights Reserved.
82ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller *******************************************************************************
92ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller */
102ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
112ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerpackage android.icu.text;
122ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
132ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.io.IOException;
142ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport java.nio.ByteBuffer;
152ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
162ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.Assert;
172ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.ICUBinary;
182ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.ICUData;
192ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.impl.ICUResourceBundle;
202ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerimport android.icu.util.UResourceBundle;
212ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
222ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fullerfinal class DictionaryData {
232ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    // disallow instantiation
242ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private DictionaryData() { }
252ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
262ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRIE_TYPE_BYTES = 0;
272ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRIE_TYPE_UCHARS = 1;
282ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRIE_TYPE_MASK = 7;
292ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRIE_HAS_VALUES = 8;
302ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRANSFORM_NONE = 0;
312ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRANSFORM_TYPE_OFFSET = 0x1000000;
322ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRANSFORM_TYPE_MASK = 0x7f000000;
332ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int TRANSFORM_OFFSET_MASK = 0x1fffff;
342ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
352ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_STRING_TRIE_OFFSET = 0;
362ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_RESERVED1_OFFSET = 1;
372ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_RESERVED2_OFFSET = 2;
382ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_TOTAL_SIZE = 3;
392ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_TRIE_TYPE = 4;
402ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_TRANSFORM = 5;
412ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_RESERVED6 = 6;
422ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_RESERVED7 = 7;
432ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static final int IX_COUNT = 8;
442ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
452ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    private static final int DATA_FORMAT_ID = 0x44696374;
462ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller
472ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    public static DictionaryMatcher loadDictionaryFor(String dictType) throws IOException {
48f86f25d102340da66b9c7cb6b2d5ecdc0de43ecfFredrik Roubert        ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BRKITR_BASE_NAME);
492ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        String dictFileName = rb.getStringWithFallback("dictionaries/" + dictType);
502ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        dictFileName = ICUData.ICU_BRKITR_NAME + '/' + dictFileName;
512ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ByteBuffer bytes = ICUBinary.getRequiredData(dictFileName);
522ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        ICUBinary.readHeader(bytes, DATA_FORMAT_ID, null);
532ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int[] indexes = new int[IX_COUNT];
542ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        // TODO: read indexes[IX_STRING_TRIE_OFFSET] first, then read a variable-length indexes[]
552ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        for (int i = 0; i < IX_COUNT; i++) {
562ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            indexes[i] = bytes.getInt();
572ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
582ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int offset = indexes[IX_STRING_TRIE_OFFSET];
592ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        Assert.assrt(offset >= (4 * IX_COUNT));
602ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (offset > (4 * IX_COUNT)) {
612ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int diff = offset - (4 * IX_COUNT);
622ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            ICUBinary.skipBytes(bytes, diff);
632ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
642ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int trieType = indexes[IX_TRIE_TYPE] & TRIE_TYPE_MASK;
652ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        int totalSize = indexes[IX_TOTAL_SIZE] - offset;
662ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        DictionaryMatcher m = null;
672ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        if (trieType == TRIE_TYPE_BYTES) {
682ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            int transform = indexes[IX_TRANSFORM];
692ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            byte[] data = new byte[totalSize];
702ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            bytes.get(data);
712ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            m = new BytesDictionaryMatcher(data, transform);
722ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else if (trieType == TRIE_TYPE_UCHARS) {
732ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            Assert.assrt(totalSize % 2 == 0);
742ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            String data = ICUBinary.getString(bytes, totalSize / 2, totalSize & 1);
752ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            m = new CharsDictionaryMatcher(data);
762ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        } else {
772ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller            m = null;
782ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        }
792ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller        return m;
802ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller    }
812ae130017183d2f66d55bf0ca51f8da3294644fdNeil Fuller}
82