Skip to content
Browse files

Correctly parse the year from a date string.

  • Loading branch information...
1 parent 8593bfd commit a4427576adbd143a5d0e517aa0b51ff272ad9df6 @Raptor399 Raptor399 committed Jul 22, 2013
Showing with 27 additions and 11 deletions.
  1. +27 −11 src/main/java/net/pms/dlna/LibMediaInfoParser.java
View
38 src/main/java/net/pms/dlna/LibMediaInfoParser.java
@@ -1,19 +1,32 @@
package net.pms.dlna;
+import static org.apache.commons.lang3.StringUtils.isBlank;
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
+import static org.apache.commons.lang3.StringUtils.lowerCase;
+import static org.apache.commons.lang3.StringUtils.substringAfterLast;
+
+import java.io.File;
+import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import net.pms.configuration.FormatConfiguration;
import net.pms.formats.v2.SubtitleType;
+
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.File;
-import java.util.StringTokenizer;
-
-import static org.apache.commons.lang3.StringUtils.*;
-
public class LibMediaInfoParser {
private static final Logger logger = LoggerFactory.getLogger(LibMediaInfoParser.class);
+
+ /** Regular expression to parse a 4 digit year number from a string */
+ private static final String YEAR_REGEX = ".*([\\d]{4}).*";
+
+ /** Pattern to parse the year from a string */
+ private static final Pattern yearPattern = Pattern.compile(YEAR_REGEX);
+
private static MediaInfo MI;
private static Base64 base64;
@@ -178,12 +191,15 @@ public synchronized static void parse(DLNAMediaInfo media, InputFile inputFile,
} else if (key.equals("Genre") && streamType == MediaInfo.StreamType.General) {
currentAudioTrack.setGenre(ovalue);
} else if (key.equals("Recorded_Date") && streamType == MediaInfo.StreamType.General) {
- try {
- // Try to parse incorrectly stored date
- String recordedDate = value.replaceAll("[^\\d]{4}", "");
- currentAudioTrack.setYear(Integer.parseInt(recordedDate));
- } catch (NumberFormatException nfe) {
- logger.debug("Could not parse year \"" + value + "\"");
+ // Try to parse the year from the stored date
+ Matcher matcher = yearPattern.matcher(value);
+
+ if (matcher.matches()) {
+ try {
+ currentAudioTrack.setYear(Integer.parseInt(matcher.group(1)));
+ } catch (NumberFormatException nfe) {
+ logger.debug("Could not parse year from recorded date \"" + value + "\"");
+ }
}
} else if (key.equals("Track/Position") && streamType == MediaInfo.StreamType.General) {
try {

0 comments on commit a442757

Please sign in to comment.
Something went wrong with that request. Please try again.