Skip to content

Commit

Permalink
Implement Loggeable
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed May 22, 2014
1 parent e08f1c3 commit e044c24
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
47 changes: 27 additions & 20 deletions jpos/src/main/java/org/jpos/tlv/TLVList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import org.jpos.iso.ISOException;
import org.jpos.iso.ISOUtil;
import org.jpos.util.Loggeable;

import java.io.PrintStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.nio.ByteBuffer;
Expand All @@ -33,18 +35,14 @@
* @author bharavi
*/
@SuppressWarnings("unchecked")
public class TLVList implements Serializable {
public class TLVList implements Serializable, Loggeable {

private List<TLVMsg> tags = new ArrayList();
private int tagToFind = 0;
private int indexLastOccurrence = -1;

/**
* empty constructor
*/
public TLVList() {


super();
}

/**
Expand Down Expand Up @@ -72,10 +70,11 @@ public Enumeration elements() {
/**
* unpack a message with a starting offset
* @param buf - raw message
* @param offset
* @param offset theoffset
* @throws org.jpos.iso.ISOException
*/
public void unpack(byte[] buf, int offset) throws ISOException {
System.out.println (ISOUtil.hexdump(buf));
ByteBuffer buffer=ByteBuffer.wrap(buf,offset,buf.length-offset);
TLVMsg currentNode;
while (hasNext(buffer)) {
Expand All @@ -94,16 +93,16 @@ public void append(TLVMsg tlvToAppend) {

/**
* Append TLVMsg to the TLVList
* @param tag
* @param value
* @param tag tag id
* @param value tag value
*/
public void append(int tag, byte[] value) {
append(new TLVMsg(tag, value));
}

/**
* Append TLVMsg to the TLVList
* @param tag
* @param tag id
* @param value in hexadecimal character representation
*/
public void append(int tag, String value) {
Expand All @@ -112,15 +111,15 @@ public void append(int tag, String value) {

/**
* delete the specified TLV from the list using a Zero based index
* @param index
* @param index number
*/
public void deleteByIndex(int index) {
tags.remove(index);
}

/**
* Delete the specified TLV from the list by tag value
* @param tag
* @param tag id
*/
public void deleteByTag(int tag) {
List t = new ArrayList();
Expand All @@ -133,7 +132,7 @@ public void deleteByTag(int tag) {

/**
* searches the list for a specified tag and returns a TLV object
* @param tag
* @param tag id
* @return TLVMsg
*/
public TLVMsg find(int tag) {
Expand Down Expand Up @@ -183,7 +182,7 @@ public TLVMsg findNextTLV() {
/**
* Returns a TLV object which represents the TLVMsg stored within the TLVList
* at the given index
* @param index
* @param index number
* @return TLVMsg
*/
public TLVMsg index(int index) {
Expand All @@ -207,7 +206,7 @@ public byte[] pack() {

/**
* Read next TLV Message from stream and return it
* @param buffer
* @param buffer the buffer
* @return TLVMsg
*/
private TLVMsg getTLVMsg(ByteBuffer buffer) throws ISOException {
Expand Down Expand Up @@ -273,7 +272,7 @@ private int getTAG(ByteBuffer buffer) {

/**
* Read length bytes and return the int value
* @param buffer
* @param buffer buffer
* @return value length
*/
protected int getValueLength(ByteBuffer buffer) {
Expand All @@ -295,7 +294,7 @@ protected int getValueLength(ByteBuffer buffer) {

/**
* searches the list for a specified tag and returns a hex String
* @param tag
* @param tag id
* @return hexString
*/
public String getString(int tag) {
Expand All @@ -310,7 +309,7 @@ public String getString(int tag) {

/**
* searches the list for a specified tag and returns it raw
* @param tag
* @param tag id
* @return byte[]
*/
public byte[] getValue(int tag) {
Expand All @@ -329,6 +328,14 @@ public byte[] getValue(int tag) {
*/
public boolean hasTag(int tag) {
return (findIndex(tag) > -1);
}

}

@Override
public void dump(PrintStream p, String indent) {
String inner = indent + " ";
p.println (indent + "<tlvlist>");
for (TLVMsg msg : getTags())
msg.dump (p, inner);
p.println (indent + "</tlvlist");
}
}
22 changes: 16 additions & 6 deletions jpos/src/main/java/org/jpos/tlv/TLVMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@

package org.jpos.tlv;

import java.io.PrintStream;
import java.math.BigInteger;
import org.jpos.iso.ISOUtil;
import org.jpos.util.Loggeable;

/**
* @author bharavi
*/
public class TLVMsg {

public class TLVMsg implements Loggeable {
private int tag;
protected byte[] value;

/**
* empty constructor
*/
public TLVMsg() {

super();
}

/**
* constructs a TLV Message from tag and value
*
* @param tag
* @param value
* @param tag id
* @param value tag value
*/
public TLVMsg(int tag, byte[] value) {
this.tag = tag;
Expand Down Expand Up @@ -150,5 +151,14 @@ public String toString(){
return String.format("[tag: 0x%s, %s]", t
,value==null?null:getStringValue());
}
}

@Override
public void dump(PrintStream p, String indent) {
p.print (indent);
p.print ("<tag id='");
p.print (Integer.toHexString(getTag()));
p.print ("' value='");
p.print (ISOUtil.hexString(getValue()));
p.println ("' />");
}
}

0 comments on commit e044c24

Please sign in to comment.