Skip to content

Commit

Permalink
Merge pull request #9 from dcm4che/dev
Browse files Browse the repository at this point in the history
Shadow the gradle plugin and relocate dependencies
  • Loading branch information
Nirostar authored May 5, 2023
2 parents 375c652 + 1f781ac commit 61832ae
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {

```groovy
dependencies {
implementation 'org.dcm4che:dcm4che-typeddicom-lib-std:0.5.3'
implementation 'org.dcm4che:dcm4che-typeddicom-lib-std:0.5.4'
}
```

Expand All @@ -41,7 +41,7 @@ repositories {

```kotlin
dependencies {
implementation("org.dcm4che:dcm4che-typeddicom-lib-std:0.5.3")
implementation("org.dcm4che:dcm4che-typeddicom-lib-std:0.5.4")
}
```

Expand All @@ -60,7 +60,7 @@ dependencies {
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-typeddicom</artifactId>
<version>0.5.3</version>
<version>0.5.4</version>
</dependency>
```
### Use private tags with the gradle plugin
Expand All @@ -78,7 +78,7 @@ buildscript {
plugins {
id 'java-library'
id 'org.dcm4che.typeddicom-java-generator' version '0.5.3'
id 'org.dcm4che.typeddicom-java-generator' version '0.5.4'
}
generateTypeddicomJavaSources {
Expand All @@ -99,7 +99,7 @@ buildscript {

plugins {
id("java-library")
id("org.dcm4che.typeddicom-java-generator") version "0.5.3"
id("org.dcm4che.typeddicom-java-generator") version "0.5.4"
}

generateTypeddicomJavaSources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
id("com.gradle.plugin-publish") version "1.1.0"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("org.dcm4che.typeddicom-xml-parser")
id("org.dcm4che.typeddicom-publisher")
}
Expand Down Expand Up @@ -45,6 +48,12 @@ java {
targetCompatibility = JavaVersion.VERSION_11
}

tasks.named<ShadowJar>("shadowJar") {
isEnableRelocation = true
relocate("org.dcm4che.typeddicom.generator.gradleplugin", "org.dcm4che.typeddicom.generator.gradleplugin")
archiveClassifier.set("")
}

tasks.withType<Jar> {
exclude("dicom-standard-xml/")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Auto generated
package org.dcm4che.typeddicom.modules;

import org.dcm4che.typeddicom.AttributesWrapper;
import org.dcm4che.typeddicom.AbstractAttributesWrapper;
{{#value.contains}}
import org.dcm4che.typeddicom.dataelements.{{.}};
Expand All @@ -13,7 +14,7 @@ import org.dcm4che3.data.Attributes;
* @see <a href="{{value.href}}">DICOM Standard Part 3 - {{value.name}}</a>
*
*/
public interface {{key}} extends {{{value.implementsHolderInterfaces}}} {
public interface {{key}} extends AttributesWrapper, {{{value.implementsHolderInterfaces}}} {
public static {{key}} wrap(Attributes attributes) {
return new SimpleWrapper(attributes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import org.dcm4che.typeddicom.AttributesWrapper;
import org.dcm4che.typeddicom.UniversalAttributesWrapper;
import org.dcm4che.typeddicom.dataelements.DisplayedAreaSelectionSequence;
import org.dcm4che.typeddicom.dataelements.PixelAspectRatio;
import org.dcm4che.typeddicom.dataelements.ReferencedImageSequence;
import org.dcm4che.typeddicom.dataelements.ReferencedSeriesSequence;
import org.dcm4che.typeddicom.dataelements.*;
import org.dcm4che.typeddicom.iods.CRImageIOD;
import org.dcm4che.typeddicom.iods.GrayscaleSoftcopyPresentationStateIOD;
import org.dcm4che.typeddicom.modules.ImagePixelModule;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Sequence;
import org.dcm4che3.data.Tag;
Expand All @@ -15,9 +14,9 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

class SimpleTests {
public static final String HPStateTag_CREATOR = "AGFA-AG_HPState";
Expand Down Expand Up @@ -162,4 +161,50 @@ void callingBuildOnHolderBuilderReturnsAValidObjectContainingTheAttribute() {
.setPixelAspectRatio().asInts(ratio);
assertArrayEquals(ratio, pixelAspectRatioHolderBuilder.build().getPixelAspectRatio().getInts());
}

@Test
void afterAddingAnUnknownTag_theMethodGetUnknownTagReturnsThisTag() {
Attributes attributes = new Attributes();
int tag = 0x0000_0001;
attributes.setString(tag, VR.ST, "Test");
PixelAspectRatio.Holder pixelAspectRatioHolder = PixelAspectRatio.Holder.wrap(attributes);
int[] unknownAttributeTags = pixelAspectRatioHolder.getUnknownAttributeTags();
assertArrayEquals(new int[]{tag}, unknownAttributeTags);
}

@Test
void aHolderOnlyContainsItsTagInTheArrayOfKnownTags() {
PixelAspectRatio.Holder pixelAspectRatioHolder = PixelAspectRatio.Holder.builder().build();
int[] knownAttributeTags = pixelAspectRatioHolder.getKnownAttributeTags();
assertArrayEquals(new int[]{PixelAspectRatio.TAG}, knownAttributeTags);
}

@Test
void aModuleContainsItsTagsInTheArrayOfKnownTags() {
ImagePixelModule imagePixelModule = ImagePixelModule.wrap(new Attributes());
assertThatAttributesWrappersKnownAttributesContainTheseTags(imagePixelModule,
PixelAspectRatio.TAG,
SamplesPerPixel.TAG,
Columns.TAG,
Rows.TAG);
}

@Test
void anIodContainsItsTagsInTheArrayOfKnownTags() {
CRImageIOD crImageIod = CRImageIOD.builder().build();
assertThatAttributesWrappersKnownAttributesContainTheseTags(crImageIod,
PixelAspectRatio.TAG,
SamplesPerPixel.TAG,
Columns.TAG,
Rows.TAG,
PatientName.TAG,
PatientID.TAG);
}

void assertThatAttributesWrappersKnownAttributesContainTheseTags(AttributesWrapper attributesWrapper, int... tags) {
int[] knownAttributeTags = attributesWrapper.getKnownAttributeTags();
for (int tag : tags) {
assertTrue(Arrays.stream(knownAttributeTags).anyMatch((knownTag -> knownTag == tag)), "Expected " + tag + " in the array of knownTags.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class TypeddicomPublisherPlugin implements Plugin<Project> {
private static final String TYPEDDICOM_GROUP = "org.dcm4che";
private static final String TYPEDDICOM_VERSION = "0.5.3";
private static final String TYPEDDICOM_VERSION = "0.5.4";

private static final String STANDARD_PUBLICATION_NAME = "mavenJava";
private static final String GRADLE_PLUGIN_PUBLICATION_NAME = "pluginMaven";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public interface AttributesWrapper {
static <T extends AttributesWrapper> T wrap(Attributes attributes, Class<T> wrapperClass) {
Expand All @@ -16,6 +21,33 @@ static <T extends AttributesWrapper> T wrap(Attributes attributes, Class<T> wrap
}
}

static IntStream getKnownAttributeTags(Class<? extends AttributesWrapper> attributesWrapperClass) {
return getImplementedDataElementHolders(attributesWrapperClass)
.map(Class::getDeclaringClass)
.mapToInt(clazz -> {
try {
return clazz.getField("TAG").getInt(null);
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
});
}

@SuppressWarnings({
"unchecked", // It's okay we are checking the type by reflection
"java:S1872", // We want a simple name comparison and no instanceof because there are multiple nested classes with that name
})
static Stream<Class<? extends AttributesWrapper>> getImplementedDataElementHolders(Class<? extends AttributesWrapper> clazz) {
if ("Holder".equals(clazz.getSimpleName())) {
return Stream.of(clazz);
} else {
return Arrays.stream(clazz.getInterfaces())
.filter(AttributesWrapper.class::isAssignableFrom)
.map(parent -> (Class<? extends AttributesWrapper>) parent)
.flatMap(AttributesWrapper::getImplementedDataElementHolders);
}
}

/**
* Get the wrapped Attributes.
*
Expand Down Expand Up @@ -44,4 +76,16 @@ default <T extends AttributesWrapper> T copy(Class<T> wrapperClass) {
default boolean isEmpty() {
return getAttributes().isEmpty();
}

default int[] getUnknownAttributeTags() {
Set<Integer> knownAttributeTags = getKnownAttributeTags(this.getClass())
.boxed()
.collect(Collectors.toUnmodifiableSet());
return Arrays.stream(this.getAttributes().tags())
.filter(tag -> !knownAttributeTags.contains(tag)).toArray();
}

default int[] getKnownAttributeTags() {
return getKnownAttributeTags(this.getClass()).toArray();
}
}

0 comments on commit 61832ae

Please sign in to comment.