001/* 002 * Copyright (c) 2012, the Last.fm Java Project and Committers 003 * All rights reserved. 004 * 005 * Redistribution and use of this software in source and binary forms, with or without modification, are 006 * permitted provided that the following conditions are met: 007 * 008 * - Redistributions of source code must retain the above 009 * copyright notice, this list of conditions and the 010 * following disclaimer. 011 * 012 * - Redistributions in binary form must reproduce the above 013 * copyright notice, this list of conditions and the 014 * following disclaimer in the documentation and/or other 015 * materials provided with the distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 025 */ 026 027package de.umass.lastfm; 028 029import java.util.*; 030 031import de.umass.xml.DomElement; 032 033/** 034 * Abstract superclass for all items that may contain images (such as {@link Artist}s, {@link Album}s or {@link Track}s). 035 * 036 * @author Janni Kovacs 037 */ 038public abstract class ImageHolder { 039 040 protected Map<ImageSize, String> imageUrls = new HashMap<ImageSize, String>(); 041 042 /** 043 * Returns a Set of all {@link ImageSize}s available. 044 * 045 * @return all sizes 046 */ 047 public Set<ImageSize> availableSizes() { 048 return imageUrls.keySet(); 049 } 050 051 /** 052 * Returns the URL of the image in the specified size, or <code>null</code> if not available. 053 * 054 * @param size The preferred size 055 * @return an image URL 056 */ 057 public String getImageURL(ImageSize size) { 058 return imageUrls.get(size); 059 } 060 061 protected static void loadImages(ImageHolder holder, DomElement element) { 062 Collection<DomElement> images = element.getChildren("image"); 063 for (DomElement image : images) { 064 String attribute = image.getAttribute("size"); 065 ImageSize size = null; 066 if (attribute == null) { 067 size = ImageSize.MEDIUM; // workaround for image responses without size attr. 068 } else { 069 try { 070 size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH)); 071 } catch (IllegalArgumentException e) { 072 // if they suddenly again introduce a new image size 073 } 074 } 075 if (size != null) 076 holder.imageUrls.put(size, image.getText()); 077 } 078 } 079}