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.rc; 020 021import org.jpos.core.Configurable; 022import org.jpos.core.Configuration; 023import org.jpos.core.ConfigurationException; 024import org.jpos.iso.ISOUtil; 025 026import java.io.IOException; 027import java.io.InputStream; 028import java.util.*; 029 030public class CMFConverter implements IRCConverter, Configurable { 031 private static final Map<Integer, RC> rcs = new HashMap<>(); 032 private static final Map<String, IRC> ircs = new HashMap<>(); 033 private Configuration cfg; 034 035 static { 036 try { 037 load ("org/jpos/rc/CMF.properties"); 038 load ("META-INF/org/jpos/rc/CMF.properties"); 039 } catch (IOException ignored) { } 040 } 041 042 public CMFConverter() { 043 super(); 044 } 045 046 public CMFConverter(Configuration cfg) { 047 this.cfg = cfg; 048 } 049 050 @Override 051 public RC convert(IRC irc) { 052 String s = cfg != null ? cfg.get(Long.toString(irc.irc()), null) : null; 053 if (s != null) { 054 return buildRC(s); 055 } else if (rcs.containsKey(irc.irc())) 056 return rcs.get(irc.irc()); 057 058 s = irc.irc() > 9999 ? Long.toString(irc.irc()) : ISOUtil.zeropad(irc.irc(),4); 059 return new SimpleRC(s, (irc instanceof CMF ? ((CMF)irc).name().replace("_", " ") : null)); 060 } 061 062 @Override 063 public IRC convert (RC rc) { 064 IRC irc = ircs.get(rc.rc()); 065 if (irc == null) { 066 irc = CMF.valueOf(Integer.parseInt(rc.rc())); 067 } 068 return irc; 069 } 070 071 private static void load (String base) throws IOException { 072 try (InputStream in=loadResourceAsStream(base)) { 073 if (in != null) 074 addBundle(new PropertyResourceBundle(in)); 075 } 076 } 077 private static InputStream loadResourceAsStream(String name) { 078 InputStream in = null; 079 080 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 081 if (contextClassLoader != null) 082 in = contextClassLoader.getResourceAsStream(name); 083 if (in == null) 084 in = CMFConverter.class.getClassLoader().getResourceAsStream(name); 085 return in; 086 } 087 088 private static SimpleRC buildRC (String s) { 089 String[] ss = ISOUtil.commaDecode(s); 090 String rc = null; 091 String display = null; 092 if (ss.length > 0) 093 rc = ss[0]; 094 if (ss.length > 1) 095 display = ss[1]; 096 Objects.requireNonNull(rc, "Invalid result code"); 097 return new SimpleRC (rc, display); 098 } 099 100 private static void addBundle(ResourceBundle r) { 101 Enumeration en = r.getKeys(); 102 while (en.hasMoreElements()) { 103 String key = (String) en.nextElement(); 104 String value = r.getString(key); 105 RC rc = buildRC(value); 106 int irc = Integer.parseInt(key); 107 rcs.put (irc, rc); 108 ircs.put (rc.rc(), CMF.valueOf(irc)); 109 } 110 } 111 112 @Override 113 public void setConfiguration(Configuration cfg) throws ConfigurationException { 114 this.cfg = cfg; 115 } 116}