Package net.sf.okapi.lib.xliff2
Class Util
- java.lang.Object
-
- net.sf.okapi.lib.xliff2.Util
-
public class Util extends Object
Provides various helper methods for the library.
-
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
checkValueList(String allowedValues, String value, String name)
Checks if a given value is in a given list of allowed values.static int
compareAllowingNull(String s1, String s2)
Compares two strings allowing them to be null.static List<String>
createFixedSequences(List<CTag> list, boolean errorOnMissingFirstNo)
Creates a list of sequences of code markers that must not be re-ordered from the list of all code markers in a given source or target content.static boolean
equals(Object o1, Object o2)
Checks if two objects are equal, even if either is null.static boolean
isModule(String nsUri, String moduleSuffix)
Indicates if the given namespace URI is of a given module or not.static boolean
isNoE(String string)
Checks if a string is null or empty.static boolean
isValidInXML(int cp)
Indicates if a given code point is valid in XML or not.static boolean
isValidNmtoken(String value)
Verifies if a given value is a valid NMTOKEN value.static void
removeExtensions(Object object)
Removes extension attributes and elements from a given object.static void
removeModules(Object object, String moduleSuffix)
Removes modules attributes and elements from a given object.static boolean
supports(String nsUri)
Indicates if a given module is directly supported by this version of the library.static String
toSafeXML(String text)
Converts a text to an XML-escaped text and using <cp> elements for the characters that are invalid in XML.static String
toXML(String text, boolean attribute)
Converts a text to an XML-escaped text.static void
validateCopyOf(Unit unit)
Verifies the copyOf attributes in a given unit.static String
validateLang(String lang)
Verifies the syntax of a language code.static void
verifyReordering(List<CTag> original, List<CTag> toVerify, boolean errorOnMissingFirstNo)
Verifies if the sequences of codes that cannot be re-ordered have been preserved.
-
-
-
Method Detail
-
isNoE
public static boolean isNoE(String string)
Checks if a string is null or empty.- Parameters:
string
- the string to check.- Returns:
- true if the given string is null or empty.
-
toXML
public static String toXML(String text, boolean attribute)
Converts a text to an XML-escaped text. This method assumes the output is in UTF-16 or UTF-8 and that all characters are supported. It also assumes attribute values are between double-quotes.Use
toSafeXML(String)
to convert content where <cp> is allowed.- Parameters:
text
- the text to convert.attribute
- true if the text is to be an XML attribute value.- Returns:
- the escaped text.
-
toSafeXML
public static String toSafeXML(String text)
Converts a text to an XML-escaped text and using <cp> elements for the characters that are invalid in XML. This method assumes the output is in UTF-16 or UTF-8 and that all characters are supported. It also assumes the text is an element content, not an attribute value. The initial angled bracket for an opening CDATA section is not escaped (fixes Okapi issue #527).- Parameters:
text
- the text to convert.- Returns:
- the escaped text.
-
compareAllowingNull
public static int compareAllowingNull(String s1, String s2)
Compares two strings allowing them to be null.- Parameters:
s1
- the first string.s2
- the second string.- Returns:
- 0 if the two string are identical.
-
equals
public static boolean equals(Object o1, Object o2)
Checks if two objects are equal, even if either is null.- Parameters:
o1
- the first object (can be null).o2
- the second object (can be null).- Returns:
- true if the objects are equals, false otherwise.
-
checkValueList
public static boolean checkValueList(String allowedValues, String value, String name)
Checks if a given value is in a given list of allowed values.- Parameters:
allowedValues
- list of allowed values (each value must be separated by a semicolon).value
- the value to check.name
- the name of the object to check (e.g. name of the attribute).- Returns:
- true if the value is allowed and not null, false if it's null.
- Throws:
InvalidParameterException
- if the value is invalid.
-
createFixedSequences
public static List<String> createFixedSequences(List<CTag> list, boolean errorOnMissingFirstNo)
Creates a list of sequences of code markers that must not be re-ordered from the list of all code markers in a given source or target content.- Parameters:
list
- the list of code markers to build from.errorOnMissingFirstNo
- true to throw an exception if a 'no' is detected instead of a 'firstNo' as a start of a sequence, false to assume it's a firstNo and process without error.- Returns:
- a list of fixed sequences.
Entries in the list are separated by a '|' and are made of a prefix 'c' for closing marker or 'z' for opening or placeholder, followed by the id of the code. the list ends with a '|'.
- Throws:
InvalidParameterException
- if errorOnMissingFirstNo is true and a firstNo is missing.
-
verifyReordering
public static void verifyReordering(List<CTag> original, List<CTag> toVerify, boolean errorOnMissingFirstNo)
Verifies if the sequences of codes that cannot be re-ordered have been preserved.Use
createFixedSequences(List, boolean)
to create the lists to compare.- Parameters:
original
- the list of the inline codes of the original state.toVerify
- the list of the inline codes of the state to verify.errorOnMissingFirstNo
- true to throw an exception if a 'no' is detected instead of a 'firstNo' as a start of a sequence, false to assume it's a firstNo and process without error.- Throws:
InvalidMarkerOrderException
- if one marker of the list to verify is out-of-order.- See Also:
createFixedSequences(List, boolean)
-
isValidNmtoken
public static boolean isValidNmtoken(String value)
Verifies if a given value is a valid NMTOKEN value. Empty and null values are not valid.- Parameters:
value
- the value to check.- Returns:
- true if it is a valid NMTOKEN, false otherwise.
-
removeExtensions
public static void removeExtensions(Object object)
Removes extension attributes and elements from a given object. Attributes and elements that are modules are not removed. Note that the extensions on the objects inside the given object are also removed. For example if the given object is a unit and it has a note with extensions, those extensions will be removed as well.- Parameters:
object
- the object from where the extension must be removed.
-
removeModules
public static void removeModules(Object object, String moduleSuffix)
Removes modules attributes and elements from a given object. Attributes and elements that are extensions are not removed. Note that the modules on the objects inside the given object are also removed.- Parameters:
object
- the object from where the modules must be removed.moduleSuffix
- suffix part of the module's namespace to remove, or null for all modules.
-
isModule
public static boolean isModule(String nsUri, String moduleSuffix)
Indicates if the given namespace URI is of a given module or not.- Parameters:
nsUri
- the full URI for the namespace to test.moduleSuffix
- the suffix of the namespace of the given module, use null for all modules.- Returns:
- true if the given URI is for the given module, false otherwise.
-
isValidInXML
public static boolean isValidInXML(int cp)
Indicates if a given code point is valid in XML or not.- Parameters:
cp
- the code point to evaluate.- Returns:
- true if it is valid, false otherwise.
-
validateCopyOf
public static void validateCopyOf(Unit unit)
Verifies the copyOf attributes in a given unit.- Parameters:
unit
- the unit to verify.
-
validateLang
public static String validateLang(String lang)
Verifies the syntax of a language code.- Parameters:
lang
- the language code to verify.- Returns:
- null if it is valid, an error message if it is not valid.
-
supports
public static boolean supports(String nsUri)
Indicates if a given module is directly supported by this version of the library.- Parameters:
nsUri
- the namespace URI of the module to query.- Returns:
- true if the given module is directly supported, false if it is not and you need to access its data using the extension mechanism.
-
-