protected Element addTextElement(
      Element element, String name, String label, boolean localizable) {

    Element dynamicElement = element.addElement("dynamic-element");

    dynamicElement.addAttribute("dataType", "string");
    dynamicElement.addAttribute("localizable", String.valueOf(localizable));
    dynamicElement.addAttribute("name", name);
    dynamicElement.addAttribute("type", "text");

    Element metadataElement = dynamicElement.addElement("meta-data");

    metadataElement.addAttribute("locale", LocaleUtil.toLanguageId(LocaleUtil.US));

    Element entryElement = metadataElement.addElement("entry");

    entryElement.addAttribute("name", "label");
    entryElement.setText(label);

    return dynamicElement;
  }
  protected void format(
      String oldCompanyId,
      long newCompanyId,
      long groupId,
      String articleId,
      double version,
      Element root)
      throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
      Element el = itr.next();

      Element dynamicContent = el.element("dynamic-content");

      String elInstanceId = StringPool.BLANK;
      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);
      String elLanguage = StringPool.BLANK;

      if (dynamicContent != null) {
        elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
          elLanguage = "_" + elLanguage;
        }
      }

      if (elType.equals("image") || elType.equals("text")) {
        String oldImageId = dynamicContent.getText();

        if (oldImageId.startsWith(_IMG_ID_PATH)
            || oldImageId.startsWith("@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("http://@portal_url@" + _IMG_ID_PATH)
            || oldImageId.startsWith("https://@portal_url@" + _IMG_ID_PATH)) {

          int pos = oldImageId.indexOf(_IMG_ID_PATH);

          String preOldImageId = oldImageId.substring(0, pos);

          oldImageId = oldImageId.substring(pos + _IMG_ID_PATH.length(), oldImageId.length());

          String newImageId = getNewImageId(oldCompanyId, oldImageId);

          dynamicContent.setText(preOldImageId + _IMG_ID_PATH + newImageId);

          if (elType.equals("image")) {
            dynamicContent.addAttribute("id", newImageId);

            long articleImageId = GetterUtil.getLong(newImageId);

            JournalArticleImageLocalServiceUtil.addArticleImageId(
                articleImageId, groupId, articleId, version, elInstanceId, elName, elLanguage);
          }
        }
      }

      format(oldCompanyId, newCompanyId, groupId, articleId, version, el);
    }
  }
  protected void format(
      long groupId,
      String articleId,
      double version,
      String previewArticleId,
      Element root,
      UploadServletRequest uploadRequest)
      throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
      Element el = itr.next();

      Element dynamicContent = el.element("dynamic-content");

      String elInstanceId = el.attributeValue("instance-id", StringPool.BLANK);
      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);
      String elContent = StringPool.BLANK;
      String elLanguage = StringPool.BLANK;

      if (dynamicContent != null) {
        elContent = dynamicContent.getTextTrim();

        elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
          elLanguage = "_" + elLanguage;
        }
      }

      if (elType.equals("image") && Validator.isNull(elContent)) {
        File file = uploadRequest.getFile("structure_image_" + elName + elLanguage);
        byte[] bytes = FileUtil.getBytes(file);

        if ((bytes != null) && (bytes.length > 0)) {
          long imageId =
              JournalArticleImageLocalServiceUtil.getArticleImageId(
                  groupId, previewArticleId, version, elInstanceId, elName, elLanguage, true);

          String token = ImageServletTokenUtil.getToken(imageId);

          dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);

          ImageLocalServiceUtil.updateImage(imageId, bytes);
        } else {
          if (Validator.isNotNull(articleId)) {
            long imageId =
                JournalArticleImageLocalServiceUtil.getArticleImageId(
                    groupId, articleId, version, elInstanceId, elName, elLanguage);

            String token = ImageServletTokenUtil.getToken(imageId);

            dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);
          }
        }
      }

      format(groupId, articleId, version, previewArticleId, el, uploadRequest);
    }
  }