Package se.despotify.util

Examples of se.despotify.util.XMLElement


    /* Load XML. */
    String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><playlist>" +
        new String(data, Charset.forName("UTF-8")) +
        "</playlist>";
    XMLElement playlistElement = XML.load(xml);
    if (log.isDebugEnabled()) {
      log.debug(xml);
    }
    /* Create and return playlist. */
    Playlist.fromXMLElement(playlistElement, store, playlist);
View Full Code Here



  public static PlaylistContainer fromXMLElement(XMLElement playlistsElement, Store store, PlaylistContainer playlists) {

    /* Get "change" element. */
    XMLElement changeElement = playlistsElement.getChild("next-change").getChild("change");

    if (changeElement.hasChild("user")) {
      playlists.id = changeElement.getChildText("user").trim();
    }

    /* Get items (comma separated list). */
    if (changeElement.getChild("ops").hasChild("add")) {
      String items = changeElement.getChild("ops").getChild("add").getChildText("items");

      for (String playlistUUID : items.split(",")) {
        playlistUUID = playlistUUID.trim();
        if (playlistUUID.length() == 34 && playlistUUID.endsWith("02")) {
          playlistUUID = playlistUUID.substring(0, 32);
        }
        Playlist playlist = store.getPlaylist(playlistUUID);
        if (!playlists.getItems().contains(playlist)) {
          playlists.getItems().add(playlist);
        }
        // todo remove deleted?
      }
    }

    /* Get "version" element. */
    XMLElement versionElement = playlistsElement.getChild("next-change").getChild("version");

    /* Split version string into parts. */
    String[] versionTagValues = versionElement.getText().split(",", 4);

    playlists.setRevision(Long.parseLong(versionTagValues[0]));
    playlists.setChecksum(Long.parseLong(versionTagValues[2]));

    if (playlists.getItems().size() != Long.parseLong(versionTagValues[1])) {
View Full Code Here

  }

  public static void fromXMLElement(XMLElement playlistElement, Store store, Playlist playlist) throws DespotifyException {

    /* Get "change" element. */
    XMLElement changeElement = playlistElement.getChild("next-change").getChild("change");

    /* Set author. */
    playlist.author = changeElement.getChildText("user");

    /* Set name. */
    playlist.name = changeElement.getChild("ops").getChildText("name");

    /* Get items (comma separated list). */
    if (changeElement.getChild("ops").hasChild("add")) {
      String items = changeElement.getChild("ops").getChild("add").getChildText("items");

      if (playlist.tracks == null) {
        playlist.tracks = new ArrayList<Track>();
      }


      /* Add track items. */
      int position = 0;
      String[] split = items.split(",");

      List<Track> tracks = new ArrayList<Track>(split.length);


      for (String trackData : split) {
        trackData = trackData.trim();
        final String trackHexUUID;
        if (trackData.length() != 34) {
          if (SpotifyURI.isHex(trackData)) {
            // not sure why playlist UUID is send sometimes. notice it is lacking UUID prefix byte
            if (!trackData.equals(playlist.getId())) {
              throw new DespotifyException("32 byte hex UUID does not equal the playlist UUID!");
            }
            continue;
          } else {
            throw new RuntimeException(trackData + " is not a valid 32 byte hex UUID!");
          }
        } else if (trackData.length() == 34) {
          trackHexUUID = trackData.substring(0, 32);
          if (!"01".equals(trackData.substring(32, 34))) {
            throw new DespotifyException("Expected hex UUID type suffix 01, got " + trackData.substring(32, 34));
          }
        } else {
          throw new RuntimeException("track UUID was not 16+1 or 16 byte!");
        }

        Track track = store.getTrack(trackHexUUID);
        tracks.add(track);

        position++; // todo perhaps we should use this to syncronize any discrepancy
      }

      playlist.setTracks(tracks);
    }

    /* Get "version" element. */
    XMLElement versionElement = playlistElement.getChild("next-change").getChild("version");

    /* Split version string into parts. */
    String[] parts = versionElement.getText().split(",", 4);

    /* Set values. */

    String[] versionTagValues = versionElement.getText().split(",", 4);

    playlist.setRevision(Long.parseLong(versionTagValues[0]));
    playlist.setChecksum(Long.parseLong(versionTagValues[2]));
    playlist.collaborative = (Integer.parseInt(parts[3]) == 1);

View Full Code Here

    } else if (packetType == PacketType.productInformation) {
      /* Payload is uncompressed UTF8 formatted XML. */

      String xml = new String(payload, UTF8);
      XMLElement root = XML.load(xml);
      productType = ProductType.valueOf(root.getChild("product").getChild("type").getText());
      if (!allowProductType(productType)) {
        // todo more generic message
        log.error("Sorry, you need a premium account to use Despotify (this is a restriction by Spotify).\nTry setting property despotify.allowProductType = true");
        System.exit(0);
      }
View Full Code Here

    if (trackElement.hasChild("popularity")) {
      track.popularity = Float.parseFloat(trackElement.getChildText("popularity"));
    }


    XMLElement restrictionsNode = trackElement.getChild("restrictions");
    if (restrictionsNode != null) {
      RestrictedMedia.fromXMLElement(restrictionsNode, track);
    }

    if (fullyLoadedDate != null) {
View Full Code Here

          track.setAlbum(album);

        }
      }

      XMLElement restrictionsNode = albumElement.getChild("restrictions");
      if (restrictionsNode != null) {
        RestrictedMedia.fromXMLElement(restrictionsNode, album);
      }

      if (albumElement.hasChild("copyright")) {
View Full Code Here

      artist.name = artistNode.getChildText("name");
    }

    /* Set portrait. */
    if (artistNode.hasChild("portrait")) {
      XMLElement portraitNode = artistNode.getChild("portrait");
      if (!"".equals(portraitNode.getText().trim())) {
        artist.portrait = Image.fromXMLElement(portraitNode, store);
      }
    }

    /* Set popularity. */
    if (artistNode.hasChild("popularity")) {
      artist.popularity = Float.parseFloat(artistNode.getChildText("popularity"));
    }

    XMLElement biosNode = artistNode.getChild("bios");
    if (biosNode != null) {

      List<Biography> biographies = new ArrayList<Biography>();

      for (XMLElement bioNode : biosNode.getChildren()) {
        if (!"bio".equals(bioNode.getElement().getNodeName())) {
          log.warn("Unknown bios child node " + bioNode.getElement().getNodeName());
        } else {
          Biography biography = new Biography();
          biography.setText(bioNode.getChildText("text"));
          if (bioNode.hasChild("portraits")) {
            biography.setPortraits(new ArrayList<Image>());
            for (XMLElement portraitNode : bioNode.getChild("portraits").getChildren()) {
              biography.getPortraits().add(Image.fromXMLElement(portraitNode, store));
            }
          }
          biographies.add(biography);
        }
        artist.biographies = biographies;
      }
    }

    if (artistNode.hasChild("years-active")) {
      artist.yearsActive = new ArrayList<String>(Arrays.asList(artistNode.getChildText("years-active").split(",")));
    }

    if (artistNode.hasChild("genres")) {
      String[] genreIds = artistNode.getChildText("genres").split(",");
      Set<String> genres = new LinkedHashSet<String>(genreIds.length);
      for (String genre : genreIds) {
        if (!"".equals(genre)) {
          genres.add(genre);
        }
      }
      artist.genres = genres;
    }

    XMLElement albumsNode = artistNode.getChild("albums");
    if (albumsNode != null) {
      List<Album> allAlbumsWithTrackPresent = new ArrayList<Album>();
      for (XMLElement albumNode : albumsNode.getChildren()) {
        Album album = Album.fromXMLElement(albumNode, store, fullyLoadedDate);

        // add to all albums
        allAlbumsWithTrackPresent.add(album);

View Full Code Here

          tracks.add(track);
          track.setAlbum(album);
        }
      }

      XMLElement restrictionsNode = albumElement.getChild("restrictions");
      if (restrictionsNode != null) {
        RestrictedMedia.fromXMLElement(restrictionsNode, album);
      }

      if (albumElement.hasChild("copyright")) {
View Full Code Here

      artist.name = artistNode.getChildText("name");
    }

    /* Set portrait. */
    if (artistNode.hasChild("portrait")) {
      XMLElement portraitNode = artistNode.getChild("portrait");
      if (!"".equals(portraitNode.getText().trim())) {
        artist.portrait = Image.fromXMLElement(portraitNode, store);
      }
    }

    /* Set popularity. */
    if (artistNode.hasChild("popularity")) {
      artist.popularity = Float.parseFloat(artistNode.getChildText("popularity"));
    }

    XMLElement biosNode = artistNode.getChild("bios");
    if (biosNode != null) {

      List<Biography> biographies = new ArrayList<Biography>();

      for (XMLElement bioNode : biosNode.getChildren()) {
        if (!"bio".equals(bioNode.getElement().getNodeName())) {
          log.warn("Unknown bios child node " + bioNode.getElement().getNodeName());
        } else {
          Biography biography = new Biography();
          biography.setText(bioNode.getChildText("text"));
          if (bioNode.hasChild("portraits")) {
            biography.setPortraits(new ArrayList<Image>());
            for (XMLElement portraitNode : bioNode.getChild("portraits").getChildren()) {
              biography.getPortraits().add(Image.fromXMLElement(portraitNode, store));
            }
          }
          biographies.add(biography);
        }
        artist.biographies = biographies;
      }
    }

    if (artistNode.hasChild("years-active")) {
      artist.yearsActive = new ArrayList<String>(Arrays.asList(artistNode.getChildText("years-active").split(",")));
    }

    if (artistNode.hasChild("genres")) {
      String[] genreIds = artistNode.getChildText("genres").split(",");
      Set<String> genres = new LinkedHashSet<String>(genreIds.length);
      for(String genre : genreIds) {
        if (!"".equals(genre)) {
          genres.add(genre);
        }
      }     
      artist.genres = genres;
    }

    XMLElement albumsNode = artistNode.getChild("albums");
    if (albumsNode != null) {
      List<Album> albums = new ArrayList<Album>();
      for (XMLElement albumNode : albumsNode.getChildren()) {
        albums.add(Album.fromXMLElement(albumNode, store));
      }
      artist.albums = albums;
    }

View Full Code Here

    if (trackElement.hasChild("popularity")) {
      track.popularity = Float.parseFloat(trackElement.getChildText("popularity"));
    }


    XMLElement restrictionsNode = trackElement.getChild("restrictions");
    if (restrictionsNode != null) {
      RestrictedMedia.fromXMLElement(restrictionsNode, track);
    }

    return track;
View Full Code Here

TOP

Related Classes of se.despotify.util.XMLElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.