Search in sources :

Example 1 with RuleBasedCollator

use of java.text.RuleBasedCollator in project robovm by robovm.

the class CollatorTest method assertGetCollationElementIteratorString.

private void assertGetCollationElementIteratorString(Locale l, String s, Integer... offsets) {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(l);
    assertCollationElementIterator(coll.getCollationElementIterator(s), offsets);
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator)

Example 2 with RuleBasedCollator

use of java.text.RuleBasedCollator in project lucene-solr by apache.

the class CollationField method setup.

/**
   * Setup the field according to the provided parameters
   */
private void setup(ResourceLoader loader, Map<String, String> args) {
    String custom = args.remove("custom");
    String language = args.remove("language");
    String country = args.remove("country");
    String variant = args.remove("variant");
    String strength = args.remove("strength");
    String decomposition = args.remove("decomposition");
    final Collator collator;
    if (custom == null && language == null)
        throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or language is required.");
    if (custom != null && (language != null || country != null || variant != null))
        throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both language and custom. " + "To tailor rules for a built-in language, see the javadocs for RuleBasedCollator. " + "Then save the entire customized ruleset to a file, and use with the custom parameter");
    if (language != null) {
        // create from a system collator, based on Locale.
        collator = createFromLocale(language, country, variant);
    } else {
        // create from a custom ruleset
        collator = createFromRules(custom, loader);
    }
    // set the strength flag, otherwise it will be the default.
    if (strength != null) {
        if (strength.equalsIgnoreCase("primary"))
            collator.setStrength(Collator.PRIMARY);
        else if (strength.equalsIgnoreCase("secondary"))
            collator.setStrength(Collator.SECONDARY);
        else if (strength.equalsIgnoreCase("tertiary"))
            collator.setStrength(Collator.TERTIARY);
        else if (strength.equalsIgnoreCase("identical"))
            collator.setStrength(Collator.IDENTICAL);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid strength: " + strength);
    }
    // set the decomposition flag, otherwise it will be the default.
    if (decomposition != null) {
        if (decomposition.equalsIgnoreCase("no"))
            collator.setDecomposition(Collator.NO_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("canonical"))
            collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
        else if (decomposition.equalsIgnoreCase("full"))
            collator.setDecomposition(Collator.FULL_DECOMPOSITION);
        else
            throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
    }
    analyzer = new CollationKeyAnalyzer(collator);
}
Also used : CollationKeyAnalyzer(org.apache.lucene.collation.CollationKeyAnalyzer) SolrException(org.apache.solr.common.SolrException) Collator(java.text.Collator) RuleBasedCollator(java.text.RuleBasedCollator)

Example 3 with RuleBasedCollator

use of java.text.RuleBasedCollator in project jdk8u_jdk by JetBrains.

the class CollatorProviderImpl method getInstance.

/**
     * Returns a new <code>Collator</code> instance for the specified locale.
     * @param locale the desired locale.
     * @return the <code>Collator</code> for the desired locale.
     * @exception NullPointerException if
     * <code>locale</code> is null
     * @exception IllegalArgumentException if <code>locale</code> isn't
     *     one of the locales returned from
     *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
     *     getAvailableLocales()}.
     * @see java.text.Collator#getInstance(java.util.Locale)
     */
@Override
public Collator getInstance(Locale locale) {
    if (locale == null) {
        throw new NullPointerException();
    }
    Collator result = null;
    // Load the resource of the desired locale from resource
    // manager.
    String colString = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCollationData();
    try {
        result = new RuleBasedCollator(CollationRules.DEFAULTRULES + colString);
    } catch (ParseException foo) {
        // predefined tables should contain correct grammar
        try {
            result = new RuleBasedCollator(CollationRules.DEFAULTRULES);
        } catch (ParseException bar) {
            // the default rules should always be parsable.
            throw new InternalError(bar);
        }
    }
    // Now that RuleBasedCollator adds expansions for pre-composed characters
    // into their decomposed equivalents, the default collators don't need
    // to have decomposition turned on.  Laura, 5/5/98, bug 4114077
    result.setDecomposition(Collator.NO_DECOMPOSITION);
    return (Collator) result.clone();
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) ParseException(java.text.ParseException) RuleBasedCollator(java.text.RuleBasedCollator) Collator(java.text.Collator)

Example 4 with RuleBasedCollator

use of java.text.RuleBasedCollator in project robovm by robovm.

the class OldCollationKeyTest method test_toByteArray.

public void test_toByteArray() {
    // Test for method byte [] java.text.CollationKey.toByteArray()
    Collator collator = Collator.getInstance();
    collator.setStrength(Collator.PRIMARY);
    CollationKey key1 = collator.getCollationKey("abc");
    byte[] bytes = key1.toByteArray();
    assertTrue("Not enough bytes", bytes.length >= 3);
    try {
        collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7");
    } catch (ParseException e) {
        fail("ParseException");
        return;
    }
    /*
         * CollationElementIterator it =
         * ((RuleBasedCollator)collator).getCollationElementIterator("1234567");
         * int order; while ((order = it.next()) !=
         * CollationElementIterator.NULLORDER) {
         * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length;
         * i+=2) { System.out.print(Integer.toHexString(bytes[i]) +
         * Integer.toHexString(bytes[i+1]) + " "); } System.out.println();
         */
    // The RI has a different algorithm to generate the collation keys.
    // bytes = collator.getCollationKey("1234567").toByteArray();
    // byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1,
    //         0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 };
    byte[] bytes1 = collator.getCollationKey("12").toByteArray();
    byte[] bytes2 = collator.getCollationKey("123").toByteArray();
    byte[] bytes3 = collator.getCollationKey("124").toByteArray();
    byte[] bytes4 = collator.getCollationKey("1245").toByteArray();
    byte[] bytes5 = collator.getCollationKey("1245").toByteArray();
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes1, bytes2) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes2, bytes3) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes3, bytes4) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes4, bytes5) == 0);
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) CollationKey(java.text.CollationKey) ParseException(java.text.ParseException) RuleBasedCollator(java.text.RuleBasedCollator) Collator(java.text.Collator)

Example 5 with RuleBasedCollator

use of java.text.RuleBasedCollator in project robovm by robovm.

the class OldCollationElementIteratorTest method testPrevious.

public void testPrevious() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
    String text = "abc";
    CollationElementIterator iterator = coll.getCollationElementIterator(text);
    int[] orders = new int[text.length()];
    int order = iterator.next();
    int i = 0;
    while (order != CollationElementIterator.NULLORDER) {
        orders[i++] = order;
        order = iterator.next();
    }
    int offset = iterator.getOffset();
    assertEquals(text.length(), offset);
    order = iterator.previous();
    while (order != CollationElementIterator.NULLORDER) {
        assertEquals(orders[--i], order);
        order = iterator.previous();
    }
    assertEquals(0, iterator.getOffset());
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) CollationElementIterator(java.text.CollationElementIterator)

Aggregations

RuleBasedCollator (java.text.RuleBasedCollator)11 Collator (java.text.Collator)3 ParseException (java.text.ParseException)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 Locale (java.util.Locale)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CharacterIterator (java.text.CharacterIterator)1 CollationElementIterator (java.text.CollationElementIterator)1 CollationKey (java.text.CollationKey)1 StringCharacterIterator (java.text.StringCharacterIterator)1 CollationKeyAnalyzer (org.apache.lucene.collation.CollationKeyAnalyzer)1 SolrException (org.apache.solr.common.SolrException)1