Skip to content

Commit

Permalink
Support random parts in strings generated by AttributesFormat fix dcm…
Browse files Browse the repository at this point in the history
  • Loading branch information
gunterze authored and Patrick Peer committed Oct 17, 2019
1 parent 22119b6 commit a4a3d18
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
51 changes: 44 additions & 7 deletions dcm4che-core/src/main/java/org/dcm4che3/util/AttributesFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

import org.dcm4che3.data.Attributes;

Expand Down Expand Up @@ -127,13 +129,14 @@ private MessageFormat buildMessageFormat(ArrayList<String> tokens) {
formatBuilder.append(tokens.get(j++)).append('{').append(i);
String tagStr = tokens.get(j++);
int typeStart = tagStr.indexOf(',') + 1;
if (!tagStr.startsWith("now")) {
boolean rnd = tagStr.startsWith("rnd");
if (!rnd && !tagStr.startsWith("now")) {
int tagStrLen = typeStart != 0
? typeStart - 1
: tagStr.length();

int indexStart = tagStr.charAt(tagStrLen-1) == ']'
? tagStr.lastIndexOf('[', tagStrLen-3) + 1
? tagStr.lastIndexOf('[', tagStrLen-3) + 1
: 0;
try {
tagPaths[i] = TagUtils.parseTagPath(tagStr.substring(0, indexStart != 0 ? indexStart - 1 : tagStrLen));
Expand All @@ -151,12 +154,28 @@ private MessageFormat buildMessageFormat(ArrayList<String> tokens) {
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(pattern);
}
if (types[i] != Type.hash && types[i] != Type.md5 && types[i] != Type.urlencoded)
formatBuilder.append(
typeStart > 0 ? tagStr.substring(typeStart-1) : tagStr);
switch (types[i]) {
case number:
case date:
case time:
case choice:
formatBuilder.append(
typeStart > 0 ? tagStr.substring(typeStart - 1) : tagStr);
}
} else {
types[i] = Type.none;
}
if (rnd) {
switch (types[i]) {
case none:
types[i] = Type.rnd;
case uuid:
case uid:
break;
default:
throw new IllegalArgumentException(pattern);
}
}
formatBuilder.append('}');
}
if (j < tokens.size())
Expand Down Expand Up @@ -205,7 +224,7 @@ public String toString() {
return pattern;
}

private static enum Type {
private enum Type {
none {
@Override
Object toArg(Attributes attrs, int tag, int index) {
Expand Down Expand Up @@ -260,6 +279,24 @@ Object toArg(Attributes attrs, int tag, int index) {
throw new AssertionError(e);
}
}
},
rnd {
@Override
Object toArg(Attributes attrs, int tag, int index) {
return TagUtils.toHexString(ThreadLocalRandom.current().nextInt());
}
},
uuid {
@Override
Object toArg(Attributes attrs, int tag, int index) {
return UUID.randomUUID();
}
},
uid {
@Override
Object toArg(Attributes attrs, int tag, int index) {
return UIDUtils.createUID();
}
};

abstract Object toArg(Attributes attrs, int tag, int index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.VR;
import org.dcm4che3.util.AttributesFormat;
import org.junit.Test;

import java.util.regex.Pattern;

/**
* @author Gunter Zeilinger <gunterze@gmail.com>
*
Expand All @@ -56,6 +57,10 @@ public class AttributesFormatTest {
"{00080020,date,yyyy/MM/dd}/{00080030,time,HH}/{0020000D,hash}/{0020000E,hash}/{00080008[1]}/{00080018}.dcm";
private static final String TEST_PATTERN_MD5 =
"{00080020,date,yyyy/MM/dd}/{00080030,time,HH}/{0020000D,hash}/{0020000E,hash}/{00080018,md5}.dcm";
private static final String TEST_PATTERN_RND =
"{rnd}/{rnd,uuid}/{rnd,uid}";
private static final Pattern ASSERT_PATTERN_RND =
Pattern.compile("[0-9A-F]{8}+/[0-9a-f]{8}+(-[0-9a-f]{4}+){3}+-[0-9a-f]{12}+/2\\.25\\.\\d*");

@Test
public void testFormat() {
Expand All @@ -82,4 +87,9 @@ public void testFormatMD5() {
new AttributesFormat(TEST_PATTERN_MD5).format(attrs));
}

@Test
public void testFormatRND() {
assertTrue(ASSERT_PATTERN_RND.matcher(
new AttributesFormat(TEST_PATTERN_RND).format(new Attributes())).matches());
}
}

0 comments on commit a4a3d18

Please sign in to comment.