Package net.sf.okapi.common.resource
Class TextFragmentUtil
- java.lang.Object
-
- net.sf.okapi.common.resource.TextFragmentUtil
-
public final class TextFragmentUtil extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static CodeComparatorOnCloseTypeCMP_CLOSE_TYPEstatic CodeComparatorOnDataCMP_DATAstatic CodeComparatorOnIdCMP_IDstatic CodeComparatorOnTagTypeCMP_TAG_TYPEstatic CodeComparatorOnTypeCMP_TYPEstatic intMAX_INLINE_CODES
-
Constructor Summary
Constructors Constructor Description TextFragmentUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddMissingCodes(TextFragment from, TextFragment to, CodeMatches codeMatches)Add missing leading and trailing codes from source (aka "from") to target (aka 'to').static CodeMatchesalignAndCopyCodeMetadata(TextFragment from, TextFragment to)Update the meta fields inCode"to" that match those of "from" But only if the "to" code has empty data.static CodeMatchesalignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes)Update the meta fields inCode"to" that match those of "from" But only if the "to" code has empty data (@see{forceCopy}).static CodeMatchesalignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes, boolean forceCopy)static CodeMatchesalignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes, boolean forceCopy, CodeMatchStrategy strategy)Update the meta fields inCode"to" that match those of "from" But only if the "to" code has empty data (@see{forceCopy}).static voidcopyCodeMetadata(Code sc, Code tc, boolean forceCopy)static voidcopyCodeMetadata(TextFragment sf, TextFragment tf, boolean forceCopy, CodeMatches cm)static intfindMatch(Code orig, List<Code> codes, int[] fromMatches, Comparator<Code>... cmps)Find a match in codes based on the list ofComparatorstatic booleanisLeadingCode(Code code, TextFragment source)Determine if the code is at the beginning of theTextFragmentwithout intervening text.static voidlogCodeMismatchErrors(CodeMatches matches, TextFragment from, TextFragment to, String textUnitId)Log all code mismatch issuesstatic booleanmoreThanMaxCodes(TextFragment tf)static TextFragmentremoveMoreThanMaxCodes(TextFragment tf)static CodeMatchessynchronizeCodeIds(TextFragment from, TextFragment to)static CodeMatchessynchronizeCodeIds(TextFragment from, TextFragment to, CodeMatchStrategy strategy)Matches the code IDs of thefromfragment with the ones of thetofragment.static StringtoText(TextFragment tf)Render theTextFragmentincluding allCodes.
-
-
-
Field Detail
-
MAX_INLINE_CODES
public static final int MAX_INLINE_CODES
- See Also:
- Constant Field Values
-
CMP_DATA
public static final CodeComparatorOnData CMP_DATA
-
CMP_ID
public static final CodeComparatorOnId CMP_ID
-
CMP_TAG_TYPE
public static final CodeComparatorOnTagType CMP_TAG_TYPE
-
CMP_TYPE
public static final CodeComparatorOnType CMP_TYPE
-
CMP_CLOSE_TYPE
public static final CodeComparatorOnCloseType CMP_CLOSE_TYPE
-
-
Method Detail
-
alignAndCopyCodeMetadata
public static CodeMatches alignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes, boolean forceCopy, CodeMatchStrategy strategy)
Update the meta fields inCode"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 bothCode.dataandCode.tagTypeare the same.- Parameters:
from-TextFragmentcodes are used to match codes in "to"to-TextFragmentthat has its code id's updated to matchaddMissingCodes- 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/>".
-
alignAndCopyCodeMetadata
public static CodeMatches alignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes, boolean forceCopy)
-
logCodeMismatchErrors
public static void logCodeMismatchErrors(CodeMatches matches, TextFragment from, TextFragment to, String textUnitId)
Log all code mismatch issues- Parameters:
matches- holds matchesfrom- base or original fragment (aka source)to- new fragment (aka target)
-
alignAndCopyCodeMetadata
public static CodeMatches alignAndCopyCodeMetadata(TextFragment from, TextFragment to)
Update the meta fields inCode"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 bothCode.dataandCode.tagTypeare the same.- Parameters:
from-TextFragmentcodes are used to match codes in "to"to-TextFragmentthat has its code id's updated to match
-
alignAndCopyCodeMetadata
public static CodeMatches alignAndCopyCodeMetadata(TextFragment from, TextFragment to, boolean addMissingCodes)
Update the meta fields inCode"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 bothCode.dataandCode.tagTypeare the same.- Parameters:
from-TextFragmentcodes are used to match codes in "to"to-TextFragmentthat has its code id's updated to matchaddMissingCodes- 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 codesto- object where we need to add the missing codescodeMatches-CodeMatchesshows us what is missing
-
isLeadingCode
public static boolean isLeadingCode(Code code, TextFragment source)
Determine if the code is at the beginning of theTextFragmentwithout 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 ofComparator- Parameters:
orig- the code we are looking forcodes- the code list we are looking infromMatches- boolean arrays marking the fromMatches already foundcmps- the list ofComparator- Returns:
- the index of the match in from or -1 if already matched or nothing found
-
copyCodeMetadata
public static void copyCodeMetadata(TextFragment sf, TextFragment tf, boolean forceCopy, CodeMatches cm)
-
synchronizeCodeIds
public static CodeMatches synchronizeCodeIds(TextFragment from, TextFragment to, CodeMatchStrategy strategy)
Matches the code IDs of thefromfragment with the ones of thetofragment. 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 %sand the target is%s equals %dand%sand%dare 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.
-
synchronizeCodeIds
public static CodeMatches synchronizeCodeIds(TextFragment from, TextFragment to)
-
moreThanMaxCodes
public static boolean moreThanMaxCodes(TextFragment tf)
-
removeMoreThanMaxCodes
public static TextFragment removeMoreThanMaxCodes(TextFragment tf)
-
toText
public static String toText(TextFragment tf)
Render theTextFragmentincluding allCodes. differs from TextFragment.toText() by also using outerData.- Parameters:
tf- The TextFragment to render- Returns:
- the rendered string
-
-