📄 fsdmsg.java
字号:
private String getSeparatorType(String type) { if (type.length() > 2) { return type.substring(1); } return null; } private char getSeparator(String type) { if (type.length() > 2) { if (separators.containsKey(getSeparatorType(type))) { return ((Character)separators.get(getSeparatorType(type))).charValue(); } else { if (type.endsWith("DS")) { // Dummy separator type, return 0 to indicate nothing to add. return 0; } } } throw new RuntimeException("getSeparator called on for type="+type+" which does not resolve to a known separator."); } protected void pack (Element schema, StringBuffer sb) throws JDOMException, MalformedURLException, IOException, ISOException { String keyOff = ""; Iterator iter = schema.getChildren("field").iterator(); while (iter.hasNext()) { Element elem = (Element) iter.next (); String id = elem.getAttributeValue ("id"); int length = Integer.parseInt (elem.getAttributeValue ("length")); String type = elem.getAttributeValue ("type"); boolean key = "true".equals (elem.getAttributeValue ("key")); String defValue = elem.getText(); String value = get (id, type, length, defValue); sb.append (value); if (isSeparated(type)) { char c = getSeparator(type); if (c > 0) sb.append(c); } if (key) keyOff = keyOff + value; } if (keyOff.length() > 0) pack (getSchema (getId (schema) + keyOff), sb); } protected void unpack (InputStream is, Element schema) throws IOException, JDOMException, MalformedURLException { Iterator iter = schema.getChildren("field").iterator(); String keyOff = ""; while (iter.hasNext()) { Element elem = (Element) iter.next(); String id = elem.getAttributeValue ("id"); int length = Integer.parseInt (elem.getAttributeValue ("length")); String type = elem.getAttributeValue ("type").toUpperCase(); boolean key = "true".equals (elem.getAttributeValue ("key")); String value = readField ( is, id, length, type ); if (key) keyOff = keyOff + value; if ("K".equals(type) && !value.equals (elem.getText())) throw new IllegalArgumentException ( "Field "+id + " value='" +value + "' expected='" + elem.getText () + "'" ); } if (keyOff.length() > 0) { unpack (is, getSchema (getId (schema) + keyOff) ); } } private String getId (Element e) { String s = e.getAttributeValue ("id"); return s == null ? "" : s; } protected String read (InputStream is, int len, String type) throws IOException { StringBuffer sb = new StringBuffer(); byte[] b = new byte[1]; boolean expectSeparator = isSeparated(type); boolean separated = expectSeparator; for (int i=0; i<len; i++) { if (is.read (b) < 0) { if (!type.endsWith("EOF")) throw new EOFException (); else { separated = false; break; } } if (expectSeparator && (b[0] == getSeparator(type))) { separated = false; break; } sb.append ((char) (b[0] & 0xff)); } if (separated && !type.endsWith("EOF")) { if (is.read (b) < 0) { throw new EOFException (); } } return sb.toString (); } protected String readField (InputStream is, String fieldName, int len, String type) throws IOException { String fieldValue = read (is, len, type); if (isBinary(type)) fieldValue = ISOUtil.hexString (fieldValue.getBytes ("ISO8859_1")); fields.put (fieldName, fieldValue);// System.out.println ("++++ "+fieldName + ":" + fieldValue + " " + type + "," + isBinary(type)); return fieldValue; } public void set (String name, String value) { if (value != null) fields.put (name, value); else fields.remove (name); } public void setHeader (byte[] h) { this.header = h; } public byte[] getHeader () { return header; } public String getHexHeader () { return header != null ? ISOUtil.hexString (header).substring (2) : ""; } public String get (String fieldName) { return (String) fields.get (fieldName); } public String get (String fieldName, String def) { String s = (String) fields.get (fieldName); return s != null ? s : def; } public void copy (String fieldName, FSDMsg msg) { fields.put (fieldName, msg.get (fieldName)); } public byte[] getHexBytes (String name) { String s = get (name); return s == null ? null : ISOUtil.hex2byte (s); } public int getInt (String name) { int i = 0; try { i = Integer.parseInt (get (name)); } catch (Exception e) { } return i; } public int getInt (String name, int def) { int i = def; try { i = Integer.parseInt (get (name)); } catch (Exception e) { } return i; } public Element toXML () { Element e = new Element ("message"); if (header != null) { e.addContent ( new Element ("header") .setText (getHexHeader ()) ); } Iterator iter = fields.keySet().iterator(); while (iter.hasNext()) { String fieldName = (String) iter.next(); Element inner = new Element (fieldName); inner.addContent (ISOUtil.normalize ((String) fields.get (fieldName))); e.addContent (inner); } return e; } protected Element getSchema () throws JDOMException, MalformedURLException, IOException { return getSchema (baseSchema); } protected Element getSchema (String message) throws JDOMException, MalformedURLException, IOException { StringBuffer sb = new StringBuffer (basePath); sb.append (message); sb.append (".xml"); String uri = sb.toString (); Space sp = SpaceFactory.getSpace(); Element schema = (Element) sp.rdp (uri); if (schema == null) { synchronized (FSDMsg.class) { schema = (Element) sp.rdp (uri); if (schema == null) { SAXBuilder builder = new SAXBuilder (); URL url = new URL (uri); File f = new File(url.getFile()); if (f.exists()) { schema = builder.build (url).getRootElement (); } else { throw new RuntimeException(f.getCanonicalPath().toString() + " not found"); } } sp.out (uri, schema); } } return schema; } /** * @return message's Map */ public Map getMap () { return fields; } public void setMap (Map fields) { this.fields = fields; } public void dump (PrintStream p, String indent) { String inner = indent + " "; p.println (indent + "<fsdmsg schema='" + basePath + baseSchema + "'>"); if (header != null) { append (p, "header", getHexHeader(), inner); } Iterator iter = fields.keySet().iterator(); while (iter.hasNext()) { String f = (String) iter.next(); String v = ((String) fields.get (f)); append (p, f, v, inner); } p.println (indent + "</fsdmsg>"); } private void append (PrintStream p, String f, String v, String indent) { p.println (indent + f + ": '" + v + "'"); } public boolean hasField(String fieldName) { return fields.containsKey(fieldName); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -