Example usage for java.text RuleBasedCollator getRules

List of usage examples for java.text RuleBasedCollator getRules

Introduction

In this page you can find the example usage for java.text RuleBasedCollator getRules.

Prototype

public String getRules() 

Source Link

Document

Gets the table-based rules for the collation object.

Usage

From source file:nl.inl.util.StringUtil.java

/**
 * Returns a new collator that takes spaces into account (unlike the default Java collators,
 * which ignore spaces), so we can sort "per word".
 *
 * Example: with the default collator, "cat dog" would be sorted after "catapult" (a after d).
 * With the per-word collator, "cat dog" would be sorted before "catapult" (cat before
 * catapult)./*w ww.  j av  a2  s  .  com*/
 *
 * NOTE: the base collator must be a RuleBasedCollator, but the argument has type Collator for
 * convenience (not having to explicitly cast when calling)
 *
 * @param base
 *            the collator to base the per-word collator on.
 * @return the per-word collator
 */
public static RuleBasedCollator getPerWordCollator(Collator base) {
    if (!(base instanceof RuleBasedCollator))
        throw new IllegalArgumentException("Base collator must be rule-based!");

    try {
        // Insert a collation rule to sort the space character before the underscore
        RuleBasedCollator ruleBasedCollator = (RuleBasedCollator) base;
        String rules = ruleBasedCollator.getRules();
        return new RuleBasedCollator(rules.replaceAll("<'_'", "<' '<'_'"));
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.inl.util.StringUtil.java

/**
 * Returns a new collator that sort digits at the end of the alphabet instead of the beginning.
 *
 * NOTE: the base collator must be a RuleBasedCollator, but the argument has type Collator for
 * convenience (not having to explicitly cast when calling)
 *
 * @param base/*from   www .java  2  s. c  om*/
 *            the collator to base the new collator on.
 * @return the new collator
 */
public static RuleBasedCollator getSortDigitsAtEndCollator(Collator base) {
    if (!(base instanceof RuleBasedCollator))
        throw new IllegalArgumentException("Base collator must be rule-based!");

    try {
        // Insert a collation rule to sort the space character before the underscore
        RuleBasedCollator ruleBasedCollator = (RuleBasedCollator) base;
        String rules = ruleBasedCollator.getRules();
        rules = rules.replaceAll("<0<1<2<3<4<5<6<7<8<9", "");
        rules += "<0<1<2<3<4<5<6<7<8<9";
        return new RuleBasedCollator(rules);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.solr.schema.TestCollationField.java

/**
 * Ugly: but what to do? We want to test custom sort, which reads rules in as a resource.
 * These are largish files, and jvm-specific (as our documentation says, you should always
 * look out for jvm differences with collation).
 * So it's preferable to create this file on-the-fly.
 *///from  ww  w  .  ja v a 2  s  . co  m
public static String setupSolrHome() throws Exception {
    // make a solr home underneath the test's TEMP_DIR
    File tmpFile = createTempDir("collation1").toFile();

    // make data and conf dirs
    new File(tmpFile, "data").mkdir();
    File confDir = new File(tmpFile + "/collection1", "conf");
    confDir.mkdirs();

    // copy over configuration files
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig-basic.xml"),
            new File(confDir, "solrconfig.xml"));
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig.snippet.randomindexconfig.xml"),
            new File(confDir, "solrconfig.snippet.randomindexconfig.xml"));
    FileUtils.copyFile(getFile("solr/collection1/conf/schema-collate.xml"), new File(confDir, "schema.xml"));

    // generate custom collation rules (DIN 5007-2), saving to customrules.dat
    RuleBasedCollator baseCollator = (RuleBasedCollator) Collator.getInstance(new Locale("de", "DE"));

    String DIN5007_2_tailorings = "& ae , a\u0308 & AE , A\u0308" + "& oe , o\u0308 & OE , O\u0308"
            + "& ue , u\u0308 & UE , u\u0308";

    RuleBasedCollator tailoredCollator = new RuleBasedCollator(baseCollator.getRules() + DIN5007_2_tailorings);
    String tailoredRules = tailoredCollator.getRules();
    FileOutputStream os = new FileOutputStream(new File(confDir, "customrules.dat"));
    IOUtils.write(tailoredRules, os, "UTF-8");
    os.close();

    return tmpFile.getAbsolutePath();
}

From source file:org.apache.solr.schema.TestCollationFieldDocValues.java

/**
 * Ugly: but what to do? We want to test custom sort, which reads rules in as a resource.
 * These are largish files, and jvm-specific (as our documentation says, you should always
 * look out for jvm differences with collation).
 * So it's preferable to create this file on-the-fly.
 *//*from   ww w .j  a  va 2s  .  c  o  m*/
public static String setupSolrHome() throws Exception {
    // make a solr home underneath the test's TEMP_DIR
    File tmpFile = createTempDir("collation1").toFile();

    // make data and conf dirs
    new File(tmpFile, "data").mkdir();
    File confDir = new File(tmpFile + "/collection1", "conf");
    confDir.mkdirs();

    // copy over configuration files
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig-basic.xml"),
            new File(confDir, "solrconfig.xml"));
    FileUtils.copyFile(getFile("solr/collection1/conf/solrconfig.snippet.randomindexconfig.xml"),
            new File(confDir, "solrconfig.snippet.randomindexconfig.xml"));
    FileUtils.copyFile(getFile("solr/collection1/conf/schema-collate-dv.xml"), new File(confDir, "schema.xml"));

    // generate custom collation rules (DIN 5007-2), saving to customrules.dat
    RuleBasedCollator baseCollator = (RuleBasedCollator) Collator.getInstance(new Locale("de", "DE"));

    String DIN5007_2_tailorings = "& ae , a\u0308 & AE , A\u0308" + "& oe , o\u0308 & OE , O\u0308"
            + "& ue , u\u0308 & UE , u\u0308";

    RuleBasedCollator tailoredCollator = new RuleBasedCollator(baseCollator.getRules() + DIN5007_2_tailorings);
    String tailoredRules = tailoredCollator.getRules();
    FileOutputStream os = new FileOutputStream(new File(confDir, "customrules.dat"));
    IOUtils.write(tailoredRules, os, "UTF-8");
    os.close();

    return tmpFile.getAbsolutePath();
}

From source file:org.sakaiproject.tool.assessment.util.BeanSortComparator.java

private int subCompare(String s1, String s2) {
    //we do not want to use null values for sorting
    if (s1 == null) {
        s1 = "";/*from ww  w .  jav  a2 s  .  co m*/
    }

    if (s2 == null) {
        s2 = "";
    }

    // Deal with n/a case
    if (s1.toLowerCase().startsWith("n/a") && !s2.toLowerCase().startsWith("n/a"))
        return 1;

    if (s2.toLowerCase().startsWith("n/a") && !s1.toLowerCase().startsWith("n/a"))
        return -1;

    String finalS1 = s1.replaceAll("<.*?>", "");
    String finalS2 = s2.replaceAll("<.*?>", "");
    RuleBasedCollator collator_ini = (RuleBasedCollator) Collator.getInstance();
    try {
        RuleBasedCollator collator = new RuleBasedCollator(
                collator_ini.getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
        return collator.compare(finalS1.toLowerCase(), finalS2.toLowerCase());
    } catch (ParseException e) {
    }
    return Collator.getInstance().compare(finalS1.toLowerCase(), finalS2.toLowerCase());
}