Package org.rssowl.contrib.podcast.content

Source Code of org.rssowl.contrib.podcast.content.ContentLogic

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2006 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   All rights reserved                                                    **
**                                                                          **
**   This program and the accompanying materials are made available under   **
**   the terms of the Eclipse Public License v1.0 which accompanies this    **
**   distribution, and is available at:                                     **
**   http://www.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     Christophe Bouhier - podcast plugin                         **
**                                                                          **
**  **********************************************************************  */

package org.rssowl.contrib.podcast.content;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.rssowl.contrib.podcast.player.IPlayer;
import org.rssowl.contrib.podcast.player.PlayerPluginService;

/**
* The content Logic can return a plugin application for a certain MIME type. It
* creates a map of applications and MIME types. Various convenience methods are
* available to build, query, print etc the MIME type map.
*
* @author <a href="mailto:christophe@kualasoft.com" >Christophe Bouhier </a>
* @version 1.1
*/
public class ContentLogic {

  private Map<String, List<String>> mMIMEToAppMap = new HashMap<String, List<String>>();

  private static ContentLogic sSelf;

  public static ContentLogic getInstance() {
    if (sSelf == null) {
      sSelf = new ContentLogic();
    }
    return sSelf;
  }

  public ContentLogic() {
    buildApplicationMap();
  }

  /**
   * Get a map of which MIME types, are supported with which player. This will
   * be the default setting for the application map. <br>
   *
   * MIME types could be overriden to be supported by external applications
   * instead of plugins.
   *
   */
  public void buildApplicationMap() {
    // Iterate through the MIME types and associate a player.
    // If multiple players support the MIME type, the preffered
    // player is set, otherwise any other matching player.
    for (int i = 0; i < IContent.BASE_TYPES.length; i++) {
      String lBaseType = IContent.BASE_TYPES[i];
      List<String> lSupportedMIMETypes = new ArrayList<String>();
      Collection<IPlayer> lPlayers = PlayerPluginService.getInstance().getPlayers();
      for(IPlayer lPlayer : lPlayers){
          IContent[] lPlayerTypes = lPlayer.getMIMETypes();
          if (lPlayerTypes != null) {
            for (int k = 0; k < lPlayerTypes.length; k++) {
              IContent lType = lPlayerTypes[k];
              if (lType.getName().equals(lBaseType)) {
                // A MIME type name/string is matched.
                // We will map the name to the player, depending
                // on the default player.
                lSupportedMIMETypes.add(lPlayer.getName());
              }
            }// Checked all MIME types for this player.
          }// This player doesn't have any MIME types?
      } // Checked All players.
      // Add the list of players to the MIME type only if the list
      // contains at least 1 player.
      mMIMEToAppMap.put(lBaseType, lSupportedMIMETypes);
      // Add also an entry for the variants.
    }
  }

  /**
   * Print the mapping of the mime types and the players.
   */
  public String printApplicationMap() {
    StringBuffer s = new StringBuffer();
    for (int i = 0; i < IContent.BASE_TYPES.length; i++) {
      String type = IContent.BASE_TYPES[i];
      List<String> list = mMIMEToAppMap.get(type);
      s.append("Mapping:" + type + " to:\n");
      for(String name : list ) {
        // Player pl = (Player)playerMap.get(name);
        s.append(" -" + name + "\n");
      }
    }
    return s.toString();
  }

  /**
   * Get the player based on the MIME type. If no player exists for this MIME
   * type, return no player.
   *
   * @param mime
   * @return Returns the player for a certain mimetype.
   */
  public IPlayer getPlayer(IContent pContent) {

    IPlayer lPlayer = null;
    // Sequence:
    // 1. Get the MIME type, base type if any.
    // 2. From the list players

    if (pContent == null) {
      return lPlayer;
    }

    // Get the base type for this exotic type.
    if (ContentVariants.hasVariants(pContent)) {
      if (!ContentVariants.isBaseType(pContent)) {
        pContent = ContentVariants.getBaseType(pContent);
        if (pContent == null) {
          return lPlayer;
        }
      }
    }
    List<String> lPlayerNames = mMIMEToAppMap.get(pContent.getName());
    // We have one or more players select a player from the list.
    if (lPlayerNames != null && lPlayerNames.size() > 0) {
      String name = lPlayerNames.get(0);
      PlayerPluginService.getInstance().get(name);
    }
    // This MIME type is not supported by any player.
    // Return null.
    return lPlayer;
  }

  public boolean supportsContent(String pMime, IPlayer pPlayer) {
    try {
      return supportsContent(new Content(pMime), pPlayer);
    } catch (ContentException ce) {
      return false;
    }
  }

  public boolean supportsContent(IContent pContent, IPlayer pPlayer) {

    if (ContentVariants.hasVariants(pContent)) {
      // Get the base type for this exotic type.
      if (!ContentVariants.isBaseType(pContent)) {
        pContent = ContentVariants.getBaseType(pContent);
        if (pContent == null) {
          return false;
        }
      }

    }
    IContent[] lList = pPlayer.getMIMETypes();
    if (lList != null) {
      for (int i = 0; i < lList.length; i++) {
        IContent lType = lList[i];
        if (lType.getContent().equals(pContent.getContent())) {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * @return Returns the MIME to application MAP.
   */
  public Map<String, List<String>> getPlayerMap() {
    return mMIMEToAppMap;
  }

  /**
   * Iterate through the player map and retrieve all mime types.
   *
   * @return MIMEType[] An array of mime type objects.
   */
  public IContent[] getPlayerContentArray() {
    ArrayList<IContent> lContentList = new ArrayList<IContent>();
   
    Collection<IPlayer> lPlayers = PlayerPluginService.getInstance().getPlayers();
    for (IPlayer lPlayer : lPlayers) {
      IContent[] types = lPlayer.getMIMETypes();
      for (int i = 0; i < types.length; i++) {
        if (!lContentList.contains(types[i])) {
          lContentList.add(types[i]);
        }
      }
    }
    return lContentList.toArray(null);
  }
}
TOP

Related Classes of org.rssowl.contrib.podcast.content.ContentLogic

TOP
Copyright © 2018 www.massapi.com. 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.