/**
   * Builds a list of format tags within the supplied string. Format tags are 'protected parts' and
   * OmegaT style tags: <xx02> or </yy01>.
   */
  public static void buildTagList(
      String str, ProtectedPart[] protectedParts, List<String> tagList) {
    List<TagOrder> tags = new ArrayList<TagOrder>();
    if (protectedParts != null) {
      for (ProtectedPart pp : protectedParts) {
        int pos = -1;
        if ((pos = str.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
          tags.add(new TagOrder(pos, pp.getTextInSourceSegment()));
        }
      }
    }

    if (tags.isEmpty()) {
      return;
    }
    Collections.sort(
        tags,
        new Comparator<TagOrder>() {
          @Override
          public int compare(TagOrder o1, TagOrder o2) {
            return o1.pos - o2.pos;
          }
        });
    for (TagOrder t : tags) {
      tagList.add(t.tag);
    }
  }
  /** Builds a list of all occurrences of all protected parts. */
  public static List<TagOrder> buildAllTagList(String str, ProtectedPart[] protectedParts) {
    List<TagOrder> tags = new ArrayList<TagOrder>();
    if (protectedParts != null) {
      for (ProtectedPart pp : protectedParts) {
        int pos = -1;
        do {
          if ((pos = str.indexOf(pp.getTextInSourceSegment(), pos + 1)) >= 0) {
            tags.add(new TagOrder(pos, pp.getTextInSourceSegment()));
          }
        } while (pos >= 0);
      }
    }

    if (tags.isEmpty()) {
      return Collections.emptyList();
    }
    Collections.sort(
        tags,
        new Comparator<TagOrder>() {
          @Override
          public int compare(TagOrder o1, TagOrder o2) {
            return o1.pos - o2.pos;
          }
        });
    return tags;
  }
  /**
   * Identify all the placeholders in the source text and automatically inserts them into the target
   * text.
   */
  public void editTagPainterMenuItemActionPerformed() {
    SourceTextEntry ste = Core.getEditor().getCurrentEntry();

    // insert tags
    String tr = Core.getEditor().getCurrentTranslation();
    for (ProtectedPart pp : ste.getProtectedParts()) {
      if (!tr.contains(pp.getTextInSourceSegment())) {
        Core.getEditor().insertText(pp.getTextInSourceSegment());
      }
    }
  }
  /**
   * Find some protected parts defined in Tag Validation Options dialog: printf variables, java
   * MessageFormat patterns, user defined cusom tags.
   *
   * <p>These protected parts shouldn't affect statistic but just be displayed in gray in editor and
   * take part in tag validation.
   */
  public static List<ProtectedPart> applyCustomProtectedParts(
      String source, Pattern protectedPartsPatterns, List<ProtectedPart> protectedParts) {
    List<ProtectedPart> result;
    if (protectedParts != null) {
      // Remove already define protected parts first for prevent intersection
      for (ProtectedPart pp : protectedParts) {
        source = source.replace(pp.getTextInSourceSegment(), StaticUtils.TAG_REPLACEMENT);
      }
      result = protectedParts;
    } else {
      result = new ArrayList<ProtectedPart>();
    }

    Matcher placeholderMatcher = protectedPartsPatterns.matcher(source);
    while (placeholderMatcher.find()) {
      ProtectedPart pp = new ProtectedPart();
      pp.setTextInSourceSegment(placeholderMatcher.group());
      pp.setDetailsFromSourceFile(placeholderMatcher.group());
      pp.setReplacementWordsCountCalculation(placeholderMatcher.group());
      pp.setReplacementUniquenessCalculation(placeholderMatcher.group());
      pp.setReplacementMatchCalculation(placeholderMatcher.group());
      result.add(pp);
    }
    return result;
  }
  public void editTagNextMissedMenuItemActionPerformed() {
    String trans = Core.getEditor().getCurrentTranslation();
    if (trans == null) {
      return;
    }

    SourceTextEntry ste = Core.getEditor().getCurrentEntry();

    // insert next tag
    String tr = Core.getEditor().getCurrentTranslation();
    for (ProtectedPart pp : ste.getProtectedParts()) {
      if (!tr.contains(pp.getTextInSourceSegment())) {
        Core.getEditor().insertText(pp.getTextInSourceSegment());
        break;
      }
    }
  }
Beispiel #6
0
  @Override
  public String constructShortcuts(List<Element> elements, List<ProtectedPart> protectedParts) {
    protectedParts.clear();
    // create shortcuts
    InlineTagHandler tagHandler = new InlineTagHandler();

    StringBuilder r = new StringBuilder();
    for (Element el : elements) {
      if (el instanceof XMLContentBasedTag) {
        XMLContentBasedTag tag = (XMLContentBasedTag) el;
        String shortcut = null;
        int shortcutLetter;
        int tagIndex;
        boolean tagProtected;
        if ("bpt".equals(tag.getTag())) {
          // XLIFF specification requires 'rid' and 'id' attributes,
          // but some tools uses 'i' attribute like for TMX
          tagHandler.startBPT(
              tag.getAttribute("rid"), tag.getAttribute("id"), tag.getAttribute("i"));
          shortcutLetter = calcTagShortcutLetter(tag, ignoreTypeForBptTags);
          tagHandler.setTagShortcutLetter(shortcutLetter);
          tagIndex = tagHandler.endBPT();
          shortcut =
              "<"
                  + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f')
                  + tagIndex
                  + '>';
          tagProtected = false;
        } else if ("ept".equals(tag.getTag())) {
          tagHandler.startEPT(
              tag.getAttribute("rid"), tag.getAttribute("id"), tag.getAttribute("i"));
          tagIndex = tagHandler.endEPT();
          shortcutLetter = tagHandler.getTagShortcutLetter();
          shortcut =
              "</"
                  + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f')
                  + tagIndex
                  + '>';
          tagProtected = false;
        } else if ("it".equals(tag.getTag())) {
          tagHandler.startOTHER();
          tagHandler.setCurrentPos(tag.getAttribute("pos"));
          tagIndex = tagHandler.endOTHER();
          // XLIFF specification requires 'open/close' values,
          // but some tools may use 'begin/end' values like for TMX
          shortcutLetter = calcTagShortcutLetter(tag);
          if ("close".equals(tagHandler.getCurrentPos())
              || "end".equals(tagHandler.getCurrentPos())) {
            // In some cases, even if we're able to compute a shortcut, it's better to force to "f"
            // for better compatibility with corresponding TMX files
            if (forceShortCutToF) {
              shortcutLetter = 'f';
            }
            shortcut =
                "</"
                    + (shortcutLetter != 0
                        ? String.valueOf(Character.toChars(shortcutLetter))
                        : 'f')
                    + tagIndex
                    + '>';
          } else {
            shortcut =
                "<"
                    + (shortcutLetter != 0
                        ? String.valueOf(Character.toChars(shortcutLetter))
                        : 'f')
                    + tagIndex
                    + '>';
          }
          tagProtected = false;
        } else if ("ph".equals(tag.getTag())) {
          tagHandler.startOTHER();
          tagIndex = tagHandler.endOTHER();
          shortcutLetter = calcTagShortcutLetter(tag, ignoreTypeForPhTags);
          shortcut =
              "<"
                  + (shortcutLetter != 0 ? String.valueOf(Character.toChars(shortcutLetter)) : 'f')
                  + tagIndex
                  + "/>";
          tagProtected = false;
        } else if ("mrk".equals(tag.getTag())) {
          tagHandler.startOTHER();
          tagIndex = tagHandler.endOTHER();
          shortcutLetter = 'm';
          shortcut =
              "<m"
                  + tagIndex
                  + ">"
                  + tag.getIntactContents().sourceToOriginal()
                  + "</m"
                  + tagIndex
                  + ">";
          tagProtected = true;
        } else {
          shortcutLetter = 'f';
          tagIndex = -1;
          tagProtected = false;
        }
        tag.setShortcutLetter(shortcutLetter);
        tag.setShortcutIndex(tagIndex);
        tag.setShortcut(shortcut);
        r.append(shortcut);
        ProtectedPart pp = new ProtectedPart();
        pp.setTextInSourceSegment(shortcut);
        pp.setDetailsFromSourceFile(tag.toOriginal());
        if (tagProtected) {
          // protected text with related tags, like <m0>Acme</m0>
          if (StatisticsSettings.isCountingProtectedText()) {
            // Protected texts are counted, but related tags are not counted in the word count
            pp.setReplacementWordsCountCalculation(
                StaticUtils.TAG_REPLACEMENT
                    + tag.getIntactContents().sourceToOriginal()
                    + StaticUtils.TAG_REPLACEMENT);
          } else {
            // All protected parts are not counted in the word count(default)
            pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
          }
          pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
          pp.setReplacementMatchCalculation(tag.getIntactContents().sourceToOriginal());
        } else {
          // simple tag, like <i0>
          if (StatisticsSettings.isCountingStandardTags()) {
            pp.setReplacementWordsCountCalculation(tag.toSafeCalcShortcut());
          } else {
            pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
          }
          pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
          pp.setReplacementMatchCalculation(StaticUtils.TAG_REPLACEMENT);
        }
        protectedParts.add(pp);
      } else if (el instanceof Tag) {
        Tag tag = (Tag) el;
        int tagIndex = tagHandler.paired(tag.getTag(), tag.getType());
        tag.setIndex(tagIndex);
        String shortcut = tag.toShortcut();
        r.append(shortcut);
        ProtectedPart pp = new ProtectedPart();
        pp.setTextInSourceSegment(shortcut);
        pp.setDetailsFromSourceFile(tag.toOriginal());
        if (StatisticsSettings.isCountingStandardTags()) {
          pp.setReplacementWordsCountCalculation(tag.toSafeCalcShortcut());
        } else {
          pp.setReplacementWordsCountCalculation(StaticUtils.TAG_REPLACEMENT);
        }
        pp.setReplacementUniquenessCalculation(StaticUtils.TAG_REPLACEMENT);
        pp.setReplacementMatchCalculation(StaticUtils.TAG_REPLACEMENT);
        protectedParts.add(pp);
      } else {
        r.append(el.toShortcut());
      }
    }
    return r.toString();
  }