refactor: drop unused standalone comicinfo.xml parser (#218)
This commit is contained in:
-53
@@ -564,59 +564,6 @@ public class CbxMetadataExtractor implements FileMetadataExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts metadata from a standalone ComicInfo.xml file.
|
||||
* This method is useful for processing ComicInfo.xml files that have been
|
||||
* extracted from comic book archives or exist independently.
|
||||
*
|
||||
* @param xmlFile The ComicInfo.xml file to parse
|
||||
* @return BookMetadata extracted from the XML file, or fallback metadata if parsing fails
|
||||
* @throws IllegalArgumentException if xmlFile is null
|
||||
*/
|
||||
public BookMetadata extractFromComicInfoXml(File xmlFile) {
|
||||
if (xmlFile == null) {
|
||||
throw new IllegalArgumentException("XML file cannot be null");
|
||||
}
|
||||
|
||||
String fallbackTitle = determineFallbackTitle(xmlFile);
|
||||
|
||||
try (InputStream is = new FileInputStream(xmlFile)) {
|
||||
Document document = buildSecureDocument(is);
|
||||
return mapDocumentToMetadata(document, fallbackTitle);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to read ComicInfo.xml file '{}': {}",
|
||||
xmlFile.getAbsolutePath(), e.getMessage(), e);
|
||||
return BookMetadata.builder().title(fallbackTitle).build();
|
||||
} catch (SAXException | ParserConfigurationException e) {
|
||||
log.warn("Failed to parse ComicInfo.xml from file '{}': {}",
|
||||
xmlFile.getAbsolutePath(), e.getMessage(), e);
|
||||
return BookMetadata.builder().title(fallbackTitle).build();
|
||||
} catch (Exception e) {
|
||||
log.error("Unexpected error processing ComicInfo.xml '{}': {}",
|
||||
xmlFile.getAbsolutePath(), e.getMessage(), e);
|
||||
return BookMetadata.builder().title(fallbackTitle).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the fallback title to use when metadata extraction fails.
|
||||
* Uses the parent directory name if available, otherwise the file name.
|
||||
*
|
||||
* @param file The file to determine the fallback title for
|
||||
* @return A fallback title string
|
||||
*/
|
||||
private String determineFallbackTitle(File file) {
|
||||
// Try parent directory first (usually the comic folder/archive name)
|
||||
if (file.getParentFile() != null) {
|
||||
String parentName = file.getParentFile().getName();
|
||||
if (parentName != null && !parentName.isBlank()) {
|
||||
return parentName;
|
||||
}
|
||||
}
|
||||
// Fall back to filename without extension
|
||||
return FilenameUtils.getBaseName(file.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] extractCover(File file) {
|
||||
ArchiveUtils.ArchiveType type = ArchiveUtils.detectArchiveType(file);
|
||||
|
||||
-64
@@ -1143,70 +1143,6 @@ class CbxMetadataExtractorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ExtractFromComicInfoXml {
|
||||
|
||||
@Test
|
||||
void throwsForNullFile() {
|
||||
assertThatThrownBy(() -> extractor.extractFromComicInfoXml(null))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessage("XML file cannot be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractsMetadataFromStandaloneXml() throws IOException {
|
||||
String xml = wrapInComicInfo("""
|
||||
<Title>Standalone Comic</Title>
|
||||
<Writer>Grant Morrison</Writer>
|
||||
<Publisher>DC Comics</Publisher>
|
||||
""");
|
||||
Path xmlPath = tempDir.resolve("mycomic");
|
||||
Files.createDirectories(xmlPath);
|
||||
Path xmlFile = xmlPath.resolve("ComicInfo.xml");
|
||||
Files.writeString(xmlFile, xml);
|
||||
|
||||
BookMetadata metadata = extractor.extractFromComicInfoXml(xmlFile.toFile());
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Standalone Comic");
|
||||
assertThat(metadata.getAuthors()).containsExactly("Grant Morrison");
|
||||
assertThat(metadata.getPublisher()).isEqualTo("DC Comics");
|
||||
}
|
||||
|
||||
@Test
|
||||
void usesParentDirectoryAsFallbackTitle() throws IOException {
|
||||
String xml = wrapInComicInfo("<Publisher>Marvel</Publisher>");
|
||||
Path comicDir = tempDir.resolve("Batman - Year One (2005)");
|
||||
Files.createDirectories(comicDir);
|
||||
Path xmlFile = comicDir.resolve("ComicInfo.xml");
|
||||
Files.writeString(xmlFile, xml);
|
||||
|
||||
BookMetadata metadata = extractor.extractFromComicInfoXml(xmlFile.toFile());
|
||||
|
||||
assertThat(metadata.getTitle()).isEqualTo("Batman - Year One (2005)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlesNonExistentFile() {
|
||||
File nonExistent = tempDir.resolve("does_not_exist.xml").toFile();
|
||||
|
||||
BookMetadata metadata = extractor.extractFromComicInfoXml(nonExistent);
|
||||
|
||||
assertThat(metadata).isNotNull();
|
||||
assertThat(metadata.getTitle()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlesMalformedXml() throws IOException {
|
||||
Path xmlFile = tempDir.resolve("bad.xml");
|
||||
Files.writeString(xmlFile, "<ComicInfo><Title>Unclosed");
|
||||
|
||||
BookMetadata metadata = extractor.extractFromComicInfoXml(xmlFile.toFile());
|
||||
|
||||
assertThat(metadata).isNotNull();
|
||||
assertThat(metadata.getTitle()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ComicInfoCaseInsensitive {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user