FwdCollatorProvider.java

/*
** Module   : FwdCollatorProvider.java
** Abstract : Collator SPI implementation
**
** Copyright (c) 2004-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- -JPRM- ------------------------------------Description------------------------------------
** 001 ECF 20080321 @37637 Created initial version. A service provider for specialized collators which are
**                         governed by Progress-like collation rules.
** 002 OM  20210624        Added new collations using new naming schema.
** 003 OM  20210716        New naming schema for collations which is compatible with Linux locales.
**     OM  20210728        Renamed FwdCollatorProvider class and fwdspi.jar library.
**     OM  20220311        Added support for IBM850 collation.
**     TJD 20220504        Java 11 compatibility minor changes
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.spi;

import java.text.*;
import java.text.spi.*;
import java.util.*;

/**
 * A service provider for specialized collators which are governed by Progress-like collation rules.
 * 
 * A collation is shared among multiple languages of the same encoding. As such we will use the following
 * naming schema:<br>
 * {@code ab_CD_cp1234_fwd_basic}
 * <p>
 * Here are each part explained:
 * <ol>
 *    <li>ab - the base language, the very same as the first parameter of {@code Locale} c'tor;</li>
 *    <li>CD - the territory / country, the very same as the second parameter of {@code Locale} c'tor;</li>
 *    <li>cp1234 - the text encoding, sometime incorrectly named CodePage. Always in lowercase. The dashes (-)
 *          and underscores (_) are omitted. This is one of the supported CPs from 4GL.
 *          Initially only {@code ISO8859-1}, {@code ISO8859-15} and {@code CP1252} were supported;</li>
 *    <li>fwd - (in lowercase) this particle acts as a magic number to mark FWD specific collations. Should
 *          appear in all collation names, the only exception is the 'legacy' {@code en_US_P2J} where it is
 *          replaced by 'p2j'.</li>
 *    <li>basic - the 4GL collation. The default is {@code basic} in 4GL, but it is not optional here. The
 *          list of 4GL collations is CP-specific.</li>
 * </ol>
 */
public class FwdCollatorProvider
extends CollatorProvider
{
   /** Mapping of locales to fully qualified collator class names */
   private static final Map<Locale, String> collatorClasses;
   
   /** All collator implementations managed by this provider */
   private static final Map<Locale, Collator> collators = new HashMap<>(12);
   
   static
   {
      Map<Locale, String> map = new HashMap<>(10);
      // register all supported collator implementation class names
      try
      {
         // ISO-8859-1 / Latin-1 Collator
         map.put(new Locale("en", "US", "iso88591_fwd_basic"),  "com.goldencode.p2j.spi.ISO_8859_1Collator");
         map.put(new Locale("en", "GB", "iso88591_fwd_basic"),  "com.goldencode.p2j.spi.ISO_8859_1Collator");
         map.put(new Locale("nl", "NL", "iso88591_fwd_basic"),  "com.goldencode.p2j.spi.ISO_8859_1Collator");
         // legacy, keeping it for making transition smoother, at a risk of incorrect sorting if the new
         // collator is not updated in [p2j.cfg.xml] / [directory.xml]
         map.put(new Locale("en", "US", "P2J"),                 "com.goldencode.p2j.spi.ISO_8859_1Collator");
         
         // ISO-8859-15 / Latin-1 Collator
         map.put(new Locale("en", "US", "iso885915_fwd_basic"), "com.goldencode.p2j.spi.ISO_8859_15Collator");
         map.put(new Locale("en", "GB", "iso885915_fwd_basic"), "com.goldencode.p2j.spi.ISO_8859_15Collator");
         map.put(new Locale("nl", "NL", "iso885915_fwd_basic"), "com.goldencode.p2j.spi.ISO_8859_15Collator");
         
         // Windows-1252 / CP1252 Collator
         map.put(new Locale("en", "US", "cp1252_fwd_basic"),    "com.goldencode.p2j.spi.CP1252Collator");
         map.put(new Locale("en", "GB", "cp1252_fwd_basic"),    "com.goldencode.p2j.spi.CP1252Collator");
         map.put(new Locale("nl", "NL", "cp1252_fwd_basic"),    "com.goldencode.p2j.spi.CP1252Collator");
         
         // IBM850 / CP850 / OEM850 Collator
         map.put(new Locale("en", "US", "ibm850_fwd_basic"),    "com.goldencode.p2j.spi.IBM850Collator");
         map.put(new Locale("pt", "BR", "ibm850_fwd_basic"),    "com.goldencode.p2j.spi.IBM850Collator");
      }
      catch (Exception exc)
      {
         throw new RuntimeException("Error initializing FWD collator(s)", exc);
      }
      collatorClasses = Collections.unmodifiableMap(map);
   }
   
   /**
    * Default constructor.
    */
   public FwdCollatorProvider()
   {
   }
   
   /**
    * Get an instance of a collator, if this provider is responsible for the specified locale. The collator
    * is lazily instantiated and cached for future requests.
    * 
    * @param   locale
    *          A locale possibly managed by this provider.
    * 
    * @return  Appropriate collator, or {@code null} if the specified locale is unknown to this provider.
    */
   public Collator getInstance(Locale locale)
   {
      synchronized (collators)
      {
         Collator collator = collators.get(locale);
         
         if (collator == null)
         {
            String className = collatorClasses.get(locale);
            if (className == null)
            {
               return null;
            }
            
            try
            {
               collator = (Collator) Class.forName(className).getDeclaredConstructor().newInstance();
               collators.put(locale, collator);
            }
            catch (Exception exc)
            {
               // TODO:  what is the appropriate way to log using the Java Extension Mechanism?
               exc.printStackTrace();
               
               return null;
            }
         }
         
         return collator;
      }
   }
   
   /**
    * Report all locales managed by this provider.
    * 
    * @return  Array of locales.
    */
   public Locale[] getAvailableLocales()
   {
      Set<Locale> set = collatorClasses.keySet();
      
      return set.toArray(new Locale[set.size()]);
   }

}