OSDN Git Service

overhaul
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / lastfm / api / Image.java
1 /*\r
2  * Copyright (c) 2012, the Last.fm Java Project and Committers\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use of this software in source and binary forms, with or without modification, are\r
6  * permitted provided that the following conditions are met:\r
7  *\r
8  * - Redistributions of source code must retain the above\r
9  *   copyright notice, this list of conditions and the\r
10  *   following disclaimer.\r
11  *\r
12  * - Redistributions in binary form must reproduce the above\r
13  *   copyright notice, this list of conditions and the\r
14  *   following disclaimer in the documentation and/or other\r
15  *   materials provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED\r
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\r
19  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\r
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR\r
23  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25  */\r
26 \r
27 package com.andrew.apollo.lastfm.api;\r
28 \r
29 import java.util.Locale;\r
30 \r
31 import com.andrew.apollo.utils.DomElement;\r
32 \r
33 /**\r
34  * An <code>Image</code> contains metadata and URLs for an artist's image.\r
35  * Metadata contains title, votes, format and other. Images are available in\r
36  * various sizes, see {@link ImageSize} for all sizes.\r
37  * \r
38  * @author Janni Kovacs\r
39  * @see ImageSize\r
40  * @see Artist#getImages(String, String)\r
41  */\r
42 public class Image extends ImageHolder {\r
43 \r
44     static final ItemFactory<Image> FACTORY = new ImageFactory();\r
45 \r
46     private String url;\r
47 \r
48     private Image() {\r
49     }\r
50 \r
51     public String getUrl() {\r
52         return url;\r
53     }\r
54 \r
55     private static class ImageFactory implements ItemFactory<Image> {\r
56         @Override\r
57         public Image createItemFromElement(DomElement element) {\r
58             Image i = new Image();\r
59             i.url = element.getChildText("url");\r
60             DomElement sizes = element.getChild("sizes");\r
61             for (DomElement image : sizes.getChildren("size")) {\r
62                 // code copied from ImageHolder.loadImages\r
63                 String attribute = image.getAttribute("name");\r
64                 ImageSize size = null;\r
65                 if (attribute == null) {\r
66                     size = ImageSize.LARGESQUARE;\r
67                 } else {\r
68                     try {\r
69                         size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH));\r
70                     } catch (IllegalArgumentException e) {\r
71                         // if they suddenly again introduce a new image size\r
72                     }\r
73                 }\r
74                 if (size != null)\r
75                     i.imageUrls.put(size, image.getText());\r
76             }\r
77             return i;\r
78         }\r
79     }\r
80 }\r