Program: 47105
Project: 47105
Buggy source:
/** 
 * This method populates the supplied   {@link OutputParams} object with the correct subtitles (sid)based on the given filename, its MediaInfo metadata and PMS configuration settings. TODO: Rewrite this crazy method to be more concise and logical.
 * @param fileName The file name used to determine the availability of subtitles.
 * @param media The MediaInfo metadata for the file.
 * @param params The parameters to populate.
 */
public static void setSubtitleOutputParameters(String fileName,DLNAMediaInfo media,OutputParams params){
  PmsConfiguration configuration=PMS.getConfiguration(params);
  String currentLang=null;
  DLNAMediaSubtitle matchedSub=null;
  if (params.aid != null) {
    currentLang=params.aid.getLang();
  }
  if (params.sid != null && params.sid.getId() == -1) {
    LOGGER.trace("String_Node_Str");
    params.sid=null;
    return;
  }
  if (params.sid != null && !StringUtils.isEmpty(params.sid.getLiveSubURL())) {
    LOGGER.debug("String_Node_Str" + params.sid.getLiveSubURL());
    try {
      matchedSub=params.sid;
      String file=OpenSubtitle.fetchSubs(matchedSub.getLiveSubURL(),matchedSub.getLiveSubFile());
      if (!StringUtils.isEmpty(file)) {
        matchedSub.setExternalFile(new File(file));
        params.sid=matchedSub;
        return;
      }
    }
 catch (    IOException e) {
    }
  }
  StringTokenizer st=new StringTokenizer(configuration.getAudioSubLanguages(),"String_Node_Str");
  boolean matchedInternalSubtitles=false;
  boolean matchedExternalSubtitles=false;
  while (st.hasMoreTokens()) {
    String pair=st.nextToken();
    if (pair.contains("String_Node_Str")) {
      String audio=pair.substring(0,pair.indexOf(','));
      String sub=pair.substring(pair.indexOf(',') + 1);
      audio=audio.trim();
      sub=sub.trim();
      LOGGER.trace("String_Node_Str" + currentLang + "String_Node_Str"+ audio+ "String_Node_Str"+ sub);
      if (Iso639.isCodesMatching(audio,currentLang) || (currentLang != null && audio.equals("String_Node_Str"))) {
        if (sub.equals("String_Node_Str")) {
          if (configuration.isForceExternalSubtitles()) {
            for (            DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
              if (present_sub.getExternalFile() != null) {
                matchedSub=present_sub;
                matchedExternalSubtitles=true;
                LOGGER.trace("String_Node_Str");
                break;
              }
            }
          }
          if (!matchedExternalSubtitles) {
            matchedSub=new DLNAMediaSubtitle();
            matchedSub.setLang("String_Node_Str");
          }
        }
 else {
          for (          DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
            if (present_sub.matchCode(sub) || sub.equals("String_Node_Str")) {
              if (present_sub.getExternalFile() != null) {
                if (configuration.isAutoloadExternalSubtitles()) {
                  matchedSub=present_sub;
                  LOGGER.trace("String_Node_Str" + matchedSub);
                  break;
                }
 else {
                  LOGGER.trace("String_Node_Str" + present_sub);
                }
              }
 else               if (!matchedInternalSubtitles) {
                matchedSub=present_sub;
                LOGGER.trace("String_Node_Str" + matchedSub);
                if (configuration.isAutoloadExternalSubtitles()) {
                  matchedInternalSubtitles=true;
                }
 else {
                  break;
                }
              }
            }
          }
        }
        if (matchedSub != null && !matchedInternalSubtitles) {
          break;
        }
      }
    }
  }
  if (matchedSub == null && configuration.isForceExternalSubtitles()) {
    for (    DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
      if (present_sub.getExternalFile() != null) {
        matchedSub=present_sub;
        LOGGER.trace("String_Node_Str" + matchedSub);
        break;
      }
    }
  }
  if (matchedSub != null && params.sid == null) {
    if (configuration.isDisableSubtitles() || (matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str"))) {
      LOGGER.trace("String_Node_Str" + matchedSub);
    }
 else {
      params.sid=matchedSub;
    }
  }
  if (!configuration.isDisableSubtitles() && params.sid == null && media != null) {
    File video=new File(fileName);
    FileUtil.isSubtitlesExists(video,media,false);
    if (configuration.isAutoloadExternalSubtitles()) {
      boolean forcedSubsFound=false;
      for (      DLNAMediaSubtitle sub : media.getSubtitleTracksList()) {
        if (matchedSub != null && matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str")) {
          st=new StringTokenizer(configuration.getForcedSubtitleTags(),"String_Node_Str");
          while (sub.getSubtitlesTrackTitleFromMetadata() != null && st.hasMoreTokens()) {
            String forcedTags=st.nextToken();
            forcedTags=forcedTags.trim();
            if (sub.getSubtitlesTrackTitleFromMetadata().toLowerCase().contains(forcedTags) && Iso639.isCodesMatching(sub.getLang(),configuration.getForcedSubtitleLanguage())) {
              LOGGER.trace("String_Node_Str" + sub.getLang() + "String_Node_Str"+ sub.getSubtitlesTrackTitleFromMetadata());
              LOGGER.trace("String_Node_Str" + sub);
              if (sub.getExternalFile() != null) {
                LOGGER.trace("String_Node_Str" + sub.getExternalFile().getAbsolutePath());
              }
              params.sid=sub;
              forcedSubsFound=true;
              break;
            }
          }
          if (forcedSubsFound == true) {
            break;
          }
        }
 else {
          LOGGER.trace("String_Node_Str" + sub);
          if (sub.getExternalFile() != null) {
            LOGGER.trace("String_Node_Str" + sub.getExternalFile().getAbsolutePath());
            params.sid=sub;
            break;
          }
        }
      }
    }
    if (matchedSub != null && matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str")) {
      return;
    }
    if (params.sid == null) {
      st=new StringTokenizer(UMSUtils.getLangList(params.mediaRenderer),"String_Node_Str");
      while (st.hasMoreTokens()) {
        String lang=st.nextToken();
        lang=lang.trim();
        LOGGER.trace("String_Node_Str" + lang);
        for (        DLNAMediaSubtitle sub : media.getSubtitleTracksList()) {
          if (sub.matchCode(lang) && !(!configuration.isAutoloadExternalSubtitles() && sub.getExternalFile() != null)) {
            params.sid=sub;
            LOGGER.trace("String_Node_Str" + params.sid);
            return;
          }
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * This method populates the supplied   {@link OutputParams} object with the correct subtitles (sid)based on the given filename, its MediaInfo metadata and PMS configuration settings. TODO: Rewrite this crazy method to be more concise and logical.
 * @param fileName The file name used to determine the availability of subtitles.
 * @param media The MediaInfo metadata for the file.
 * @param params The parameters to populate.
 */
public static void setSubtitleOutputParameters(String fileName,DLNAMediaInfo media,OutputParams params){
  PmsConfiguration configuration=PMS.getConfiguration(params);
  String currentLang=null;
  DLNAMediaSubtitle matchedSub=null;
  if (params.aid != null) {
    currentLang=params.aid.getLang();
  }
  if (params.sid != null && params.sid.getId() == -1) {
    LOGGER.trace("String_Node_Str");
    params.sid=null;
    return;
  }
  if (params.sid != null && !StringUtils.isEmpty(params.sid.getLiveSubURL())) {
    LOGGER.debug("String_Node_Str" + params.sid.getLiveSubURL());
    try {
      matchedSub=params.sid;
      String file=OpenSubtitle.fetchSubs(matchedSub.getLiveSubURL(),matchedSub.getLiveSubFile());
      if (!StringUtils.isEmpty(file)) {
        matchedSub.setExternalFile(new File(file),null);
        params.sid=matchedSub;
        return;
      }
    }
 catch (    IOException e) {
    }
  }
  StringTokenizer st=new StringTokenizer(configuration.getAudioSubLanguages(),"String_Node_Str");
  boolean matchedInternalSubtitles=false;
  boolean matchedExternalSubtitles=false;
  while (st.hasMoreTokens()) {
    String pair=st.nextToken();
    if (pair.contains("String_Node_Str")) {
      String audio=pair.substring(0,pair.indexOf(','));
      String sub=pair.substring(pair.indexOf(',') + 1);
      audio=audio.trim();
      sub=sub.trim();
      LOGGER.trace("String_Node_Str" + currentLang + "String_Node_Str"+ audio+ "String_Node_Str"+ sub);
      if (Iso639.isCodesMatching(audio,currentLang) || (currentLang != null && audio.equals("String_Node_Str"))) {
        if (sub.equals("String_Node_Str")) {
          if (configuration.isForceExternalSubtitles()) {
            for (            DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
              if (present_sub.getExternalFile() != null) {
                matchedSub=present_sub;
                matchedExternalSubtitles=true;
                LOGGER.trace("String_Node_Str");
                break;
              }
            }
          }
          if (!matchedExternalSubtitles) {
            matchedSub=new DLNAMediaSubtitle();
            matchedSub.setLang("String_Node_Str");
          }
        }
 else {
          for (          DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
            if (present_sub.matchCode(sub) || sub.equals("String_Node_Str")) {
              if (present_sub.getExternalFile() != null) {
                if (configuration.isAutoloadExternalSubtitles()) {
                  matchedSub=present_sub;
                  LOGGER.trace("String_Node_Str" + matchedSub);
                  break;
                }
 else {
                  LOGGER.trace("String_Node_Str" + present_sub);
                }
              }
 else               if (!matchedInternalSubtitles) {
                matchedSub=present_sub;
                LOGGER.trace("String_Node_Str" + matchedSub);
                if (configuration.isAutoloadExternalSubtitles()) {
                  matchedInternalSubtitles=true;
                }
 else {
                  break;
                }
              }
            }
          }
        }
        if (matchedSub != null && !matchedInternalSubtitles) {
          break;
        }
      }
    }
  }
  if (matchedSub == null && configuration.isForceExternalSubtitles()) {
    for (    DLNAMediaSubtitle present_sub : media.getSubtitleTracksList()) {
      if (present_sub.getExternalFile() != null) {
        matchedSub=present_sub;
        LOGGER.trace("String_Node_Str" + matchedSub);
        break;
      }
    }
  }
  if (matchedSub != null && params.sid == null) {
    if (configuration.isDisableSubtitles() || (matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str"))) {
      LOGGER.trace("String_Node_Str" + matchedSub);
    }
 else {
      params.sid=matchedSub;
    }
  }
  if (!configuration.isDisableSubtitles() && params.sid == null && media != null) {
    File video=new File(fileName);
    FileUtil.isSubtitlesExists(video,media,false);
    if (configuration.isAutoloadExternalSubtitles()) {
      boolean forcedSubsFound=false;
      for (      DLNAMediaSubtitle sub : media.getSubtitleTracksList()) {
        if (matchedSub != null && matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str")) {
          st=new StringTokenizer(configuration.getForcedSubtitleTags(),"String_Node_Str");
          while (sub.getSubtitlesTrackTitleFromMetadata() != null && st.hasMoreTokens()) {
            String forcedTags=st.nextToken();
            forcedTags=forcedTags.trim();
            if (sub.getSubtitlesTrackTitleFromMetadata().toLowerCase().contains(forcedTags) && Iso639.isCodesMatching(sub.getLang(),configuration.getForcedSubtitleLanguage())) {
              LOGGER.trace("String_Node_Str" + sub.getLang() + "String_Node_Str"+ sub.getSubtitlesTrackTitleFromMetadata());
              LOGGER.trace("String_Node_Str" + sub);
              if (sub.getExternalFile() != null) {
                LOGGER.trace("String_Node_Str" + sub.getExternalFile().getAbsolutePath());
              }
              params.sid=sub;
              forcedSubsFound=true;
              break;
            }
          }
          if (forcedSubsFound == true) {
            break;
          }
        }
 else {
          LOGGER.trace("String_Node_Str" + sub);
          if (sub.getExternalFile() != null) {
            LOGGER.trace("String_Node_Str" + sub.getExternalFile().getAbsolutePath());
            params.sid=sub;
            break;
          }
        }
      }
    }
    if (matchedSub != null && matchedSub.getLang() != null && matchedSub.getLang().equals("String_Node_Str")) {
      return;
    }
    if (params.sid == null) {
      st=new StringTokenizer(UMSUtils.getLangList(params.mediaRenderer),"String_Node_Str");
      while (st.hasMoreTokens()) {
        String lang=st.nextToken();
        lang=lang.trim();
        LOGGER.trace("String_Node_Str" + lang);
        for (        DLNAMediaSubtitle sub : media.getSubtitleTracksList()) {
          if (sub.matchCode(lang) && !(!configuration.isAutoloadExternalSubtitles() && sub.getExternalFile() != null)) {
            params.sid=sub;
            LOGGER.trace("String_Node_Str" + params.sid);
            return;
          }
        }
      }
    }
  }
}