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 1f4a62aa..64bd4258 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