📄 fsdmsg.java
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2009 Alejandro P. Revilla * * 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 should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */package org.jpos.util;import java.io.ByteArrayInputStream;import java.io.EOFException;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jpos.iso.ISOException;import org.jpos.iso.ISOUtil;import org.jpos.space.Space;import org.jpos.space.SpaceFactory;/** * General purpose, Field Separator delimited message * * @author Alejandro Revila * @author Mark Salter * @since 1.4.7 */public class FSDMsg implements Loggeable { public static char FS = '\034'; public static char US = '\037'; public static char RS = '\035'; public static char GS = '\036'; public static char EOF = '\000'; Map fields; Map separators; String baseSchema; String basePath; byte[] header; /** * @param basePath schema path */ public FSDMsg (String basePath) { this (basePath, "base"); } /** * @param basePath schema path * @param baseSchema schema name */ public FSDMsg (String basePath, String baseSchema) { super(); fields = new LinkedHashMap(); separators = new LinkedHashMap(); this.basePath = basePath; this.baseSchema = baseSchema; setSeparator("FS", FS); setSeparator("US", US); setSeparator("GS", GS); setSeparator("RS", RS); setSeparator("EOF", EOF); } public String getBasePath() { return basePath; } public String getBaseSchema() { return baseSchema; } /* * add a new or override an existing separator type/char pair. * * @param separatorName string of type used in definition (FS, US etc) * @param separator char representing type */ public void setSeparator(String separatorName, char separator) { separators.put(separatorName,new Character(separator)); } /* * add a new or override an existing separator type/char pair. * * @param separatorName string of type used in definition (FS, US etc) * @param separator char representing type */ public void unsetSeparator(String separatorName) { if (separators.containsKey(separatorName)) { separators.remove(separatorName); } else { throw new RuntimeException("unsetSeparator was attempted for "+separatorName+" which was not previously defined."); } } /** * parse message * * @param is input stream * * @throws IOException * @throws JDOMException * @throws MalformedURLException */ public void unpack (InputStream is) throws IOException, JDOMException, MalformedURLException { try { unpack (is, getSchema (baseSchema)); } catch (EOFException e) { fields.put ("EOF", "true"); } } /** * parse message * * @param b message image * * @throws IOException * @throws JDOMException * @throws MalformedURLException * @throws ISOException */ public void unpack (byte[] b) throws IOException, JDOMException, MalformedURLException { unpack (new ByteArrayInputStream (b)); } /** * @return message string * @throws ISOException */ public String pack () throws JDOMException, MalformedURLException, IOException, ISOException { StringBuffer sb = new StringBuffer (); pack (getSchema (baseSchema), sb); return sb.toString (); } public byte[] packToBytes () throws JDOMException, MalformedURLException, IOException, ISOException, UnsupportedEncodingException { return pack().getBytes("ISO8859_1"); } protected String get (String id, String type, int length, String defValue) throws ISOException { String value = (String) fields.get (id); if (value == null) value = defValue == null ? "" : defValue; type = type.toUpperCase (); switch (type.charAt (0)) { case 'N': if (isSeparated(type)) { // Leave value unpadded. } else { value = ISOUtil.zeropad (value, length); } break; case 'A': if (isSeparated(type)) { // Leave value unpadded. } else { value = ISOUtil.strpad (value, length); } if (value.length() > length) value = value.substring(0,length); break; case 'K': if (defValue != null) value = defValue; break; case 'B': try { if ((length << 1) >= value.length()) { if (isSeparated(type)) { // Convert but do not pad if this field ends with a separator value = new String ( ISOUtil.hex2byte (value), "ISO8859_1"); } else { value = new String ( ISOUtil.hex2byte (ISOUtil.zeropad(value,length << 1).substring (0, length << 1)), "ISO8859_1"); } } else { throw new RuntimeException("field content="+value+" is too long to fit in field "+id+" whose length is "+length); } } catch (UnsupportedEncodingException e) { // ISO8859_1 is supported } break; } return (isSeparated(type)) ? ISOUtil.blankUnPad(value) : value; } private boolean isSeparated(String type) { /* * if type's last two characters appear in our Map of separators, * return true */ if (type.length() > 2) { if (separators.containsKey(getSeparatorType(type))) { return true; } else { if (type.endsWith("DS")) { return true; } throw new RuntimeException("FSDMsg.isSeparated(String) found that type of "+type+" is used, but "+getSeparatorType(type)+" has not been defined as a separator!"); } } return false; } private boolean isBinary(String type) { /* * if type's first digit is a 'B' return true */ return type.startsWith("B"); } public boolean isSeparator(byte b) { return separators.containsValue(new Character((char)b)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -