net.sf.okapi.common.resource
Class Code

java.lang.Object
  extended by net.sf.okapi.common.resource.Code

public class Code
extends java.lang.Object

Represents an abstracted in-line code used in a TextFragment object. For example, a <b> tag in an HTML paragraph.


Field Summary
protected  java.util.LinkedHashMap<java.lang.String,InlineAnnotation> annotations
          Annotations for this code.
protected static int CLONEABLE
          Indicates that this code can be duplicated in the text.
protected  java.lang.StringBuilder data
          Native data for this code.
protected static int DELETEABLE
          Indicates that this code can be removed from the text.
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
          Id for this code.
protected  java.lang.StringBuilder outerData
          Outer data.
protected  TextFragment.TagType tagType
          Tag type for this code.
protected  java.lang.String type
          Type of the code.
static java.lang.String TYPE_BOLD
          Code type value for bold.
static java.lang.String TYPE_COMMENT
          Code type value for comment.
static java.lang.String TYPE_IMAGE
          Code type value for image.
static java.lang.String TYPE_ITALIC
          Code type value for italic.
static java.lang.String TYPE_LB
          Code type value for line-break.
static java.lang.String TYPE_LINK
          Code type value for link.
static java.lang.String TYPE_REFERENCE
          Code type value for reference.
static java.lang.String TYPE_UNDERLINED
          Code type value for underline.
static java.lang.String TYPE_XML_PROCESSING_INSTRUCTION
          Code type value for processing instruction.
 
Constructor Summary
Code(java.lang.String type)
          Creates a new code with a null type, and empty data.
Code(TextFragment.TagType tagType, java.lang.String type)
          Creates a new code with empty data.
Code(TextFragment.TagType tagType, java.lang.String type, java.lang.String data)
          Creates a new code.
 
Method Summary
 void append(java.lang.String data)
          Appends data to the current code data
 void appendOuterData(java.lang.String outerData)
          Appends data to the current code outerData
 void appendReference(java.lang.String id)
          Appends a reference marker to the current code data.
 void appendReference(java.lang.String id, java.lang.String propertyName)
          Appends a reference marker for a given property to the current code data.
 Code clone()
          Clone the code.
static java.lang.String codesToString(java.util.List<Code> list)
          Helper method to convert a list of codes into a string.
static java.lang.String codesToString(java.util.List<Code> list, boolean stripOuterData)
          Helper method to convert a list of codes into a string.
 InlineAnnotation getAnnotation(java.lang.String type)
          Gets the annotation of a given type.
 java.lang.String getData()
          Gets the raw data for the code.
 int getId()
          Gets the identifier of the code.
static int getIndex(java.util.List<Code> codes, boolean forClosing, int id)
          Gets the index in a list of codes for the id of a given code.
 java.lang.String getOuterData()
          Gets the outer data for this in-line code.
 TextFragment.TagType getTagType()
          Gets the tag type of the code.
 java.lang.String getType()
          Gets the abstract type for the code.
 boolean hasAnnotation()
          Indicates if this code has any type of annotation.
 boolean hasAnnotation(java.lang.String type)
          Indicates if this code has a given type of annotation.
 boolean hasData()
          Indicates if this code has data (i.e.
 boolean hasOuterData()
          Indicates if this code has outer data (i.e.
 boolean hasReference()
          Indicates whether the code has at least one sub-flow part.
 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.
 void removeAnnotation(java.lang.String type)
          Removes the annotation of a given type in this code.
 void removeAnnotations()
          Removes all annotations from this code.
static boolean sameCodes(java.util.List<Code> codes1, java.util.List<Code> codes2)
          Indicates if two codes-storing strings have the same codes or not.
 void setAnnotation(java.lang.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(java.lang.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 setId(int value)
          Sets the identifier of the code.
 void setOuterData(java.lang.String value)
          Sets the complete data for this in-line code (inner data and outer).
 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(java.lang.String value)
          Sets the abstract type of the code.
static java.util.List<Code> stringToCodes(java.lang.String data)
          Helper method to convert a storage string into a list of codes.
 java.lang.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
 

Field Detail

TYPE_BOLD

public static final java.lang.String TYPE_BOLD
Code type value for bold.

See Also:
Constant Field Values

TYPE_ITALIC

public static final java.lang.String TYPE_ITALIC
Code type value for italic.

See Also:
Constant Field Values

TYPE_UNDERLINED

public static final java.lang.String TYPE_UNDERLINED
Code type value for underline.

See Also:
Constant Field Values

TYPE_LB

public static final java.lang.String TYPE_LB
Code type value for line-break.

See Also:
Constant Field Values

TYPE_LINK

public static final java.lang.String TYPE_LINK
Code type value for link.

See Also:
Constant Field Values

TYPE_IMAGE

public static final java.lang.String TYPE_IMAGE
Code type value for image.

See Also:
Constant Field Values

TYPE_COMMENT

public static final java.lang.String TYPE_COMMENT
Code type value for comment.

See Also:
Constant Field Values

TYPE_XML_PROCESSING_INSTRUCTION

public static final java.lang.String TYPE_XML_PROCESSING_INSTRUCTION
Code type value for processing instruction.

See Also:
Constant Field Values

TYPE_REFERENCE

public static final java.lang.String TYPE_REFERENCE
Code type value for reference.

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

protected static final int CLONEABLE
Indicates that this code can be duplicated in the text.

See Also:
Constant Field Values

DELETEABLE

protected static final int DELETEABLE
Indicates that this code can be removed from the text.

See Also:
Constant Field Values

tagType

protected TextFragment.TagType tagType
Tag type for this code.


id

protected int id
Id for this code.


type

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


data

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


outerData

protected java.lang.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.


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.)


annotations

protected java.util.LinkedHashMap<java.lang.String,InlineAnnotation> annotations
Annotations for this code.

Constructor Detail

Code

public Code(TextFragment.TagType tagType,
            java.lang.String type,
            java.lang.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,
            java.lang.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(java.lang.String type)
Creates a new code with a null type, and empty data. You must set the type later on.

Parameters:
type - the type of code (e.g. the name of the tag).
Method Detail

codesToString

public static java.lang.String codesToString(java.util.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 java.lang.String codesToString(java.util.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(java.util.List<Code> codes1,
                                java.util.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(java.util.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 java.util.List<Code> stringToCodes(java.lang.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(java.lang.String data)
Appends data to the current code data

Parameters:
data - the data to append.

appendOuterData

public void appendOuterData(java.lang.String outerData)
Appends data to the current code outerData

Parameters:
outerData - the outer data to append.

appendReference

public void appendReference(java.lang.String id)
Appends a reference marker to the current code data.

Parameters:
id - the identifier of the referent resource.

appendReference

public void appendReference(java.lang.String id,
                            java.lang.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 java.lang.String toString()
Gets the string representation of this code: its data.

Overrides:
toString in class java.lang.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(java.lang.String type)
Indicates if this code has a given type of annotation.

Returns:
the type of annotation for this code or and empty string.

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.

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 java.lang.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 java.lang.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 "null" if not otherwise set).

setType

public void setType(java.lang.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 "null".

getData

public java.lang.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(java.lang.String value)
Sets the raw data for the code.

Parameters:
value - the new raw data of the code.

getId

public int getId()
Gets the identifier of the code.

Returns:
The identifier of the code.

setId

public void setId(int value)
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:
value - the new identifier value to be applied.

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(java.lang.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 java.lang.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.

setAnnotation

public void setAnnotation(java.lang.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(java.lang.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.

removeAnnotations

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


removeAnnotation

public void removeAnnotation(java.lang.String type)
Removes the annotation of a given type in this code.

Parameters:
type - the type of annotation to remove.