@Override
  public String getSnippet(
      Query query,
      String field,
      String s,
      int maxNumFragments,
      int fragmentLength,
      String fragmentSuffix,
      Formatter formatter)
      throws IOException {

    QueryScorer queryScorer = new QueryScorer(query, field);

    Highlighter highlighter = new Highlighter(formatter, queryScorer);

    highlighter.setTextFragmenter(new SimpleFragmenter(fragmentLength));

    TokenStream tokenStream = getAnalyzer().tokenStream(field, new UnsyncStringReader(s));

    try {
      String snippet =
          highlighter.getBestFragments(tokenStream, s, maxNumFragments, fragmentSuffix);

      if (Validator.isNotNull(snippet)
          && !StringUtil.endsWith(snippet, fragmentSuffix)
          && !s.equals(snippet)) {

        snippet = snippet.concat(fragmentSuffix);
      }

      return snippet;
    } catch (InvalidTokenOffsetsException itoe) {
      throw new IOException(itoe);
    }
  }
  protected void validate(
      Map<Locale, String> nameMap,
      String xsl,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      byte[] smallImageBytes)
      throws PortalException, SystemException {

    if (nameMap.isEmpty()) {
      throw new TemplateNameException();
    } else if (Validator.isNull(xsl)) {
      throw new TemplateXslException();
    }

    String[] imageExtensions =
        PrefsPropsUtil.getStringArray(PropsKeys.JOURNAL_IMAGE_EXTENSIONS, StringPool.COMMA);

    if (smallImage
        && Validator.isNull(smallImageURL)
        && (smallImageFile != null)
        && (smallImageBytes != null)) {

      String smallImageName = smallImageFile.getName();

      if (smallImageName != null) {
        boolean validSmallImageExtension = false;

        for (int i = 0; i < imageExtensions.length; i++) {
          if (StringPool.STAR.equals(imageExtensions[i])
              || StringUtil.endsWith(smallImageName, imageExtensions[i])) {

            validSmallImageExtension = true;

            break;
          }
        }

        if (!validSmallImageExtension) {
          throw new TemplateSmallImageNameException(smallImageName);
        }
      }

      long smallImageMaxSize = PrefsPropsUtil.getLong(PropsKeys.JOURNAL_IMAGE_SMALL_MAX_SIZE);

      if ((smallImageMaxSize > 0)
          && ((smallImageBytes == null) || (smallImageBytes.length > smallImageMaxSize))) {

        throw new TemplateSmallImageSizeException();
      }
    }
  }
  protected void validate(
      long companyId,
      long itemId,
      String sku,
      String name,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      byte[] smallImageBytes,
      boolean mediumImage,
      String mediumImageURL,
      File mediumImageFile,
      byte[] mediumImageBytes,
      boolean largeImage,
      String largeImageURL,
      File largeImageFile,
      byte[] largeImageBytes)
      throws PortalException, SystemException {

    if (Validator.isNull(sku)) {
      throw new ItemSKUException();
    }

    ShoppingItem item = shoppingItemPersistence.fetchByC_S(companyId, sku);

    if (item != null) {
      if (itemId > 0) {
        if (item.getItemId() != itemId) {
          throw new DuplicateItemSKUException();
        }
      } else {
        throw new DuplicateItemSKUException();
      }
    }

    if (Validator.isNull(name)) {
      throw new ItemNameException();
    }

    String[] imageExtensions =
        PrefsPropsUtil.getStringArray(PropsKeys.SHOPPING_IMAGE_EXTENSIONS, StringPool.COMMA);

    // Small image

    if (smallImage
        && Validator.isNull(smallImageURL)
        && (smallImageFile != null)
        && (smallImageBytes != null)) {

      String smallImageName = smallImageFile.getName();

      if (smallImageName != null) {
        boolean validSmallImageExtension = false;

        for (int i = 0; i < imageExtensions.length; i++) {
          if (StringPool.STAR.equals(imageExtensions[i])
              || StringUtil.endsWith(smallImageName, imageExtensions[i])) {

            validSmallImageExtension = true;

            break;
          }
        }

        if (!validSmallImageExtension) {
          throw new ItemSmallImageNameException(smallImageName);
        }
      }

      long smallImageMaxSize = PrefsPropsUtil.getLong(PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);

      if ((smallImageMaxSize > 0)
          && ((smallImageBytes == null) || (smallImageBytes.length > smallImageMaxSize))) {

        throw new ItemSmallImageSizeException();
      }
    }

    // Medium image

    if (mediumImage
        && Validator.isNull(mediumImageURL)
        && (mediumImageFile != null)
        && (mediumImageBytes != null)) {

      String mediumImageName = mediumImageFile.getName();

      if (mediumImageName != null) {
        boolean validMediumImageExtension = false;

        for (int i = 0; i < imageExtensions.length; i++) {
          if (StringPool.STAR.equals(imageExtensions[i])
              || StringUtil.endsWith(mediumImageName, imageExtensions[i])) {

            validMediumImageExtension = true;

            break;
          }
        }

        if (!validMediumImageExtension) {
          throw new ItemMediumImageNameException(mediumImageName);
        }
      }

      long mediumImageMaxSize = PrefsPropsUtil.getLong(PropsKeys.SHOPPING_IMAGE_MEDIUM_MAX_SIZE);

      if ((mediumImageMaxSize > 0)
          && ((mediumImageBytes == null) || (mediumImageBytes.length > mediumImageMaxSize))) {

        throw new ItemMediumImageSizeException();
      }
    }

    // Large image

    if (!largeImage
        || Validator.isNotNull(largeImageURL)
        || (largeImageFile == null)
        || (largeImageBytes == null)) {

      return;
    }

    String largeImageName = largeImageFile.getName();

    if (largeImageName != null) {
      boolean validLargeImageExtension = false;

      for (int i = 0; i < imageExtensions.length; i++) {
        if (StringPool.STAR.equals(imageExtensions[i])
            || StringUtil.endsWith(largeImageName, imageExtensions[i])) {

          validLargeImageExtension = true;

          break;
        }
      }

      if (!validLargeImageExtension) {
        throw new ItemLargeImageNameException(largeImageName);
      }
    }

    long largeImageMaxSize = PrefsPropsUtil.getLong(PropsKeys.SHOPPING_IMAGE_LARGE_MAX_SIZE);

    if ((largeImageMaxSize > 0)
        && ((largeImageBytes == null) || (largeImageBytes.length > largeImageMaxSize))) {

      throw new ItemLargeImageSizeException();
    }
  }