Package net.sf.okapi.common.resource
Class Code
- java.lang.Object
-
- net.sf.okapi.common.resource.Code
-
- All Implemented Interfaces:
Cloneable
,IWithProperties
public class Code extends Object implements IWithProperties, Cloneable
Represents an abstracted in-line code used in a TextFragment object. For example, a<b>
tag in an HTML paragraph.
-
-
Field Summary
Fields Modifier and Type Field Description static int
ADDED
Indicates that this code was added to the text post-extraction.static int
CLONEABLE
Indicates that this code can be duplicated in the text.protected StringBuilder
data
Native data for this code.static int
DELETEABLE
Indicates that this code can be removed from the text.protected String
displayText
Display text that is displayed as hint to the end user.static String
EXTENDED_CODE_TYPE_DELIMITER
An extended code type delimiter.static String
EXTENDED_CODE_TYPE_PREFIX
An extended code type prefix.static String
EXTENDED_CODE_TYPE_VALUE_DELIMITER
An extended code type value delimiter.protected int
flag
Flag for this code.protected static int
HASREF
Indicates that this code has one reference or more in its data part.protected int
id
Index for this code.protected LinkedHashMap<String,InlineAnnotation>
inlineAnnotations
InlineAnnotations for this code.static int
MARKER_MASKING
Indicates that this code was merged with another code.static int
MERGED
Indicates that this code was merged with another code.protected String
mergedData
serialized data for this merged code.protected String
originalId
id for this code.protected StringBuilder
outerData
Outer data.static String
PROPERTIES
static String
SEP_CODE
static String
SEP_CODEANNOTATIONKEY
static String
SEP_CODEANNOTATIONVALUE
static String
SEP_CODEFIELD
protected TextFragment.TagType
tagType
Tag type for this code.protected String
type
Type of the code.static String
TYPE_ANNOTATION_ONLY
Special type value indicating the code/marker is only there to hold an annotation.static String
TYPE_BOLD
Code type value for bold.static String
TYPE_CDATA
Code type value for CDATA.static String
TYPE_COMMENT
Code type value for comment.static String
TYPE_IMAGE
Code type value for image.static String
TYPE_INLINE_EXCLUDED
For inline excluded codesstatic String
TYPE_ITALIC
Code type value for italic.static String
TYPE_LB
Code type value for line-break.static String
TYPE_LINK
Code type value for link.static String
TYPE_NULL
Code type value for null type, to prevent it from ever being truly null.static String
TYPE_OKAPI_MARKER
Special type value indicating the code/marker is masking an Okapi markerstatic String
TYPE_REFERENCE
Code type value for reference.static String
TYPE_SUB
Code type value for subflow.static String
TYPE_UNDERLINED
Code type value for underline.static String
TYPE_XML_PROCESSING_INSTRUCTION
Code type value for processing instruction.
-
Constructor Summary
Constructors Constructor Description Code()
Code(String type)
Creates a new code with a null tag type and empty data.Code(TextFragment.TagType tagType, String type)
Creates a new code with empty data.Code(TextFragment.TagType tagType, String type, String data)
Creates a new code.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(String data)
Appends data to the current code datavoid
appendOuterData(String outerData)
Appends data to the current code outerDatavoid
appendReference(String id)
Appends a reference marker to the current code data.void
appendReference(String id, String propertyName)
Appends a reference marker for a given property to the current code data.Code
clone()
Clone the code.static String
codesToString(List<Code> list)
Helper method to convert a list of codes into a string.static String
codesToString(List<Code> list, boolean stripOuterData)
Helper method to convert a list of codes into a string.InlineAnnotation
getAnnotation(String type)
Gets the annotation of a given type.Set<String>
getAnnotationsTypes()
Gets all the types of annotations for this code.String
getData()
Gets the raw data for the code.String
getDisplayText()
Get the display equivalent text for this code.int
getFlag()
GenericAnnotations
getGenericAnnotations()
Gets the generic annotation set assigned to the code if any.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.int
getId()
Id for this code.*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.String
getMergedData()
String
getOriginalId()
The original id is from the filtered formatString
getOuterData()
Gets the outer data for this in-line code.Map<String,Property>
getProperties()
Property
getProperty(String name)
Gets the resource-level property for a given name.Set<String>
getPropertyNames()
Gets the names of all the resource-level properties for this resource.TextFragment.TagType
getTagType()
Gets the tag type of the code.String
getType()
Gets the abstract type for the code.boolean
hasAnnotation()
Indicates if this code has any type of annotation.boolean
hasAnnotation(String type)
Indicates if this code has a given type of annotation.boolean
hasData()
Indicates if this code has data (i.e.boolean
hasOnlyAnnotation()
Indicates if the code is only a marker for an annotation.boolean
hasOriginalId()
Indicates if this code has an original id (non-numeric string)boolean
hasOuterData()
Indicates if this code has outer data (i.e.boolean
hasProperty(String name)
Indicates if a resource-level property exists for a given name.boolean
hasReference()
Indicates whether the code has at least one sub-flow part.boolean
isAdded()
Indicates if this in-line code was added to the text (not found in the source).boolean
isCloneable()
Indicates if this in-line code can be duplicated in the text.boolean
isDeleteable()
Indicates if this in-line code can be removed from the text.boolean
isMarkerMasking()
Indicates if this in-line code is masking an Okapi marker.boolean
isMerged()
Indicates if this in-line code was merged with another code.void
removeAnnotation(String type)
Removes the annotation of a given type in this code.void
removeAnnotations()
Removes all annotations from this code.void
removeProperty(String name)
Removes a resource-level property of a given name.static boolean
sameCodes(List<Code> codes1, List<Code> codes2)
Indicates if two codes-storing strings have the same codes or not.void
setAdded(boolean value)
Sets the flag of this in-line code to indicate if the code was added (not found in the source).void
setAnnotation(String type, InlineAnnotation annotation)
Sets the annotation for this code.void
setCloneable(boolean value)
Sets the flag of this in-line code to indicate if it can be duplicated or not.void
setData(String value)
Sets the raw data for the code.void
setDeleteable(boolean value)
Sets the flag of this in-line code to indicate if it can be removed or not.void
setDisplayText(String text)
Set the display equivalent text for this code.void
setFlag(int flag)
void
setId(int id)
Sets the identifier of the code.void
setMarkerMasking(boolean value)
Sets the flag of this in-line code to indicate if the code is masking an Okapi marker.void
setMerged(boolean value)
Sets the flag of this in-line code to indicate if the code was merged.void
setMergedData(String mergedData)
void
setOriginalId(String originalId)
The original id is from the filtered formatvoid
setOuterData(String value)
Sets the complete data for this in-line code (inner data and outer).Property
setProperty(Property property)
Sets a resource-level property.void
setReferenceFlag(boolean value)
Sets the flag that indicates if this code has a reference (sub-flow) or not.void
setTagType(TextFragment.TagType value)
Sets the tag type for the code.void
setType(String value)
Sets the abstract type of the code.static List<Code>
stringToCodes(String data)
Helper method to convert a storage string into a list of codes.String
toString()
Gets the string representation of this code: its data.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.okapi.common.resource.IWithProperties
propertyIterator
-
-
-
-
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_BOLD
public static final String TYPE_BOLD
Code type value for bold.- See Also:
- Constant Field Values
-
TYPE_ITALIC
public static final String TYPE_ITALIC
Code type value for italic.- See Also:
- Constant Field Values
-
TYPE_UNDERLINED
public static final String TYPE_UNDERLINED
Code type value for underline.- See Also:
- Constant Field Values
-
TYPE_LB
public static final String TYPE_LB
Code type value for line-break.- See Also:
- Constant Field Values
-
TYPE_LINK
public static final String TYPE_LINK
Code type value for link.- See Also:
- Constant Field Values
-
TYPE_IMAGE
public static final String TYPE_IMAGE
Code type value for image.- See Also:
- Constant Field Values
-
TYPE_COMMENT
public static final String TYPE_COMMENT
Code type value for comment.- See Also:
- Constant Field Values
-
TYPE_CDATA
public static final String TYPE_CDATA
Code type value for CDATA.- See Also:
- Constant Field Values
-
TYPE_SUB
public static final String TYPE_SUB
Code type value for subflow.- 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_REFERENCE
public static final String TYPE_REFERENCE
Code type value for reference.- 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
-
TYPE_INLINE_EXCLUDED
public static final String TYPE_INLINE_EXCLUDED
For inline excluded codes- 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
-
SEP_CODEFIELD
public static final String SEP_CODEFIELD
- See Also:
- Constant Field Values
-
SEP_CODE
public static final String SEP_CODE
- See Also:
- Constant Field Values
-
SEP_CODEANNOTATIONKEY
public static final String SEP_CODEANNOTATIONKEY
- See Also:
- Constant Field Values
-
SEP_CODEANNOTATIONVALUE
public static final String SEP_CODEANNOTATIONVALUE
- See Also:
- Constant Field Values
-
PROPERTIES
public static final String PROPERTIES
- See Also:
- Constant Field Values
-
tagType
protected TextFragment.TagType tagType
Tag type for this code.
-
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 inTextFragment
-
type
protected String type
Type of the code. It MUST NEVER be null (so internal compare can be fast), null is mapped toTYPE_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.)
-
inlineAnnotations
protected LinkedHashMap<String,InlineAnnotation> inlineAnnotations
InlineAnnotations for this code.
-
-
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.
-
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. UseisCloneable()
to check.
-
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 ofTextFragment.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 toTYPE_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 inTextFragment
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 asgetData()
).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 lookupfieldName
- 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 interfaceIWithProperties
- 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 interfaceIWithProperties
- 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 interfaceIWithProperties
- 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 interfaceIWithProperties
- Parameters:
property
- The new property to set.- Returns:
- The property that has been set.
-
getProperties
public Map<String,Property> getProperties()
- Specified by:
getProperties
in interfaceIWithProperties
- Returns:
Map
of properties for the implementer of interface
-
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 interfaceIWithProperties
- Returns:
- All the names of the resource-level properties for this resource.
-
-