Class Code

    • Field Detail

      • TYPE_NULL

        public static final String TYPE_NULL
        Code type value for null type, to prevent it from ever being truly null.
        See Also:
        Constant Field Values
      • TYPE_XML_PROCESSING_INSTRUCTION

        public static final String TYPE_XML_PROCESSING_INSTRUCTION
        Code type value for processing instruction.
        See Also:
        Constant Field Values
      • TYPE_ANNOTATION_ONLY

        public static final String TYPE_ANNOTATION_ONLY
        Special type value indicating the code/marker is only there to hold an annotation.
        See Also:
        Constant Field Values
      • TYPE_OKAPI_MARKER

        public static final String TYPE_OKAPI_MARKER
        Special type value indicating the code/marker is masking an Okapi marker
        See Also:
        Constant Field Values
      • EXTENDED_CODE_TYPE_PREFIX

        public static final String EXTENDED_CODE_TYPE_PREFIX
        An extended code type prefix.
        See Also:
        Constant Field Values
      • EXTENDED_CODE_TYPE_DELIMITER

        public static final String EXTENDED_CODE_TYPE_DELIMITER
        An extended code type delimiter. Should be considered the following usage scheme: x-bold; x-bold;underlined;highlighted;
        See Also:
        Constant Field Values
      • EXTENDED_CODE_TYPE_VALUE_DELIMITER

        public static final String EXTENDED_CODE_TYPE_VALUE_DELIMITER
        An extended code type value delimiter. Should be considered the following usage scheme: x-color:red;
        See Also:
        Constant Field Values
      • HASREF

        protected static final int HASREF
        Indicates that this code has one reference or more in its data part.
        See Also:
        Constant Field Values
      • CLONEABLE

        public static final int CLONEABLE
        Indicates that this code can be duplicated in the text.
        See Also:
        Constant Field Values
      • DELETEABLE

        public static final int DELETEABLE
        Indicates that this code can be removed from the text.
        See Also:
        Constant Field Values
      • ADDED

        public static final int ADDED
        Indicates that this code was added to the text post-extraction.
        See Also:
        Constant Field Values
      • MERGED

        public static final int MERGED
        Indicates that this code was merged with another code.
        See Also:
        Constant Field Values
      • MARKER_MASKING

        public static final int MARKER_MASKING
        Indicates that this code was merged with another code.
        See Also:
        Constant Field Values
      • originalId

        protected String originalId
        id for this code. Populated from the original format if available
      • id

        protected int id
        Index for this code. Points to the corresponding marker in TextFragment
      • type

        protected String type
        Type of the code. It MUST NEVER be null (so internal compare can be fast), null is mapped to TYPE_NULL
      • data

        protected StringBuilder data
        Native data for this code. This is used to generate the text output, except if outerData is not null.
      • mergedData

        protected String mergedData
        serialized data for this merged code. Combines serialized string from multiple codes using Code.codesToString()
      • outerData

        protected StringBuilder outerData
        Outer data. It must be null (not just empty) for data to be used. Outer data is reserved to store inline native codes in formats that are extraction formats such as XLIFF, TS, etc.
      • displayText

        protected String displayText
        Display text that is displayed as hint to the end user.
      • flag

        protected int flag
        Flag for this code. This flag holds various information about the code (is it deletable, does it have a reference, etc.)
    • Constructor Detail

      • Code

        public Code()
      • Code

        public Code​(TextFragment.TagType tagType,
                    String type,
                    String data)
        Creates a new code. By default codes can be both deleted and cloned.
        Parameters:
        tagType - the tag type.
        type - the type of code (e.g. the name of the tag). The type must be exactly the same between the opening and closing codes.
        data - the content of the code.
      • Code

        public Code​(TextFragment.TagType tagType,
                    String type)
        Creates a new code with empty data.
        Parameters:
        tagType - the tag type.
        type - the type of code (e.g. the name of the tag).
      • Code

        public Code​(String type)
        Creates a new code with a null tag type and empty data. You must set the tag type later on.
        Parameters:
        type - the type of code (e.g. the name of the tag).
    • Method Detail

      • codesToString

        public static String codesToString​(List<Code> list)
        Helper method to convert a list of codes into a string. This method preserves the outerData in the codes.
        Parameters:
        list - the list of the codes to flatten into a string.
        Returns:
        the string with all the codes.
        See Also:
        codesToString(List, boolean), stringToCodes(String)
      • codesToString

        public static String codesToString​(List<Code> list,
                                           boolean stripOuterData)
        Helper method to convert a list of codes into a string. This method allows optionally to strip the outerData in the codes.
        Parameters:
        list - the list of the codes to flatten into a string.
        stripOuterData - true to remove the outerData in the storage string, false to keep it.
        Returns:
        the string with all the codes.
        See Also:
        codesToString(List), stringToCodes(String)
      • sameCodes

        public static boolean sameCodes​(List<Code> codes1,
                                        List<Code> codes2)
        Indicates if two codes-storing strings have the same codes or not.
        Parameters:
        codes1 - the first codes-storing string.
        codes2 - the second codes-storing string.
        Returns:
        true if both codes-storing strings are identical.
      • getIndex

        public static int getIndex​(List<Code> codes,
                                   boolean forClosing,
                                   int id)
        Gets the index in a list of codes for the id of a given code.
        Parameters:
        codes - the list of codes to lookup.
        forClosing - true to get the index of the closing code.
        id - the id to search for.
        Returns:
        the index of the first opening or closing code with the given id, or -1 if not found.
      • stringToCodes

        public static List<Code> stringToCodes​(String data)
        Helper method to convert a storage string into a list of codes.
        Parameters:
        data - the storage string to convert (can be null).
        Returns:
        a list of the codes in the storage string.
        See Also:
        codesToString(List), codesToString(List, boolean)
      • append

        public void append​(String data)
        Appends data to the current code data
        Parameters:
        data - the data to append.
      • appendOuterData

        public void appendOuterData​(String outerData)
        Appends data to the current code outerData
        Parameters:
        outerData - the outer data to append.
      • appendReference

        public void appendReference​(String id)
        Appends a reference marker to the current code data.
        Parameters:
        id - the identifier of the referent resource.
      • appendReference

        public void appendReference​(String id,
                                    String propertyName)
        Appends a reference marker for a given property to the current code data.
        Parameters:
        id - the identifier of the referent resource where the property is located.
        propertyName - the name of the property.
      • toString

        public String toString()
        Gets the string representation of this code: its data.
        Overrides:
        toString in class Object
        Returns:
        the raw data of this code.
      • hasAnnotation

        public boolean hasAnnotation()
        Indicates if this code has any type of annotation.
        Returns:
        true when this code has any type of annotation.
      • hasAnnotation

        public boolean hasAnnotation​(String type)
        Indicates if this code has a given type of annotation.
        Parameters:
        type - the type of annotation.
        Returns:
        true if the code has an annotation of the requested type.
      • hasOnlyAnnotation

        public boolean hasOnlyAnnotation()
        Indicates if the code is only a marker for an annotation.
        Returns:
        true if the code has an annotation (not null) and its type is set to Code.TYPE_ANNOTATION_ONLY. False if there is no annotation (even with a type set to Code.TYPE_ANNOTATION_ONLY), or if the type is not Code.TYPE_ANNOTATION_ONLY.
      • hasData

        public boolean hasData()
        Indicates if this code has data (i.e. data is empty, as data is never null)
        Returns:
        true if this code has data.
      • hasOriginalId

        public boolean hasOriginalId()
        Indicates if this code has an original id (non-numeric string)
        Returns:
        true if this code has an original id
      • hasOuterData

        public boolean hasOuterData()
        Indicates if this code has outer data (i.e. outerData is not null and not empty)
        Returns:
        true if this code has outer data.
      • clone

        public Code clone()
        Clone the code. Note that this method does not check if this code can be duplicated or not. Use isCloneable() to check.
        Overrides:
        clone in class Object
        Returns:
        a copy of the object.
      • getTagType

        public TextFragment.TagType getTagType()
        Gets the tag type of the code.
        Returns:
        the tag type of the code.
      • setTagType

        public void setTagType​(TextFragment.TagType value)
        Sets the tag type for the code.
        Parameters:
        value - the new tag type to apply. The value must be one of the values of TextFragment.TagType.
      • getType

        public String getType()
        Gets the abstract type for the code. For example: "bold".
        Returns:
        the abstract type of the code. This value should never be null (it is set to TYPE_NULL if not otherwise set).
      • setType

        public void setType​(String value)
        Sets the abstract type of the code. This member is used to match up together opening and closing codes.
        Parameters:
        value - the new abstract type of the code. Null is mapped to TYPE_NULL.
      • getData

        public String getData()
        Gets the raw data for the code. This does not build a string with sub-flows content.
        Returns:
        the raw data of the code.
      • setData

        public void setData​(String value)
        Sets the raw data for the code.
        Parameters:
        value - the new raw data of the code.
      • getMergedData

        public String getMergedData()
      • setMergedData

        public void setMergedData​(String mergedData)
      • getId

        public int getId()
        Id for this code.*
      • setId

        public void setId​(int id)
        Sets the identifier of the code. Be aware that identifiers for in-line codes are generated automatically when used in TextFragment and overriding the values may result in codes with duplicate IDs.
        Parameters:
        id - the new index to be applied.
      • getOriginalId

        public String getOriginalId()
        The original id is from the filtered format
      • setOriginalId

        public void setOriginalId​(String originalId)
        The original id is from the filtered format
      • hasReference

        public boolean hasReference()
        Indicates whether the code has at least one sub-flow part. A sub-flow is a text unit contained within the code. For example: the text of the ALT attribute in the HTML IMG element: If the IMG tag is a code, the value of ALT is one of its sub-flows.
        Returns:
        true if the code has at least one sub-flow.
      • setReferenceFlag

        public void setReferenceFlag​(boolean value)
        Sets the flag that indicates if this code has a reference (sub-flow) or not.
        Parameters:
        value - the new value to apply.
      • setOuterData

        public void setOuterData​(String value)
        Sets the complete data for this in-line code (inner data and outer). Outer data is used for format that implements in-line codes like TMX or XLIFF. For example "<ph id='1'>code</ph>" is the outer data, and "code" in the inner data.
        Parameters:
        value - the data to set (can be null).
      • getOuterData

        public String getOuterData()
        Gets the outer data for this in-line code. If there is no outer data, the inner data is returned (same as getData()).

        Use hasOuterData() to know if there is true outer data.

        Returns:
        the outer data or, if there is none, the inner data.
      • isCloneable

        public boolean isCloneable()
        Indicates if this in-line code can be duplicated in the text. For example a HTML bold element could be duplicated, while a %s should not.
        Returns:
        true if this in-line code can be duplicated.
      • setCloneable

        public void setCloneable​(boolean value)
        Sets the flag of this in-line code to indicate if it can be duplicated or not.
        Parameters:
        value - true to allow duplication, false to forbid it.
      • isDeleteable

        public boolean isDeleteable()
        Indicates if this in-line code can be removed from the text. For example a HTML bold element could be removed, while a %s should not.
        Returns:
        true if this in-line code can be removed.
      • setDeleteable

        public void setDeleteable​(boolean value)
        Sets the flag of this in-line code to indicate if it can be removed or not.
        Parameters:
        value - true to allow deletion, false to forbid it.
      • isAdded

        public boolean isAdded()
        Indicates if this in-line code was added to the text (not found in the source). For example a HTML bold element added after extraction.
        Returns:
        true if this in-line code can be added.
      • setAdded

        public void setAdded​(boolean value)
        Sets the flag of this in-line code to indicate if the code was added (not found in the source).
        Parameters:
        value - true to allow adding, false to forbid it.
      • isMerged

        public boolean isMerged()
        Indicates if this in-line code was merged with another code.
        Returns:
        true if this in-line code was merged.
      • setMerged

        public void setMerged​(boolean value)
        Sets the flag of this in-line code to indicate if the code was merged.
        Parameters:
        value - true if merged, false otherwise.
      • isMarkerMasking

        public boolean isMarkerMasking()
        Indicates if this in-line code is masking an Okapi marker.
        Returns:
        true if this in-line code is masking an Okapi marker.
      • setMarkerMasking

        public void setMarkerMasking​(boolean value)
        Sets the flag of this in-line code to indicate if the code is masking an Okapi marker.
        Parameters:
        value - true if masking an Okapi marker, false otherwise.
      • setAnnotation

        public void setAnnotation​(String type,
                                  InlineAnnotation annotation)
        Sets the annotation for this code.
        Parameters:
        type - type of the annotation to set.
        annotation - the annotation to set. This parameter can be null for example when the annotation is used like a boolean flag.
      • getAnnotation

        public InlineAnnotation getAnnotation​(String type)
        Gets the annotation of a given type.
        Parameters:
        type - the type of annotation to retrieve.
        Returns:
        the annotation of the given type, or null if there is no such annotation for this code.
      • getAnnotationsTypes

        public Set<String> getAnnotationsTypes()
        Gets all the types of annotations for this code.
        Returns:
        a set of all the types of annotations for this code, or null if there are no annotations.
      • removeAnnotations

        public void removeAnnotations()
        Removes all annotations from this code.
      • removeAnnotation

        public void removeAnnotation​(String type)
        Removes the annotation of a given type in this code.
        Parameters:
        type - the type of annotation to remove.
      • getGenericAnnotations

        public GenericAnnotations getGenericAnnotations()
        Gets the generic annotation set assigned to the code if any.
        Returns:
        the annotation set, or null if there is none.
      • getGenericAnnotationString

        public String getGenericAnnotationString​(String type,
                                                 String fieldName)
        Gets the value of a string field for the first generic annotation of a given type in the annotation set associated with this code. If several annotations of the given type exist, this method return only the value for the given field for the first one.
        Parameters:
        type - the type of generic annotation to lookup
        fieldName - the name of the field to retrieve.
        Returns:
        the string value of the field or null if there is no such field, annotation, or annotation set.
      • setDisplayText

        public void setDisplayText​(String text)
        Set the display equivalent text for this code. This is intended to be an explanatory hint displayed to the user.
        Parameters:
        text - The display text for this code
      • getDisplayText

        public String getDisplayText()
        Get the display equivalent text for this code.
        Returns:
        The display text for this code, or null if none exists
      • getFlag

        public int getFlag()
      • setFlag

        public void setFlag​(int flag)
      • getProperty

        public Property getProperty​(String name)
        Description copied from interface: IWithProperties
        Gets the resource-level property for a given name.
        Specified by:
        getProperty in interface IWithProperties
        Parameters:
        name - Name of the property to retrieve.
        Returns:
        The property or null if it does not exist.
      • hasProperty

        public boolean hasProperty​(String name)
        Description copied from interface: IWithProperties
        Indicates if a resource-level property exists for a given name.
        Specified by:
        hasProperty in interface IWithProperties
        Parameters:
        name - The name of the resource-level property to query.
        Returns:
        True if a resource-level property exists, false otherwise.
      • removeProperty

        public void removeProperty​(String name)
        Description copied from interface: IWithProperties
        Removes a resource-level property of a given name. If the property does not exists nothing happens.
        Specified by:
        removeProperty in interface IWithProperties
        Parameters:
        name - The name of the property to remove.
      • setProperty

        public Property setProperty​(Property property)
        Description copied from interface: IWithProperties
        Sets a resource-level property. If a property already exists it is overwritten.
        Specified by:
        setProperty in interface IWithProperties
        Parameters:
        property - The new property to set.
        Returns:
        The property that has been set.
      • getPropertyNames

        public Set<String> getPropertyNames()
        Description copied from interface: IWithProperties
        Gets the names of all the resource-level properties for this resource.
        Specified by:
        getPropertyNames in interface IWithProperties
        Returns:
        All the names of the resource-level properties for this resource.