Skip to content

Commit

Permalink
Added TagPath.toString with optional keyword lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlSjostrand committed Oct 27, 2018
1 parent f718132 commit 692ab8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/main/scala/se/nimsa/dicom/data/TagPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,31 @@ sealed trait TagPath {
take(path = this, i = depth - n)
}

override def toString: String = {
def toString(lookup: Boolean): String = {
def toTagString(tag: Int): String = if (lookup) {
val keyword = Dictionary.keywordOf(tag)
if (keyword.isEmpty) tagToString(tag) else keyword
}
else
tagToString(tag)

@tailrec
def toTagPathString(path: TagPath, tail: String): String = {
val itemIndexSuffix = path match {
case _: TagPathSequenceAny => "[*]"
case s: TagPathSequenceItem => s"[${s.item}]"
case _ => ""
}
val head = tagToString(path.tag) + itemIndexSuffix
val head = toTagString(path.tag) + itemIndexSuffix
val part = head + tail
if (path.isRoot) part else toTagPathString(path.previous, "." + part)
}

if (isEmpty) "<empty path>" else toTagPathString(path = this, tail = "")
}

override def toString: String = toString(lookup = true)

override def hashCode(): Int = this match {
case EmptyTagPath => 0
case s: TagPathSequenceItem => 31 * (31 * (31 * previous.hashCode() + tag.hashCode()) * s.item.hashCode())
Expand Down Expand Up @@ -407,20 +416,24 @@ object TagPath {
def isSeq(s: String): Boolean = s.last == ']'

def indexPart(s: String): String = s.substring(s.lastIndexOf('[') + 1, s.length - 1)

def tagPart(s: String): String = s.substring(0, s.indexOf('['))

def parseTag(s: String): Int = try Integer.parseInt(s.substring(1, 5) + s.substring(6, 10), 16) catch {
case _: Throwable =>
Dictionary.tagOf(s)
}

def parseIndex(s: String): Option[Int] = if (s == "*") None else Some(Integer.parseInt(s))

def createTag(s: String): TagPathTag = TagPath.fromTag(parseTag(s))

def addTag(s: String, path: TagPathTrunk): TagPathTag = path.thenTag(parseTag(s))

def createSeq(s: String): TagPathTrunk = parseIndex(indexPart(s))
.map(index => TagPath.fromSequence(parseTag(tagPart(s)), index))
.getOrElse(TagPath.fromSequence(parseTag(tagPart(s))))

def addSeq(s: String, path: TagPathTrunk): TagPathTrunk = parseIndex(indexPart(s))
.map(index => path.thenSequence(parseTag(tagPart(s)), index))
.getOrElse(path.thenSequence(parseTag(tagPart(s))))
Expand Down
7 changes: 6 additions & 1 deletion src/test/scala/se/nimsa/dicom/data/TagPathTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class TagPathTest extends FlatSpec with Matchers {

"A tag path" should "have a legible string representation" in {
val path = TagPath.fromSequence(Tag.DerivationCodeSequence).thenSequence(Tag.DerivationCodeSequence, 3).thenSequence(Tag.DerivationCodeSequence).thenTag(Tag.PatientID)
path.toString shouldBe "(0008,9215)[*].(0008,9215)[3].(0008,9215)[*].(0010,0020)"
path.toString(lookup = false) shouldBe "(0008,9215)[*].(0008,9215)[3].(0008,9215)[*].(0010,0020)"
}

it should "support string representations with keywords instead of tag numbers where possible" in {
val path = TagPath.fromSequence(Tag.DerivationCodeSequence).thenSequence(0x11110100, 3).thenSequence(Tag.DetectorInformationSequence).thenTag(Tag.PatientID)
path.toString(lookup = true) shouldBe "DerivationCodeSequence[*].(1111,0100)[3].DetectorInformationSequence[*].PatientID"
}

it should "be root when pointing to root dataset" in {
Expand Down

0 comments on commit 692ab8f

Please sign in to comment.