Skip to content

Commit 39adbb1

Browse files
authored
Merge pull request #1502 from lesserwhirls/nullAttrDap4
Dap4 null valued attributes
2 parents d9b44f0 + d065b5b commit 39adbb1

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

dap4/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ dependencies {
2222

2323
testImplementation(project(":cdm-test-utils"))
2424

25+
testImplementation(libs.google.truth)
26+
2527
testCompileOnly(libs.junit4)
2628

2729
testRuntimeOnly(libs.junit5.platformLauncher)

dap4/src/main/java/dap4/core/dmr/DMRPrinter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright 2012, UCAR/Unidata.
3-
* See the LICENSE file for more information.
2+
* Copyright (c) 2012-2025 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
44
*/
55

66
package dap4.core.dmr;
@@ -531,10 +531,14 @@ void printAttribute(DapAttribute attr) throws IOException {
531531
printer.marginPrintln(cs);
532532
}
533533
} else {
534-
for (int i = 0; i < svec.length; i++) {
535-
String s = Escape.entityEscape(svec[i], null);
536-
String cs = String.format("<Value value=\"%s\"/>", s);
537-
printer.marginPrintln(cs);
534+
if (svec.length == 0) {
535+
printer.marginPrintln("<Value/>");
536+
} else {
537+
for (String string : svec) {
538+
String s = Escape.entityEscape(string, null);
539+
String cs = String.format("<Value value=\"%s\"/>", s);
540+
printer.marginPrintln(cs);
541+
}
538542
}
539543
}
540544
printer.outdent();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2025 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
4+
*/
5+
6+
package dap4.core.dmr;
7+
8+
import static com.google.common.truth.Truth.assertThat;
9+
10+
import dap4.core.util.IndentWriter;
11+
import java.io.IOException;
12+
import java.io.StringWriter;
13+
import org.junit.Test;
14+
15+
public class TestDMRPrinterEdgeCases {
16+
@Test
17+
public void testNullValueAttr() throws IOException {
18+
DapAttribute attr = new DapAttribute("name", DapType.STRING);
19+
attr.setValues(new String[] {});
20+
DMRPrinter dmrPrinter = new DMRPrinter();
21+
StringWriter sw = new StringWriter();
22+
dmrPrinter.printer = new IndentWriter(sw);
23+
dmrPrinter.printAttribute(attr);
24+
String encodedAttribute = sw.toString();
25+
assertThat(encodedAttribute).isNotEmpty();
26+
assertThat(encodedAttribute).ignoringCase().contains("<value/>");
27+
}
28+
}

0 commit comments

Comments
 (0)