From 795ba5f4b23f9d091ef1153f9095d2bfc078e6fb Mon Sep 17 00:00:00 2001 From: Jan van Mansum Date: Sat, 7 Dec 2024 15:21:37 +0100 Subject: [PATCH] More pruning --- .../dansbag/DansBagMappingServiceImpl.java | 7 +--- .../dansbag/deposit/BagDirResolverImpl.java | 18 +++------ .../dansbag/deposit/DepositReaderImpl.java | 8 ++-- .../dvingest/core/dansbag/io/FileService.java | 29 -------------- .../core/dansbag/io/FileServiceImpl.java | 38 ------------------- .../core/dansbag/DansConversionFixture.java | 7 +--- 6 files changed, 13 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileService.java delete mode 100644 src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileServiceImpl.java diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java index ab45b3b..a9d0589 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java @@ -27,8 +27,6 @@ import nl.knaw.dans.dvingest.core.dansbag.exception.InvalidDepositException; import nl.knaw.dans.dvingest.core.dansbag.io.BagDataManager; import nl.knaw.dans.dvingest.core.dansbag.io.BagDataManagerImpl; -import nl.knaw.dans.dvingest.core.dansbag.io.FileService; -import nl.knaw.dans.dvingest.core.dansbag.io.FileServiceImpl; import nl.knaw.dans.dvingest.core.dansbag.mapper.DepositToDvDatasetMetadataMapper; import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper; import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelperImpl; @@ -76,10 +74,9 @@ public DansBagMappingServiceImpl(DepositToDvDatasetMetadataMapper depositToDvDat DepositFileLister depositFileLister = new DepositFileListerImpl(); BagDataManager bagDataManager = new BagDataManagerImpl(bagReader); XmlReader xmlReader = new XmlReaderImpl(); - FileService fileService = new FileServiceImpl(); - BagDirResolver bagDirResolver = new BagDirResolverImpl(fileService); + BagDirResolver bagDirResolver = new BagDirResolverImpl(); - depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, fileService, bagDataManager, depositFileLister, manifestHelper); + depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagDataManager, depositFileLister, manifestHelper); this.supportedLicenses = supportedLicenses; this.fileExclusionPattern = fileExclusionPattern; this.embargoExclusions = embargoExclusions; diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/BagDirResolverImpl.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/BagDirResolverImpl.java index 11975bd..5ec93e8 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/BagDirResolverImpl.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/BagDirResolverImpl.java @@ -16,27 +16,21 @@ package nl.knaw.dans.dvingest.core.dansbag.deposit; import nl.knaw.dans.dvingest.core.dansbag.exception.InvalidDepositException; -import nl.knaw.dans.dvingest.core.dansbag.io.FileService; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; -import java.util.stream.Collectors; public class BagDirResolverImpl implements BagDirResolver { - private final FileService fileService; - - public BagDirResolverImpl(FileService fileService) { - this.fileService = fileService; - } @Override public Path getBagDir(Path depositDir) throws InvalidDepositException, IOException { - if (!fileService.isDirectory(depositDir)) { + if (!Files.isDirectory(depositDir)) { throw new InvalidDepositException(String.format("%s is not a directory", depositDir)); } - try (var substream = fileService.listDirectories(depositDir)) { - var directories = substream.collect(Collectors.toList()); + try (var substream = Files.list(depositDir).filter(Files::isDirectory)) { + var directories = substream.toList(); // only 1 directory allowed, not 0 or more than 1 if (directories.size() != 1) { @@ -46,7 +40,7 @@ public Path getBagDir(Path depositDir) throws InvalidDepositException, IOExcepti } // check for the presence of deposit.properties and bagit.txt - if (!fileService.fileExists(depositDir.resolve("deposit.properties"))) { + if (!Files.exists(depositDir.resolve("deposit.properties"))) { throw new InvalidDepositException(String.format( "%s does not contain a deposit.properties file", depositDir )); @@ -54,7 +48,7 @@ public Path getBagDir(Path depositDir) throws InvalidDepositException, IOExcepti var bagDir = directories.get(0); - if (!fileService.fileExists(bagDir.resolve("bagit.txt"))) { + if (!Files.exists(bagDir.resolve("bagit.txt"))) { throw new InvalidDepositException(String.format( "%s does not contain a bag", depositDir )); diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java index c524511..368e079 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java @@ -19,7 +19,6 @@ import nl.knaw.dans.dvingest.core.dansbag.domain.Deposit; import nl.knaw.dans.dvingest.core.dansbag.exception.InvalidDepositException; import nl.knaw.dans.dvingest.core.dansbag.io.BagDataManager; -import nl.knaw.dans.dvingest.core.dansbag.io.FileService; import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper; import nl.knaw.dans.dvingest.core.dansbag.service.XmlReader; import org.apache.commons.configuration2.Configuration; @@ -29,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.time.OffsetDateTime; import java.util.List; @@ -37,16 +37,14 @@ public class DepositReaderImpl implements DepositReader { private final XmlReader xmlReader; private final BagDirResolver bagDirResolver; - private final FileService fileService; private final BagDataManager bagDataManager; private final DepositFileLister depositFileLister; private final ManifestHelper manifestHelper; - public DepositReaderImpl(XmlReader xmlReader, BagDirResolver bagDirResolver, FileService fileService, BagDataManager bagDataManager, DepositFileLister depositFileLister, ManifestHelper manifestHelper) { + public DepositReaderImpl(XmlReader xmlReader, BagDirResolver bagDirResolver, BagDataManager bagDataManager, DepositFileLister depositFileLister, ManifestHelper manifestHelper) { this.xmlReader = xmlReader; this.bagDirResolver = bagDirResolver; - this.fileService = fileService; this.bagDataManager = bagDataManager; this.depositFileLister = depositFileLister; this.manifestHelper = manifestHelper; @@ -78,7 +76,7 @@ public Deposit readDeposit(Path depositDir) throws InvalidDepositException { } Document readOptionalXmlFile(Path path) throws ParserConfigurationException, IOException, SAXException { - if (fileService.fileExists(path)) { + if (Files.exists(path)) { return xmlReader.readXmlFile(path); } diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileService.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileService.java deleted file mode 100644 index 9d1ee8d..0000000 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileService.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package nl.knaw.dans.dvingest.core.dansbag.io; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.stream.Stream; - -public interface FileService { - - boolean isDirectory(Path path); - - Stream listDirectories(Path path) throws IOException; - - boolean fileExists(Path path); -} diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileServiceImpl.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileServiceImpl.java deleted file mode 100644 index 4541779..0000000 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/io/FileServiceImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package nl.knaw.dans.dvingest.core.dansbag.io; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.stream.Stream; - -public class FileServiceImpl implements FileService { - @Override - public boolean isDirectory(Path path) { - return Files.isDirectory(path); - } - - @Override - public Stream listDirectories(Path path) throws IOException { - return Files.list(path).filter(Files::isDirectory); - } - - @Override - public boolean fileExists(Path path) { - return Files.exists(path); - } -} diff --git a/src/test/java/nl/knaw/dans/dvingest/core/dansbag/DansConversionFixture.java b/src/test/java/nl/knaw/dans/dvingest/core/dansbag/DansConversionFixture.java index a49c1bc..91b76f3 100644 --- a/src/test/java/nl/knaw/dans/dvingest/core/dansbag/DansConversionFixture.java +++ b/src/test/java/nl/knaw/dans/dvingest/core/dansbag/DansConversionFixture.java @@ -26,8 +26,6 @@ import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositReaderImpl; import nl.knaw.dans.dvingest.core.dansbag.io.BagDataManager; import nl.knaw.dans.dvingest.core.dansbag.io.BagDataManagerImpl; -import nl.knaw.dans.dvingest.core.dansbag.io.FileService; -import nl.knaw.dans.dvingest.core.dansbag.io.FileServiceImpl; import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper; import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelperImpl; import nl.knaw.dans.dvingest.core.dansbag.service.XmlReader; @@ -62,10 +60,9 @@ public void setUp() throws Exception { DepositFileLister depositFileLister = new DepositFileListerImpl(); BagDataManager bagDataManager = new BagDataManagerImpl(bagReader); XmlReader xmlReader = new XmlReaderImpl(); - FileService fileService = new FileServiceImpl(); - BagDirResolver bagDirResolver = new BagDirResolverImpl(fileService); + BagDirResolver bagDirResolver = new BagDirResolverImpl(); - depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, fileService, bagDataManager, depositFileLister, manifestHelper); + depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagDataManager, depositFileLister, manifestHelper); var defaultConfigDir = Paths.get("src/main/assembly/dist/cfg"); var mapper = new DepositToDvDatasetMetadataMapper( false,