-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from DrRename/feature/improved-nfo-parsing
Introduce NFO file parser
- Loading branch information
Showing
7 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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 = "<NfoRoot>" + String.join("", Files.readAllLines(filePath)) + "</NfoRoot>"; | ||
|
||
return xmlMapper.readValue(content, NfoRoot.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<movie> | ||
<title>Kung Fu Killer</title> | ||
<genre>Martial arts</genre> | ||
</movie> | ||
http://www.imdb.com/title/tt2952602/ |