Class FSDMsg
java.lang.Object
org.jpos.util.FSDMsg
General purpose, Field Separator delimited message.
How to use
The message format (or schema) is defined in xml files containing a schema element, with an optional id attribute, and multiple field elements. A field element is made up of the following attributes:
- id
- The name of the field. This is used in calls to
set(String, String). It should be unique amongst the fields in an FSDMsg. - length
- The maximum length of the data allowed in this field. Fixed length fields will be padded to this length. A zero length is allowed, and can be useful to define extra separator characters in the message.
- type
- The type of the included data, including an optional separator for marking the end of the field and the beginning of the next one. The data type is defined by the first char of the type, and the separator is defined by the following chars. If a field separator is specified, then no padding is done on values for this field.
- key
- If this optional attribute has a value of "true", then fields from another schema, specified by the value, are appended to this schema.
- separator
- An optional attribute containing the separator for the field. This is the preferred method of specifying the separator. See the list of optional
Possible types are:
- A
- Alphanumeric. Padding if any is done with spaces to the right.
- B
- Binary. Padding, if any, is done with zeros to the left.
- K
- Constant. The value is specified by the field content. No padding is done.
- N
- Numeric. Padding, if any, is done with zeros to the left.
Supported field separators are:
- FS
- Field separator using '034' as the separator.
- US
- Field separator using '037' as the separator.
- GS
- Group separator using '035' as the separator.
- RS
- Row separator using '036' as the separator.
- PIPE
- Field separator using '|' as the separator.
- EOF
- End of File - no separator character is emitted, but also no padding is done. Also if the end of file is reached parsing a message, then no exception is thrown.
- DS
- A dummy separator. This is similar to EOF, but the message stream must not end before it is allowed.
- EOM
- End of message separator. This reads all bytes available in the stream.
Key fields allow you to specify a tree of possible message formats. The key fields are the fork points of the tree. Multiple key fields are supported. It is also possible to have more key fields specified in appended schemas.
- Since:
- 1.4.7
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic charEnd of File character (null byte).static charEnd of Message marker (null byte).static charField Separator character (ASCII 0x1C).static charGroup Separator character (ASCII 0x1D).static charPipe character (ASCII 0x7C).static charRecord Separator character (ASCII 0x1E).static charUnit Separator character (ASCII 0x1F). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()voidCopies a field value from another FSDMsg into this message.voidCopies a field value from another FSDMsg into this message, using a default if not present.voiddump(PrintStream p, String indent) Dumps a human-readable representation of this object to the print stream.booleanReturns the value of the named field.Returns the value of the named field, or a default if not set.protected StringReturns the formatted value for the named field.protected StringReturns the formatted value for the named field with optional unpadding.Returns the base schema path used to load field definitions.Returns the base schema name used to load field definitions.byte[]Returns the binary header bytes.byte[]getHexBytes(String name) Returns the value of the named field decoded from hex.Returns the header as a hex string, or empty string if no header is set.intReturns the integer value of the named field, or 0 if absent or non-numeric.intReturns the integer value of the named field, or a default if absent or non-numeric.getMap()Returns the underlying fields map.protected org.jdom2.ElementReturns the root schema Element for the base schema.protected org.jdom2.ElementReturns the root schema Element for the named message schema.protected org.jdom2.ElementReturns the root schema Element located at the given path.booleanReturns true if this message has a value for the named field.inthashCode()booleanisSeparator(byte b) Tests whether a byte matches any configured separator character.protected org.jdom2.ElementloadSchema(String uri, boolean throwex) Loads and parses the schema from the given URI.voidCopies all fields from the given message into this message.pack()Packs this FSDMsg into its string representation.protected voidpack(org.jdom2.Element schema, StringBuilder sb) Packs this message into the given StringBuilder using the provided schema.byte[]Packs this message into a byte array.protected Stringread(InputStreamReader r, int len, String type, String separator) Reads a field value from the stream.protected StringreadField(InputStreamReader r, String fieldName, int len, String type, String separator) Reads a named field value from the stream.protected InputStreamschemaResouceInputStream(String resource) Returns an InputStream for the given classpath resource.voidSets a field value; removes the field if value is null.voidsetCharset(Charset charset) Sets the character set used for packing and unpacking string fields.voidsetHeader(byte[] h) Sets the binary header bytes for this message.voidSets the underlying fields map.voidsetSeparator(String separatorName, char separator) Adds or overrides a separator type/char pair.org.jdom2.ElementtoXML()Serializes this FSDMsg to a JDOM XML Element.voidunpack(byte[] b) parse message.voidunpack(InputStream is) parse message.protected voidunpack(InputStreamReader r, org.jdom2.Element schema) Unpacks a message from the reader using the provided schema.voidunsetSeparator(String separatorName) Removes a previously defined separator type.Methods inherited from class Object
finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Loggeable
dumpModifier and TypeMethodDescriptiondefault voiddump(PrintStream p, String indent, LogRenderer.Type type) Dumps a representation of this object using the specified renderer type.
-
Field Details
-
FS
Field Separator character (ASCII 0x1C). -
US
Unit Separator character (ASCII 0x1F). -
GS
Group Separator character (ASCII 0x1D). -
RS
Record Separator character (ASCII 0x1E). -
EOF
End of File character (null byte). -
PIPE
Pipe character (ASCII 0x7C). -
EOM
End of Message marker (null byte).
-
-
Constructor Details
-
FSDMsg
-
FSDMsg
Creates a FSDMsg with a specific base path for the message format schema, and a base schema name. For instance, FSDMsg("file:src/data/NDC-", "root") will look for a file: src/data/NDC-root.xml- Parameters:
basePath- schema pathbaseSchema- schema name
-
-
Method Details
-
getBasePath
Returns the base schema path used to load field definitions.- Returns:
- the base schema path
-
getBaseSchema
Returns the base schema name used to load field definitions.- Returns:
- the base schema name
-
setCharset
Sets the character set used for packing and unpacking string fields.- Parameters:
charset- the character set to use
-
setSeparator
Adds or overrides a separator type/char pair.- Parameters:
separatorName- string identifier for the separator type (e.g. "FS", "US")separator- the character representing this separator
-
unsetSeparator
Removes a previously defined separator type.- Parameters:
separatorName- string identifier for the separator type to remove- Throws:
IllegalArgumentException- if the separator was not previously defined
-
unpack
parse message. If the stream ends before the message is completely read, then the method adds an EOF field.- Parameters:
is- input stream- Throws:
IOException- on I/O errororg.jdom2.JDOMException- on XML parsing error
-
unpack
parse message. If the stream ends before the message is completely read, then the method adds an EOF field.- Parameters:
b- message image- Throws:
IOException- on I/O error while reading the byte arrayorg.jdom2.JDOMException- on schema parsing error
-
pack
Packs this FSDMsg into its string representation.- Returns:
- the packed message string
- Throws:
org.jdom2.JDOMException- on schema parsing errorIOException- on I/O errorISOException- on packing error
-
packToBytes
Packs this message into a byte array.- Returns:
- the packed bytes
- Throws:
ISOException- on pack errorIOException- on I/O errororg.jdom2.JDOMException- on schema parse error
-
get
protected String get(String id, String type, int length, String defValue, String separator) throws ISOException Returns the formatted value for the named field.- Parameters:
id- field identifiertype- field typelength- field lengthdefValue- default value if field is absentseparator- field separator- Returns:
- formatted field value
- Throws:
ISOException- on error
-
get
protected String get(String id, String type, int length, String defValue, String separator, boolean unPad) throws ISOException Returns the formatted value for the named field with optional unpadding.- Parameters:
id- field identifiertype- field typelength- field lengthdefValue- default valueseparator- field separatorunPad- if true, strip padding- Returns:
- formatted field value
- Throws:
ISOException- on error
-
isSeparator
Tests whether a byte matches any configured separator character.- Parameters:
b- the byte to test- Returns:
- true if the byte corresponds to any configured separator
-
pack
protected void pack(org.jdom2.Element schema, StringBuilder sb) throws org.jdom2.JDOMException, IOException, ISOException Packs this message into the given StringBuilder using the provided schema.- Parameters:
schema- the schema elementsb- the target StringBuilder- Throws:
ISOException- on pack errorIOException- on I/O errororg.jdom2.JDOMException- on schema error
-
unpack
protected void unpack(InputStreamReader r, org.jdom2.Element schema) throws IOException, org.jdom2.JDOMException Unpacks a message from the reader using the provided schema.- Parameters:
r- the readerschema- the schema element- Throws:
IOException- on I/O errororg.jdom2.JDOMException- on schema error
-
read
protected String read(InputStreamReader r, int len, String type, String separator) throws IOException Reads a field value from the stream.- Parameters:
r- the readerlen- maximum field lengthtype- field typeseparator- field separator- Returns:
- the field value
- Throws:
IOException- on I/O error
-
readField
protected String readField(InputStreamReader r, String fieldName, int len, String type, String separator) throws IOException Reads a named field value from the stream.- Parameters:
r- the readerfieldName- the field namelen- maximum field lengthtype- field typeseparator- field separator- Returns:
- the field value
- Throws:
IOException- on I/O error
-
set
-
setHeader
Sets the binary header bytes for this message.- Parameters:
h- the header bytes to set
-
getHeader
Returns the binary header bytes.- Returns:
- the header bytes, or null if not set
-
getHexHeader
Returns the header as a hex string, or empty string if no header is set.- Returns:
- the header as a hex string
-
get
-
get
-
copy
-
copy
-
getHexBytes
Returns the value of the named field decoded from hex.- Parameters:
name- the field name containing a hex-encoded value- Returns:
- decoded bytes, or null if field not set
-
getInt
-
getInt
-
toXML
Serializes this FSDMsg to a JDOM XML Element.- Returns:
- XML Element representing this message
-
getSchema
Returns the root schema Element for the base schema.- Returns:
- the root schema Element
- Throws:
org.jdom2.JDOMException- on XML parsing errorIOException- on I/O error
-
getSchema
Returns the root schema Element for the named message schema.- Parameters:
message- the schema message name- Returns:
- the root schema Element
- Throws:
org.jdom2.JDOMException- on XML parsing errorIOException- on I/O error
-
getSchema
protected org.jdom2.Element getSchema(String prefix, String suffix, String defSuffix) throws org.jdom2.JDOMException, IOException Returns the root schema Element located at the given path.- Parameters:
prefix- the schema path prefixsuffix- the schema path suffixdefSuffix- the default suffix to use if the path is not found- Returns:
- the root schema Element
- Throws:
org.jdom2.JDOMException- on XML parsing errorIOException- on I/O error
-
loadSchema
protected org.jdom2.Element loadSchema(String uri, boolean throwex) throws org.jdom2.JDOMException, IOException Loads and parses the schema from the given URI.- Parameters:
uri- the URI of the schema resource to loadthrowex- if true, throw an exception if the resource is not found- Returns:
- the parsed root Element, or null if not found and throwex is false
- Throws:
org.jdom2.JDOMException- on XML parsing errorIOException- on I/O error
-
schemaResouceInputStream
protected InputStream schemaResouceInputStream(String resource) throws org.jdom2.JDOMException, IOException Returns an InputStream for the given classpath resource.- Parameters:
resource- the classpath resource path- Returns:
- the InputStream, or null if the resource is not found
- Throws:
IOException- on I/O errororg.jdom2.JDOMException- on XML error
-
getMap
-
setMap
-
dump
-
hasField
-
clone
-
merge
-
equals
-
hashCode
-