Package net.sf.okapi.common.resource
Class TextUnit
- java.lang.Object
-
- net.sf.okapi.common.resource.BaseNameable
-
- net.sf.okapi.common.resource.BaseReferenceable
-
- net.sf.okapi.common.resource.TextUnit
-
- All Implemented Interfaces:
Cloneable
,IResource
,IMultilingual
,INameable
,IReferenceable
,ITextUnit
,IWithAnnotations
,IWithProperties
,IWithSkeleton
,IWithSourceProperties
,IWithTargetProperties
public class TextUnit extends BaseReferenceable implements ITextUnit
Basic unit of extraction from a filter and also the resource associated with the filter event TEXT_UNIT. The TextUnit object holds the extracted source text in one or more versions, all its properties and annotations, and any target corresponding data.
-
-
Field Summary
-
Fields inherited from class net.sf.okapi.common.resource.BaseReferenceable
parentId, refCount
-
Fields inherited from class net.sf.okapi.common.resource.BaseNameable
id, isTranslatable, mimeType, name, preserveWS, type
-
Fields inherited from interface net.sf.okapi.common.IResource
COPY_ALL, COPY_CONTENT, COPY_PROPERTIES, COPY_SEGMENTATION, COPY_SEGMENTED_CONTENT, CREATE_EMPTY
-
Fields inherited from interface net.sf.okapi.common.resource.ITextUnit
TYPE_CDATA, TYPE_HEADER, TYPE_LIST_ELEMENT, TYPE_PARA, TYPE_TITLE
-
-
Constructor Summary
Constructors Constructor Description TextUnit()
TextUnit(String id)
Creates a new TextUnit object with its identifier.TextUnit(String id, String sourceText)
Creates a new TextUnit object with its identifier and a text.TextUnit(String id, String sourceText, boolean isReferent)
Creates a new TextUnit object with its ID, a text, and a flag indicating if it is a referent or not.TextUnit(String id, String sourceText, boolean isReferent, String mimeType)
Creates a new TextUnit object with its identifier, a text, a flag indicating if it is a referent or not, and a given MIME type.TextUnit(String id, TextContainer source, boolean isReferent, String mimeType)
Creates a new TextUnit object with its identifier, a text, a flag indicating if it is a referent or not, and a given MIME type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TextUnit
clone()
Clones this TextUnit.void
createSourceSegmentation(ISegmenter segmenter)
Segments the default source content based on the rules provided by a given ISegmenter.TextContainer
createTarget(LocaleId targetLocale, boolean overwriteExisting, int creationOptions)
Creates or get the target for this TextUnit.Property
createTargetProperty(LocaleId locId, String name, boolean overwriteExisting, int creationOptions)
Create targetProperty
without access to the source propertyvoid
createTargetSegmentation(ISegmenter segmenter, LocaleId targetLocale)
Segments the specified target content based on the rules provided by a given ISegmenter.IAlignedSegments
getAlignedSegments()
Creates a newIAlignedSegments
object to access and manipulate the segments of this text unit.TextContainer
getSource()
Gets the source object for this text unit (aTextContainer
object).Map<String,Property>
getSourceProperties()
Property
getSourceProperty(String name)
Gets the source property for a given name.Set<String>
getSourcePropertyNames()
Gets the names of all the source properties for this resource.Segment
getSourceSegment(String segId, boolean createIfNeeded)
Gets the source segment for a given segment id.ISegments
getSourceSegments()
Gets the segments for the source.TextContainer
getTarget(LocaleId locId)
Gets the target object for this text unit for a given locale.Set<LocaleId>
getTargetLocales()
Gets all the target locales for this resource.Property
getTargetProperty(LocaleId locId, String name)
Gets the target property for a given name and target locale.Set<String>
getTargetPropertyNames(LocaleId locId)
Gets the names of all the properties for a given target locale in this resource.Segment
getTargetSegment(LocaleId trgLoc, String segId, boolean createIfNeeded)
Gets the segment for a given segment id in a given target.ISegments
getTargetSegments(LocaleId trgLoc)
Get the segments for a given target.boolean
hasSourceProperty(String name)
Indicates if a source property exists for a given name.boolean
hasTarget(LocaleId locId)
Indicates if there is a target object for a given locale for this text unit.boolean
hasTargetProperty(LocaleId locId, String name)
Indicates if a property exists for a given name and target locale.boolean
isEmpty()
Indicates if the source text of this TextUnit is empty.void
removeAllSegmentations()
Removes all segmentations (source and targets) in this text unit.void
removeSourceProperty(String name)
Removes a source property of a given name.void
removeTarget(LocaleId locId)
Removes a given target object from this text unit.void
removeTargetProperty(LocaleId locId, String name)
Removes a target property of a given name.void
setSkeleton(ISkeleton skeleton)
Sets the skeleton object for this resource.TextContainer
setSource(TextContainer textContainer)
Sets the source object for this TextUnit.TextFragment
setSourceContent(TextFragment content)
Sets the content of the source for this TextUnit.Property
setSourceProperty(Property property)
Sets a source property.TextContainer
setTarget(LocaleId locId, TextContainer text)
Sets the target object for this text unit for a given locale.TextFragment
setTargetContent(LocaleId locId, TextFragment content)
Sets the content of the target for a given locale for this TextUnit.Property
setTargetProperty(LocaleId locId, Property property)
Sets a target property.String
toString()
Gets the string representation of the default source container.-
Methods inherited from class net.sf.okapi.common.resource.BaseReferenceable
getParentId, getReferenceCount, isReferent, setIsReferent, setParentId, setReferenceCount
-
Methods inherited from class net.sf.okapi.common.resource.BaseNameable
getAnnotation, getAnnotations, getId, getMimeType, getName, getProperties, getProperty, getPropertyNames, getSkeleton, getType, hasProperty, isTranslatable, preserveWhitespaces, removeProperty, setAnnotation, setId, setIsTranslatable, setMimeType, setName, setPreserveWhitespaces, setProperty, setType
-
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.INameable
getMimeType, getName, getType, isTranslatable, preserveWhitespaces, setIsTranslatable, setMimeType, setName, setPreserveWhitespaces, setType
-
Methods inherited from interface net.sf.okapi.common.resource.IReferenceable
getReferenceCount, isReferent, setIsReferent, setReferenceCount
-
Methods inherited from interface net.sf.okapi.common.resource.IWithAnnotations
annotationIterator, clear, getAnnotation, getAnnotations, getAnnotationsTypesAsSet, hasAnnotation, hasAnnotations, remove, setAnnotation
-
Methods inherited from interface net.sf.okapi.common.resource.IWithProperties
getProperties, getProperty, getPropertyNames, hasProperty, propertyIterator, removeProperty, setProperty
-
Methods inherited from interface net.sf.okapi.common.resource.IWithSkeleton
getSkeleton
-
Methods inherited from interface net.sf.okapi.common.resource.IWithTargetProperties
createTargetProperty, getAnnotations, getTargetProperties
-
-
-
-
Constructor Detail
-
TextUnit
public TextUnit()
-
TextUnit
public TextUnit(String id)
Creates a new TextUnit object with its identifier.- Parameters:
id
- the identifier of this resource.
-
TextUnit
public TextUnit(String id, String sourceText)
Creates a new TextUnit object with its identifier and a text.- Parameters:
id
- the identifier of this resource.sourceText
- the initial text of the source.
-
TextUnit
public TextUnit(String id, String sourceText, boolean isReferent)
Creates a new TextUnit object with its ID, a text, and a flag indicating if it is a referent or not.- Parameters:
id
- the identifier of this resource.sourceText
- the initial text of the source (can be null).isReferent
- indicates if this resource is a referent (i.e. is referred to by another resource) or not.
-
TextUnit
public TextUnit(String id, String sourceText, boolean isReferent, String mimeType)
Creates a new TextUnit object with its identifier, a text, a flag indicating if it is a referent or not, and a given MIME type.- Parameters:
id
- the identifier of this resource.sourceText
- the initial text of the source (can be null).isReferent
- indicates if this resource is a referent (i.e. is referred to by another resource) or not.mimeType
- the MIME type identifier for the content of this TextUnit.
-
TextUnit
public TextUnit(String id, TextContainer source, boolean isReferent, String mimeType)
Creates a new TextUnit object with its identifier, a text, a flag indicating if it is a referent or not, and a given MIME type.- Parameters:
id
- the identifier of this resource.source
- the initial text container of the source (can be null).isReferent
- indicates if this resource is a referent (i.e. is referred to by another resource) or not.mimeType
- the MIME type identifier for the content of this TextUnit.
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Description copied from interface:ITextUnit
Indicates if the source text of this TextUnit is empty.
-
getSource
public TextContainer getSource()
Description copied from interface:ITextUnit
Gets the source object for this text unit (aTextContainer
object).
-
setSource
public TextContainer setSource(TextContainer textContainer)
Description copied from interface:ITextUnit
Sets the source object for this TextUnit. Any existing source object is overwritten.
-
setSourceContent
public TextFragment setSourceContent(TextFragment content)
Description copied from interface:ITextUnit
Sets the content of the source for this TextUnit.- Specified by:
setSourceContent
in interfaceITextUnit
- Parameters:
content
- the new content to set.- Returns:
- the new content of the source for this TextUnit.
-
createTarget
public TextContainer createTarget(LocaleId targetLocale, boolean overwriteExisting, int creationOptions)
Description copied from interface:ITextUnit
Creates or get the target for this TextUnit.If a variant source exists for the target locale, creationOptions apply to the variant source.
- Specified by:
createTarget
in interfaceITextUnit
- Parameters:
targetLocale
- the target locale.overwriteExisting
- true to overwrite any existing target for the given locale. False to not create a new target object if one already exists for the given locale.creationOptions
- creation options:- CREATE_EMPTY: Create an empty target object.
- COPY_PROPERTIES: Copy the source properties.
- COPY_CONTENT: Copy the text of the source (and any associated in-line code), but not the segmenation.
- COPY_SEGMENTATION: Copy the source segmentation.
- COPY_SEGMENTED_CONTENT: Same as (COPY_CONTENT|COPY_SEGMENTATION).
- COPY_ALL: Same as (COPY_SEGMENTED_CONTENT|COPY_PROPERTIES).
- Returns:
- the target object that was created, or retrieved.
-
getTarget
public TextContainer getTarget(LocaleId locId)
Description copied from interface:ITextUnit
Gets the target object for this text unit for a given locale. If the target does not exists a null is retruned.- Specified by:
getTarget
in interfaceITextUnit
- Parameters:
locId
- the locale to query.- Returns:
- the target object for this text unit for the given locale, or null if the target does not exist.
- See Also:
ITextUnit.createTarget(LocaleId, boolean, int)
-
setTarget
public TextContainer setTarget(LocaleId locId, TextContainer text)
Description copied from interface:ITextUnit
Sets the target object for this text unit for a given locale.If the target does not exists, one is created. Any existing content for the given locale is overwritten. To set a target object based on the source, use the
ITextUnit.createTarget(LocaleId, boolean, int)
method.
-
removeTarget
public void removeTarget(LocaleId locId)
Description copied from interface:ITextUnit
Removes a given target object from this text unit. If the given locale does not exist in this text unit nothing happens. Variant source for this target locale will not be affected.- Specified by:
removeTarget
in interfaceITextUnit
- Parameters:
locId
- the target locale to remove.
-
hasTarget
public boolean hasTarget(LocaleId locId)
Description copied from interface:ITextUnit
Indicates if there is a target object for a given locale for this text unit.
-
setTargetContent
public TextFragment setTargetContent(LocaleId locId, TextFragment content)
Description copied from interface:ITextUnit
Sets the content of the target for a given locale for this TextUnit.If the target does not exists, one is created. Any existing content for the given locale is overwritten. To set a target object based on the source, use the
ITextUnit.createTarget(LocaleId, boolean, int)
method.- Specified by:
setTargetContent
in interfaceITextUnit
- Parameters:
locId
- the locale to set.content
- the new content to set.- Returns:
- the new content for the given target locale for this text unit.
-
getAlignedSegments
public IAlignedSegments getAlignedSegments()
Description copied from interface:ITextUnit
Creates a newIAlignedSegments
object to access and manipulate the segments of this text unit.- Specified by:
getAlignedSegments
in interfaceITextUnit
- Returns:
- a new
IAlignedSegments
object.
-
getSourceSegments
public ISegments getSourceSegments()
Description copied from interface:ITextUnit
Gets the segments for the source. Un-segmented content return a single segment.- Specified by:
getSourceSegments
in interfaceITextUnit
- Returns:
- an object implementing ISegments for the source content.
-
getSourceSegment
public Segment getSourceSegment(String segId, boolean createIfNeeded)
Description copied from interface:ITextUnit
Gets the source segment for a given segment id.If the segment does not exists, one is created if
createIfNeeded
is true.- Specified by:
getSourceSegment
in interfaceITextUnit
- Parameters:
segId
- the id of the segment to retrieve.createIfNeeded
- true to append a segment at the end of the content and return it if the segment does not exist yet. False to return null when the segment does not exists.- Returns:
- the found or created segment, or null.
-
getTargetSegments
public ISegments getTargetSegments(LocaleId trgLoc)
Description copied from interface:ITextUnit
Get the segments for a given target. Un-segmented content return a single segment. If the target does not exists, one is created, with the same segments as the source, but empty.- Specified by:
getTargetSegments
in interfaceITextUnit
- Parameters:
trgLoc
- the locale of the target to retrieve.- Returns:
- an object implementing ISegments for the given target content.
-
getTargetSegment
public Segment getTargetSegment(LocaleId trgLoc, String segId, boolean createIfNeeded)
Description copied from interface:ITextUnit
Gets the segment for a given segment id in a given target.If the target does not exists, one is created.
If the segment does not exists, one is created if
createIfNeeded
is true.- Specified by:
getTargetSegment
in interfaceITextUnit
- Parameters:
trgLoc
- the target locale to look up.segId
- the id of the segment to retrieve.createIfNeeded
- true to append a segment at the end of the target content and return it if the segment does not exist yet. False to return null when the segment does not exists.- Returns:
- the found or created segment, or null.
-
getTargetLocales
public Set<LocaleId> getTargetLocales()
Description copied from interface:IWithTargetProperties
Gets all the target locales for this resource.- Specified by:
getTargetLocales
in interfaceIWithTargetProperties
- Returns:
- all the target locales for this resource.
-
setSkeleton
public void setSkeleton(ISkeleton skeleton)
Description copied from interface:IWithSkeleton
Sets the skeleton object for this resource.- Specified by:
setSkeleton
in interfaceIWithSkeleton
- Overrides:
setSkeleton
in classBaseNameable
- Parameters:
skeleton
- the skeleton object to set.
-
toString
public String toString()
Gets the string representation of the default source container. If the container is segmented, the representation shows the merged segments. Inline codes are also included.- Overrides:
toString
in classBaseNameable
- Returns:
- the string representation of the source container.
-
clone
public TextUnit clone()
Clones this TextUnit.
-
removeAllSegmentations
public void removeAllSegmentations()
Description copied from interface:ITextUnit
Removes all segmentations (source and targets) in this text unit. All entries are converted to non-segmented entries.- Specified by:
removeAllSegmentations
in interfaceITextUnit
-
createSourceSegmentation
public void createSourceSegmentation(ISegmenter segmenter)
Description copied from interface:ITextUnit
Segments the default source content based on the rules provided by a given ISegmenter.- Specified by:
createSourceSegmentation
in interfaceITextUnit
- Parameters:
segmenter
- the segmenter to use to create the segments.
-
createTargetSegmentation
public void createTargetSegmentation(ISegmenter segmenter, LocaleId targetLocale)
Description copied from interface:ITextUnit
Segments the specified target content based on the rules provided by a given ISegmenter.- Specified by:
createTargetSegmentation
in interfaceITextUnit
- Parameters:
segmenter
- the segmenter to use to create the segments.targetLocale
-LocaleId
of the target we want to segment.
-
getSourceProperty
public Property getSourceProperty(String name)
Description copied from interface:IWithSourceProperties
Gets the source property for a given name.- Specified by:
getSourceProperty
in interfaceIWithSourceProperties
- Parameters:
name
- The name of the source property to retrieve.- Returns:
- The property or null if it does not exist.
-
setSourceProperty
public Property setSourceProperty(Property property)
Description copied from interface:IWithSourceProperties
Sets a source property. If a property already exists it is overwritten.- Specified by:
setSourceProperty
in interfaceIWithSourceProperties
- Parameters:
property
- The new property to set.- Returns:
- The property that has been set.
-
removeSourceProperty
public void removeSourceProperty(String name)
Description copied from interface:IWithSourceProperties
Removes a source property of a given name. If the property does not exists nothing happens.- Specified by:
removeSourceProperty
in interfaceIWithSourceProperties
- Parameters:
name
- The name of the property to remove.
-
getSourcePropertyNames
public Set<String> getSourcePropertyNames()
Description copied from interface:IWithSourceProperties
Gets the names of all the source properties for this resource.- Specified by:
getSourcePropertyNames
in interfaceIWithSourceProperties
- Returns:
- All the names of the source properties for this resource.
-
hasSourceProperty
public boolean hasSourceProperty(String name)
Description copied from interface:IWithSourceProperties
Indicates if a source property exists for a given name.- Specified by:
hasSourceProperty
in interfaceIWithSourceProperties
- Parameters:
name
- The name of the source property to query.- Returns:
- True if a source property exists, false otherwise.
-
getTargetProperty
public Property getTargetProperty(LocaleId locId, String name)
Description copied from interface:IWithTargetProperties
Gets the target property for a given name and target locale.- Specified by:
getTargetProperty
in interfaceIWithTargetProperties
- Parameters:
locId
- the locale of the property to retrieve.name
- The name of the property to retrieve. This name is case-sensitive.- Returns:
- The property or null if it does not exist.
-
setTargetProperty
public Property setTargetProperty(LocaleId locId, Property property)
Description copied from interface:IWithTargetProperties
Sets a target property. If a property already exists it is overwritten.- Specified by:
setTargetProperty
in interfaceIWithTargetProperties
- Parameters:
locId
- The target locale for which this property should be set.property
- The new property to set. This name is case-sensitive.- Returns:
- The property that has been set.
-
removeTargetProperty
public void removeTargetProperty(LocaleId locId, String name)
Description copied from interface:IWithTargetProperties
Removes a target property of a given name. If the property does not exists nothing happens.- Specified by:
removeTargetProperty
in interfaceIWithTargetProperties
- Parameters:
locId
- The target locale for which this property should be set.name
- The name of the property to remove.
-
getTargetPropertyNames
public Set<String> getTargetPropertyNames(LocaleId locId)
Description copied from interface:IWithTargetProperties
Gets the names of all the properties for a given target locale in this resource.- Specified by:
getTargetPropertyNames
in interfaceIWithTargetProperties
- Parameters:
locId
- the target locale to query.- Returns:
- all the names of the target properties for the given locale in this resource.
-
hasTargetProperty
public boolean hasTargetProperty(LocaleId locId, String name)
Description copied from interface:IWithTargetProperties
Indicates if a property exists for a given name and target locale.- Specified by:
hasTargetProperty
in interfaceIWithTargetProperties
- Parameters:
locId
- the target locale to query.name
- the name of the property to query.- Returns:
- true if a property exists, false otherwise.
-
getSourceProperties
public Map<String,Property> getSourceProperties()
- Specified by:
getSourceProperties
in interfaceIWithSourceProperties
- Returns:
Map
of source properties for the implementer of interface
-
createTargetProperty
public Property createTargetProperty(LocaleId locId, String name, boolean overwriteExisting, int creationOptions)
Description copied from interface:IWithTargetProperties
Create targetProperty
without access to the source property- Specified by:
createTargetProperty
in interfaceIWithTargetProperties
- Returns:
-
-