me.iste.subsonicdialer.Factory.java Source code

Java tutorial

Introduction

Here is the source code for me.iste.subsonicdialer.Factory.java

Source

/**
 * 
 */
package me.iste.subsonicdialer;

import static me.iste.subsonicdialer.Constants.NS;

import java.util.List;

import me.iste.subsonicdialer.exception.SubsonicException;
import me.iste.subsonicdialer.xml.Artist;
import me.iste.subsonicdialer.xml.Channel;
import me.iste.subsonicdialer.xml.ChatMessage;
import me.iste.subsonicdialer.xml.Child;
import me.iste.subsonicdialer.xml.Directory;
import me.iste.subsonicdialer.xml.Episode;
import me.iste.subsonicdialer.xml.Index;
import me.iste.subsonicdialer.xml.Indexes;
import me.iste.subsonicdialer.xml.JukeboxPlaylist;
import me.iste.subsonicdialer.xml.JukeboxStatus;
import me.iste.subsonicdialer.xml.License;
import me.iste.subsonicdialer.xml.MusicFolder;
import me.iste.subsonicdialer.xml.NowPlaying;
import me.iste.subsonicdialer.xml.Playlist;
import me.iste.subsonicdialer.xml.SearchResult;
import me.iste.subsonicdialer.xml.Share;
import me.iste.subsonicdialer.xml.Shortcut;
import me.iste.subsonicdialer.xml.User;

import org.jdom2.Document;
import org.jdom2.Element;

/**
 * @author Iste
 * @version 1.2 30 avr. 2012
 * 
 */
public final class Factory {

    /**
     * @param document The XML {@link Document}.
     * @return The {@link License}.
     */
    public static License createLicense(final Document document) {
        final Element element = document.getRootElement().getChild("license", NS);

        final String valid = element.getAttributeValue("valid");
        final String email = element.getAttributeValue("email");
        final String key = element.getAttributeValue("key");
        final String date = element.getAttributeValue("date");

        return new License(valid, email, key, date);
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link MusicFolder}s.
     */
    public static MusicFolder[] createMusicFolders(final Document document) {
        final List<Element> musicFolders = document.getRootElement().getChild("musicFolders", NS)
                .getChildren("musicFolder", NS);

        final MusicFolder[] folders = new MusicFolder[musicFolders.size()];
        int i = 0;
        for (final Element musicFolder : musicFolders) {
            folders[i++] = new MusicFolder(musicFolder.getAttributeValue("id"),
                    musicFolder.getAttributeValue("name"));
        }

        return folders;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link NowPlaying}s.
     */
    public static NowPlaying[] createNowPlaying(final Document document) {
        final List<Element> elements = document.getRootElement().getChild("nowPlaying", NS).getChildren("entry",
                NS);

        final NowPlaying[] nowPlayings = new NowPlaying[elements.size()];
        int i = 0;
        for (final Element nowPlaying : elements) {
            nowPlayings[i++] = createNowPlaying(nowPlaying);
        }

        return nowPlayings;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link Indexes}.
     */
    public static Indexes createIndexes(final Document document) {
        final Element index = document.getRootElement().getChild("indexes", NS);

        final List<Element> shortcutsElement = index.getChildren("shortcut", NS);
        final List<Element> foldersElement = index.getChildren("index", NS);
        final List<Element> playablesElement = index.getChildren("child", NS);

        final Shortcut[] shortcuts = new Shortcut[shortcutsElement.size()];
        int i = 0;
        for (final Element shortcut : shortcutsElement) {
            shortcuts[i++] = new Shortcut(shortcut.getAttributeValue("id"), shortcut.getAttributeValue("name"));
        }

        final Index[] folders = new Index[foldersElement.size()];
        i = 0;
        for (final Element folder : foldersElement) {
            final List<Element> artistsElement = folder.getChildren("artist", NS);

            final Artist[] artists = new Artist[artistsElement.size()];
            int j = 0;
            for (final Element artist : artistsElement) {
                artists[j++] = new Artist(artist.getAttributeValue("id"), artist.getAttributeValue("name"));
            }

            folders[i++] = new Index(folder.getAttributeValue("name"), artists);
        }

        final Child[] childs = new Child[playablesElement.size()];
        i = 0;
        for (final Element childElement : playablesElement) {
            childs[i++] = createChild(childElement);
        }

        return new Indexes(index.getAttributeValue("lastModified"), shortcuts, folders, childs);
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link Directory}.
     */
    public static Directory createMusicDirectory(final Document document) {
        final Element directoryElement = document.getRootElement().getChild("directory", NS);

        final List<Element> childElements = directoryElement.getChildren("child", NS);
        final Child[] childs = new Child[childElements.size()];
        int i = 0;
        for (final Element childElement : childElements) {
            childs[i++] = createChild(childElement);
        }

        return new Directory(directoryElement.getAttributeValue("id"), directoryElement.getAttributeValue("name"),
                childs);
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Child}.
     */
    public static Child[] createSearch(final Document document) {
        final Element searchResult = document.getRootElement().getChild("searchResult", NS);
        final List<Element> matchs = searchResult.getChildren("match", NS);

        final Child[] childs = new Child[matchs.size()];

        int i = 0;
        for (final Element childElement : matchs) {
            childs[i++] = createChild(childElement);
        }

        return childs;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link SearchResult}.
     */
    public static SearchResult createSearch2(final Document document) {
        final Element searchResultElement = document.getRootElement().getChild("searchResult2", NS);

        final List<Element> artistElements = searchResultElement.getChildren("artist", NS);
        final Artist[] artists = new Artist[artistElements.size()];
        int i = 0;
        for (final Element artist : artistElements) {
            artists[i++] = new Artist(artist.getAttributeValue("id"), artist.getAttributeValue("name"));
        }

        final List<Element> albumElements = searchResultElement.getChildren("album", NS);
        final Child[] albums = new Child[albumElements.size()];
        i = 0;
        for (final Element album : albumElements) {
            albums[i++] = createChild(album);
        }

        final List<Element> songElements = searchResultElement.getChildren("song", NS);
        final Child[] songs = new Child[songElements.size()];
        i = 0;
        for (final Element song : songElements) {
            songs[i++] = createChild(song);
        }

        return new SearchResult(artists, albums, songs);
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Playlist}.
     */
    public static Playlist[] createPlaylists(final Document document) {
        final Element searchResult = document.getRootElement().getChild("playlists", NS);

        final List<Element> playlistsElement = searchResult.getChildren("playlist", NS);
        final Playlist[] playlists = new Playlist[playlistsElement.size()];
        int i = 0;
        for (final Element playlist : playlistsElement) {

            final List<Element> allowedUsersElement = playlist.getChildren("allowedUser", NS);
            final String[] allowedUsers = new String[allowedUsersElement.size()];
            int j = 0;
            for (final Element allowedUser : allowedUsersElement) {
                allowedUsers[j++] = allowedUser.getValue();
            }

            playlists[i++] = createPlaylist(playlist, allowedUsers, null);
        }

        return playlists;
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Child}.
     */
    public static Playlist createPlaylist(final Document document) {
        final Element playlist = document.getRootElement().getChild("playlist", NS);
        final List<Element> entries = playlist.getChildren("entry", NS);

        final Child[] childs = new Child[entries.size()];
        int i = 0;
        for (final Element childElement : entries) {
            childs[i++] = createChild(childElement);
        }

        final List<Element> allowedUsersElement = playlist.getChildren("allowedUser", NS);
        final String[] allowedUsers = new String[allowedUsersElement.size()];
        i = 0;
        for (final Element allowedUser : allowedUsersElement) {
            allowedUsers[i++] = allowedUser.getValue();
        }

        return createPlaylist(playlist, allowedUsers, childs);
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link User}.
     */
    public static User createUser(final Document document) {
        final Element user = document.getRootElement().getChild("user", NS);
        return createUser(user);
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link ChatMessage}.
     */
    public static ChatMessage[] createChatMessages(final Document document) {
        final Element chatMessagesElement = document.getRootElement().getChild("chatMessages", NS);

        final List<Element> entries = chatMessagesElement.getChildren("chatMessage", NS);

        final ChatMessage[] chatMessages = new ChatMessage[entries.size()];
        int i = 0;
        for (final Element messageElement : entries) {
            chatMessages[i++] = new ChatMessage(messageElement.getAttributeValue("username"),
                    messageElement.getAttributeValue("time"), messageElement.getAttributeValue("message"));
        }

        return chatMessages;
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Child}.
     */
    public static Child[] createAlbumList(final Document document) {
        final Element albumListElement = document.getRootElement().getChild("albumList", NS);

        final List<Element> entries = albumListElement.getChildren("album", NS);

        final Child[] albums = new Child[entries.size()];
        int i = 0;
        for (final Element childElement : entries) {
            albums[i++] = createChild(childElement);
        }

        return albums;
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Child}.
     */
    public static Child[] createRandomSongs(final Document document) {
        final Element songsListElement = document.getRootElement().getChild("randomSongs", NS);

        final List<Element> entries = songsListElement.getChildren("song", NS);

        final Child[] songs = new Child[entries.size()];
        int i = 0;
        for (final Element childElement : entries) {
            songs[i++] = createChild(childElement);
        }

        return songs;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The lyrics.
     */
    public static String createLyrics(final Document document) {
        final Element lyricsElement = document.getRootElement().getChild("lyrics", NS);

        return lyricsElement.getValue();
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link JukeboxStatus}.
     */
    public static JukeboxStatus createjukeboxControl(final Document document) {
        final JukeboxStatus jukeboxStatus;

        final Element jukeboxStatusElement = document.getRootElement().getChild("jukeboxStatus", NS);
        if (jukeboxStatusElement == null) {
            final Element jukeboxPlaylistElement = document.getRootElement().getChild("jukeboxPlaylist", NS);
            final List<Element> entries = jukeboxPlaylistElement.getChildren("entry", NS);
            final Child[] childs = new Child[entries.size()];
            int i = 0;
            for (final Element childElement : entries) {
                childs[i++] = createChild(childElement);
            }
            jukeboxStatus = new JukeboxPlaylist(jukeboxPlaylistElement.getAttributeValue("currentIndex"),
                    jukeboxPlaylistElement.getAttributeValue("playing"),
                    jukeboxPlaylistElement.getAttributeValue("gain"),
                    jukeboxPlaylistElement.getAttributeValue("position"), childs);
        } else {
            jukeboxStatus = new JukeboxStatus(jukeboxStatusElement.getAttributeValue("currentIndex"),
                    jukeboxStatusElement.getAttributeValue("playing"),
                    jukeboxStatusElement.getAttributeValue("gain"),
                    jukeboxStatusElement.getAttributeValue("position"));
        }

        return jukeboxStatus;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link JukeboxStatus}.
     */
    public static Channel[] createPodcasts(final Document document) {
        final Element podcastsElement = document.getRootElement().getChild("podcasts", NS);

        final List<Element> channelElements = podcastsElement.getChildren("channel", NS);
        final Channel[] channels = new Channel[channelElements.size()];
        int i = 0;
        for (final Element channelElement : channelElements) {

            final List<Element> episodeElements = channelElement.getChildren("episode", NS);
            final Episode[] episodes = new Episode[episodeElements.size()];
            int j = 0;
            for (final Element episodeElement : episodeElements) {
                episodes[j++] = createEpisode(episodeElement);
            }

            channels[i++] = new Channel(channelElement.getAttributeValue("id"),
                    channelElement.getAttributeValue("url"), channelElement.getAttributeValue("title"),
                    channelElement.getAttributeValue("description"), channelElement.getAttributeValue("status"),
                    channelElement.getAttributeValue("errorMessage"), episodes);
        }

        return channels;
    }

    /**
     * @param document The XML {@link Document}.
     * @return Array of {@link Share}.
     */
    public static Share[] createShares(final Document document) {
        final Element sharesElement = document.getRootElement().getChild("shares", NS);

        final List<Element> shareElements = sharesElement.getChildren("share", NS);
        final Share[] shares = new Share[shareElements.size()];
        int i = 0;
        for (final Element share : shareElements) {

            final List<Element> entryElements = share.getChildren("entry", NS);
            final Child[] childs = new Child[entryElements.size()];
            int j = 0;
            for (final Element childElement : entryElements) {
                childs[j++] = createChild(childElement);
            }

            shares[i++] = new Share(share.getAttributeValue("id"), share.getAttributeValue("url"),
                    share.getAttributeValue("description"), share.getAttributeValue("username"),
                    share.getAttributeValue("created"), share.getAttributeValue("lastVisited"),
                    share.getAttributeValue("expires"), share.getAttributeValue("visitCount"), childs);
        }

        return shares;
    }

    /**
     * @param document The XML {@link Document}.
     * @return The {@link Share}.
     */
    public static Share createNewShares(final Document document) {
        final Element sharesElement = document.getRootElement().getChild("shares", NS);
        final Element shareElement = sharesElement.getChild("share", NS);

        final List<Element> entryElements = shareElement.getChildren("entry", NS);
        final Child[] childs = new Child[entryElements.size()];
        int i = 0;
        for (final Element childElement : entryElements) {
            childs[i++] = createChild(childElement);
        }

        return new Share(shareElement.getAttributeValue("id"), shareElement.getAttributeValue("url"),
                shareElement.getAttributeValue("description"), shareElement.getAttributeValue("username"),
                shareElement.getAttributeValue("created"), shareElement.getAttributeValue("lastVisited"),
                shareElement.getAttributeValue("expires"), shareElement.getAttributeValue("visitCount"), childs);
    }

    /*
     * 
     * 
     * 
     */

    /**
     * @param document the XML {@link Document}.
     * @throws SubsonicException Throws a {@link SubsonicException} if found.
     */
    public static void checkError(final Document document) throws SubsonicException {
        if (document.getRootElement().getAttributeValue("status").equals("failed")) {
            final Element error = document.getRootElement().getChild("error", Constants.NS);
            throw new SubsonicException(error.getAttributeValue("message"), error.getAttributeValue("code"),
                    document.getRootElement().getAttributeValue("version"));
        }
    }

    /**
     * @param document the XML {@link Document}.
     * @return The server version.
     */
    public static String getVersion(final Document document) {
        return document.getRootElement().getAttributeValue("version");
    }

    private static Child createChild(final Element childElement) {
        return new Child(childElement.getAttributeValue("id"), childElement.getAttributeValue("parent"),
                childElement.getAttributeValue("title"), childElement.getAttributeValue("isDir"),
                childElement.getAttributeValue("album"), childElement.getAttributeValue("artist"),
                childElement.getAttributeValue("track"), childElement.getAttributeValue("year"),
                childElement.getAttributeValue("genre"), childElement.getAttributeValue("coverArt"),
                childElement.getAttributeValue("size"), childElement.getAttributeValue("contentType"),
                childElement.getAttributeValue("isVideo"), childElement.getAttributeValue("suffix"),
                childElement.getAttributeValue("transcodedContentType"),
                childElement.getAttributeValue("transcodedSuffix"), childElement.getAttributeValue("duration"),
                childElement.getAttributeValue("bitRate"), childElement.getAttributeValue("path"));
    }

    private static NowPlaying createNowPlaying(final Element childElement) {
        return new NowPlaying(childElement.getAttributeValue("userName"),
                childElement.getAttributeValue("minutesAgo"), childElement.getAttributeValue("playerId"),
                childElement.getAttributeValue("id"), childElement.getAttributeValue("parent"),
                childElement.getAttributeValue("title"), childElement.getAttributeValue("isDir"),
                childElement.getAttributeValue("album"), childElement.getAttributeValue("artist"),
                childElement.getAttributeValue("track"), childElement.getAttributeValue("year"),
                childElement.getAttributeValue("genre"), childElement.getAttributeValue("coverArt"),
                childElement.getAttributeValue("size"), childElement.getAttributeValue("contentType"),
                childElement.getAttributeValue("isVideo"), childElement.getAttributeValue("suffix"),
                childElement.getAttributeValue("transcodedContentType"),
                childElement.getAttributeValue("transcodedSuffix"), childElement.getAttributeValue("duration"),
                childElement.getAttributeValue("bitRate"), childElement.getAttributeValue("path"));
    }

    private static Playlist createPlaylist(final Element playlistElement, final String[] allowedUsers,
            final Child[] childs) {
        return new Playlist(playlistElement.getAttributeValue("id"), playlistElement.getAttributeValue("name"),
                playlistElement.getAttributeValue("comment"), playlistElement.getAttributeValue("owner"),
                playlistElement.getAttributeValue("public"), playlistElement.getAttributeValue("songCount"),
                playlistElement.getAttributeValue("duration"), playlistElement.getAttributeValue("created"),
                allowedUsers, childs);
    }

    private static User createUser(final Element userElement) {
        return new User(userElement.getAttributeValue("username"), userElement.getAttributeValue("email"),
                userElement.getAttributeValue("scrobblingEnabled"), userElement.getAttributeValue("adminRole"),
                userElement.getAttributeValue("settingsRole"), userElement.getAttributeValue("downloadRole"),
                userElement.getAttributeValue("uploadRole"), userElement.getAttributeValue("playlistRole"),
                userElement.getAttributeValue("coverArtRole"), userElement.getAttributeValue("commentRole"),
                userElement.getAttributeValue("podcastRole"), userElement.getAttributeValue("streamRole"),
                userElement.getAttributeValue("jukeboxRole"), userElement.getAttributeValue("shareRole"));
    }

    private static Episode createEpisode(final Element childElement) {
        return new Episode(childElement.getAttributeValue("id"), childElement.getAttributeValue("parent"),
                childElement.getAttributeValue("title"), childElement.getAttributeValue("isDir"),
                childElement.getAttributeValue("album"), childElement.getAttributeValue("artist"),
                childElement.getAttributeValue("track"), childElement.getAttributeValue("year"),
                childElement.getAttributeValue("genre"), childElement.getAttributeValue("coverArt"),
                childElement.getAttributeValue("size"), childElement.getAttributeValue("contentType"),
                childElement.getAttributeValue("isVideo"), childElement.getAttributeValue("suffix"),
                childElement.getAttributeValue("transcodedContentType"),
                childElement.getAttributeValue("transcodedSuffix"), childElement.getAttributeValue("duration"),
                childElement.getAttributeValue("bitRate"), childElement.getAttributeValue("path"),
                childElement.getAttributeValue("streamId"), childElement.getAttributeValue("description"),
                childElement.getAttributeValue("publishDate"), childElement.getAttributeValue("status"));
    }

    private Factory() {
    }

}