Exemple #1
0
        private Track ParseTrack()
        {
            Track  track = new Track();
            string name;

            /* Go to next element and check if it is a start element. */
            this.Next();
            while (this.reader.IsStartElement())
            {
                name = this.reader.LocalName;

                /* Process depending on element name. */
                if (name.Equals("id"))
                {
                    /* TODO: handle different ID types. */
                    if (this.GetAttributeString("type") == null)
                    {
                        track.Id = this.GetElementString();
                    }
                }
                else if (name.Equals("redirect"))
                {
                    track.AddRedirect(this.GetElementString());
                }
                else if (name.Equals("title") || name.Equals("name"))
                {
                    track.Title = this.GetElementString();
                }
                else if (name.Equals("artist"))
                {
                    Artist artist = (track.Artist != null) ? track.Artist : new Artist();
                    /* Get artist name. */
                    artist.Name  = this.GetElementString();
                    track.Artist = artist;
                }
                else if (name.Equals("artist-id"))
                {
                    Artist artist = (track.Artist != null) ? track.Artist : new Artist();
                    artist.Id    = this.GetElementString();
                    track.Artist = artist;
                }
                else if (name.Equals("album"))
                {
                    Album album = (track.Album != null) ? track.Album : new Album();
                    /* Get album name. */
                    album.Name  = this.GetElementString();
                    track.Album = album;
                }
                else if (name.Equals("album-id"))
                {
                    Album album = (track.Album != null) ? track.Album : new Album();
                    album.Id    = this.GetElementString();
                    track.Album = album;
                }
                else if (name.Equals("album-artist"))
                {
                    Album  album  = (track.Album != null) ? track.Album : new Album();
                    Artist artist = (track.Artist != null) ? track.Artist : new Artist();
                    artist.Name  = this.GetElementString();
                    album.Artist = artist;
                    track.Album  = album;
                }
                else if (name.Equals("album-artist-id"))
                {
                    Album  album  = (track.Album != null) ? track.Album : new Album();
                    Artist artist = (track.Artist != null) ? track.Artist : new Artist();
                    artist.Id    = this.GetElementString();
                    album.Artist = artist;
                    track.Album  = album;
                }
                else if (name.Equals("year"))
                {
                    track.Year = this.GetElementInteger();
                }
                else if (name.Equals("track-number"))
                {
                    track.TrackNumber = this.GetElementInteger();
                }
                else if (name.Equals("length"))
                {
                    int length = this.GetElementInteger();

                    if (length > 0)
                    {
                        track.Length = length;
                    }
                }
                else if (name.Equals("files"))
                {
                    track.Files = ParseFiles();
                }
                /* TODO: currently skipped. */
                else if (name.Equals("links"))
                {
                    SkipLinks();
                }
                /* TODO: currently skipped. */
                else if (name.Equals("album-links"))
                {
                    SkipLinks();
                }
                else if (name.Equals("cover"))
                {
                    track.Cover = this.GetElementString();
                }
                else if (name.Equals("cover-small"))
                {
                    track.CoverSmall = this.GetElementString();
                }
                else if (name.Equals("cover-large"))
                {
                    track.CoverLarge = this.GetElementString();
                }
                else if (name.Equals("popularity"))
                {
                    track.Popularity = this.GetElementFloat();
                }
                else if (name.Equals("restrictions"))
                {
                    track.Restrictions = this.ParseRestrictions();
                }
                else if (name.Equals("explicit"))
                {
                    track.IsExplicit = this.GetElementBoolean();
                }
                /* Seems to be deprecated. */
                else if (name.Equals("allowed"))
                {
                    /* Skip text. */
                    this.GetElementString();
                }
                /* Seems to be deprecated. */
                else if (name.Equals("forbidden"))
                {
                    /* Skip text. */
                    this.GetElementString();
                }
                else if (name.Equals("similar-tracks"))
                {
                    List <Track> similarTracks = new List <Track>();

                    /* Go to next element and check if it is a start element. */
                    this.Next();
                    while (this.reader.IsStartElement())
                    {
                        name = this.reader.LocalName;

                        /* Process depending on element name. */
                        if (name.Equals("id"))
                        {
                            similarTracks.Add(new Track(this.GetElementString()));
                        }
                        else
                        {
                            throw new XMLParserException("Unexpected element '<" + name + ">'");
                        }

                        this.Next();
                    }

                    /* Set similar tracks. */
                    track.SimilarTracks = similarTracks;
                }
                else if (name.Equals("alternatives"))
                {
                    var tracks = ParseTracks();
                }
                else if (name.Equals("external-ids"))
                {
                    track.ExternalIds = ParseExternalIds();
                }
                else
                {
                    throw new XMLParserException("Unexpected element '<" + name + ">'");
                }

                this.Next();
            }

            /* If album artist of this track is not yet set, then set it. */
            if (track.Album != null && track.Album.Artist == null)
            {
                track.Album.Artist = track.Artist;
            }

            return(track);
        }