ConvCp.java

/*
** Module   : ConvCp.java
** Abstract : Utility for converting MIME types.
**
** Copyright (c) 2019, Golden Code Development Corporation.
**
** -#- -I- --Date-- -------------------------------Description--------------------------------
** 001 CA  20190423 Created the first version, by converting the legacy skeleton .cls file and
**                  using that output.
** 002 CA  20190508 Fixed the converted package.
** 002 CA  20190710 Added LegacyResource annotation.  Implemented Equals, NotEquals, IsFalse, 
**                  IsTrue.
** 003 IAS 20190923 Added implementation of additional methods
** 004 ME  20210204 Use upper case when looking-up the codepage.
*/

/*
** 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.oo.core;

import java.util.*;
import java.util.stream.*;

import com.goldencode.p2j.util.*;

/**
 * 
 * Utility for converting MIME types
 *
 */
public class ConvCp
{
   /** MIME types. */
   private static final List<String> MIME = Collections.unmodifiableList(
         Arrays.asList(
               "windows-1250",
               "windows-1251",
               "windows-1252",
               "windows-1253",
               "windows-1254",
               "windows-1255",
               "windows-1256",
               "windows-1257",
               "windows-1258",
               "TIS-620",
               "Big5",
               "EUC-JP",
               "GB_2312-80",
               "IBM037",
               "IBM273",
               "IBM277",
               "IBM278",
               "IBM284",
               "IBM297",
               "IBM437",
               "IBM500",
               "IBM850",
               "IBM851",
               "IBM852",
               "IBM857",
               "IBM00858",
               "IBM861",
               "IBM862",
               "IBM866",
               "ISO-8859-1",
               "ISO-8859-10",
               "ISO-8859-15",
               "ISO-8859-2",
               "ISO-8859-3",
               "ISO-8859-4",
               "ISO-8859-5",
               "ISO-8859-6",
               "ISO-8859-7",
               "ISO-8859-8",
               "ISO-8859-9",
               "KOI8-R",
               "KS_C_5601-1987",
               "hp-roman8",
               "Shift_JIS",
               "UTF-8",
               "GB18030",
               "GBK"
         )
   );

   /** Progress encodings. */
   private static final List<String> PROGRESS = Collections.unmodifiableList(
         Arrays.asList(
               "1250",
               "1251",
               "1252",
               "1253",
               "1254",
               "1255",
               "1256",
               "1257",
               "1258",
               "620-2533",
               "BIG-5",
               "EUCJIS",
               "GB2312",
               "IBM037",
               "IBM273",
               "IBM277",
               "IBM278",
               "IBM284",
               "IBM297",
               "IBM437",
               "IBM500",
               "IBM850",
               "IBM851",
               "IBM852",
               "IBM857",
               "IBM858",
               "IBM861",
               "IBM862",
               "IBM866",
               "ISO8859-1",
               "ISO8859-10",
               "ISO8859-15",
               "ISO8859-2",
               "ISO8859-3",
               "ISO8859-4",
               "ISO8859-5",
               "ISO8859-6",
               "ISO8859-7",
               "ISO8859-8",
               "ISO8859-9",
               "KOI8-R",
               "KSC5601",
               "ROMAN-8",
               "SHIFT-JIS",
               "UTF-8",
               "GB18030",
               "CP936"
         )
   );
   
   /**
    * Convert Progress encoding to MIME type.
    * 
    * @param enc Progress encoding.
    * 
    * @return MIME type.
    */
   public static character toMime(character enc)
   {
      if (enc == null || enc.isUnknown())
      {
         return new character(MIME.stream().collect(Collectors.joining(",")));
      }
      int n = PROGRESS.indexOf(enc.getValue().toUpperCase());
      return n < 0 ? enc : new character(MIME.get(n));
   }
   
   /**
    * Convert MIME type to Progress encoding.
    * 
    * @param mime MIME type.
    * 
    * @return Progress encoding.
    */
   public static character fromMime(character mime)
   {
      if (mime == null || mime.isUnknown())
      {
         return new character(PROGRESS.stream().collect(Collectors.joining(",")));
      }
      int n = MIME.indexOf(mime.getValue());
      return n < 0 ? mime : new character(PROGRESS.get(n));
   }
}