From 66cf786b19f9d225ad2539ea13cda0b84d834069 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 1 Nov 2008 13:54:31 +0000 Subject: [PATCH] * unified embedded checksum handling --- .../net/sourceforge/filebot/FileBotUtil.java | 23 +++++++++++++++++-- .../filebot/ui/panel/sfv/ChecksumRow.java | 13 +++++------ .../sfv/HighlightPatternCellRenderer.java | 15 ++++++++++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/source/net/sourceforge/filebot/FileBotUtil.java b/source/net/sourceforge/filebot/FileBotUtil.java index 632ceec6..cb49ceb2 100644 --- a/source/net/sourceforge/filebot/FileBotUtil.java +++ b/source/net/sourceforge/filebot/FileBotUtil.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sourceforge.tuned.FileUtil; @@ -28,8 +29,6 @@ public final class FileBotUtil { 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 EMBEDDED_CHECKSUM_PATTERN = Pattern.compile("[(\\[](\\p{XDigit}{8})[\\])]"); - /** * Strip filename of invalid characters @@ -47,6 +46,26 @@ public final class FileBotUtil { 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[] SFV_FILE_EXTENSIONS = { "sfv" }; private static final String[] LIST_FILE_EXTENSIONS = { "txt", "list", "" }; diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumRow.java b/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumRow.java index a81231d3..87be2caa 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumRow.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/ChecksumRow.java @@ -6,7 +6,6 @@ import java.io.File; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.regex.Matcher; import net.sourceforge.filebot.FileBotUtil; @@ -50,13 +49,13 @@ public class ChecksumRow { * @return the checksum or null, if parameter did not contain a checksum */ private static Long getEmbeddedChecksum(String name) { - // look for a checksum pattern like [49A93C5F] - Matcher matcher = FileBotUtil.EMBEDDED_CHECKSUM_PATTERN.matcher(name); + // look for a checksum pattern like [49A93C5F] + String match = FileBotUtil.getEmbeddedChecksum(name); - if (matcher.find()) - return Long.parseLong(matcher.group(1), 16); - else - return null; + if (match != null) + return Long.parseLong(match, 16); + + return null; } diff --git a/source/net/sourceforge/filebot/ui/panel/sfv/HighlightPatternCellRenderer.java b/source/net/sourceforge/filebot/ui/panel/sfv/HighlightPatternCellRenderer.java index f0e584d8..5bc92c78 100644 --- a/source/net/sourceforge/filebot/ui/panel/sfv/HighlightPatternCellRenderer.java +++ b/source/net/sourceforge/filebot/ui/panel/sfv/HighlightPatternCellRenderer.java @@ -3,6 +3,7 @@ package net.sourceforge.filebot.ui.panel.sfv; import java.awt.Component; +import java.util.regex.Matcher; import java.util.regex.Pattern; 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 // 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("[$1]"); + Matcher matcher = pattern.matcher(value.toString()); // use no-break, because we really don't want line-wrapping in our table cells - setText("" + htmlText + ""); + StringBuffer htmlText = new StringBuffer(""); + + while (matcher.find()) { + matcher.appendReplacement(htmlText, "$1"); + } + + matcher.appendTail(htmlText); + + htmlText.append(""); + + setText(htmlText.toString()); return this; }