<commit_before>package org.lodder.subtools.multisubdownloader.lib.library;

import java.io.File;

import org.lodder.subtools.multisubdownloader.settings.model.LibrarySettings;
import org.lodder.subtools.sublibrary.JTheTVDBAdapter;
import org.lodder.subtools.sublibrary.model.TvRelease;
import org.lodder.subtools.sublibrary.model.MovieRelease;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.VideoType;
import org.lodder.subtools.sublibrary.util.StringUtils;

public class PathLibraryBuilder extends LibraryBuilder {

  public PathLibraryBuilder(LibrarySettings librarySettings) {
    super(librarySettings);
  }

  public String build(Release release) {
    if (librarySettings.getLibraryAction().equals(LibraryActionType.MOVE)
        || librarySettings.getLibraryAction().equals(LibraryActionType.MOVEANDRENAME)) {
      String folder = "";
      if (release.getVideoType() == VideoType.EPISODE)
        folder = buildEpisode((TvRelease) release);
      else if (release.getVideoType() == VideoType.MOVIE)
        folder = buildMovie((MovieRelease) release);
      return new File(librarySettings.getLibraryFolder(), folder).toString();
    } else {
      return release.getPath().toString();
    }
  }

  protected String buildEpisode(TvRelease tvRelease) {
    String folder = librarySettings.getLibraryFolderStructure();
    String show = "";
    if (librarySettings.isLibraryUseTVDBNaming()) {
      final JTheTVDBAdapter jtvdb = JTheTVDBAdapter.getAdapter();
      show = jtvdb.getSerie(tvRelease).getSerieName();
    } else {
      show = tvRelease.getShow();
    }
    if (librarySettings.isLibraryReplaceChars()) {
      show = StringUtils.removeIllegalWindowsChars(show);
    }

    folder = folder.replaceAll("%SHOW NAME%", show);
    // order is important!
    folder = replaceFormatedEpisodeNumber(folder, "%EEX%", tvRelease.getEpisodeNumbers(), true);
    folder = replaceFormatedEpisodeNumber(folder, "%EX%", tvRelease.getEpisodeNumbers(), false);
    folder = folder.replaceAll("%SS%", formatedNumber(tvRelease.getSeason(), true));
    folder = folder.replaceAll("%S%", formatedNumber(tvRelease.getSeason(), false));
    folder = folder.replaceAll("%EE%", formatedNumber(tvRelease.getEpisodeNumbers().get(0), true));
    folder = folder.replaceAll("%E%", formatedNumber(tvRelease.getEpisodeNumbers().get(0), false));
    folder = folder.replaceAll("%TITLE%", tvRelease.getTitle());
    try {
      folder = folder.replaceAll("%SEPARATOR%", File.separator);
    } catch (IndexOutOfBoundsException | IllegalArgumentException ioobe) // windows hack needs "\\" instead of "\"
    {
      folder = folder.replaceAll("%SEPARATOR%", File.separator + File.separator);
    }
    folder = folder.replaceAll("%QUALITY%", tvRelease.getQuality());

    if (librarySettings.isLibraryFolderReplaceSpace()) {
      folder = folder.replaceAll(" ", librarySettings.getLibraryFolderReplacingSpaceSign());
    }

    return folder;
  }

  protected String buildMovie(MovieRelease movieRelease) {
    String folder = librarySettings.getLibraryFolderStructure();
    String title = movieRelease.getTitle();<commit_msg>Fix null pointer<commit_after>package org.lodder.subtools.multisubdownloader.lib.library;

import java.io.File;

import org.lodder.subtools.multisubdownloader.settings.model.LibrarySettings;
import org.lodder.subtools.sublibrary.JTheTVDBAdapter;
import org.lodder.subtools.sublibrary.data.thetvdb.model.TheTVDBSerie;
import org.lodder.subtools.sublibrary.model.TvRelease;
import org.lodder.subtools.sublibrary.model.MovieRelease;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.VideoType;
import org.lodder.subtools.sublibrary.util.StringUtils;

public class PathLibraryBuilder extends LibraryBuilder {

  public PathLibraryBuilder(LibrarySettings librarySettings) {
    super(librarySettings);
  }

  public String build(Release release) {
    if (librarySettings.getLibraryAction().equals(LibraryActionType.MOVE)
        || librarySettings.getLibraryAction().equals(LibraryActionType.MOVEANDRENAME)) {
      String folder = "";
      if (release.getVideoType() == VideoType.EPISODE)
        folder = buildEpisode((TvRelease) release);
      else if (release.getVideoType() == VideoType.MOVIE)
        folder = buildMovie((MovieRelease) release);
      return new File(librarySettings.getLibraryFolder(), folder).toString();
    } else {
      return release.getPath().toString();
    }
  }

  protected String buildEpisode(TvRelease tvRelease) {
    String folder = librarySettings.getLibraryFolderStructure();
    String show = "";
    if (librarySettings.isLibraryUseTVDBNaming()) {
      final JTheTVDBAdapter jtvdb = JTheTVDBAdapter.getAdapter();
      TheTVDBSerie tvdbs = jtvdb.getSerie(tvRelease);
      if (tvdbs == null) {
        //use showname found for release as tvdb returns null
        show = tvRelease.getShow();
      } else {
        show = tvdbs.getSerieName();
      }
    } else {
      show = tvRelease.getShow();
    }
    if (librarySettings.isLibraryReplaceChars()) {
      show = StringUtils.removeIllegalWindowsChars(show);
    }

    folder = folder.replaceAll("%SHOW NAME%", show);
    // order is important!
    folder = replaceFormatedEpisodeNumber(folder, "%EEX%", tvRelease.getEpisodeNumbers(), true);
    folder = replaceFormatedEpisodeNumber(folder, "%EX%", tvRelease.getEpisodeNumbers(), false);
    folder = folder.replaceAll("%SS%", formatedNumber(tvRelease.getSeason(), true));
    folder = folder.replaceAll("%S%", formatedNumber(tvRelease.getSeason(), false));
    folder = folder.replaceAll("%EE%", formatedNumber(tvRelease.getEpisodeNumbers().get(0), true));
    folder = folder.replaceAll("%E%", formatedNumber(tvRelease.getEpisodeNumbers().get(0), false));
    folder = folder.replaceAll("%TITLE%", tvRelease.getTitle());
    try {
      folder = folder.replaceAll("%SEPARATOR%", File.separator);
    } catch (IndexOutOfBoundsException | IllegalArgumentException ioobe) // windows hack needs "\\"
                                                                         // instead of "\"
    {
      folder = folder.replaceAll("%SEPARATOR%", File.separator + File.separator);
    }
    folder = folder.replaceAll("%QUALITY%", tvRelease.getQuality());

    if (librarySettings.isLibraryFolderReplaceSpace()) {
      folder = folder.replaceAll(" ", librarySettings.getLibraryFolderReplacingSpaceSign());
    }

    return folder;
  }

  protected String buildMovie(MovieRelease movieRelease) {
    String folder = librarySettings.getLibraryFolderStructure();
    String title = movieRelease.getTitle();
{'fraction_non_alphanumeric': 0.12357357357357357, 'fraction_numerical': 0.0006006006006006006, 'mean_word_length': 4.303343949044586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533488', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1878, 'n_words': 327}