* unified embedded checksum handling

This commit is contained in:
Reinhard Pointner 2008-11-01 13:54:31 +00:00
parent 4267899842
commit 66cf786b19
3 changed files with 40 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.sourceforge.tuned.FileUtil; import net.sourceforge.tuned.FileUtil;
@ -28,8 +29,6 @@ public final class FileBotUtil {
public static final String INVALID_CHARACTERS = "\\/:*?\"<>|\r\n"; public static final String INVALID_CHARACTERS = "\\/:*?\"<>|\r\n";
public static final Pattern INVALID_CHARACTERS_PATTERN = Pattern.compile(String.format("[%s]+", Pattern.quote(INVALID_CHARACTERS))); public static final Pattern INVALID_CHARACTERS_PATTERN = Pattern.compile(String.format("[%s]+", Pattern.quote(INVALID_CHARACTERS)));
public static final Pattern EMBEDDED_CHECKSUM_PATTERN = Pattern.compile("[(\\[](\\p{XDigit}{8})[\\])]");
/** /**
* Strip filename of invalid characters * Strip filename of invalid characters
@ -47,6 +46,26 @@ public final class FileBotUtil {
return INVALID_CHARACTERS_PATTERN.matcher(filename).find(); return INVALID_CHARACTERS_PATTERN.matcher(filename).find();
} }
/**
* A {@link Pattern} that will match checksums enclosed in brackets ("[]" or "()"). A
* checksum string is a hex number with at least 8 digits. Capturing group 1 will contain
* the matched checksum string.
*/
public static final Pattern EMBEDDED_CHECKSUM_PATTERN = Pattern.compile("(?<=\\[|\\()(\\p{XDigit}{8,})(?=\\]|\\))");
public static String getEmbeddedChecksum(String string) {
Matcher matcher = FileBotUtil.EMBEDDED_CHECKSUM_PATTERN.matcher(string);
String embeddedChecksum = null;
// get last match
while (matcher.find()) {
embeddedChecksum = matcher.group(1);
}
return embeddedChecksum;
}
private static final String[] TORRENT_FILE_EXTENSIONS = { "torrent" }; private static final String[] TORRENT_FILE_EXTENSIONS = { "torrent" };
private static final String[] SFV_FILE_EXTENSIONS = { "sfv" }; private static final String[] SFV_FILE_EXTENSIONS = { "sfv" };
private static final String[] LIST_FILE_EXTENSIONS = { "txt", "list", "" }; private static final String[] LIST_FILE_EXTENSIONS = { "txt", "list", "" };

View File

@ -6,7 +6,6 @@ import java.io.File;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.regex.Matcher;
import net.sourceforge.filebot.FileBotUtil; import net.sourceforge.filebot.FileBotUtil;
@ -51,12 +50,12 @@ public class ChecksumRow {
*/ */
private static Long getEmbeddedChecksum(String name) { private static Long getEmbeddedChecksum(String name) {
// look for a checksum pattern like [49A93C5F] // look for a checksum pattern like [49A93C5F]
Matcher matcher = FileBotUtil.EMBEDDED_CHECKSUM_PATTERN.matcher(name); String match = FileBotUtil.getEmbeddedChecksum(name);
if (matcher.find()) if (match != null)
return Long.parseLong(matcher.group(1), 16); return Long.parseLong(match, 16);
else
return null; return null;
} }

View File

@ -3,6 +3,7 @@ package net.sourceforge.filebot.ui.panel.sfv;
import java.awt.Component; import java.awt.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.swing.JTable; import javax.swing.JTable;
@ -32,10 +33,20 @@ class HighlightPatternCellRenderer extends DefaultTableCellRenderer {
// highlight CRC32 checksum patterns by using a smaller font-size and changing the font-color to a dark green // highlight CRC32 checksum patterns by using a smaller font-size and changing the font-color to a dark green
// do not change the font-color if cell is selected, because that would look ugly (imagine green text on blue background ...) // do not change the font-color if cell is selected, because that would look ugly (imagine green text on blue background ...)
String htmlText = pattern.matcher(value.toString()).replaceAll("[<span style='font-size: " + cssFontSize + ";" + (!isSelected ? "color: " + cssColor + ";" : "") + "'>$1</span>]"); Matcher matcher = pattern.matcher(value.toString());
// use no-break, because we really don't want line-wrapping in our table cells // use no-break, because we really don't want line-wrapping in our table cells
setText("<html><nobr>" + htmlText + "</nobr></html>"); StringBuffer htmlText = new StringBuffer("<html><nobr>");
while (matcher.find()) {
matcher.appendReplacement(htmlText, "<span style='font-size: " + cssFontSize + ";" + (!isSelected ? "color: " + cssColor + ";" : "") + "'>$1</span>");
}
matcher.appendTail(htmlText);
htmlText.append("</nobr></html>");
setText(htmlText.toString());
return this; return this;
} }