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
-
Fields inherited from class net.sf.okapi.common.resource.BaseNameable
id, mimeType, name
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TextUnitclone()Clones this TextUnit.voidcreateSourceSegmentation(ISegmenter segmenter)Segments the default source content based on the rules provided by a given ISegmenter.TextContainercreateTarget(LocaleId targetLocale, boolean overwriteExisting, int creationOptions)Creates or get the target for this TextUnit.PropertycreateTargetProperty(LocaleId locId, String name, boolean overwriteExisting, int creationOptions)Create targetPropertywithout access to the source propertyvoidcreateTargetSegmentation(ISegmenter segmenter, LocaleId targetLocale)Segments the specified target content based on the rules provided by a given ISegmenter.IAlignedSegmentsgetAlignedSegments()Creates a newIAlignedSegmentsobject to access and manipulate the segments of this text unit.intgetReferenceCount()Gets the number of time this referent is referenced to.TextContainergetSource()Gets the source object for this text unit (aTextContainerobject).Map<String,Property>getSourceProperties()PropertygetSourceProperty(String name)Gets the source property for a given name.Set<String>getSourcePropertyNames()Gets the names of all the source properties for this resource.SegmentgetSourceSegment(String segId, boolean createIfNeeded)Gets the source segment for a given segment id.ISegmentsgetSourceSegments()Gets the segments for the source.TextContainergetTarget(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.PropertygetTargetProperty(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.SegmentgetTargetSegment(LocaleId trgLoc, String segId, boolean createIfNeeded)Gets the segment for a given segment id in a given target.ISegmentsgetTargetSegments(LocaleId trgLoc)Get the segments for a given target.StringgetType()Gets the type information associated with this resource.booleanhasSourceProperty(String name)Indicates if a source property exists for a given name.booleanhasTarget(LocaleId locId)Indicates if there is a target object for a given locale for this text unit.booleanhasTargetProperty(LocaleId locId, String name)Indicates if a property exists for a given name and target locale.booleanisEmpty()Indicates if the source text of this TextUnit is empty.booleanisReferent()Indicates if this resource is a referent (i.e.booleanisTranslatable()Indicates if the content of this resource is translatable.booleanpreserveWhitespaces()Indicates if the white-spaces in the content of this resource should be preserved.voidremoveAllSegmentations()Removes all segmentations (source and targets) in this text unit.voidremoveSourceProperty(String name)Removes a source property of a given name.voidremoveTarget(LocaleId locId)Removes a given target object from this text unit.voidremoveTargetProperty(LocaleId locId, String name)Removes a target property of a given name.voidsetIsReferent(boolean value)Sets the flag indicating if this resource is a referent (i.e.voidsetIsTranslatable(boolean value)Sets the flag indicating if the content of this resource is translatable.voidsetPreserveWhitespaces(boolean value)sets the flag indicating if the white-spaces in the content of this resource should be preserved.voidsetReferenceCount(int value)Sets the number of time this referent is referenced to.voidsetSkeleton(ISkeleton skeleton)Sets the skeleton object for this resource.TextContainersetSource(TextContainer textContainer)Sets the source object for this TextUnit.TextFragmentsetSourceContent(TextFragment content)Sets the content of the source for this TextUnit.PropertysetSourceProperty(Property property)Sets a source property.TextContainersetTarget(LocaleId locId, TextContainer text)Sets the target object for this text unit for a given locale.TextFragmentsetTargetContent(LocaleId locId, TextFragment content)Sets the content of the target for a given locale for this TextUnit.PropertysetTargetProperty(LocaleId locId, Property property)Sets a target property.voidsetType(String value)Sets the type information associated with this resource.StringtoString()Gets the string representation of the default source container.-
Methods inherited from class net.sf.okapi.common.resource.BaseReferenceable
getParentId, setParentId
-
Methods inherited from class net.sf.okapi.common.resource.BaseNameable
getAnnotation, getAnnotations, getId, getMimeType, getName, getProperties, getProperty, getPropertyNames, getSkeleton, hasProperty, removeProperty, setAnnotation, setId, setMimeType, setName, setProperty
-
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, setMimeType, setName
-
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.
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Description copied from interface:ITextUnitIndicates if the source text of this TextUnit is empty.
-
getSource
public TextContainer getSource()
Description copied from interface:ITextUnitGets the source object for this text unit (aTextContainerobject).
-
setSource
public TextContainer setSource(TextContainer textContainer)
Description copied from interface:ITextUnitSets the source object for this TextUnit. Any existing source object is overwritten.
-
setSourceContent
public TextFragment setSourceContent(TextFragment content)
Description copied from interface:ITextUnitSets the content of the source for this TextUnit.- Specified by:
setSourceContentin 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:ITextUnitCreates or get the target for this TextUnit.If a variant source exists for the target locale, creationOptions apply to the variant source.
- Specified by:
createTargetin 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:ITextUnitGets the target object for this text unit for a given locale. If the target does not exists a null is retruned.- Specified by:
getTargetin 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:ITextUnitSets 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:ITextUnitRemoves 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:
removeTargetin interfaceITextUnit- Parameters:
locId- the target locale to remove.
-
hasTarget
public boolean hasTarget(LocaleId locId)
Description copied from interface:ITextUnitIndicates 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:ITextUnitSets 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:
setTargetContentin 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:ITextUnitCreates a newIAlignedSegmentsobject to access and manipulate the segments of this text unit.- Specified by:
getAlignedSegmentsin interfaceITextUnit- Returns:
- a new
IAlignedSegmentsobject.
-
getSourceSegments
public ISegments getSourceSegments()
Description copied from interface:ITextUnitGets the segments for the source. Un-segmented content return a single segment.- Specified by:
getSourceSegmentsin interfaceITextUnit- Returns:
- an object implementing ISegments for the source content.
-
getSourceSegment
public Segment getSourceSegment(String segId, boolean createIfNeeded)
Description copied from interface:ITextUnitGets the source segment for a given segment id.If the segment does not exists, one is created if
createIfNeededis true.- Specified by:
getSourceSegmentin 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:ITextUnitGet 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:
getTargetSegmentsin 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:ITextUnitGets 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
createIfNeededis true.- Specified by:
getTargetSegmentin 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:IWithTargetPropertiesGets all the target locales for this resource.- Specified by:
getTargetLocalesin interfaceIWithTargetProperties- Returns:
- all the target locales for this resource.
-
getType
public String getType()
Description copied from interface:INameableGets the type information associated with this resource. For example "button".- Specified by:
getTypein interfaceINameable- Overrides:
getTypein classBaseNameable- Returns:
- The type information associated with this resource.
-
setType
public void setType(String value)
Description copied from interface:INameableSets the type information associated with this resource. For example "button".- Specified by:
setTypein interfaceINameable- Overrides:
setTypein classBaseNameable- Parameters:
value- The new type information.
-
isTranslatable
public boolean isTranslatable()
Description copied from interface:INameableIndicates if the content of this resource is translatable. By default this indicator is set to true for all resources.- Specified by:
isTranslatablein interfaceINameable- Overrides:
isTranslatablein classBaseNameable- Returns:
- True if the content of this resource is translatable. False if it is not translatable.
-
setIsTranslatable
public void setIsTranslatable(boolean value)
Description copied from interface:INameableSets the flag indicating if the content of this resource is translatable.- Specified by:
setIsTranslatablein interfaceINameable- Overrides:
setIsTranslatablein classBaseNameable- Parameters:
value- True to indicate that the content of this resource is translatable.
-
preserveWhitespaces
public boolean preserveWhitespaces()
Description copied from interface:INameableIndicates if the white-spaces in the content of this resource should be preserved. By default this indicator is set to false for all resources.- Specified by:
preserveWhitespacesin interfaceINameable- Overrides:
preserveWhitespacesin classBaseNameable- Returns:
- True if the white-spaces in the content of this resource should be preserved.
-
setPreserveWhitespaces
public void setPreserveWhitespaces(boolean value)
Description copied from interface:INameablesets the flag indicating if the white-spaces in the content of this resource should be preserved.- Specified by:
setPreserveWhitespacesin interfaceINameable- Overrides:
setPreserveWhitespacesin classBaseNameable- Parameters:
value- True to indicate that the white-spaces in the content of this resource should be preserved.
-
setSkeleton
public void setSkeleton(ISkeleton skeleton)
Description copied from interface:IWithSkeletonSets the skeleton object for this resource.- Specified by:
setSkeletonin interfaceIWithSkeleton- Overrides:
setSkeletonin classBaseNameable- Parameters:
skeleton- the skeleton object to set.
-
isReferent
public boolean isReferent()
Description copied from interface:IReferenceableIndicates if this resource is a referent (i.e. is referred to by another resource) or not.- Specified by:
isReferentin interfaceIReferenceable- Overrides:
isReferentin classBaseReferenceable- Returns:
- true if this resource is a referent, false if it is not.
-
setIsReferent
public void setIsReferent(boolean value)
Description copied from interface:IReferenceableSets the flag indicating if this resource is a referent (i.e. is referred to by another resource) or not. This also sets the count of time this referent is referenced to 1.- Specified by:
setIsReferentin interfaceIReferenceable- Overrides:
setIsReferentin classBaseReferenceable- Parameters:
value- true if the resource is a referent, false if it is not.
-
getReferenceCount
public int getReferenceCount()
Description copied from interface:IReferenceableGets the number of time this referent is referenced to.- Specified by:
getReferenceCountin interfaceIReferenceable- Overrides:
getReferenceCountin classBaseReferenceable- Returns:
- the number of time this referent is referenced to.
-
setReferenceCount
public void setReferenceCount(int value)
Description copied from interface:IReferenceableSets the number of time this referent is referenced to.- Specified by:
setReferenceCountin interfaceIReferenceable- Overrides:
setReferenceCountin classBaseReferenceable- Parameters:
value- the number of time this referent is referenced to.
-
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:
toStringin classBaseNameable- Returns:
- the string representation of the source container.
-
clone
public TextUnit clone()
Clones this TextUnit.
-
removeAllSegmentations
public void removeAllSegmentations()
Description copied from interface:ITextUnitRemoves all segmentations (source and targets) in this text unit. All entries are converted to non-segmented entries.- Specified by:
removeAllSegmentationsin interfaceITextUnit
-
createSourceSegmentation
public void createSourceSegmentation(ISegmenter segmenter)
Description copied from interface:ITextUnitSegments the default source content based on the rules provided by a given ISegmenter.- Specified by:
createSourceSegmentationin interfaceITextUnit- Parameters:
segmenter- the segmenter to use to create the segments.
-
createTargetSegmentation
public void createTargetSegmentation(ISegmenter segmenter, LocaleId targetLocale)
Description copied from interface:ITextUnitSegments the specified target content based on the rules provided by a given ISegmenter.- Specified by:
createTargetSegmentationin interfaceITextUnit- Parameters:
segmenter- the segmenter to use to create the segments.targetLocale-LocaleIdof the target we want to segment.
-
getSourceProperty
public Property getSourceProperty(String name)
Description copied from interface:IWithSourcePropertiesGets the source property for a given name.- Specified by:
getSourcePropertyin 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:IWithSourcePropertiesSets a source property. If a property already exists it is overwritten.- Specified by:
setSourcePropertyin 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:IWithSourcePropertiesRemoves a source property of a given name. If the property does not exists nothing happens.- Specified by:
removeSourcePropertyin interfaceIWithSourceProperties- Parameters:
name- The name of the property to remove.
-
getSourcePropertyNames
public Set<String> getSourcePropertyNames()
Description copied from interface:IWithSourcePropertiesGets the names of all the source properties for this resource.- Specified by:
getSourcePropertyNamesin interfaceIWithSourceProperties- Returns:
- All the names of the source properties for this resource.
-
hasSourceProperty
public boolean hasSourceProperty(String name)
Description copied from interface:IWithSourcePropertiesIndicates if a source property exists for a given name.- Specified by:
hasSourcePropertyin 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:IWithTargetPropertiesGets the target property for a given name and target locale.- Specified by:
getTargetPropertyin 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:IWithTargetPropertiesSets a target property. If a property already exists it is overwritten.- Specified by:
setTargetPropertyin 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:IWithTargetPropertiesRemoves a target property of a given name. If the property does not exists nothing happens.- Specified by:
removeTargetPropertyin 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:IWithTargetPropertiesGets the names of all the properties for a given target locale in this resource.- Specified by:
getTargetPropertyNamesin 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:IWithTargetPropertiesIndicates if a property exists for a given name and target locale.- Specified by:
hasTargetPropertyin 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:
getSourcePropertiesin interfaceIWithSourceProperties- Returns:
Mapof source properties for the implementer of interface
-
createTargetProperty
public Property createTargetProperty(LocaleId locId, String name, boolean overwriteExisting, int creationOptions)
Description copied from interface:IWithTargetPropertiesCreate targetPropertywithout access to the source property- Specified by:
createTargetPropertyin interfaceIWithTargetProperties- Returns:
-
-