Skip to content

Commit

Permalink
Merge pull request #31 from DrRename/feature/improved-nfo-parsing
Browse files Browse the repository at this point in the history
Introduce NFO file parser
  • Loading branch information
kerner1000 authored Oct 18, 2022
2 parents c220e13 + 3101233 commit 1eccea6
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/drrename/RenameUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/drrename/model/nfo/NfoFileParser.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package drrename.model;
package drrename.model.nfo;

import lombok.Getter;
import lombok.Setter;
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/drrename/model/nfo/NfoRoot.java
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;
}
48 changes: 48 additions & 0 deletions src/test/java/drrename/model/nfo/NfoFileParserTest.java
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);
}
}
5 changes: 5 additions & 0 deletions src/test/resources/test-nfo-01.nfo
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/

0 comments on commit 1eccea6

Please sign in to comment.