Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with MODS exclusions removing the root element if there are no more child elements #1683

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void filterExclusions(Document doc) {
Element detachEl = el;
Element parentEl = detachEl.getParentElement();
// The parent is empty if it has no children other than the element being removed and no attributes
while (parentEl != null && parentEl.getChildren().size() == 1 && parentEl.getAttributes().isEmpty()) {
while (parentEl != null && !parentEl.isRootElement() && parentEl.getChildren().size() == 1
&& parentEl.getAttributes().isEmpty()) {
detachEl = parentEl;
parentEl = detachEl.getParentElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class XmlDocumentFilteringServiceTest {
private XmlDocumentFilteringService service;
private Document doc;
private Document exclusionDoc;

@TempDir
public Path tmpFolder;
Expand All @@ -35,6 +36,7 @@ public void setup() throws Exception {
service = new XmlDocumentFilteringService();
SAXBuilder saxBuilder = SecureXMLFactory.createSAXBuilder();
doc = saxBuilder.build(new File("src/test/resources/mods/test_record.xml"));
exclusionDoc = saxBuilder.build(new File("src/test/resources/mods/exclusion_record.xml"));
tmpFolder.resolve("testFolder");
Files.createDirectory(tmpFolder.resolve("testFolder"));
}
Expand All @@ -56,6 +58,23 @@ public void excludeNoMatchesTest() throws Exception {
assertEquals(before, after);
}

@Test
public void excludeAllElementsTest() throws Exception {
initWithXPath("//mods:originInfo[@displayLabel=\"Digital Scan Date Raw Scan\"]/mods:dateCaptured",
"//mods:originInfo[@displayLabel=\"Digital Scan Date filename\"]/mods:dateCaptured",
"//mods:identifier[@type=\"filename\"]",
"//mods:physicalDescription/mods:note[@type=\"technical\"]",
"//mods:originInfo/mods:place/mods:placeTerm[@type=\"code\" and @authority=\"marccountry\"]",
"//mods:originInfo/mods:dateIssued[@encoding=\"marc\"]",
"//mods:identifier[@displayLabel=\"HookID\" and @type=\"local\"]",
"//mods:physicalDescription/mods:note[@displayLabel=\"Container type\"]",
"//mods:identifier[@displayLabel=\"CONTENTdm number\" and @type=\"local\"]",
"//mods:accessCondition[@type=\"use and reproduction\" and @displayLabel=\"CONTENTdm Usage Rights\"]");

service.filterExclusions(exclusionDoc);
assertEquals(0, exclusionDoc.getRootElement().getChildren().size());
}

@Test
public void excludeSingleElementTest() throws Exception {
initWithXPath("mods:mods/mods:titleInfo/mods:title");
Expand Down
7 changes: 7 additions & 0 deletions web-common/src/test/resources/mods/exclusion_record.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<mods:mods xmlns:mods="http://www.loc.gov/mods/v3">
<mods:identifier displayLabel="CONTENTdm number" type="local">7063/original_file</mods:identifier>
<mods:identifier displayLabel="CONTENTdm file name" type="filename">7054.jp2</mods:identifier>
<mods:identifier displayLabel="Full resolution" type="filename">Volume37/Dwane Powell_026.tif</mods:identifier>
<mods:identifier displayLabel="filename" type="filename">Dwane Powell_026.tif</mods:identifier>
</mods:mods>
Loading