Skip to content

Commit

Permalink
Merge 7847a1d into 8872008
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-exini committed Jan 28, 2021
2 parents 8872008 + 7847a1d commit b5dd3a2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Expand Up @@ -58,12 +58,14 @@ sourceGenerators in Compile += Def.task {
val tagToVRFile = (sourceManaged in Compile).value / "sbt-dicomdata" / "TagToVR.scala"
val tagToVMFile = (sourceManaged in Compile).value / "sbt-dicomdata" / "TagToVM.scala"
val tagToKeywordFile = (sourceManaged in Compile).value / "sbt-dicomdata" / "TagToKeyword.scala"
val uidToNameFile = (sourceManaged in Compile).value / "sbt-dicomdata" / "UIDToName.scala"
IO.write(tagFile, generateTag())
IO.write(uidFile, generateUID())
IO.write(tagToKeywordFile, generateTagToKeyword())
IO.write(tagToVRFile, generateTagToVR())
IO.write(tagToVMFile, generateTagToVM())
Seq(tagFile, uidFile, tagToKeywordFile, tagToVRFile, tagToVMFile)
IO.write(uidToNameFile, generateUIDToName())
Seq(tagFile, uidFile, tagToKeywordFile, tagToVRFile, tagToVMFile, uidToNameFile)
}.taskValue

// include managed sources among other sources when publishing
Expand All @@ -76,4 +78,4 @@ mappings in (Compile, packageSrc) ++= {

// coverage

coverageExcludedPackages := ".*\\.BuildInfo.*;.*\\.Tag.*;.*\\.UID.*;.*\\.TagToKeyword.*;.*\\.TagToVR.*;.*\\.TagToVM.*"
coverageExcludedPackages := ".*\\.BuildInfo.*;.*\\.Tag.*;.*\\.UID.*;.*\\.TagToKeyword.*;.*\\.TagToVR.*;.*\\.TagToVM.*\\.UIDToName.*"
23 changes: 20 additions & 3 deletions project/DicomSourceGenerators.scala
Expand Up @@ -171,7 +171,7 @@ object DicomSourceGenerators {
}

def generateTagToKeyword(): String = {
val (_, splitValue, tagMappingsLow, tagMappingsHigh) = generateTagMappings()
val (_, _, tagMappingsLow, tagMappingsHigh) = generateTagMappings()

s"""package com.exini.dicom.data
|
Expand Down Expand Up @@ -212,7 +212,7 @@ object DicomSourceGenerators {
}

def generateTagToVR(): String = {
val (_, splitValue, tagMappingsLow, tagMappingsHigh) = generateTagMappings()
val (_, _, tagMappingsLow, tagMappingsHigh) = generateTagMappings()

s"""package com.exini.dicom.data
|
Expand Down Expand Up @@ -260,7 +260,7 @@ object DicomSourceGenerators {
}

def generateTagToVM(): String = {
val (_, splitValue, tagMappingsLow, tagMappingsHigh) = generateTagMappings()
val (_, _, tagMappingsLow, tagMappingsHigh) = generateTagMappings()

s"""package com.exini.dicom.data
|
Expand Down Expand Up @@ -308,4 +308,21 @@ object DicomSourceGenerators {
|
|}""".stripMargin
}

def generateUIDToName(): String = {
val pairs = uids
.map(uid => (uid.uidValue, uid.uidName.replaceAll(":.*", "").trim))
.filter(_._2.nonEmpty)
.distinct

s"""package com.exini.dicom.data
|
|private[data] object UIDToName {
|
| def nameOf(uid: String): Option[String] = uid match {
|${pairs.map(p => s""" case "${p._1}" => Some("${p._2}")""").mkString("\r\n")}
| case _ => None
| }
|}""".stripMargin
}
}
6 changes: 3 additions & 3 deletions src/main/scala/com/exini/dicom/data/KeywordToTag.scala
Expand Up @@ -18,9 +18,9 @@ package com.exini.dicom.data

private[data] object KeywordToTag {

def tagOf(keyword: String): Int =
try Tag.getClass.getMethod(keyword).invoke(Tag).asInstanceOf[Int]
def tagOf(keyword: String): Option[Int] =
try Some(Tag.getClass.getMethod(keyword).invoke(Tag).asInstanceOf[Int])
catch {
case _: Throwable => throw new IllegalArgumentException(s"Unknown keyword $keyword")
case _: Throwable => None
}
}
4 changes: 3 additions & 1 deletion src/main/scala/com/exini/dicom/data/Lookup.scala
Expand Up @@ -24,7 +24,9 @@ object Lookup {

def keywordOf(tag: Int): Option[String] = TagToKeyword.keywordOf(tag)

def tagOf(keyword: String): Int = KeywordToTag.tagOf(keyword)
def tagOf(keyword: String): Option[Int] = KeywordToTag.tagOf(keyword)

def keywords(): List[String] = Tag.getClass.getMethods.map(_.getName).filter(_.head.isUpper).toList

def uidToName(uid: String): Option[String] = UIDToName.nameOf(uid)
}
2 changes: 1 addition & 1 deletion src/main/scala/com/exini/dicom/data/TagPath.scala
Expand Up @@ -241,7 +241,7 @@ object TagPath {
try Integer.parseInt(s.substring(1, 5) + s.substring(6, 10), 16)
catch {
case _: Throwable =>
Lookup.tagOf(s)
Lookup.tagOf(s).getOrElse(throw new RuntimeException(s"$s is not a recognizable tag or keyword"))
}

def parseIndex(s: String): Int = Integer.parseInt(s)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/exini/dicom/data/TagTree.scala
Expand Up @@ -342,7 +342,7 @@ object TagTree {
try Integer.parseInt(s.substring(1, 5) + s.substring(6, 10), 16)
catch {
case _: Throwable =>
Lookup.tagOf(s)
Lookup.tagOf(s).getOrElse(throw new RuntimeException(s"$s is not a recognizable tag or keyword"))
}

def parseIndex(s: String): Option[Int] = if (s == "*") None else Some(Integer.parseInt(s))
Expand Down
8 changes: 3 additions & 5 deletions src/test/scala/com/exini/dicom/data/KeywordToTagTest.scala
Expand Up @@ -8,12 +8,10 @@ class KeywordToTagTest extends AnyFlatSpec with Matchers {
"A keyword" should "be correctly mapped to its corresponding tag" in {
val realTag = Tag.PatientName
val mappedTag = KeywordToTag.tagOf("PatientName")
mappedTag shouldBe realTag
mappedTag shouldBe Some(realTag)
}

it should "throw exception for unknown keywords" in {
intercept[IllegalArgumentException] {
KeywordToTag.tagOf("oups")
}
it should "return None for unknown keywords" in {
KeywordToTag.tagOf("oups") shouldBe None
}
}
3 changes: 2 additions & 1 deletion src/test/scala/com/exini/dicom/data/LookupTest.scala
Expand Up @@ -20,7 +20,8 @@ class LookupTest extends AnyFlatSpec with Matchers {
}

it should "support getting the tag for a keyword" in {
Lookup.tagOf("PatientName") shouldBe Tag.PatientName
Lookup.tagOf("PatientName") shouldBe Some(Tag.PatientName)
Lookup.tagOf("not-a-keyword") shouldBe None
}

it should "support listing all keywords" in {
Expand Down

0 comments on commit b5dd3a2

Please sign in to comment.