net.sf.okapi.lib.translation

## Class QueryUtil

- java.lang.Object
- - net.sf.okapi.lib.translation.QueryUtil

-     public class QueryUtil
      extends java.lang.Object

  Collection of helper method for preparing and querying translation resources.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `QueryUtil()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `TextFragment` | `createNewFragmentWithCodes(java.lang.String plainText)` Appends the codes stored apart using `separateCodesFromText(TextFragment)` at the end of a given plain text.
    `java.lang.String` | `fromCodedHTML(java.lang.String htmlText, TextFragment fragment, boolean addMissingCodes)` Converts back a coded HTML to a coded text.
    `java.lang.String` | `fromCodedHTML(java.lang.String htmlText, TextFragment fragment, boolean addMissingCodes, boolean removeSpans)` Converts back a coded HTML to a coded text.
    `TextFragment` | `fromCodedHTMLToFragment(java.lang.String text, TextFragment fragment)` Converts an HTML string created with `toCodedHTML(TextFragment)` back into a text fragment, but with empty inline codes.
    `java.lang.String` | `fromPlainTextHTML(java.lang.String text)` Converts an HTML plain text into a plain text one.
    `TextFragment` | `fromXLIFF(org.w3c.dom.Element elem, TextFragment original)` Converts back an XLIFF text contained in a given element into a TextFragment.
    `boolean` | `hasCode()` Indicates if the last text fragment passed to `separateCodesFromText(TextFragment)` has codes or not.
    `static java.util.ArrayList<QueryResult>` | `removeDuplicates(java.util.List<QueryResult> queryResults)` Removes duplicates based on the Equals method of `QueryResult`.
    `java.lang.String` | `separateCodesFromText(TextFragment frag)` Separate and store codes of a given text fragment.
    `java.lang.String` | `toCodedHTML(TextFragment fragment)` Converts from coded text to coded HTML.
    `java.lang.String` | `toXLIFF(TextFragment fragment)` Converts from coded text to XLIFF.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### QueryUtil

          public QueryUtil()

  - ### Method Detail

    - #### hasCode

          public boolean hasCode()

      Indicates if the last text fragment passed to `separateCodesFromText(TextFragment)` has codes or not.
      Returns:
        true if the fragment has one or more code, false if it does not.

    - #### separateCodesFromText

          public java.lang.String separateCodesFromText(TextFragment frag)

      Separate and store codes of a given text fragment.
      Parameters:
        `frag` - the fragment to process. Use `createNewFragmentWithCodes(String)` to reconstruct the text back with its codes at the end.
      Returns:
        the fragment content stripped of its codes.

    - #### createNewFragmentWithCodes

          public TextFragment createNewFragmentWithCodes(java.lang.String plainText)

      Appends the codes stored apart using `separateCodesFromText(TextFragment)` at the end of a given plain text. The text fragment provided must be the same and without code modifications, as the one used for the splitting.
      Parameters:
        `plainText` - new text to use (must be plain)
      Returns:
        the provided fragment, but with the new text and the original codes appended at the end.

    - #### toCodedHTML

          public java.lang.String toCodedHTML(TextFragment fragment)

      Converts from coded text to coded HTML. The resulting string is also valid XML.
      Parameters:
        `fragment` - the fragment to convert.
      Returns:
        The resulting HTML string.

    - #### fromCodedHTMLToFragment

          public TextFragment fromCodedHTMLToFragment(java.lang.String text,
                                             TextFragment fragment)

      Converts an HTML string created with `toCodedHTML(TextFragment)` back into a text fragment, but with empty inline codes.
      Parameters:
        `text` - the HTML string to convert.
        `fragment` - an optional text fragment where to place the converted content. Any existing codes will be replaced or removed by the codes coming from the HTML string. Use null to create a new fragment.
      Returns:
        the modified or created text fragment.

    - #### fromCodedHTML

          public java.lang.String fromCodedHTML(java.lang.String htmlText,
                                       TextFragment fragment,
                                       boolean addMissingCodes)

      Converts back a coded HTML to a coded text. (extra span elements are removed).
      Parameters:
        `htmlText` - the coded HTML to convert back.
        `fragment` - the original text fragment.
        `addMissingCodes` - true to added codes that are in the original fragment but not in the HTML string.
      Returns:
        the coded text with its code markers.

    - #### fromCodedHTML

          public java.lang.String fromCodedHTML(java.lang.String htmlText,
                                       TextFragment fragment,
                                       boolean addMissingCodes,
                                       boolean removeSpans)

      Converts back a coded HTML to a coded text.
      Parameters:
        `htmlText` - the coded HTML to convert back.
        `fragment` - the original text fragment.
        `addMissingCodes` - true to added codes that are in the original fragment but not in the HTML string.
        `removeSpans` - true to remove extra span HTML codes.
      Returns:
        the coded text with its code markers.

    - #### fromPlainTextHTML

          public java.lang.String fromPlainTextHTML(java.lang.String text)

      Converts an HTML plain text into a plain text one.

      This method assumes there is no elements in the text, just escaped characters. The methods supported NCRs as well as HTML CERs.

      Parameters:
        `text` - the HTML string to convert.
      Returns:
        the un-escaped text.

    - #### toXLIFF

          public java.lang.String toXLIFF(TextFragment fragment)

      Converts from coded text to XLIFF.
      Parameters:
        `fragment` - the fragment to convert.
      Returns:
        The resulting XLIFF string.
      See Also:
        `fromXLIFF(Element, TextFragment)`

    - #### fromXLIFF

          public TextFragment fromXLIFF(org.w3c.dom.Element elem,
                               TextFragment original)

      Converts back an XLIFF text contained in a given element into a TextFragment.
      Parameters:
        `elem` - The element containing the XLIFF data.
        `original` - the original TextFragment (cannot be null).
      Returns:
        the newly created text fragment.
      See Also:
        `toXLIFF(TextFragment)`

    - #### removeDuplicates

          public static java.util.ArrayList<QueryResult> removeDuplicates(java.util.List<QueryResult> queryResults)

      Removes duplicates based on the Equals method of `QueryResult`. Preserve the highest ranked query results. For example, duplicate results with the newest creation date are always retained.
      WARNING: order is not preserved!
      Parameters:
        `queryResults` - the list of QueryResults to process.
      Returns:
        a new list of the QueryResults without duplicates.