001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.tlv.packager.bertlv;
020
021
022import org.jpos.emv.EMVProprietaryTagType;
023import org.jpos.emv.EMVStandardTagType;
024import org.jpos.emv.EMVTagType;
025import org.jpos.tlv.TLVDataFormat;
026import org.jpos.emv.UnknownTagNumberException;
027import org.jpos.iso.ISOException;
028
029/**
030 * Default {@link BERTLVFormatMapper} that resolves a TLV tag number to an
031 * {@link TLVDataFormat} via the EMV standard tag table, deferring proprietary
032 * tags to {@link #getProprietaryTagType(Integer)}.
033 *
034 * @author Vishnu Pillai
035 */
036public class DefaultICCBERTLVFormatMapper implements BERTLVFormatMapper {
037    /** Public constructor; prefer {@link #INSTANCE} for repeated use. */
038    public DefaultICCBERTLVFormatMapper() {}
039
040    /** Shared singleton instance. */
041    public static DefaultICCBERTLVFormatMapper INSTANCE = new DefaultICCBERTLVFormatMapper();
042
043    private EMVTagType getTagType(final Integer tagNumber) throws UnknownTagNumberException {
044        if (EMVStandardTagType.isProprietaryTag(tagNumber)) {
045            return getProprietaryTagType(tagNumber);
046        } else {
047            return EMVStandardTagType.forCode(tagNumber);
048        }
049    }
050
051    @Override
052    public TLVDataFormat getFormat(Integer tagNumber) throws ISOException {
053        EMVTagType tagType;
054        try {
055            tagType = getTagType(tagNumber);
056        } catch (UnknownTagNumberException e) {
057            throw new ISOException(e);
058        }
059        return tagType.getFormat();
060    }
061
062    /**
063     * Subclasses should override this method to provide an implementation of org.jpos.emv.EMVProprietaryTagType
064     * @param tagNumber proprietary tag number to resolve
065     * @return EMVProprietaryTagType
066     * @throws UnknownTagNumberException if the tag number is not registered with this mapper
067     */
068    protected EMVProprietaryTagType getProprietaryTagType(Integer tagNumber) throws UnknownTagNumberException {
069        throw new UnknownTagNumberException(Integer.toHexString(tagNumber));
070    }
071
072}