From dbe0d9170c1a37b627130f7ab00f550f5bcb3f69 Mon Sep 17 00:00:00 2001 From: North Date: Mon, 10 Sep 2012 12:32:54 +0300 Subject: [PATCH] [refactoring] improved support for HQ image archives The names for the images in the HQ arhives don't have consistent capitalization --- .../plugins/card/images/DownloadPictures.java | 24 +----- .../plugins/card/utils/CardImageUtils.java | 81 +++++++++---------- 2 files changed, 42 insertions(+), 63 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java index 9a71f5da1a7..e7adf91966e 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/images/DownloadPictures.java @@ -216,11 +216,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab for (Card card : allCards) { if (card.getCardNumber() > 0 && !card.getExpansionSetCode().isEmpty()) { CardInfo url = new CardInfo(card.getName(), card.getExpansionSetCode(), card.getCardNumber(), 0, false, card.canTransform(), card.isNightCard()); - boolean withCollectorId = false; - if (basicLandPattern.matcher(card.getName()).matches()) { - withCollectorId = true; - } - file = new TFile(CardImageUtils.getImagePath(url, withCollectorId, imagesPath)); + file = new TFile(CardImageUtils.getImagePath(url, imagesPath)); if (!file.exists()) { return true; } @@ -282,11 +278,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab * check to see which cards we already have */ for (CardInfo card : allCardsUrls) { - boolean withCollectorId = false; - if (basicLandPattern.matcher(card.getName()).matches()) { - withCollectorId = true; - } - file = new TFile(CardImageUtils.getImagePath(card, withCollectorId, imagesPath)); + file = new TFile(CardImageUtils.getImagePath(card, imagesPath)); if (!file.exists()) { cardsToDownload.add(card); } @@ -453,10 +445,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab @Override public void run() { try { - boolean withCollectorId = false; - if (basicLandPattern.matcher(card.getName()).matches()) { - withCollectorId = true; - } File temporaryFile = new File(Constants.IO.imageBaseDir + File.separator + card.hashCode() + "." + card.getName() + ".jpg"); BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream()); @@ -480,7 +468,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab out.flush(); out.close(); - TFile outputFile = new TFile(CardImageUtils.getImagePath(card, withCollectorId)); + TFile outputFile = new TFile(CardImageUtils.getImagePath(card, imagesPath)); if (card.isTwoFacedCard()) { BufferedImage image = ImageIO.read(temporaryFile); temporaryFile.delete(); @@ -543,11 +531,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab Iterator cardsIterator = DownloadPictures.this.cards.iterator(); while (cardsIterator.hasNext()) { CardInfo cardInfo = cardsIterator.next(); - boolean withCollectorId = false; - if (basicLandPattern.matcher(cardInfo.getName()).matches()) { - withCollectorId = true; - } - TFile file = new TFile(CardImageUtils.getImagePath(cardInfo, withCollectorId)); + TFile file = new TFile(CardImageUtils.getImagePath(cardInfo, imagesPath)); if (file.exists()) { cardsIterator.remove(); } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java index a5480b53fd6..daf69e53a4b 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java @@ -2,12 +2,14 @@ package org.mage.plugins.card.utils; import de.schlichtherle.truezip.file.TFile; import java.util.HashMap; +import java.util.regex.Pattern; import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.images.CardInfo; import org.mage.plugins.card.properties.SettingsManager; public class CardImageUtils { + private static final Pattern basicLandPattern = Pattern.compile("^(Forest|Mountain|Swamp|Island|Plains)$"); private static HashMap pathCache = new HashMap(); /** @@ -19,7 +21,6 @@ public class CardImageUtils { */ public static String getImagePath(CardInfo card) { String filePath; - String suffix = ".jpg"; TFile file; if (card.isToken()) { @@ -38,20 +39,7 @@ public class CardImageUtils { pathCache.put(card, filePath); } } else { - filePath = getImagePath(card, false); - file = new TFile(filePath); - - if (!file.exists()) { - filePath = getImagePath(card, true); - file = new TFile(filePath); - } - } - - /** - * try current directory - */ - if (file == null || !file.exists()) { - filePath = cleanString(card.getName()) + suffix; + filePath = getImagePath(card, null); file = new TFile(filePath); } @@ -63,18 +51,18 @@ public class CardImageUtils { } private static String getTokenImagePath(CardInfo card) { - String filename = getImagePath(card, false); + String filename = getImagePath(card, null); TFile file = new TFile(filename); if (!file.exists()) { CardInfo updated = new CardInfo(card); updated.setName(card.getName() + " 1"); - filename = getImagePath(updated, false); + filename = getImagePath(updated, null); file = new TFile(filename); if (!file.exists()) { updated = new CardInfo(card); updated.setName(card.getName() + " 2"); - filename = getImagePath(updated, false); + filename = getImagePath(updated, null); } } @@ -98,22 +86,6 @@ public class CardImageUtils { return ""; } - public static String cleanString(String in) { - in = in.trim(); - StringBuilder out = new StringBuilder(); - char c; - for (int i = 0; i < in.length(); i++) { - c = in.charAt(i); - if (c == ' ' || c == '-') { - out.append('_'); - } else if (Character.isLetterOrDigit(c)) { - out.append(c); - } - } - - return out.toString().toLowerCase(); - } - public static String updateSet(String cardSet, boolean forUrl) { String set = cardSet.toLowerCase(); if (set.equals("con")) { @@ -125,7 +97,7 @@ public class CardImageUtils { return set; } - public static String getImageDir(CardInfo card, String imagesPath) { + private static String getImageDir(CardInfo card, String imagesPath) { if (card.getSet() == null) { return ""; } @@ -138,17 +110,40 @@ public class CardImageUtils { } } - public static String getImagePath(CardInfo card, boolean withCollector) { - return getImagePath(card, withCollector, null); - } - - public static String getImagePath(CardInfo card, boolean withCollector, String imagesPath) { + public static String getImagePath(CardInfo card, String imagesPath) { + String imageDir = getImageDir(card, imagesPath); String type = card.getType() != 0 ? " " + Integer.toString(card.getType()) : ""; String name = card.getName(); - if (withCollector) { - return getImageDir(card, imagesPath) + TFile.separator + name + "." + card.getCollectorId() + ".full.jpg"; + + String path, capitalizedPath; + if (basicLandPattern.matcher(name).matches()) { + path = imageDir + TFile.separator + name + "." + card.getCollectorId() + ".full.jpg"; + capitalizedPath = imageDir + TFile.separator + capitalize(name) + "." + card.getCollectorId() + ".full.jpg"; + } else { - return getImageDir(card, imagesPath) + TFile.separator + name + type + ".full.jpg"; + path = imageDir + TFile.separator + name + type + ".full.jpg"; + capitalizedPath = imageDir + TFile.separator + capitalize(name) + type + ".full.jpg"; } + + return new TFile(capitalizedPath).exists() ? capitalizedPath : path; + } + + private static String capitalize(String str) { + int delimLen = -1; + if (str.isEmpty() || delimLen == 0) { + return str; + } + char[] buffer = str.toCharArray(); + boolean capitalizeNext = true; + for (int i = 0; i < buffer.length; i++) { + char ch = buffer[i]; + if (Character.isWhitespace(ch)) { + capitalizeNext = true; + } else if (capitalizeNext) { + buffer[i] = Character.toTitleCase(ch); + capitalizeNext = false; + } + } + return new String(buffer); } }