Example usage for java.text Collator getInstance

List of usage examples for java.text Collator getInstance

Introduction

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

Prototype

public static Collator getInstance(Locale desiredLocale) 

Source Link

Document

Gets the Collator for the desired locale.

Usage

From source file:org.nuxeo.ecm.platform.ui.web.directory.SelectItemComparator.java

public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    this.caseSensitive = caseSentitive;
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {/* w  ww .  ja  v  a 2s . c om*/
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(this.caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}

From source file:org.nuxeo.ecm.platform.ui.web.component.SelectItemComparator.java

public SelectItemComparator(String ordering, Boolean caseSentitive, Locale locale) {
    this.ordering = StringUtils.split(ordering, ",");
    caseSensitive = caseSentitive;//from   w ww .j  av a 2  s.  com
    if (locale == null) {
        FacesContext context = FacesContext.getCurrentInstance();
        this.locale = context.getViewRoot().getLocale();
    } else {
        this.locale = locale;
    }
    collator = Collator.getInstance(this.locale);
    if (Boolean.TRUE.equals(caseSensitive)) {
        collator.setStrength(Collator.TERTIARY); // TERTIARY will make a
                                                 // difference between 'a'
                                                 // and 'A'
    } else {
        collator.setStrength(Collator.SECONDARY);
    }
}

From source file:org.apache.nifi.web.docs.DocumentationController.java

/**
 *
 * @param request servlet request// ww w .ja va2  s.c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final ExtensionMapping extensionMappings = (ExtensionMapping) servletContext
            .getAttribute("nifi-extension-mapping");
    final Collator collator = Collator.getInstance(Locale.US);

    // create the processors lookup
    final Map<String, String> processors = new TreeMap<>(collator);
    for (final String processorClass : extensionMappings.getProcessorNames()) {
        processors.put(StringUtils.substringAfterLast(processorClass, "."), processorClass);
    }

    // create the controller service lookup
    final Map<String, String> controllerServices = new TreeMap<>(collator);
    for (final String controllerServiceClass : extensionMappings.getControllerServiceNames()) {
        controllerServices.put(StringUtils.substringAfterLast(controllerServiceClass, "."),
                controllerServiceClass);
    }

    // create the reporting task lookup
    final Map<String, String> reportingTasks = new TreeMap<>(collator);
    for (final String reportingTaskClass : extensionMappings.getReportingTaskNames()) {
        reportingTasks.put(StringUtils.substringAfterLast(reportingTaskClass, "."), reportingTaskClass);
    }

    // make the available components available to the documentation jsp
    request.setAttribute("processors", processors);
    request.setAttribute("controllerServices", controllerServices);
    request.setAttribute("reportingTasks", reportingTasks);
    request.setAttribute("totalComponents", GENERAL_LINK_COUNT + processors.size() + controllerServices.size()
            + reportingTasks.size() + DEVELOPER_LINK_COUNT);

    // forward appropriately
    request.getRequestDispatcher("/WEB-INF/jsp/documentation.jsp").forward(request, response);
}

From source file:edu.cornell.mannlib.vitro.webapp.filters.RequestModelsPrep.java

private void setCollator(VitroRequest vreq) {
    @SuppressWarnings("unchecked")
    Enumeration<Locale> locales = vreq.getLocales();
    while (locales.hasMoreElements()) {
        Locale locale = locales.nextElement();
        Collator collator = Collator.getInstance(locale);
        if (collator != null) {
            vreq.setCollator(collator);/*from w w w.j  ava2  s.  c om*/
            return;
        }
    }
    vreq.setCollator(Collator.getInstance());
}

From source file:org.kitodo.dataeditor.ruleset.Labeled.java

/**
 * Lists the entries alphabetically by the translated label, taking into
 * account the language preferred by the user for alphabetical sorting. In
 * the auxiliary map, the keys are sorted by the sequence translated label +
 * separator + key, to account for the case where multiple keys are
 * (inadvertently) translated equally./*from   w w  w.ja va2  s . c o m*/
 *
 * @param ruleset
 *            The ruleset. From the ruleset, we learn which language is a
 *            label that does not have a language attribute, that is what is
 *            the default language of the rule set.
 * @param elements
 *            The items to sort. The function is so generic that you can
 *            sort divisions, keys, and options with it.
 * @param keyGetter
 *            A function to extract from the element the value used in the
 *            result map as key.
 * @param labelGetter
 *            A function that allows you to extract the list of labels from
 *            the element and then select the best translated one.
 * @param priorityList
 *            The list of languages spoken by the user human
 * @return a map in the order of the best-fitting translated label
 */
public static <T> LinkedHashMap<String, String> listByTranslatedLabel(Ruleset ruleset, Collection<T> elements,
        Function<T, String> keyGetter, Function<T, Collection<Label>> labelGetter,
        List<LanguageRange> priorityList) {

    Locale sortLocale = Locale.lookup(priorityList, Arrays.asList(Collator.getAvailableLocales()));
    TreeMap<String, Pair<String, String>> byLabelSorter = sortLocale != null
            ? new TreeMap<>(Collator.getInstance(sortLocale))
            : new TreeMap<>();
    for (T element : elements) {
        String key = keyGetter.apply(element);
        Labeled labeled = new Labeled(ruleset, key, labelGetter.apply(element));
        String label = labeled.getLabel(priorityList);
        byLabelSorter.put(label + '\037' + key, Pair.of(key, label));
    }
    LinkedHashMap<String, String> result = new LinkedHashMap<>((int) Math.ceil(elements.size() / 0.75));
    for (Pair<String, String> entry : byLabelSorter.values()) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

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.
 *//* w w 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.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.
 *//* w w w . j a v a  2  s  .  c om*/
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:no.kantega.publishing.controls.standard.AlphabeticalListController.java

public Map<String, Object> handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ContentManagementService cms = new ContentManagementService(request);

    ContentQuery query = new ContentQuery();

    if (isNotBlank(associationCategory)) {
        AssociationCategory association = cms.getAssociationCategoryByPublicId(associationCategory);
        if (association != null) {
            query.setAssociationCategory(association);
        }//from   www  . j  av  a2s. com
    }

    Content current = tryGetFromRequest(request);
    ContentIdentifier cid = current.getContentIdentifier();
    query.setAssociatedId(cid);

    query.setSortOrder(new SortOrder("title", false));
    List<Content> contentList;
    if (skipAttributes) {
        contentList = cms.getContentSummaryList(query);
    } else {
        contentList = cms.getContentList(query);
    }
    Map<String, List<Content>> letters = new TreeMap<>(Collator.getInstance(new Locale("no", "NO")));

    for (Content content : contentList) {
        String title = content.getTitle();
        String letter = title.substring(0, 1).toUpperCase();

        if (letters.get(letter) == null) {
            letters.put(letter, new ArrayList<>());
        }

        List<Content> links = letters.get(letter);
        links.add(content);
    }

    return Collections.singletonMap("letters", letters);
}

From source file:org.alfresco.web.data.Sort.java

/**
 * Constructor//from   www .  j ava 2  s .c o m
 * 
 * @param data             a the List of String[] data to sort
 * @param column           the column getter method to use on the row to sort
 * @param bForward         true for a forward sort, false for a reverse sort
 * @param mode             sort mode to use (see IDataContainer constants)
 */
public Sort(List data, String column, boolean bForward, String mode) {
    this.data = data;
    this.column = column;
    this.bForward = bForward;
    this.sortMode = mode;

    if (this.data.size() != 0) {
        // setup the Collator for our Locale
        Collator collator = Collator.getInstance(Locale.getDefault());

        // set the strength according to the sort mode
        if (mode.equals(IDataContainer.SORT_CASEINSENSITIVE)) {
            collator.setStrength(Collator.SECONDARY);
        } else {
            collator.setStrength(Collator.IDENTICAL);
        }

        this.keys = buildCollationKeys(collator);
    }
}

From source file:nz.ac.otago.psyanlab.common.designer.util.LongSparseArrayAdapter.java

private Long[] sortKeys(Context context, LongSparseArray<T> items) {
    Locale locale = context.getResources().getConfiguration().locale;
    final Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);

    SortedSet<Long> sortedKeys = new TreeSet<Long>(new Comparator<Long>() {
        @Override//from w ww .j a  va  2 s. c  om
        public int compare(Long lhs, Long rhs) {
            return collator.compare(mItems.get(lhs).toString(), mItems.get(rhs).toString());
        }
    });

    for (int i = 0; i < items.size(); i++) {
        sortedKeys.add(items.keyAt(i));
    }

    return sortedKeys.toArray(new Long[sortedKeys.size()]);
}