public KodiScraper parseScraper(KodiScraper scraper, List<File> common) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();

    File scraperFile = new File(scraper.getFolder(), scraper.getScraperXml());
    String xmlFile = FileUtils.readFileToString(scraperFile, "UTF-8");
    xmlFile = KodiUtil.fixXmlHeader(xmlFile);

    Document xml;
    try {
      InputStream stream = new ByteArrayInputStream(xmlFile.getBytes(StandardCharsets.UTF_8));
      xml = parser.parse(stream);
    } catch (SAXException e) {
      LOGGER.warn("Error parsing " + scraperFile + " - trying fallback");
      // eg FilmAffinity.com scraper
      // replace all known entities with their unicode notation
      // this fixes the "entity 'Iacute' was referenced, but not declared" parsing problems, since
      // we do not have to add doctype entity declarations
      // might replace too much; so this is only a fallback
      for (String[] ent : EntityArrays.ISO8859_1_UNESCAPE()) {
        xmlFile = xmlFile.replace(ent[0], ent[1]);
      }
      InputStream stream = new ByteArrayInputStream(xmlFile.getBytes(StandardCharsets.UTF_8));
      xml = parser.parse(stream);
    }

    Element docEl = xml.getDocumentElement();
    NodeList nl = docEl.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
      Node n = nl.item(i);
      if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) n;
        ScraperFunction func = new ScraperFunction();
        func.setName(el.getNodeName());
        func.setClearBuffers(parseBoolean(el.getAttribute("clearbuffers"), true));
        func.setAppendBuffer(parseAppendBuffer(el.getAttribute("dest")));
        func.setDest(parseInt(el.getAttribute("dest")));
        scraper.addFunction(func);

        // functions contain regexp expressions, so let's get those.
        processRegexps(func, el);
      }
    }

    // get all common scraper functions
    readScraperFunctions(scraper, common);

    return scraper;
  }
public class HtmlEscapeHelper {

  public static final AggregateTranslator ESCAPE_HTML =
      new AggregateTranslator(
          StringEscapeUtils.ESCAPE_HTML4, new UnicodeControlCharacterToHtmlTranslator());
  public static final LookupTranslator ESCAPE_BASIC =
      new LookupTranslator(EntityArrays.BASIC_ESCAPE());

  public static String escape(final CharSequence text) {
    if (text == null) return null;
    return ESCAPE_HTML.translate(text);
  }

  public static String toHtml(final String string) {
    if (string == null) return null;
    return escape(string).replace("\n", "<br/>");
  }

  public static String toPlainText(final String string) {
    if (string == null) return null;
    return unescape(string.replace("<br/>", "\n").replaceAll("<!--.*?-->|<[^>]+>", ""));
  }

  public static String unescape(final String string) {
    if (string == null) return null;
    return StringEscapeUtils.unescapeHtml4(string);
  }

  public static String escapeBasic(CharSequence text) {
    return ESCAPE_BASIC.translate(text);
  }

  private static class UnicodeControlCharacterToHtmlTranslator extends CodePointTranslator {

    @Override
    public boolean translate(int codePoint, Writer out) throws IOException {
      if (Character.isISOControl(codePoint)) {
        out.append("&#x");
        final char[] chars = Character.toChars(codePoint);
        for (char c : chars) {
          out.append(Integer.toHexString(c));
        }
        out.append(';');
        return true;
      }
      return false;
    }
  }
}
/** Created by mariotaku on 15/1/11. */
public class TwitterContentUtils {

  public static final int TWITTER_BULK_QUERY_COUNT = 100;
  private static final Pattern PATTERN_TWITTER_STATUS_LINK =
      Pattern.compile("https?://twitter\\.com/(?:#!/)?(\\w+)/status(es)?/(\\d+)");

  public static String formatDirectMessageText(final DirectMessage message) {
    if (message == null) return null;
    final HtmlBuilder builder = new HtmlBuilder(message.getText(), false, true, true);
    TwitterContentUtils.parseEntities(builder, message);
    return builder.build();
  }

  public static String formatExpandedUserDescription(final User user) {
    if (user == null) return null;
    final String text = user.getDescription();
    if (text == null) return null;
    final HtmlBuilder builder = new HtmlBuilder(text, false, true, true);
    final UrlEntity[] urls = user.getDescriptionEntities();
    if (urls != null) {
      for (final UrlEntity url : urls) {
        final String expanded_url = url.getExpandedUrl();
        if (expanded_url != null) {
          builder.addLink(expanded_url, expanded_url, url.getStart(), url.getEnd());
        }
      }
    }
    return toPlainText(builder.build());
  }

  public static String formatStatusText(final Status status) {
    if (status == null) return null;
    final HtmlBuilder builder = new HtmlBuilder(status.getText(), false, true, true);
    TwitterContentUtils.parseEntities(builder, status);
    return builder.build();
  }

  public static String formatUserDescription(final User user) {
    if (user == null) return null;
    final String text = user.getDescription();
    if (text == null) return null;
    final HtmlBuilder builder = new HtmlBuilder(text, false, true, true);
    final UrlEntity[] urls = user.getDescriptionEntities();
    if (urls != null) {
      for (final UrlEntity url : urls) {
        final String expanded_url = url.getExpandedUrl();
        if (expanded_url != null) {
          builder.addLink(expanded_url, url.getDisplayUrl(), url.getStart(), url.getEnd());
        }
      }
    }
    return builder.build();
  }

  @NonNull
  public static String getInReplyToName(@NonNull final Status status) {
    final Status orig = status.isRetweet() ? status.getRetweetedStatus() : status;
    final long inReplyToUserId = status.getInReplyToUserId();
    final UserMentionEntity[] entities = status.getUserMentionEntities();
    if (entities == null) return orig.getInReplyToScreenName();
    for (final UserMentionEntity entity : entities) {
      if (inReplyToUserId == entity.getId()) return entity.getName();
    }
    return orig.getInReplyToScreenName();
  }

  public static boolean isOfficialKey(
      final Context context, final String consumerKey, final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null) return false;
    final String[] keySecrets =
        context.getResources().getStringArray(R.array.values_official_consumer_secret_crc32);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (final String keySecret : keySecrets) {
      if (Long.parseLong(keySecret, 16) == value) return true;
    }
    return false;
  }

  public static String getOfficialKeyName(
      final Context context, final String consumerKey, final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null) return null;
    final String[] keySecrets =
        context.getResources().getStringArray(R.array.values_official_consumer_secret_crc32);
    final String[] keyNames =
        context.getResources().getStringArray(R.array.names_official_consumer_secret);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (int i = 0, j = keySecrets.length; i < j; i++) {
      if (Long.parseLong(keySecrets[i], 16) == value) return keyNames[i];
    }
    return null;
  }

  @NonNull
  public static ConsumerKeyType getOfficialKeyType(
      final Context context, final String consumerKey, final String consumerSecret) {
    if (context == null || consumerKey == null || consumerSecret == null) {
      return ConsumerKeyType.UNKNOWN;
    }
    final String[] keySecrets =
        context.getResources().getStringArray(R.array.values_official_consumer_secret_crc32);
    final String[] keyNames =
        context.getResources().getStringArray(R.array.types_official_consumer_secret);
    final CRC32 crc32 = new CRC32();
    final byte[] consumerSecretBytes = consumerSecret.getBytes(Charset.forName("UTF-8"));
    crc32.update(consumerSecretBytes, 0, consumerSecretBytes.length);
    final long value = crc32.getValue();
    crc32.reset();
    for (int i = 0, j = keySecrets.length; i < j; i++) {
      if (Long.parseLong(keySecrets[i], 16) == value) {
        return ConsumerKeyType.parse(keyNames[i]);
      }
    }
    return ConsumerKeyType.UNKNOWN;
  }

  private static final CharSequenceTranslator UNESCAPE_TWITTER_RAW_TEXT =
      new LookupTranslator(EntityArrays.BASIC_UNESCAPE());
  private static final CharSequenceTranslator ESCAPE_TWITTER_RAW_TEXT =
      new LookupTranslator(EntityArrays.BASIC_ESCAPE());

  public static String unescapeTwitterStatusText(final CharSequence text) {
    if (text == null) return null;
    return UNESCAPE_TWITTER_RAW_TEXT.translate(text);
  }

  public static String escapeTwitterStatusText(final CharSequence text) {
    if (text == null) return null;
    return ESCAPE_TWITTER_RAW_TEXT.translate(text);
  }

  public static <T extends List<Status>> T getStatusesWithQuoteData(
      Twitter twitter, @NonNull T list) throws TwitterException {
    LongSparseMap<Status> quotes = new LongSparseMap<>();
    // Phase 1: collect all statuses contains a status link, and put it in the map
    for (Status status : list) {
      if (status.isQuote()) continue;
      final UrlEntity[] entities = status.getUrlEntities();
      if (entities == null || entities.length <= 0) continue;
      // Seems Twitter will find last status link for quote target, so we search backward
      for (int i = entities.length - 1; i >= 0; i--) {
        final Matcher m = PATTERN_TWITTER_STATUS_LINK.matcher(entities[i].getExpandedUrl());
        if (!m.matches()) continue;
        final long def = -1;
        final long quoteId = NumberUtils.toLong(m.group(3), def);
        if (quoteId > 0) {
          quotes.put(quoteId, status);
        }
        break;
      }
    }
    // Phase 2: look up quoted tweets. Each lookup can fetch up to 100 tweets, so we split quote
    // ids into batches
    final long[] quoteIds = quotes.keys();
    for (int currentBulkIdx = 0, totalLength = quoteIds.length;
        currentBulkIdx < totalLength;
        currentBulkIdx += TWITTER_BULK_QUERY_COUNT) {
      final int currentBulkCount =
          Math.min(totalLength, currentBulkIdx + TWITTER_BULK_QUERY_COUNT) - currentBulkIdx;
      final long[] ids = new long[currentBulkCount];
      System.arraycopy(quoteIds, currentBulkIdx, ids, 0, currentBulkCount);
      // Lookup quoted statuses, then set each status into original status
      for (Status quoted : twitter.lookupStatuses(ids)) {
        final Set<Status> orig = quotes.get(quoted.getId());
        // This set shouldn't be null here, add null check to make inspector happy.
        if (orig == null) continue;
        for (Status status : orig) {
          Status.setQuotedStatus(status, quoted);
        }
      }
    }
    return list;
  }

  public static String getMediaUrl(MediaEntity entity) {
    return TextUtils.isEmpty(entity.getMediaUrlHttps())
        ? entity.getMediaUrl()
        : entity.getMediaUrlHttps();
  }

  public static String getProfileImageUrl(@Nullable User user) {
    if (user == null) return null;
    return TextUtils.isEmpty(user.getProfileImageUrlHttps())
        ? user.getProfileImageUrl()
        : user.getProfileImageUrlHttps();
  }

  private static void parseEntities(final HtmlBuilder builder, final EntitySupport entities) {
    // Format media.
    final MediaEntity[] mediaEntities = entities.getMediaEntities();
    if (mediaEntities != null) {
      for (final MediaEntity mediaEntity : mediaEntities) {
        final int start = mediaEntity.getStart(), end = mediaEntity.getEnd();
        final String mediaUrl = TwitterContentUtils.getMediaUrl(mediaEntity);
        if (mediaUrl != null && start >= 0 && end >= 0) {
          builder.addLink(mediaUrl, mediaEntity.getDisplayUrl(), start, end);
        }
      }
    }
    final UrlEntity[] urlEntities = entities.getUrlEntities();
    if (urlEntities != null) {
      for (final UrlEntity urlEntity : urlEntities) {
        final int start = urlEntity.getStart(), end = urlEntity.getEnd();
        final String expandedUrl = urlEntity.getExpandedUrl();
        if (expandedUrl != null && start >= 0 && end >= 0) {
          builder.addLink(expandedUrl, urlEntity.getDisplayUrl(), start, end);
        }
      }
    }
  }

  public static boolean isFiltered(
      final SQLiteDatabase database,
      final long user_id,
      final String text_plain,
      final String text_html,
      final String source,
      final long retweeted_by_id,
      final long quotedUserId) {
    return isFiltered(
        database, user_id, text_plain, text_html, source, retweeted_by_id, quotedUserId, true);
  }

  public static boolean isFiltered(
      final SQLiteDatabase database,
      final long userId,
      final String textPlain,
      final String textHtml,
      final String source,
      final long retweetedById,
      final long quotedUserId,
      final boolean filterRts) {
    if (database == null) return false;
    if (textPlain == null && textHtml == null && userId <= 0 && source == null) return false;
    final StringBuilder builder = new StringBuilder();
    final List<String> selection_args = new ArrayList<>();
    builder.append("SELECT NULL WHERE");
    if (textPlain != null) {
      selection_args.add(textPlain);
      builder.append(
          "(SELECT 1 IN (SELECT ? LIKE '%'||"
              + Filters.Keywords.TABLE_NAME
              + "."
              + Filters.VALUE
              + "||'%' FROM "
              + Filters.Keywords.TABLE_NAME
              + "))");
    }
    if (textHtml != null) {
      if (!selection_args.isEmpty()) {
        builder.append(" OR ");
      }
      selection_args.add(textHtml);
      builder.append(
          "(SELECT 1 IN (SELECT ? LIKE '%<a href=\"%'||"
              + Filters.Links.TABLE_NAME
              + "."
              + Filters.VALUE
              + "||'%\">%' FROM "
              + Filters.Links.TABLE_NAME
              + "))");
    }
    if (userId > 0) {
      if (!selection_args.isEmpty()) {
        builder.append(" OR ");
      }
      builder
          .append("(SELECT ")
          .append(userId)
          .append(" IN (SELECT ")
          .append(Filters.Users.USER_ID)
          .append(" FROM ")
          .append(Filters.Users.TABLE_NAME)
          .append("))");
    }
    if (retweetedById > 0) {
      if (!selection_args.isEmpty()) {
        builder.append(" OR ");
      }
      builder
          .append("(SELECT ")
          .append(retweetedById)
          .append(" IN (SELECT ")
          .append(Filters.Users.USER_ID)
          .append(" FROM ")
          .append(Filters.Users.TABLE_NAME)
          .append("))");
    }
    if (quotedUserId > 0) {
      if (!selection_args.isEmpty()) {
        builder.append(" OR ");
      }
      builder
          .append("(SELECT ")
          .append(quotedUserId)
          .append(" IN (SELECT ")
          .append(Filters.Users.USER_ID)
          .append(" FROM ")
          .append(Filters.Users.TABLE_NAME)
          .append("))");
    }
    if (source != null) {
      if (!selection_args.isEmpty()) {
        builder.append(" OR ");
      }
      selection_args.add(source);
      builder.append(
          "(SELECT 1 IN (SELECT ? LIKE '%>'||"
              + Filters.Sources.TABLE_NAME
              + "."
              + Filters.VALUE
              + "||'</a>%' FROM "
              + Filters.Sources.TABLE_NAME
              + "))");
    }
    final Cursor cur =
        database.rawQuery(
            builder.toString(), selection_args.toArray(new String[selection_args.size()]));
    if (cur == null) return false;
    try {
      return cur.getCount() > 0;
    } finally {
      cur.close();
    }
  }

  public static boolean isFiltered(
      final SQLiteDatabase database, final ParcelableStatus status, final boolean filter_rts) {
    if (database == null || status == null) return false;
    return isFiltered(
        database,
        status.user_id,
        status.text_plain,
        status.text_html,
        status.source,
        status.retweeted_by_user_id,
        status.quoted_user_id,
        filter_rts);
  }

  @Nullable
  public static String getBestBannerUrl(@Nullable final String baseUrl, final int width) {
    if (baseUrl == null) return null;
    final String type = getBestBannerType(width);
    final String authority = PreviewMediaExtractor.getAuthority(baseUrl);
    return authority != null && authority.endsWith(".twimg.com") ? baseUrl + "/" + type : baseUrl;
  }

  public static String getBestBannerType(final int width) {
    if (width <= 320) return "mobile";
    else if (width <= 520) return "web";
    else if (width <= 626) return "ipad";
    else if (width <= 640) return "mobile_retina";
    else if (width <= 1040) return "web_retina";
    else return "ipad_retina";
  }
}
/**
 * Classe manager des importations massives du moteur d'importExport de silverPeas
 *
 * @author sdevolder
 */
public class RepositoriesTypeManager {

  public static final CharSequenceTranslator ESCAPE_ISO8859_1 =
      new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE());

  /**
   * Méthode métier du moteur d'importExport créant toutes les publications massives définies au
   * niveau du fichier d'import xml passé en paramètre au moteur d'importExport.
   *
   * @param userDetail - contient les informations sur l'utilisateur du moteur d'importExport
   * @param repositoriesType - objet mappé par castor contenant toutes les informations de création
   *     des publications du path défini
   * @return un objet ComponentReport contenant les informations de création des publications
   *     unitaires et nécéssaire au rapport détaillé
   */
  public void processImport(
      RepositoriesType repositoriesType,
      ImportSettings settings,
      ImportReportManager reportManager) {
    List<RepositoryType> listRep_Type = repositoriesType.getListRepositoryType();
    Iterator<RepositoryType> itListRep_Type = listRep_Type.iterator();
    AttachmentImportExport attachmentIE = new AttachmentImportExport();
    VersioningImportExport versioningIE = new VersioningImportExport(settings.getUser());
    PdcImportExport pdcIE = new PdcImportExport();

    while (itListRep_Type.hasNext()) {
      RepositoryType rep_Type = itListRep_Type.next();

      String componentId = rep_Type.getComponentId();
      int topicId = rep_Type.getTopicId();
      String sPath = rep_Type.getPath();

      // Création du rapport de repository
      MassiveReport massiveReport = new MassiveReport();
      reportManager.addMassiveReport(massiveReport, componentId);
      massiveReport.setRepositoryPath(sPath);

      ComponentInst componentInst =
          OrganisationControllerFactory.getOrganisationController().getComponentInst(componentId);
      if (componentInst == null) {
        // le composant n'existe pas
        massiveReport.setError(UnitReport.ERROR_NOT_EXISTS_COMPONENT);
      } else {
        reportManager.setComponentName(componentId, componentInst.getLabel());

        File path = new File(sPath);
        if (!path.isDirectory()) {
          // La variable path ne peut contenir qu'un dossier
          massiveReport.setError(UnitReport.ERROR_NOT_EXISTS_OR_INACCESSIBLE_DIRECTORY);
        } else {
          GEDImportExport gedIE =
              ImportExportFactory.createGEDImportExport(settings.getUser(), componentId);

          Iterator<File> itListcontenuPath = getPathContent(path);
          while (itListcontenuPath.hasNext()) {
            File file = itListcontenuPath.next();
            if (file.isFile()) {
              settings.setFolderId(String.valueOf(topicId));
              importFile(file, reportManager, massiveReport, gedIE, pdcIE, settings);
            } else if (file.isDirectory()) {
              switch (rep_Type.getMassiveTypeInt()) {
                case RepositoryType.NO_RECURSIVE:
                  // on ne fait rien
                  break;
                case RepositoryType.RECURSIVE_NOREPLICATE:
                  // traitement récursif spécifique
                  settings.setPathToImport(file.getAbsolutePath());
                  processImportRecursiveNoReplicate(
                      reportManager,
                      massiveReport,
                      gedIE,
                      attachmentIE,
                      versioningIE,
                      pdcIE,
                      settings);
                  break;
                case RepositoryType.RECURSIVE_REPLICATE:
                  try {
                    NodeDetail nodeDetail = gedIE.addSubTopicToTopic(file, topicId, massiveReport);
                    // Traitement récursif spécifique
                    settings.setPathToImport(file.getAbsolutePath());
                    settings.setFolderId(nodeDetail.getNodePK().getId());
                    processImportRecursiveReplicate(
                        reportManager, massiveReport, gedIE, pdcIE, settings);
                  } catch (ImportExportException ex) {
                    massiveReport.setError(UnitReport.ERROR_NOT_EXISTS_OR_INACCESSIBLE_DIRECTORY);
                  }
                  break;
              }
            }
          }
        }
      }
    }
  }

  private PublicationDetail importFile(
      File file,
      ImportReportManager reportManager,
      MassiveReport massiveReport,
      GEDImportExport gedIE,
      PdcImportExport pdcIE,
      ImportSettings settings) {
    SilverTrace.debug(
        "importExport",
        "RepositoriesTypeManager.importFile",
        "root.MSG_GEN_ENTER_METHOD",
        "file = " + file.getName());
    String componentId = gedIE.getCurrentComponentId();
    UserDetail userDetail = gedIE.getCurentUserDetail();
    PublicationDetail pubDetailToCreate = null;
    try {
      // Création du rapport unitaire
      UnitReport unitReport = new UnitReport();
      massiveReport.addUnitReport(unitReport);

      // Check the file size
      ResourceLocator uploadSettings =
          new ResourceLocator("org.silverpeas.util.uploads.uploadSettings", "");
      long maximumFileSize = uploadSettings.getLong("MaximumFileSize", 10485760);
      long fileSize = file.length();
      if (fileSize <= 0L) {
        unitReport.setError(UnitReport.ERROR_NOT_EXISTS_OR_INACCESSIBLE_FILE);
        reportManager.addNumberOfFilesNotImported(1);
        return pubDetailToCreate;
      } else if (fileSize > maximumFileSize) {
        unitReport.setError(UnitReport.ERROR_FILE_SIZE_EXCEEDS_LIMIT);
        reportManager.addNumberOfFilesNotImported(1);
        return pubDetailToCreate;
      }

      // On récupére les infos nécéssaires à la création de la publication
      pubDetailToCreate =
          PublicationImportExport.convertFileInfoToPublicationDetail(file, settings);
      pubDetailToCreate.setPk(new PublicationPK("unknown", "useless", componentId));
      if ((settings.isDraftUsed() && pdcIE.isClassifyingMandatory(componentId))
          || settings.isDraftUsed()) {
        pubDetailToCreate.setStatus(PublicationDetail.DRAFT);
        pubDetailToCreate.setStatusMustBeChecked(false);
      }
      SilverTrace.debug(
          "importExport",
          "RepositoriesTypeManager.importFile",
          "root.MSG_GEN_PARAM_VALUE",
          "pubDetailToCreate.status = " + pubDetailToCreate.getStatus());

      // Création de la publication
      pubDetailToCreate =
          gedIE.createPublicationForMassiveImport(unitReport, pubDetailToCreate, settings);
      unitReport.setLabel(pubDetailToCreate.getPK().getId());

      SilverTrace.debug(
          "importExport",
          "RepositoriesTypeManager.importFile",
          "root.MSG_GEN_PARAM_VALUE",
          "pubDetailToCreate created");

      if (FileUtil.isMail(file.getName())) {
        // if imported file is an e-mail, its textual content is saved in a dedicated form
        // and attached files are attached to newly created publication
        processMailContent(
            pubDetailToCreate, file, reportManager, unitReport, gedIE, settings.isVersioningUsed());
      }

      // add attachment
      SimpleDocument document;
      SimpleDocumentPK pk = new SimpleDocumentPK(null, componentId);
      if (settings.isVersioningUsed()) {
        document = new HistorisedDocument();
        document.setPublicDocument(
            settings.getVersionType() == DocumentVersion.TYPE_PUBLIC_VERSION);
      } else {
        document = new SimpleDocument();
      }
      document.setPK(pk);
      document.setFile(new SimpleAttachment());
      document.setFilename(file.getName());
      document.setSize(fileSize);
      document.getFile().setCreatedBy(userDetail.getId());
      if (settings.useFileDates()) {
        document.setCreated(pubDetailToCreate.getCreationDate());
        if (pubDetailToCreate.getUpdateDate() != null) {
          document.setUpdated(pubDetailToCreate.getUpdateDate());
        }
      } else {
        document.setCreated(new Date());
      }
      document.setForeignId(pubDetailToCreate.getPK().getId());
      document.setContentType(FileUtil.getMimeType(file.getName()));
      AttachmentServiceFactory.getAttachmentService()
          .createAttachment(document, file, pubDetailToCreate.isIndexable(), false);
      reportManager.addNumberOfFilesProcessed(1);
      reportManager.addImportedFileSize(document.getSize(), componentId);
    } catch (Exception ex) {
      massiveReport.setError(UnitReport.ERROR_ERROR);
      SilverTrace.error(
          "importExport", "RepositoriesTypeManager.importFile", "root.EX_NO_MESSAGE", ex);
    }
    return pubDetailToCreate;
  }

  private void processMailContent(
      PublicationDetail pubDetail,
      File file,
      ImportReportManager reportManager,
      UnitReport unitReport,
      GEDImportExport gedIE,
      boolean isVersioningUsed)
      throws ImportExportException {

    String componentId = gedIE.getCurrentComponentId();
    UserDetail userDetail = gedIE.getCurentUserDetail();
    MailExtractor extractor = null;
    Mail mail = null;
    try {
      extractor = Extractor.getExtractor(file);
      mail = extractor.getMail();
    } catch (Exception e) {
      SilverTrace.error(
          "importExport",
          "RepositoriesTypeManager.processMailContent",
          "importExport.EX_CANT_EXTRACT_MAIL_DATA",
          e);
    }
    if (mail != null) {
      // save mail data into dedicated form
      String content = mail.getBody();
      PublicationContentType pubContent = new PublicationContentType();
      XMLModelContentType modelContent = new XMLModelContentType("mail");
      pubContent.setXMLModelContentType(modelContent);
      List<XMLField> fields = new ArrayList<XMLField>();
      modelContent.setFields(fields);

      XMLField subject = new XMLField("subject", mail.getSubject());
      fields.add(subject);

      XMLField body = new XMLField("body", ESCAPE_ISO8859_1.translate(content));
      fields.add(body);

      XMLField date = new XMLField("date", DateUtil.getOutputDateAndHour(mail.getDate(), "fr"));
      fields.add(date);

      InternetAddress address = mail.getFrom();
      String from = "";
      if (StringUtil.isDefined(address.getPersonal())) {
        from += address.getPersonal() + " - ";
      }
      from += "<a href=\"mailto:" + address.getAddress() + "\">" + address.getAddress() + "</a>";
      XMLField fieldFROM = new XMLField("from", from);
      fields.add(fieldFROM);

      Address[] recipients = mail.getAllRecipients();
      String to = "";
      for (Address recipient : recipients) {
        InternetAddress ia = (InternetAddress) recipient;
        if (StringUtil.isDefined(ia.getPersonal())) {
          to += ia.getPersonal() + " - ";
        }
        to += "<a href=\"mailto:" + ia.getAddress() + "\">" + ia.getAddress() + "</a></br>";
      }
      XMLField fieldTO = new XMLField("to", to);
      fields.add(fieldTO);

      // save form
      gedIE.createPublicationContent(
          reportManager,
          unitReport,
          Integer.parseInt(pubDetail.getPK().getId()),
          pubContent,
          userDetail.getId(),
          null);

      // extract each file from mail...
      try {
        List<AttachmentDetail> documents = new ArrayList<AttachmentDetail>();

        List<MailAttachment> attachments = extractor.getAttachments();
        for (MailAttachment attachment : attachments) {
          if (attachment != null) {
            AttachmentDetail attDetail = new AttachmentDetail();
            AttachmentPK pk = new AttachmentPK("unknown", "useless", componentId);
            attDetail.setLogicalName(attachment.getName());
            attDetail.setPhysicalName(attachment.getPath());
            attDetail.setAuthor(userDetail.getId());
            attDetail.setSize(attachment.getSize());
            attDetail.setPK(pk);

            documents.add(attDetail);
          }
        }

        // ... and save it
        if (isVersioningUsed) {
          // versioning mode
          VersioningImportExport versioningIE = new VersioningImportExport(userDetail);
          versioningIE.importDocuments(
              pubDetail.getPK().getId(),
              componentId,
              documents,
              Integer.parseInt(userDetail.getId()),
              pubDetail.isIndexable());
        } else {
          // classic mode
          AttachmentImportExport attachmentIE = new AttachmentImportExport();
          attachmentIE.importAttachments(
              pubDetail.getPK().getId(),
              componentId,
              documents,
              userDetail.getId(),
              pubDetail.isIndexable());
        }
      } catch (Exception e) {
        SilverTrace.error(
            "importExport", "RepositoriesTypeManager.processMailContent", "root.EX_NO_MESSAGE", e);
      }
    }
  }

  /**
   * Méthode récursive appelée dans le cas de l'importation massive récursive sans création de
   * nouveau topic: toutes les publications crées le seront dans le thème passé en paramètre.
   *
   * @param massiveReport - référence sur l'objet de rapport détaillé du cas import massif
   *     permettant de le compléter quelque soit le niveau de récursivité.
   * @param userDetail - contient les informations sur l'utilisateur du moteur d'importExport.
   * @param path - dossier correspondant au niveau de récursivité auquel on se trouve.
   * @param componentId - id du composant dans le lequel l'import massif est effectué.
   * @param topicId - id du thème dans lequel seront crées les éléments, l'id passé est toujours le
   *     même dans le cas présent
   * @throws ImportExportException
   */
  public void processImportRecursiveNoReplicate(
      ImportReportManager reportManager,
      MassiveReport massiveReport,
      GEDImportExport gedIE,
      AttachmentImportExport attachmentIE,
      VersioningImportExport versioningIE,
      PdcImportExport pdcIE,
      ImportSettings settings) {
    Iterator<File> itListcontenuPath = getPathContent(new File(settings.getPathToImport()));
    while (itListcontenuPath.hasNext()) {
      File file = itListcontenuPath.next();
      if (file.isFile()) {
        importFile(file, reportManager, massiveReport, gedIE, pdcIE, settings);
      } else if (file.isDirectory()) {
        // traitement récursif spécifique
        settings.setPathToImport(file.getAbsolutePath());
        processImportRecursiveNoReplicate(
            reportManager, massiveReport, gedIE, attachmentIE, versioningIE, pdcIE, settings);
      }
    }
  }

  /**
   * Méthode récursive appelée dans le cas de l'importation massive récursive avec création de
   * nouveau topic: chaque sous dossier entrainera la création d'un topic de même nom.
   *
   * @param massiveReport - référence sur l'objet de rapport détaillé du cas import massif
   *     permettant de le compléter quelque soit le niveau de récursivité.
   * @param userDetail - contient les informations sur l'utilisateur du moteur d'importExport.
   * @param path - dossier correspondant au niveau de récursivité auquel on se trouve.
   * @param componentId - id du composant dans le lequel l'import massif est effectué.
   * @param topicId - id du thème dans lequel seront crées les éléments du niveau de récursivité
   *     auquel on se trouve.
   * @return the list of publications created by the import.
   * @throws ImportExportException
   */
  public List<PublicationDetail> processImportRecursiveReplicate(
      ImportReportManager reportManager,
      MassiveReport massiveReport,
      GEDImportExport gedIE,
      PdcImportExport pdcIE,
      ImportSettings settings)
      throws ImportExportException {
    List<PublicationDetail> publications = new ArrayList<PublicationDetail>();
    File path = new File(settings.getPathToImport());
    Iterator<File> itListcontenuPath = getPathContent(path);
    while (itListcontenuPath.hasNext()) {
      File file = itListcontenuPath.next();
      if (file.isFile()) {
        PublicationDetail publication =
            importFile(file, reportManager, massiveReport, gedIE, pdcIE, settings);
        if (publication != null) {
          publications.add(publication);
        }
      } else if (file.isDirectory()) {
        NodeDetail nodeDetail =
            gedIE.addSubTopicToTopic(file, Integer.valueOf(settings.getFolderId()), massiveReport);
        // massiveReport.addOneTopicCreated();
        // Traitement récursif spécifique
        ImportSettings recursiveSettings = settings.clone();
        recursiveSettings.setPathToImport(file.getAbsolutePath());
        recursiveSettings.setFolderId(nodeDetail.getNodePK().getId());
        publications.addAll(
            processImportRecursiveReplicate(
                reportManager, massiveReport, gedIE, pdcIE, recursiveSettings));
      }
    }
    return publications;
  }

  private Iterator<File> getPathContent(File path) {
    SilverTrace.debug(
        "importExport",
        "RepositoriesTypeManager.getPathContent",
        "root.MSG_GEN_ENTER_METHOD",
        "path = " + path.getPath());
    // Récupération du contenu du dossier
    String[] listContenuStringPath = path.list();

    // Tri alphabétique du contenu
    Arrays.sort(listContenuStringPath);

    List<File> listcontenuPath = convertListStringToListFile(listContenuStringPath, path.getPath());

    return listcontenuPath.iterator();
  }

  /**
   * Transforme la table des chaines de caractères de nom de fichier en une liste de fichiers pour
   * le chemin passé en paramètre
   *
   * @param listFileName - table des nom de fichier sous forme de chaine de caractères.
   * @param path - chemin des fichiers contenu dans les chaines de caractères.
   * @return renvoie une liste d'objets File pour les noms de fichiers passés en paramètres
   */
  private List<File> convertListStringToListFile(String[] listFileName, String path) {
    List<File> listFile = new ArrayList<File>();
    if (listFileName == null) {
      return null;
    }
    for (String aListFileName : listFileName) {
      listFile.add(new File(path + File.separator + aListFileName));
    }
    return listFile;
  }
}
/**
 * Escapes and unescapes {@code String}s for Java, Java Script, HTML and XML.
 *
 * <p>#ThreadSafe#
 *
 * @since 2.0
 * @version $Id$
 */
public class StringEscapeUtils {

  /* ESCAPE TRANSLATORS */

  /**
   * Translator object for escaping Java.
   *
   * <p>While {@link #escapeJava(String)} is the expected method of use, this object allows the Java
   * escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_JAVA =
      new LookupTranslator(
              new String[][] {
                {"\"", "\\\""},
                {"\\", "\\\\"},
              })
          .with(new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()))
          .with(JavaUnicodeEscaper.outsideOf(32, 0x7f));

  /**
   * Translator object for escaping EcmaScript/JavaScript.
   *
   * <p>While {@link #escapeEcmaScript(String)} is the expected method of use, this object allows
   * the EcmaScript escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_ECMASCRIPT =
      new AggregateTranslator(
          new LookupTranslator(
              new String[][] {
                {"'", "\\'"},
                {"\"", "\\\""},
                {"\\", "\\\\"},
                {"/", "\\/"}
              }),
          new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
          JavaUnicodeEscaper.outsideOf(32, 0x7f));

  /**
   * Translator object for escaping Json.
   *
   * <p>While {@link #escapeJson(String)} is the expected method of use, this object allows the Json
   * escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.2
   */
  public static final CharSequenceTranslator ESCAPE_JSON =
      new AggregateTranslator(
          new LookupTranslator(
              new String[][] {
                {"\"", "\\\""},
                {"\\", "\\\\"},
                {"/", "\\/"}
              }),
          new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
          JavaUnicodeEscaper.outsideOf(32, 0x7f));

  /**
   * Translator object for escaping XML.
   *
   * <p>While {@link #escapeXml(String)} is the expected method of use, this object allows the XML
   * escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_XML =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_ESCAPE()),
          new LookupTranslator(EntityArrays.APOS_ESCAPE()));

  /**
   * Translator object for escaping HTML version 3.0.
   *
   * <p>While {@link #escapeHtml3(String)} is the expected method of use, this object allows the
   * HTML escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_HTML3 =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_ESCAPE()),
          new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE()));

  /**
   * Translator object for escaping HTML version 4.0.
   *
   * <p>While {@link #escapeHtml4(String)} is the expected method of use, this object allows the
   * HTML escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_HTML4 =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_ESCAPE()),
          new LookupTranslator(EntityArrays.ISO8859_1_ESCAPE()),
          new LookupTranslator(EntityArrays.HTML40_EXTENDED_ESCAPE()));

  /**
   * Translator object for escaping individual Comma Separated Values.
   *
   * <p>While {@link #escapeCsv(String)} is the expected method of use, this object allows the CSV
   * escaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator ESCAPE_CSV = new CsvEscaper();

  // TODO: Create a parent class - 'SinglePassTranslator' ?
  //       It would handle the index checking + length returning,
  //       and could also have an optimization check method.
  static class CsvEscaper extends CharSequenceTranslator {

    private static final char CSV_DELIMITER = ',';
    private static final char CSV_QUOTE = '"';
    private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
    private static final char[] CSV_SEARCH_CHARS =
        new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};

    @Override
    public int translate(final CharSequence input, final int index, final Writer out)
        throws IOException {

      if (index != 0) {
        throw new IllegalStateException("CsvEscaper should never reach the [1] index");
      }

      if (StringUtils.containsNone(input.toString(), CSV_SEARCH_CHARS)) {
        out.write(input.toString());
      } else {
        out.write(CSV_QUOTE);
        out.write(
            StringUtils.replace(input.toString(), CSV_QUOTE_STR, CSV_QUOTE_STR + CSV_QUOTE_STR));
        out.write(CSV_QUOTE);
      }
      return input.length();
    }
  }

  /* UNESCAPE TRANSLATORS */

  /**
   * Translator object for unescaping escaped Java.
   *
   * <p>While {@link #unescapeJava(String)} is the expected method of use, this object allows the
   * Java unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  // TODO: throw "illegal character: \92" as an Exception if a \ on the end of the Java (as per the
  // compiler)?
  public static final CharSequenceTranslator UNESCAPE_JAVA =
      new AggregateTranslator(
          new OctalUnescaper(), // .between('\1', '\377'),
          new UnicodeUnescaper(),
          new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE()),
          new LookupTranslator(
              new String[][] {
                {"\\\\", "\\"},
                {"\\\"", "\""},
                {"\\'", "'"},
                {"\\", ""}
              }));

  /**
   * Translator object for unescaping escaped EcmaScript.
   *
   * <p>While {@link #unescapeEcmaScript(String)} is the expected method of use, this object allows
   * the EcmaScript unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = UNESCAPE_JAVA;

  /**
   * Translator object for unescaping escaped Json.
   *
   * <p>While {@link #unescapeJson(String)} is the expected method of use, this object allows the
   * Json unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.2
   */
  public static final CharSequenceTranslator UNESCAPE_JSON = UNESCAPE_JAVA;

  /**
   * Translator object for unescaping escaped HTML 3.0.
   *
   * <p>While {@link #unescapeHtml3(String)} is the expected method of use, this object allows the
   * HTML unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator UNESCAPE_HTML3 =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_UNESCAPE()),
          new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE()),
          new NumericEntityUnescaper());

  /**
   * Translator object for unescaping escaped HTML 4.0.
   *
   * <p>While {@link #unescapeHtml4(String)} is the expected method of use, this object allows the
   * HTML unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator UNESCAPE_HTML4 =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_UNESCAPE()),
          new LookupTranslator(EntityArrays.ISO8859_1_UNESCAPE()),
          new LookupTranslator(EntityArrays.HTML40_EXTENDED_UNESCAPE()),
          new NumericEntityUnescaper());

  /**
   * Translator object for unescaping escaped XML.
   *
   * <p>While {@link #unescapeXml(String)} is the expected method of use, this object allows the XML
   * unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator UNESCAPE_XML =
      new AggregateTranslator(
          new LookupTranslator(EntityArrays.BASIC_UNESCAPE()),
          new LookupTranslator(EntityArrays.APOS_UNESCAPE()),
          new NumericEntityUnescaper());

  /**
   * Translator object for unescaping escaped Comma Separated Value entries.
   *
   * <p>While {@link #unescapeCsv(String)} is the expected method of use, this object allows the CSV
   * unescaping functionality to be used as the foundation for a custom translator.
   *
   * @since 3.0
   */
  public static final CharSequenceTranslator UNESCAPE_CSV = new CsvUnescaper();

  static class CsvUnescaper extends CharSequenceTranslator {

    private static final char CSV_DELIMITER = ',';
    private static final char CSV_QUOTE = '"';
    private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
    private static final char[] CSV_SEARCH_CHARS =
        new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};

    @Override
    public int translate(final CharSequence input, final int index, final Writer out)
        throws IOException {

      if (index != 0) {
        throw new IllegalStateException("CsvUnescaper should never reach the [1] index");
      }

      if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
        out.write(input.toString());
        return input.length();
      }

      // strip quotes
      final String quoteless = input.subSequence(1, input.length() - 1).toString();

      if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
        // deal with escaped quotes; ie) ""
        out.write(StringUtils.replace(quoteless, CSV_QUOTE_STR + CSV_QUOTE_STR, CSV_QUOTE_STR));
      } else {
        out.write(input.toString());
      }
      return input.length();
    }
  }

  /* Helper functions */

  /**
   * {@code StringEscapeUtils} instances should NOT be constructed in standard programming.
   *
   * <p>Instead, the class should be used as:
   *
   * <pre>StringEscapeUtils.escapeJava("foo");</pre>
   *
   * <p>This constructor is public to permit tools that require a JavaBean instance to operate.
   */
  public StringEscapeUtils() {
    super();
  }

  // Java and JavaScript
  // --------------------------------------------------------------------------
  /**
   * Escapes the characters in a {@code String} using Java String rules.
   *
   * <p>Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
   *
   * <p>So a tab becomes the characters {@code '\\'} and {@code 't'}.
   *
   * <p>The only difference between Java strings and JavaScript strings is that in JavaScript, a
   * single quote and forward-slash (/) are escaped.
   *
   * <p>Example:
   *
   * <pre>
   * input string: He didn't say, "Stop!"
   * output string: He didn't say, \"Stop!\"
   * </pre>
   *
   * @param input String to escape values in, may be null
   * @return String with escaped values, {@code null} if null string input
   */
  public static final String escapeJava(final String input) {
    return ESCAPE_JAVA.translate(input);
  }

  /**
   * Escapes the characters in a {@code String} using EcmaScript String rules.
   *
   * <p>Escapes any values it finds into their EcmaScript String form. Deals correctly with quotes
   * and control-chars (tab, backslash, cr, ff, etc.)
   *
   * <p>So a tab becomes the characters {@code '\\'} and {@code 't'}.
   *
   * <p>The only difference between Java strings and EcmaScript strings is that in EcmaScript, a
   * single quote and forward-slash (/) are escaped.
   *
   * <p>Note that EcmaScript is best known by the JavaScript and ActionScript dialects.
   *
   * <p>Example:
   *
   * <pre>
   * input string: He didn't say, "Stop!"
   * output string: He didn\'t say, \"Stop!\"
   * </pre>
   *
   * @param input String to escape values in, may be null
   * @return String with escaped values, {@code null} if null string input
   * @since 3.0
   */
  public static final String escapeEcmaScript(final String input) {
    return ESCAPE_ECMASCRIPT.translate(input);
  }

  /**
   * Escapes the characters in a {@code String} using Json String rules.
   *
   * <p>Escapes any values it finds into their Json String form. Deals correctly with quotes and
   * control-chars (tab, backslash, cr, ff, etc.)
   *
   * <p>So a tab becomes the characters {@code '\\'} and {@code 't'}.
   *
   * <p>The only difference between Java strings and Json strings is that in Json, forward-slash (/)
   * is escaped.
   *
   * <p>See http://www.ietf.org/rfc/rfc4627.txt for further details.
   *
   * <p>Example:
   *
   * <pre>
   * input string: He didn't say, "Stop!"
   * output string: He didn't say, \"Stop!\"
   * </pre>
   *
   * @param input String to escape values in, may be null
   * @return String with escaped values, {@code null} if null string input
   * @since 3.2
   */
  public static final String escapeJson(final String input) {
    return ESCAPE_JSON.translate(input);
  }

  /**
   * Unescapes any Java literals found in the {@code String}. For example, it will turn a sequence
   * of {@code '\'} and {@code 'n'} into a newline character, unless the {@code '\'} is preceded by
   * another {@code '\'}.
   *
   * @param input the {@code String} to unescape, may be null
   * @return a new unescaped {@code String}, {@code null} if null string input
   */
  public static final String unescapeJava(final String input) {
    return UNESCAPE_JAVA.translate(input);
  }

  /**
   * Unescapes any EcmaScript literals found in the {@code String}.
   *
   * <p>For example, it will turn a sequence of {@code '\'} and {@code 'n'} into a newline
   * character, unless the {@code '\'} is preceded by another {@code '\'}.
   *
   * @see #unescapeJava(String)
   * @param input the {@code String} to unescape, may be null
   * @return A new unescaped {@code String}, {@code null} if null string input
   * @since 3.0
   */
  public static final String unescapeEcmaScript(final String input) {
    return UNESCAPE_ECMASCRIPT.translate(input);
  }

  /**
   * Unescapes any Json literals found in the {@code String}.
   *
   * <p>For example, it will turn a sequence of {@code '\'} and {@code 'n'} into a newline
   * character, unless the {@code '\'} is preceded by another {@code '\'}.
   *
   * @see #unescapeJava(String)
   * @param input the {@code String} to unescape, may be null
   * @return A new unescaped {@code String}, {@code null} if null string input
   * @since 3.2
   */
  public static final String unescapeJson(final String input) {
    return UNESCAPE_JSON.translate(input);
  }

  // HTML and XML
  // --------------------------------------------------------------------------
  /**
   * Escapes the characters in a {@code String} using HTML entities.
   *
   * <p>For example:
   *
   * <p><code>"bread" & "butter"</code> becomes:
   *
   * <p><code>&amp;quot;bread&amp;quot; &amp;amp; &amp;quot;butter&amp;quot;</code>.
   *
   * <p>Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used
   * apostrophe escape character (&amp;apos;) is not a legal entity and so is not supported).
   *
   * @param input the {@code String} to escape, may be null
   * @return a new escaped {@code String}, {@code null} if null string input
   * @see <a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO
   *     Entities</a>
   * @see <a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO
   *     Latin-1</a>
   * @see <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity
   *     references</a>
   * @see <a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character
   *     References</a>
   * @see <a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code
   *     positions</a>
   * @since 3.0
   */
  public static final String escapeHtml4(final String input) {
    return ESCAPE_HTML4.translate(input);
  }

  /**
   * Escapes the characters in a {@code String} using HTML entities.
   *
   * <p>Supports only the HTML 3.0 entities.
   *
   * @param input the {@code String} to escape, may be null
   * @return a new escaped {@code String}, {@code null} if null string input
   * @since 3.0
   */
  public static final String escapeHtml3(final String input) {
    return ESCAPE_HTML3.translate(input);
  }

  // -----------------------------------------------------------------------
  /**
   * Unescapes a string containing entity escapes to a string containing the actual Unicode
   * characters corresponding to the escapes. Supports HTML 4.0 entities.
   *
   * <p>For example, the string "&amp;lt;Fran&amp;ccedil;ais&amp;gt;" will become
   * "&lt;Fran&ccedil;ais&gt;"
   *
   * <p>If an entity is unrecognized, it is left alone, and inserted verbatim into the result
   * string. e.g. "&amp;gt;&amp;zzzz;x" will become "&gt;&amp;zzzz;x".
   *
   * @param input the {@code String} to unescape, may be null
   * @return a new unescaped {@code String}, {@code null} if null string input
   * @since 3.0
   */
  public static final String unescapeHtml4(final String input) {
    return UNESCAPE_HTML4.translate(input);
  }

  /**
   * Unescapes a string containing entity escapes to a string containing the actual Unicode
   * characters corresponding to the escapes. Supports only HTML 3.0 entities.
   *
   * @param input the {@code String} to unescape, may be null
   * @return a new unescaped {@code String}, {@code null} if null string input
   * @since 3.0
   */
  public static final String unescapeHtml3(final String input) {
    return UNESCAPE_HTML3.translate(input);
  }

  // -----------------------------------------------------------------------
  /**
   * Escapes the characters in a {@code String} using XML entities.
   *
   * <p>For example: <tt>"bread" & "butter"</tt> => <tt>&amp;quot;bread&amp;quot; &amp;amp;
   * &amp;quot;butter&amp;quot;</tt>.
   *
   * <p>Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs
   * or external entities.
   *
   * <p>Note that Unicode characters greater than 0x7f are as of 3.0, no longer escaped. If you
   * still wish this functionality, you can achieve it via the following: {@code
   * StringEscapeUtils.ESCAPE_XML.with( NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE) );}
   *
   * @param input the {@code String} to escape, may be null
   * @return a new escaped {@code String}, {@code null} if null string input
   * @see #unescapeXml(java.lang.String)
   */
  public static final String escapeXml(final String input) {
    return ESCAPE_XML.translate(input);
  }

  // -----------------------------------------------------------------------
  /**
   * Unescapes a string containing XML entity escapes to a string containing the actual Unicode
   * characters corresponding to the escapes.
   *
   * <p>Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs
   * or external entities.
   *
   * <p>Note that numerical \\u Unicode codes are unescaped to their respective Unicode characters.
   * This may change in future releases.
   *
   * @param input the {@code String} to unescape, may be null
   * @return a new unescaped {@code String}, {@code null} if null string input
   * @see #escapeXml(String)
   */
  public static final String unescapeXml(final String input) {
    return UNESCAPE_XML.translate(input);
  }

  // -----------------------------------------------------------------------

  /**
   * Returns a {@code String} value for a CSV column enclosed in double quotes, if required.
   *
   * <p>If the value contains a comma, newline or double quote, then the String value is returned
   * enclosed in double quotes.
   *
   * <p>Any double quote characters in the value are escaped with another double quote.
   *
   * <p>If the value does not contain a comma, newline or double quote, then the String value is
   * returned unchanged. see <a
   * href="http://en.wikipedia.org/wiki/Comma-separated_values">Wikipedia</a> and <a
   * href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
   *
   * @param input the input CSV column String, may be null
   * @return the input String, enclosed in double quotes if the value contains a comma, newline or
   *     double quote, {@code null} if null string input
   * @since 2.4
   */
  public static final String escapeCsv(final String input) {
    return ESCAPE_CSV.translate(input);
  }

  /**
   * Returns a {@code String} value for an unescaped CSV column.
   *
   * <p>If the value is enclosed in double quotes, and contains a comma, newline or double quote,
   * then quotes are removed.
   *
   * <p>Any double quote escaped characters (a pair of double quotes) are unescaped to just one
   * double quote.
   *
   * <p>If the value is not enclosed in double quotes, or is and does not contain a comma, newline
   * or double quote, then the String value is returned unchanged. see <a
   * href="http://en.wikipedia.org/wiki/Comma-separated_values">Wikipedia</a> and <a
   * href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
   *
   * @param input the input CSV column String, may be null
   * @return the input String, with enclosing double quotes removed and embedded double quotes
   *     unescaped, {@code null} if null string input
   * @since 2.4
   */
  public static final String unescapeCsv(final String input) {
    return UNESCAPE_CSV.translate(input);
  }
}