Example usage for java.text RuleBasedCollator RuleBasedCollator

List of usage examples for java.text RuleBasedCollator RuleBasedCollator

Introduction

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

Prototype

private RuleBasedCollator(RuleBasedCollator that) 

Source Link

Document

"Copy constructor."

Usage

From source file:RulesDemo.java

static public void main(String[] args) {

    String englishRules = ("< a,A < b,B < c,C < d,D < e,E < f,F " + "< g,G < h,H < i,I < j,J < k,K < l,L "
            + "< m,M < n,N < o,O < p,P < q,Q < r,R " + "< s,S < t,T < u,U < v,V < w,W < x,X " + "< y,Y < z,Z");

    String smallnTilde = new String("\u00F1");
    String capitalNTilde = new String("\u00D1");

    String traditionalSpanishRules = ("< a,A < b,B < c,C " + "< ch, cH, Ch, CH " + "< d,D < e,E < f,F "
            + "< g,G < h,H < i,I < j,J < k,K < l,L " + "< ll, lL, Ll, LL " + "< m,M < n,N " + "< " + smallnTilde
            + "," + capitalNTilde + " " + "< o,O < p,P < q,Q < r,R " + "< s,S < t,T < u,U < v,V < w,W < x,X "
            + "< y,Y < z,Z");

    String[] words = { "luz", "curioso", "llama", "chalina" };

    try {/*from   w  w w .ja va  2  s  . co m*/
        RuleBasedCollator enCollator = new RuleBasedCollator(englishRules);
        RuleBasedCollator spCollator = new RuleBasedCollator(traditionalSpanishRules);

        sortStrings(enCollator, words);
        printStrings(words);

        System.out.println();

        sortStrings(spCollator, words);
        printStrings(words);
    } catch (ParseException pe) {
        System.out.println("Parse exception for rules");
    }
}

From source file:com.all.client.model.predicate.BaseCollatorPredicate.java

private void initialize() {
    rules = ((RuleBasedCollator) Collator.getInstance(Locale.US)).getRules();
    refineRules();/*from   w  w w.  j a v a 2 s.com*/

    try {
        collator = new RuleBasedCollator(rules);
        collator.setStrength(Collator.PRIMARY);
    } catch (ParseException e) {
        log.error("Error while parsing collator rules [" + rules + "]", e);
    }
}

From source file:dk.statsbiblioteket.util.CachedCollator.java

private static Collator fixCollator(Collator collator, boolean check) {
    if (!(collator instanceof RuleBasedCollator)) {
        log.warn(String.format("fixCollator expected a RuleBasedCollator but got %s. Unable to update Collator",
                collator.getClass()));/*from ww w .ja v a 2  s. c  o  m*/
        return collator;
    }
    String rules = ((RuleBasedCollator) collator).getRules();
    if (check && !rules.contains("<' '<'\u005f'")) {
        log.debug("fixCollator: The received Collator already sorts spaces first");
        return collator;
    }
    try {
        RuleBasedCollator newCollator = new RuleBasedCollator(rules.replace("<'\u005f'", "<' '<'\u005f'"));
        log.trace("Successfully updated Collator to prioritize spaces before other characters");
        return newCollator;
    } catch (ParseException e) {
        throw new RuntimeException("ParseException while parsing\n" + rules, e);
    }
}

From source file:com.ecyrd.jspwiki.plugin.AbstractReferralPlugin.java

/**
 * Helper method to initialize the comparator for this page.
 *///from  w ww .j  ava  2 s. co m
private void initSorter(WikiContext context, Map params) {
    String order = (String) params.get(PARAM_SORTORDER);
    if (order == null || order.length() == 0) {
        // Use the configured comparator
        m_sorter = context.getEngine().getPageSorter();
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_JAVA)) {
        // use Java "natural" ordering
        m_sorter = new PageSorter(JavaNaturalComparator.DEFAULT_JAVA_COMPARATOR);
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_LOCALE)) {
        // use this locale's ordering
        m_sorter = new PageSorter(LocaleComparator.DEFAULT_LOCALE_COMPARATOR);
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_HUMAN)) {
        // use human ordering
        m_sorter = new PageSorter(HumanComparator.DEFAULT_HUMAN_COMPARATOR);
    } else
        try {
            Collator collator = new RuleBasedCollator(order);
            collator.setStrength(Collator.PRIMARY);
            m_sorter = new PageSorter(new CollatorComparator(collator));
        } catch (ParseException pe) {
            log.info("Failed to parse requested collator - using default ordering", pe);
            m_sorter = context.getEngine().getPageSorter();
        }
}

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).//from  www. j  a  v  a  2  s .c  o  m
 *
 * 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// ww w . j  a  v a  2 s. co m
 *            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.analysis.CollationKeyFilterFactory.java

private Collator createFromRules(String fileName, ResourceLoader loader) {
    InputStream input = null;/*from w  w  w.j  av a 2 s.  co m*/
    try {
        input = loader.openResource(fileName);
        String rules = IOUtils.toString(input, "UTF-8");
        return new RuleBasedCollator(rules);
    } catch (IOException e) {
        // io error
        throw new RuntimeException(e);
    } catch (ParseException e) {
        // invalid rules
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

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

/**
 * Read custom rules from a file, and create a RuleBasedCollator
 * The file cannot support comments, as # might be in the rules!
 *//*from w w  w .j a v  a2  s.  c  o m*/
private Collator createFromRules(String fileName, ResourceLoader loader) {
    InputStream input = null;
    try {
        input = loader.openResource(fileName);
        String rules = IOUtils.toString(input, "UTF-8");
        return new RuleBasedCollator(rules);
    } catch (IOException | ParseException e) {
        // io error or invalid rules
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(input);
    }
}

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.
 */// ww  w. j  a  v a2s. 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.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.
 *///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();
}