From 70031e63e0c6ba31bbbb9bf3fc1c7168fbf7d314 Mon Sep 17 00:00:00 2001 From: Alexander Kerner Date: Mon, 17 Oct 2022 21:59:43 +0200 Subject: [PATCH] Introduce NFO file parser --- src/main/java/drrename/RenameUtil.java | 2 +- .../kodi/NfoFileContentCheckService.java | 2 +- .../drrename/model/nfo/NfoFileParser.java | 58 +++++++++++++++++++ .../model/{ => nfo}/NfoFileXmlModel.java | 2 +- src/main/java/drrename/model/nfo/NfoRoot.java | 34 +++++++++++ .../drrename/model/nfo/NfoFileParserTest.java | 48 +++++++++++++++ src/test/resources/test-nfo-01.nfo | 5 ++ 7 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/main/java/drrename/model/nfo/NfoFileParser.java rename src/main/java/drrename/model/{ => nfo}/NfoFileXmlModel.java (97%) create mode 100644 src/main/java/drrename/model/nfo/NfoRoot.java create mode 100644 src/test/java/drrename/model/nfo/NfoFileParserTest.java create mode 100755 src/test/resources/test-nfo-01.nfo diff --git a/src/main/java/drrename/RenameUtil.java b/src/main/java/drrename/RenameUtil.java index 1b7a6e47..2614e8c9 100644 --- a/src/main/java/drrename/RenameUtil.java +++ b/src/main/java/drrename/RenameUtil.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import drrename.model.NfoFileXmlModel; +import drrename.model.nfo.NfoFileXmlModel; import lombok.extern.slf4j.Slf4j; import java.io.IOException; diff --git a/src/main/java/drrename/kodi/NfoFileContentCheckService.java b/src/main/java/drrename/kodi/NfoFileContentCheckService.java index 0980a234..f5cf6e11 100644 --- a/src/main/java/drrename/kodi/NfoFileContentCheckService.java +++ b/src/main/java/drrename/kodi/NfoFileContentCheckService.java @@ -22,7 +22,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import drrename.model.NfoFileXmlModel; +import drrename.model.nfo.NfoFileXmlModel; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; diff --git a/src/main/java/drrename/model/nfo/NfoFileParser.java b/src/main/java/drrename/model/nfo/NfoFileParser.java new file mode 100644 index 00000000..23668eed --- /dev/null +++ b/src/main/java/drrename/model/nfo/NfoFileParser.java @@ -0,0 +1,58 @@ +/* + * Dr.Rename - A Minimalistic Batch Renamer + * + * Copyright (C) 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package drrename.model.nfo; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class NfoFileParser { + + private final XmlMapper xmlMapper; + + public NfoFileParser() { + xmlMapper = new XmlMapper(); + xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + xmlMapper.addHandler(new DeserializationProblemHandler() { + @Override + public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOException { + if (beanOrClass instanceof NfoRoot) { + ((NfoRoot) beanOrClass).setUrl(p.readValueAs(String.class)); + return true; + } + return super.handleUnknownProperty(ctxt, p, deserializer, beanOrClass, propertyName); + } + }); + } + + public NfoRoot parse(Path filePath) throws IOException { + + String content = "" + String.join("", Files.readAllLines(filePath)) + ""; + + return xmlMapper.readValue(content, NfoRoot.class); + } +} diff --git a/src/main/java/drrename/model/NfoFileXmlModel.java b/src/main/java/drrename/model/nfo/NfoFileXmlModel.java similarity index 97% rename from src/main/java/drrename/model/NfoFileXmlModel.java rename to src/main/java/drrename/model/nfo/NfoFileXmlModel.java index 9d706c89..2fddd6a7 100644 --- a/src/main/java/drrename/model/NfoFileXmlModel.java +++ b/src/main/java/drrename/model/nfo/NfoFileXmlModel.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package drrename.model; +package drrename.model.nfo; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/drrename/model/nfo/NfoRoot.java b/src/main/java/drrename/model/nfo/NfoRoot.java new file mode 100644 index 00000000..0545585b --- /dev/null +++ b/src/main/java/drrename/model/nfo/NfoRoot.java @@ -0,0 +1,34 @@ +/* + * Dr.Rename - A Minimalistic Batch Renamer + * + * Copyright (C) 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package drrename.model.nfo; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +@ToString +@Getter +@Setter +public class NfoRoot { + + public NfoFileXmlModel movie; + + public String url; +} diff --git a/src/test/java/drrename/model/nfo/NfoFileParserTest.java b/src/test/java/drrename/model/nfo/NfoFileParserTest.java new file mode 100644 index 00000000..781780d1 --- /dev/null +++ b/src/test/java/drrename/model/nfo/NfoFileParserTest.java @@ -0,0 +1,48 @@ +/* + * Dr.Rename - A Minimalistic Batch Renamer + * + * Copyright (C) 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package drrename.model.nfo; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.*; + +class NfoFileParserTest { + + @BeforeEach + void setUp() { + + } + + @AfterEach + void tearDown() { + + } + + @Test + void parse() throws IOException { + var result = new NfoFileParser().parse(Paths.get("src/test/resources/test-nfo-01.nfo")); + System.out.println(result); + } +} \ No newline at end of file diff --git a/src/test/resources/test-nfo-01.nfo b/src/test/resources/test-nfo-01.nfo new file mode 100755 index 00000000..9501e3f2 --- /dev/null +++ b/src/test/resources/test-nfo-01.nfo @@ -0,0 +1,5 @@ + + Kung Fu Killer + Martial arts + +http://www.imdb.com/title/tt2952602/ \ No newline at end of file