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.text.DateFormat; 030import java.text.ParseException; 031import java.text.SimpleDateFormat; 032import java.util.Date; 033import java.util.Locale; 034 035import de.umass.xml.DomElement; 036 037/** 038 * An <code>Image</code> contains metadata and URLs for an artist's image. Metadata contains title, votes, format and other. 039 * Images are available in various sizes, see {@link ImageSize} for all sizes. 040 * 041 * @author Janni Kovacs 042 * @see ImageSize 043 * @see Artist#getImages(String, String) 044 */ 045public class Image extends ImageHolder { 046 047 static final ItemFactory<Image> FACTORY = new ImageFactory(); 048 049 private static final DateFormat DATE_ADDED_FORMAT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", 050 Locale.ENGLISH); 051 052 private String title; 053 private String url; 054 private Date dateAdded; 055 private String format; 056 057 private String owner; 058 private int thumbsUp, thumbsDown; 059 060 private Image() { 061 } 062 063 public String getTitle() { 064 return title; 065 } 066 067 public String getUrl() { 068 return url; 069 } 070 071 public Date getDateAdded() { 072 return dateAdded; 073 } 074 075 public String getFormat() { 076 return format; 077 } 078 079 public String getOwner() { 080 return owner; 081 } 082 083 public int getThumbsUp() { 084 return thumbsUp; 085 } 086 087 public int getThumbsDown() { 088 return thumbsDown; 089 } 090 091 private static class ImageFactory implements ItemFactory<Image> { 092 public Image createItemFromElement(DomElement element) { 093 Image i = new Image(); 094 i.title = element.getChildText("title"); 095 i.url = element.getChildText("url"); 096 i.format = element.getChildText("format"); 097 try { 098 i.dateAdded = DATE_ADDED_FORMAT.parse(element.getChildText("dateadded")); 099 } catch (ParseException e1) { 100 e1.printStackTrace(); 101 } 102 DomElement owner = element.getChild("owner"); 103 if (owner != null) 104 i.owner = owner.getChildText("name"); 105 DomElement votes = element.getChild("votes"); 106 if (votes != null) { 107 i.thumbsUp = Integer.parseInt(votes.getChildText("thumbsup")); 108 i.thumbsDown = Integer.parseInt(votes.getChildText("thumbsdown")); 109 } 110 DomElement sizes = element.getChild("sizes"); 111 for (DomElement image : sizes.getChildren("size")) { 112 // code copied from ImageHolder.loadImages 113 String attribute = image.getAttribute("name"); 114 ImageSize size = null; 115 if (attribute == null) { 116 size = ImageSize.MEDIUM; // workaround for image responses without size attr. 117 } else { 118 try { 119 size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH)); 120 } catch (IllegalArgumentException e) { 121 // if they suddenly again introduce a new image size 122 } 123 } 124 if (size != null) 125 i.imageUrls.put(size, image.getText()); 126 } 127 return i; 128 } 129 } 130}