Skip to content

Commit

Permalink
Add support for CharTagMapBuilder.withTagLengthSwap
Browse files Browse the repository at this point in the history
Ability to change the order of Tag and Length tags.
  • Loading branch information
demsey committed Oct 9, 2019
1 parent 065d57e commit 94ce571
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
19 changes: 18 additions & 1 deletion jpos/src/main/java/org/jpos/tlv/CharTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class CharTag {
private final String tagId;
private final String value;

private boolean swapTagWithLength;

/**
* Internal Tag constructor.
*
Expand All @@ -61,6 +63,15 @@ protected void setLengthSize(int size) {
lengthSize = size;
}

/**
* Swap tag with length.
*
* @param swap indicates if tag element will be swapped with length element
*/
protected void withTagLengthSwap(boolean swap) {
swapTagWithLength = swap;
}

/**
* Form TLV for this tag.
*
Expand All @@ -71,7 +82,13 @@ public String getTLV() {
if (value != null)
vLen = value.length();

String tlv = tagId + ISOUtil.zeropad(vLen, lengthSize);
String length = ISOUtil.zeropad(vLen, lengthSize);
String tlv;
if (swapTagWithLength)
tlv = length + tagId;
else
tlv = tagId + length;

if (vLen == 0)
return tlv;

Expand Down
24 changes: 22 additions & 2 deletions jpos/src/main/java/org/jpos/tlv/CharTagMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class CharTagMap extends HashMap<String, CharTag> {
private int tagLen = 0x02;
private int lenLen = 0x03;

private boolean swapTagWithLength;

/**
* Creates new empty instance of text TLV tag map.
* <p>
Expand Down Expand Up @@ -92,6 +94,15 @@ protected void setLengthSize(int size) throws IllegalArgumentException {
lenLen = size;
}

/**
* Sets size of length element.
*
* @param swap indicates if tag element will be swapped with length element
*/
protected void withTagLengthSwap(boolean swap) {
swapTagWithLength = swap;
}

/**
* Unpack string to TLV tag map.
*
Expand Down Expand Up @@ -159,6 +170,7 @@ public CharTag createTLV(String tagId, String value) throws IllegalArgumentExcep

CharTag tag = new CharTag(tagId, value);
tag.setLengthSize(lenLen);
tag.withTagLengthSwap(swapTagWithLength);
return tag;
}

Expand Down Expand Up @@ -218,8 +230,16 @@ private int stripLength(CharBuffer buffer) throws IllegalArgumentException {
}

private CharTag getTLVMsg(CharBuffer buffer) throws IllegalArgumentException {
String tagId = stripTagId(buffer);
int len = stripLength(buffer);
String tagId;
int len;
if (swapTagWithLength) {
len = stripLength(buffer);
tagId = stripTagId(buffer);
} else {
tagId = stripTagId(buffer);
len = stripLength(buffer);
}

if (buffer.remaining() < len)
throw new IllegalArgumentException(
String.format("%s tag '%s' length '%03d' exceeds available"
Expand Down
20 changes: 19 additions & 1 deletion jpos/src/main/java/org/jpos/tlv/CharTagMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package org.jpos.tlv;

/**
* Builder to create TLV tag maps stored as sequence of characters.
* Builder to create TLV/LTV tag maps stored as sequence of characters.
* <p>
* Using {@code withTagLengthSwap(true)} while creating the builder causes
* switchs {@code CharTagMap} in LTV mode.
*
* @author Robert Demski <[email protected]>
*/
Expand All @@ -28,6 +31,8 @@ public class CharTagMapBuilder {
protected Integer tagSize;
protected Integer lengthSize;

protected boolean swapTagWithLength;

/**
* Constructs a new instance of the builder.
*/
Expand Down Expand Up @@ -57,6 +62,17 @@ public CharTagMapBuilder withTagSize(int size) {
return this;
}

/**
* Swap Tag with Length.
*
* @param swap indicates if tag element will be swapped with length element
* @return this, for chaining, not {@code null}
*/
public CharTagMapBuilder withTagLengthSwap(boolean swap) {
swapTagWithLength = swap;
return this;
}

/**
* Completes this builder by creating the {@code CharTagMap}.
*
Expand All @@ -71,6 +87,8 @@ public CharTagMap build() throws IllegalArgumentException {
if (lengthSize != null)
tm.setLengthSize(lengthSize);

tm.withTagLengthSwap(swapTagWithLength);

return tm;
}

Expand Down
17 changes: 17 additions & 0 deletions jpos/src/test/java/org/jpos/tlv/CharTagMapBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public void testBuild() {
assertEquals("Foo Bar", tm.getTagValue("KV1"));
}

@Test
public void testBuildSwapped() {
CharTagMap tm = instance
.withTagLengthSwap(true)
.build();

tm.unpack("019XYThe quick brown fox007KVFoo Bar");
assertEquals("The quick brown fox", tm.getTagValue("XY"));
assertEquals("Foo Bar", tm.getTagValue("KV"));

String result = tm.pack();
tm.clear();
tm.unpack(result);
assertEquals("The quick brown fox", tm.getTagValue("XY"));
assertEquals("Foo Bar", tm.getTagValue("KV"));
}

@Test
public void testBuildLongTagSize() {
thrown = assertThrows(IllegalArgumentException.class,
Expand Down

0 comments on commit 94ce571

Please sign in to comment.