Skip to content

Commit

Permalink
Fixed more issues reported by SonarCloud [comixed#354]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed May 8, 2021
1 parent ea1c123 commit 05e2edd
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 73 deletions.
Expand Up @@ -23,8 +23,8 @@
import java.util.*;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.comixedproject.loaders.AbstractEntryLoader;
import org.comixedproject.loaders.EntryLoaderException;
import org.comixedproject.model.comic.Comic;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void loadContent(
}

protected void loadXmlData(InputStream istream, Comic comic) throws XMLStreamException {
final XMLStreamReader xmlInputReader = this.xmlInputFactory.createXMLStreamReader(istream);
final var xmlInputReader = this.xmlInputFactory.createXMLStreamReader(istream);
int publishedYear = -1;
int publishedMonth = -1;

Expand Down Expand Up @@ -154,7 +154,7 @@ protected void loadXmlData(InputStream istream, Comic comic) throws XMLStreamExc

private List<String> commandSeparatedList(String text) {
List<String> result = new ArrayList<>();
StringTokenizer tokens = new StringTokenizer(text, ",");
var tokens = new StringTokenizer(text, ",");

while (tokens.hasMoreTokens()) {
result.add(tokens.nextToken().trim());
Expand All @@ -172,8 +172,8 @@ private List<String> commandSeparatedList(String text) {
*/
public byte[] saveContent(Comic comic) throws IOException {
log.debug("Generating comic info data from comic");
ByteArrayOutputStream result = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(result));
var result = new ByteArrayOutputStream();
var writer = new PrintWriter(new OutputStreamWriter(result));

writer.write(
"<?xml version=\"1.0\"?><ComicInfo xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
Expand All @@ -185,15 +185,15 @@ public byte[] saveContent(Comic comic) throws IOException {
this.writeEntry(writer, "Summary", comic.getDescription());

if (comic.getCoverDate() != null) {
Calendar gc = Calendar.getInstance();
var gc = Calendar.getInstance();
gc.setTime(comic.getCoverDate());
this.writeEntry(writer, "Year", String.valueOf(gc.get(Calendar.YEAR)));
this.writeEntry(writer, "Month", String.valueOf(gc.get(Calendar.MONTH) + 1));
}
this.writeEntry(writer, "PageCount", String.valueOf(comic.getPageCount()));
this.writeEntry(writer, "Characters", this.arrayToString(comic.getCharacters()));
this.writeEntry(writer, "Teams", this.arrayToString(comic.getTeams()));
this.writeEntry(writer, "Locations", this.arrayToString(comic.getLocations()));
this.writeEntry(writer, "Characters", StringUtils.join(comic.getCharacters(), ","));
this.writeEntry(writer, "Teams", StringUtils.join(comic.getTeams(), ","));
this.writeEntry(writer, "Locations", StringUtils.join(comic.getLocations(), ","));
writer.write("</ComicInfo>");
writer.flush();

Expand All @@ -205,18 +205,4 @@ private void writeEntry(PrintWriter writer, String tagName, String value) {
writer.write(MessageFormat.format("<{0}>{1}</{0}>", tagName, value));
}
}

private String arrayToString(List<String> items) {
if ((items == null) || items.isEmpty()) return null;

StringBuilder result = new StringBuilder();

for (String item : items) {
if (result.length() > 0) {
result.append(",");
}
result.append(item);
}
return result.toString();
}
}
Expand Up @@ -22,7 +22,6 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -59,7 +58,7 @@ public class FilenameScraperAdaptor {
};

private boolean applyRule(Comic comic, String filename, RuleSet ruleset) throws AdaptorException {
boolean result = false;
var result = false;

if (ruleset.applies(filename)) {
String[] elements = ruleset.process(filename);
Expand Down Expand Up @@ -108,10 +107,11 @@ public void execute(Comic comic) throws AdaptorException {
String filename = FilenameUtils.getName(comic.getFilename());
log.debug("Attempting to extract comic meta-data from filename: {}", filename);

boolean done = false;
for (int index = 0; !done && (index < RULESET.length); index++) {
var done = false;
var index = 0;
while (!done) {
log.debug("Attempting to use ruleset #{}", index);
done = (this.applyRule(comic, filename, RULESET[index]));
done = (this.applyRule(comic, filename, RULESET[index])) || ++index == RULESET.length;
}
}

Expand Down Expand Up @@ -150,11 +150,11 @@ public boolean applies(String filename) {

public String[] process(String filename) {
log.debug("Processing filename: {}", filename);
Matcher matches = this.expression.matcher(filename);
String[] result = new String[matches.groupCount() + 1];
var matches = this.expression.matcher(filename);
var result = new String[matches.groupCount() + 1];

while (matches.find()) {
for (int index = 0; index < result.length; index++) {
for (var index = 0; index < result.length; index++) {
result[index] = matches.group(index);
log.debug("Setting index={} to {}", index, result[index]);
}
Expand Down
Expand Up @@ -68,7 +68,7 @@ public abstract class AbstractArchiveAdaptor<I> implements ArchiveAdaptor, Initi
private Set<String> imageTypes = new HashSet<>();
private String defaultExtension;

public AbstractArchiveAdaptor(String defaultExtension) {
protected AbstractArchiveAdaptor(String defaultExtension) {
super();
this.defaultExtension = defaultExtension;
}
Expand Down Expand Up @@ -148,7 +148,7 @@ private void doLoadComic(final Comic comic, final boolean ignoreMetadata)
long started = System.currentTimeMillis();

try {
File comicFile = this.validateFile(comic);
var comicFile = this.validateFile(comic);
archiveReference = this.openArchive(comicFile);

log.debug("Loading entire comic: {}", comic.getFilename());
Expand All @@ -171,7 +171,7 @@ protected abstract void loadAllFiles(
protected byte[] loadContent(final String filename, final long size, final InputStream input)
throws IOException {
log.debug("Loading entry: name={} size={}", filename, size);
byte[] content = new byte[(int) size];
var content = new byte[(int) size];

IOUtils.readFully(input, content);

Expand All @@ -188,7 +188,7 @@ public byte[] loadSingleFile(Comic comic, String entryName) throws ArchiveAdapto
@Override
public byte[] loadSingleFile(String filename, String entryName) throws ArchiveAdaptorException {
log.debug("Loading single entry from file: filename={} entry={}", filename, entryName);
I archiveReference = this.openArchive(new File(filename));
var archiveReference = this.openArchive(new File(filename));
byte[] result = this.loadSingleFileInternal(archiveReference, entryName);
this.closeArchive(archiveReference);

Expand Down Expand Up @@ -262,16 +262,16 @@ public Comic saveComic(Comic source, boolean renamePages)
String filename =
ComicFileUtils.findAvailableFilename(
FilenameUtils.removeExtension(source.getFilename()), 0, this.defaultExtension);
File file1 = new File(tempFilename);
File file2 = new File(filename);
var file1 = new File(tempFilename);
var file2 = new File(filename);
try {
log.debug("Copying {} to {}", tempFilename, filename);
FileUtils.copyFile(file1, file2);
} catch (IOException error) {
throw new ArchiveAdaptorException("Unable to copy file", error);
}

Comic result = new Comic();
var result = new Comic();

result.setFilename(filename);

Expand All @@ -297,7 +297,7 @@ abstract void saveComicInternal(Comic source, String filename, boolean renamePag
throws ArchiveAdaptorException, IOException;

protected File validateFile(Comic comic) throws ArchiveAdaptorException {
File file = new File(comic.getFilename());
var file = new File(comic.getFilename());

if (!file.exists())
throw new ArchiveAdaptorException("File not found: " + file.getAbsolutePath());
Expand All @@ -309,7 +309,7 @@ protected File validateFile(Comic comic) throws ArchiveAdaptorException {

@Override
public String getFirstImageFileName(String filename) throws ArchiveAdaptorException {
I archiveRef = this.openArchive(new File(filename));
var archiveRef = this.openArchive(new File(filename));

// get the list of entries
List<String> entries = this.getEntryFilenames(archiveRef);
Expand All @@ -333,7 +333,7 @@ public String getFirstImageFileName(String filename) throws ArchiveAdaptorExcept
@Override
public byte[] encodeFileToStream(Map<String, byte[]> entries)
throws ArchiveAdaptorException, IOException {
throw new RuntimeException("Not supported");
throw new ArchiveAdaptorException("Not supported");
}

protected ArchiveAdaptor getSourceArchiveAdaptor(final String filename)
Expand Down
Expand Up @@ -73,9 +73,9 @@ protected void loadAllFiles(
List<FileHeader> fileHeaders = archiveReference.getFileHeaders();

for (FileHeader fileHeader : fileHeaders) {
String filename = fileHeader.getFileNameString();
var filename = fileHeader.getFileNameString();
long fileSize = fileHeader.getFullUnpackSize();
byte[] content = new byte[0];
var content = new byte[0];
try {
content = this.loadContent(filename, fileSize, archiveReference.getInputStream(fileHeader));
} catch (IOException | RarException error) {
Expand Down Expand Up @@ -118,7 +118,7 @@ protected Archive openArchive(File comicFile) throws ArchiveAdaptorException {

try {
archive = new Archive(new FileVolumeManager(comicFile));
FileHeader entry = archive.nextFileHeader();
var entry = archive.nextFileHeader();

if (entry == null) {
archive.close();
Expand Down
Expand Up @@ -31,7 +31,6 @@
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.comixedproject.model.archives.ArchiveType;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.comic.Page;
import org.springframework.stereotype.Component;

/**
Expand All @@ -50,10 +49,10 @@ private void addFileToArchive(SevenZOutputFile archive, String filename, byte[]
throws IOException {
log.debug("Saving file to archive: " + filename + " [size=" + content.length + "]");

File tempFile = File.createTempFile("comixed", "tmp");
var tempFile = File.createTempFile("comixed", "tmp");
log.debug("Saving entry as temporary filename: " + tempFile.getAbsolutePath());

try (FileOutputStream output = new FileOutputStream(tempFile)) {
try (var output = new FileOutputStream(tempFile)) {
output.write(content, 0, content.length);
}

Expand Down Expand Up @@ -94,7 +93,7 @@ protected void loadAllFiles(
throws ArchiveAdaptorException {
log.debug("Processing entries for archive");
comic.setArchiveType(ArchiveType.CB7);
boolean done = false;
var done = false;

while (!done) {
SevenZArchiveEntry entry = null;
Expand All @@ -108,7 +107,7 @@ protected void loadAllFiles(
if (entry != null) {
String filename = entry.getName();
long fileSize = entry.getSize();
byte[] content = new byte[(int) fileSize];
var content = new byte[(int) fileSize];
try {
archiveReference.read(content, 0, (int) fileSize);
this.processContent(comic, filename, content, ignoreMetadata);
Expand All @@ -127,11 +126,11 @@ protected byte[] loadSingleFileInternal(SevenZFile archiveReference, String entr
byte[] result = null;

try {
boolean done = false;
var done = false;
SevenZArchiveEntry entry = archiveReference.getNextEntry();

while (!done && (entry != null)) {
byte[] content = new byte[(int) entry.getSize()];
var content = new byte[(int) entry.getSize()];

archiveReference.read(content, 0, (int) entry.getSize());

Expand Down Expand Up @@ -163,18 +162,18 @@ protected SevenZFile openArchive(File comicFile) throws ArchiveAdaptorException
void saveComicInternal(Comic source, String filename, boolean renamePages)
throws ArchiveAdaptorException, IOException {
log.debug("Getting archive adaptor to read comic");
ArchiveAdaptor sourceArchiveAdaptor = this.getSourceArchiveAdaptor(source.getFilename());
var sourceArchiveAdaptor = this.getSourceArchiveAdaptor(source.getFilename());

try (SevenZOutputFile sevenzcomic = new SevenZOutputFile(new File(filename))) {
try (var sevenzcomic = new SevenZOutputFile(new File(filename))) {
sevenzcomic.setContentCompression(SevenZMethod.LZMA2);

log.debug("Adding the ComicInfo.xml entry");

this.addFileToArchive(
sevenzcomic, "ComicInfo.xml", this.comicInfoEntryAdaptor.saveContent(source));

for (int index = 0; index < source.getPageCount(); index++) {
Page page = source.getPage(index);
for (var index = 0; index < source.getPageCount(); index++) {
var page = source.getPage(index);
if (page.isDeleted()) {
log.debug("Skipping offset marked for deletion");
continue;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*j
* 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
Expand Down Expand Up @@ -34,7 +34,6 @@
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.comixedproject.model.archives.ArchiveType;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.comic.Page;
import org.springframework.stereotype.Component;

/**
Expand Down Expand Up @@ -101,7 +100,7 @@ protected void loadAllFiles(
@Override
protected byte[] loadSingleFileInternal(ZipFile archiveReference, String entryFilename)
throws ArchiveAdaptorException {
boolean done = false;
var done = false;
Enumeration<ZipArchiveEntry> entries = archiveReference.getEntries();
byte[] result = null;

Expand Down Expand Up @@ -139,7 +138,7 @@ protected ZipFile openArchive(File comicFile) throws ArchiveAdaptorException {
@Override
void saveComicInternal(Comic source, String filename, boolean renamePages)
throws ArchiveAdaptorException, IOException {
ArchiveAdaptor sourceArchiveAdaptor = this.getSourceArchiveAdaptor(source.getFilename());
var sourceArchiveAdaptor = this.getSourceArchiveAdaptor(source.getFilename());

log.debug("Creating temporary file: " + filename);

Expand All @@ -158,8 +157,8 @@ ArchiveStreamFactory.ZIP, new FileOutputStream(filename))) {
zoutput.write(content);
zoutput.closeArchiveEntry();

for (int index = 0; index < source.getPageCount(); index++) {
Page page = source.getPage(index);
for (var index = 0; index < source.getPageCount(); index++) {
var page = source.getPage(index);
if (page.isDeleted()) {
log.debug("Skipping offset marked for deletion");
continue;
Expand All @@ -184,7 +183,7 @@ public byte[] encodeFileToStream(Map<String, byte[]> content)
throws ArchiveAdaptorException, IOException {
log.debug("Encoding {} files", content.size());

ByteArrayOutputStream result = new ByteArrayOutputStream();
var result = new ByteArrayOutputStream();

try (ZipArchiveOutputStream zoutput =
(ZipArchiveOutputStream)
Expand Down
Expand Up @@ -21,6 +21,7 @@
import static junit.framework.TestCase.assertNotNull;

import java.io.IOException;
import java.util.Date;
import org.comixedproject.loaders.BaseLoaderTest;
import org.comixedproject.loaders.EntryLoaderException;
import org.comixedproject.model.comic.Comic;
Expand All @@ -36,6 +37,8 @@
public class ComicInfoEntryAdaptorTest extends BaseLoaderTest {
private static final String TEST_COMICINFO_FILE_COMPLETE =
"src/test/resources/ComicInfo-complete.xml";
private static final String TEST_COMICINFO_FILE_NOT_XML =
"src/test/resources/application.properties";
private static final String TEST_PUBLISHER_NAME = "Test Publisher";
private static final String TEST_SERIES_NAME = "Test Series";
private static final String TEST_VOLUME_NAME = "2011";
Expand All @@ -54,6 +57,7 @@ public void setup() {
Mockito.when(comic.getIssueNumber()).thenReturn(TEST_ISSUE_NUMBER);
Mockito.when(comic.getTitle()).thenReturn(TEST_TITLE);
Mockito.when(comic.getDescription()).thenReturn(TEST_DESCRIPTION);
Mockito.when(comic.getCoverDate()).thenReturn(new Date());
}

@Test
Expand All @@ -69,6 +73,12 @@ public void testLoadComicInfoXmlIgnoreMetadata() throws IOException, EntryLoader
Mockito.verify(comic, Mockito.never()).setDescription(Mockito.anyString());
}

@Test(expected = EntryLoaderException.class)
public void testLoadComicInfoXmlNotXml() throws IOException, EntryLoaderException {
adaptor.loadContent(
comic, TEST_COMICINFO_FILE_COMPLETE, loadFile(TEST_COMICINFO_FILE_NOT_XML), false);
}

@Test
public void testLoadComicInfoXml() throws IOException, EntryLoaderException {
adaptor.loadContent(
Expand Down

0 comments on commit 05e2edd

Please sign in to comment.