001/*
002 * Copyright 2008 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019import java.util.HashMap;
020
021/**
022 * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
023 * of ISO 18004.
024 *
025 * @author Sean Owen
026 * @since 5.0.2
027 */
028public final class CharacterSetECI {
029
030  private static HashMap<String,CharacterSetECI> NAME_TO_ECI;
031
032  private static void initialize() {
033    HashMap<String,CharacterSetECI> n = new HashMap<String, CharacterSetECI>(29);
034    // TODO figure out if these values are even right!
035    addCharacterSet(0, "Cp437", n);
036    addCharacterSet(1, new String[] {"ISO8859_1", "ISO-8859-1"}, n);
037    addCharacterSet(2, "Cp437", n);
038    addCharacterSet(3, new String[] {"ISO8859_1", "ISO-8859-1"}, n);
039    addCharacterSet(4, new String[] {"ISO8859_2", "ISO-8859-2"}, n);
040    addCharacterSet(5, new String[] {"ISO8859_3", "ISO-8859-3"}, n);
041    addCharacterSet(6, new String[] {"ISO8859_4", "ISO-8859-4"}, n);
042    addCharacterSet(7, new String[] {"ISO8859_5", "ISO-8859-5"}, n);
043    addCharacterSet(8, new String[] {"ISO8859_6", "ISO-8859-6"}, n);
044    addCharacterSet(9, new String[] {"ISO8859_7", "ISO-8859-7"}, n);
045    addCharacterSet(10, new String[] {"ISO8859_8", "ISO-8859-8"}, n);
046    addCharacterSet(11, new String[] {"ISO8859_9", "ISO-8859-9"}, n);
047    addCharacterSet(12, new String[] {"ISO8859_10", "ISO-8859-10"}, n);
048    addCharacterSet(13, new String[] {"ISO8859_11", "ISO-8859-11"}, n);
049    addCharacterSet(15, new String[] {"ISO8859_13", "ISO-8859-13"}, n);
050    addCharacterSet(16, new String[] {"ISO8859_14", "ISO-8859-14"}, n);
051    addCharacterSet(17, new String[] {"ISO8859_15", "ISO-8859-15"}, n);
052    addCharacterSet(18, new String[] {"ISO8859_16", "ISO-8859-16"}, n);
053    addCharacterSet(20, new String[] {"SJIS", "Shift_JIS"}, n);
054    NAME_TO_ECI = n;
055  }
056
057  private final String encodingName;
058  private final int value;
059
060  private CharacterSetECI(int value, String encodingName) {
061    this.encodingName = encodingName;
062    this.value = value;
063  }
064
065  public String getEncodingName() {
066    return encodingName;
067  }
068
069  public int getValue() {
070    return value;
071  }
072
073  private static void addCharacterSet(int value, String encodingName, HashMap<String,CharacterSetECI> n) {
074    CharacterSetECI eci = new CharacterSetECI(value, encodingName);
075    n.put(encodingName, eci);
076  }
077
078  private static void addCharacterSet(int value, String[] encodingNames, HashMap<String,CharacterSetECI> n) {
079    CharacterSetECI eci = new CharacterSetECI(value, encodingNames[0]);
080    for (int i = 0; i < encodingNames.length; i++) {
081      n.put(encodingNames[i], eci);
082    }
083  }
084
085  /**
086   * @param name character set ECI encoding name
087   * @return {@link CharacterSetECI} representing ECI for character encoding, or null if it is legal
088   *   but unsupported
089   */
090  public static CharacterSetECI getCharacterSetECIByName(String name) {
091    if (NAME_TO_ECI == null) {
092      initialize();
093    }
094    return NAME_TO_ECI.get(name);
095  }
096
097}