public static String getValue(
      ItemSelectorReturnType itemSelectorReturnType, FileEntry fileEntry, ThemeDisplay themeDisplay)
      throws Exception {

    String className = ClassUtil.getClassName(itemSelectorReturnType);

    if (className.equals(FileEntryItemSelectorReturnType.class.getName())) {
      return getFileEntryValue(fileEntry, themeDisplay);
    } else if (className.equals(URLItemSelectorReturnType.class.getName())) {
      return DLUtil.getPreviewURL(
          fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK, false, false);
    }

    return StringPool.BLANK;
  }
  protected static String getFileEntryValue(FileEntry fileEntry, ThemeDisplay themeDisplay)
      throws Exception {

    JSONObject fileEntryJSONObject = JSONFactoryUtil.createJSONObject();

    fileEntryJSONObject.put("fileEntryId", fileEntry.getFileEntryId());
    fileEntryJSONObject.put("groupId", fileEntry.getGroupId());
    fileEntryJSONObject.put("title", fileEntry.getTitle());
    fileEntryJSONObject.put("type", "document");
    fileEntryJSONObject.put(
        "url",
        DLUtil.getPreviewURL(
            fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK, false, false));
    fileEntryJSONObject.put("uuid", fileEntry.getUuid());

    return fileEntryJSONObject.toString();
  }
  private String _getOnclickMethod() {
    String imageEditorPortletId =
        PortletProviderUtil.getPortletId(Image.class.getName(), PortletProvider.Action.EDIT);

    PortletURL imageEditorURL =
        PortletURLFactoryUtil.create(_request, imageEditorPortletId, PortletRequest.RENDER_PHASE);

    imageEditorURL.setParameter("mvcRenderCommandName", "/image_editor/view");

    try {
      imageEditorURL.setWindowState(LiferayWindowState.POP_UP);
    } catch (Exception e) {
      throw new SystemException("Unable to set window state", e);
    }

    LiferayPortletResponse liferayPortletResponse = _getLiferayPortletResponse();

    PortletURL editURL = liferayPortletResponse.createActionURL();

    editURL.setParameter(
        ActionRequest.ACTION_NAME, "/document_library/edit_file_entry_with_image_editor");

    editURL.setParameter("fileEntryId", String.valueOf(_fileEntry.getFileEntryId()));

    String fileEntryPreviewURL =
        DLUtil.getPreviewURL(_fileEntry, _fileVersion, _themeDisplay, StringPool.BLANK);

    StringBundler sb = new StringBundler(10);

    sb.append(liferayPortletResponse.getNamespace());
    sb.append("editWithImageEditor('");
    sb.append(imageEditorURL.toString());
    sb.append("', '");
    sb.append(editURL.toString());
    sb.append("', '");
    sb.append(_fileEntry.getFileName());
    sb.append("', '");
    sb.append(fileEntryPreviewURL);
    sb.append("');");

    return sb.toString();
  }
  public String processAttachmentsReferences(
      long userId, KBArticle kbArticle, ZipReader zipReader, Map<String, FileEntry> fileEntriesMap)
      throws PortalException {

    Set<Integer> indexes = new TreeSet<>();

    int index = 0;

    while ((index = _html.indexOf("<img", index)) > -1) {
      indexes.add(index);

      index += 4;
    }

    if (indexes.isEmpty()) {
      return _html;
    }

    StringBundler sb = new StringBundler();

    int previousIndex = 0;

    for (int curIndex : indexes) {
      if (curIndex < 0) {
        break;
      }

      if (curIndex > previousIndex) {

        // Append text from previous position up to image tag

        String text = _html.substring(previousIndex, curIndex);

        sb.append(text);
      }

      int pos = _html.indexOf("/>", curIndex);

      if (pos < 0) {
        if (_log.isDebugEnabled()) {
          _log.debug("Expected close tag for image " + _html.substring(curIndex));
        }

        sb.append(_html.substring(curIndex));

        previousIndex = curIndex;

        break;
      }

      String text = _html.substring(curIndex, pos);

      String imageFileName = KBArticleImporterUtil.extractImageFileName(text);

      FileEntry imageFileEntry =
          KBArticleImporterUtil.addImageFileEntry(
              imageFileName, userId, kbArticle, zipReader, fileEntriesMap);

      if (imageFileEntry == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to find image source " + text);
        }

        sb.append("<img alt=\"missing image\" src=\"\" ");
      } else {
        String imageSrc = StringPool.BLANK;

        try {
          imageSrc =
              DLUtil.getPreviewURL(
                  imageFileEntry, imageFileEntry.getFileVersion(), null, StringPool.BLANK);
        } catch (PortalException pe) {
          if (_log.isWarnEnabled()) {
            _log.warn(
                "Unable to obtain image URL from file entry " + imageFileEntry.getFileEntryId());
          }
        }

        sb.append("<img alt=\"");
        sb.append(HtmlUtil.escapeAttribute(imageFileEntry.getTitle()));
        sb.append("\" src=\"");
        sb.append(imageSrc);
        sb.append("\" ");
      }

      previousIndex = pos;
    }

    if (previousIndex < _html.length()) {
      sb.append(_html.substring(previousIndex));
    }

    return sb.toString();
  }