Class TextFragmentUtil


  • public final class TextFragmentUtil
    extends Object
    • Constructor Detail

      • TextFragmentUtil

        public TextFragmentUtil()
    • Method Detail

      • alignAndCopyCodeMetadata

        public static CodeMatches alignAndCopyCodeMetadata​(TextFragment from,
                                                           TextFragment to,
                                                           boolean addMissingCodes,
                                                           boolean forceCopy,
                                                           CodeMatchStrategy strategy)
        Update the meta fields in Code "to" that match those of "from" But only if the "to" code has empty data (@see{forceCopy}). Otherwise, keep the to code data as-is. Codes match if both Code.data and Code.tagType are the same.
        Parameters:
        from - TextFragment codes are used to match codes in "to"
        to - TextFragment that has its code id's updated to match
        addMissingCodes - indicates if codes that are in the original source but not in the new target should be automatically added at the end of the new target copy (even if they are removable) if there are references in the original source and/or empty codes in the new target.
        forceCopy - if true always overwrite the target code data. WARNING: This will overwrite any code data changes in the target and force them to be like the source This may be needed for some simplified formats that only display the tagType and id. For example: A letter-coded format like: "<g1>text<x2/></g1><b3/>".
      • logCodeMismatchErrors

        public static void logCodeMismatchErrors​(CodeMatches matches,
                                                 TextFragment from,
                                                 TextFragment to,
                                                 String textUnitId)
        Log all code mismatch issues
        Parameters:
        matches - holds matches
        from - base or original fragment (aka source)
        to - new fragment (aka target)
      • alignAndCopyCodeMetadata

        public static CodeMatches alignAndCopyCodeMetadata​(TextFragment from,
                                                           TextFragment to)
        Update the meta fields in Code "to" that match those of "from" But only if the "to" code has empty data. Otherwise keep the to code data as-is. Codes match if both Code.data and Code.tagType are the same.
        Parameters:
        from - TextFragment codes are used to match codes in "to"
        to - TextFragment that has its code id's updated to match
      • alignAndCopyCodeMetadata

        public static CodeMatches alignAndCopyCodeMetadata​(TextFragment from,
                                                           TextFragment to,
                                                           boolean addMissingCodes)
        Update the meta fields in Code "to" that match those of "from" But only if the "to" code has empty data (@see{forceCopy}). Otherwise keep the to code data as-is. Codes match if both Code.data and Code.tagType are the same.
        Parameters:
        from - TextFragment codes are used to match codes in "to"
        to - TextFragment that has its code id's updated to match
        addMissingCodes - indicates if codes that are in the original source but not in the new target should be automatically added at the end of the new target copy (even if they are removable) if there are references in the original source and/or empty codes in the new target.
      • addMissingCodes

        public static void addMissingCodes​(TextFragment from,
                                           TextFragment to,
                                           CodeMatches codeMatches)
        Add missing leading and trailing codes from source (aka "from") to target (aka 'to').
        Parameters:
        from - object which has the missing codes
        to - object where we need to add the missing codes
        codeMatches - CodeMatches shows us what is missing
      • isLeadingCode

        public static boolean isLeadingCode​(Code code,
                                            TextFragment source)
        Determine if the code is at the beginning of the TextFragment without intervening text.
        Parameters:
        code - the code we are testing.
        source - the source (aka 'from') fragment we are searching.
        Returns:
        true if the code is leading.
      • findMatch

        @SafeVarargs
        public static int findMatch​(Code orig,
                                    List<Code> codes,
                                    int[] fromMatches,
                                    Comparator<Code>... cmps)
        Find a match in codes based on the list of Comparator
        Parameters:
        orig - the code we are looking for
        codes - the code list we are looking in
        fromMatches - boolean arrays marking the fromMatches already found
        cmps - the list of Comparator
        Returns:
        the index of the match in from or -1 if already matched or nothing found
      • copyCodeMetadata

        public static void copyCodeMetadata​(Code sc,
                                            Code tc,
                                            boolean forceCopy)
      • synchronizeCodeIds

        public static CodeMatches synchronizeCodeIds​(TextFragment from,
                                                     TextFragment to,
                                                     CodeMatchStrategy strategy)
        Matches the code IDs of the from fragment with the ones of the to fragment. This method re-assigns the IDs of the in-line codes of this fragment based on the code data of the provided fragment. If there is a code with the same data, then prefer the first code as this is the matching target code in the majority of cases. An example of usage is when source and target fragments have codes generated from regular expressions and not in the same order. For example if the source is %d equals %s and the target is %s equals %d and %s and %d are codes. You want their IDs to match for the code with the same content.
        Parameters:
        from - the fragment to use as the base for the synchronization.
        to - the fragment who's id's will be adjusted.
      • moreThanMaxCodes

        public static boolean moreThanMaxCodes​(TextFragment tf)
      • toText

        public static String toText​(TextFragment tf)
        Render the TextFragment including all Codes. differs from TextFragment.toText() by also using outerData.
        Parameters:
        tf - The TextFragment to render
        Returns:
        the rendered string