2 * Copyright (c) 2012, the Last.fm Java Project and Committers
\r
3 * All rights reserved.
\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
8 * - Redistributions of source code must retain the above
\r
9 * copyright notice, this list of conditions and the
\r
10 * following disclaimer.
\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
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
27 package com.andrew.apollo.lastfm.api;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Collection;
\r
31 import java.util.Collections;
\r
33 import com.andrew.apollo.utils.DomElement;
\r
37 * This utility class can be used to generically generate Result objects (usually Lists or {@link PaginatedResult}s) from an XML response
\r
38 * using {@link ItemFactory ItemFactories}.
\r
40 * @author Janni Kovacs
\r
42 public final class ResponseBuilder {
\r
44 private ResponseBuilder() {
\r
47 private static <T> ItemFactory<T> getItemFactory(Class<T> itemClass) {
\r
48 return ItemFactoryBuilder.getFactoryBuilder().getItemFactory(itemClass);
\r
51 public static <T> Collection<T> buildCollection(Result result, Class<T> itemClass) {
\r
52 return buildCollection(result, getItemFactory(itemClass));
\r
55 public static <T> Collection<T> buildCollection(Result result, ItemFactory<T> factory) {
\r
56 if (!result.isSuccessful())
\r
57 return Collections.emptyList();
\r
58 return buildCollection(result.getContentElement(), factory);
\r
61 public static <T> Collection<T> buildCollection(DomElement element, Class<T> itemClass) {
\r
62 return buildCollection(element, getItemFactory(itemClass));
\r
65 public static <T> Collection<T> buildCollection(DomElement element, ItemFactory<T> factory) {
\r
66 if (element == null)
\r
67 return Collections.emptyList();
\r
68 Collection<DomElement> children = element.getChildren();
\r
69 Collection<T> items = new ArrayList<T>(children.size());
\r
70 for (DomElement child : children) {
\r
71 items.add(factory.createItemFromElement(child));
\r
76 public static <T> PaginatedResult<T> buildPaginatedResult(Result result, Class<T> itemClass) {
\r
77 return buildPaginatedResult(result, getItemFactory(itemClass));
\r
80 public static <T> PaginatedResult<T> buildPaginatedResult(Result result, ItemFactory<T> factory) {
\r
81 if (!result.isSuccessful()) {
\r
82 return new PaginatedResult<T>(0, 0, Collections.<T>emptyList());
\r
85 DomElement contentElement = result.getContentElement();
\r
86 return buildPaginatedResult(contentElement, contentElement, factory);
\r
89 public static <T> PaginatedResult<T> buildPaginatedResult(DomElement contentElement, DomElement childElement, Class<T> itemClass) {
\r
90 return buildPaginatedResult(contentElement, childElement, getItemFactory(itemClass));
\r
93 public static <T> PaginatedResult<T> buildPaginatedResult(DomElement contentElement, DomElement childElement, ItemFactory<T> factory) {
\r
94 Collection<T> items = buildCollection(childElement, factory);
\r
96 String totalPagesAttribute = contentElement.getAttribute("totalPages");
\r
97 if (totalPagesAttribute == null)
\r
98 totalPagesAttribute = contentElement.getAttribute("totalpages");
\r
100 int page = Integer.parseInt(contentElement.getAttribute("page"));
\r
101 int totalPages = Integer.parseInt(totalPagesAttribute);
\r
103 return new PaginatedResult<T>(page, totalPages, items);
\r
106 public static <T> T buildItem(Result result, Class<T> itemClass) {
\r
107 return buildItem(result, getItemFactory(itemClass));
\r
110 public static <T> T buildItem(Result result, ItemFactory<T> factory) {
\r
111 if (!result.isSuccessful())
\r
113 return buildItem(result.getContentElement(), factory);
\r
116 public static <T> T buildItem(DomElement element, Class<T> itemClass) {
\r
117 return buildItem(element, getItemFactory(itemClass));
\r
120 private static <T> T buildItem(DomElement element, ItemFactory<T> factory) {
\r
121 return factory.createItemFromElement(element);
\r