Package net.sf.okapi.lib.xliff2.core
Class Unit
- java.lang.Object
-
- net.sf.okapi.lib.xliff2.core.InheritedData
-
- net.sf.okapi.lib.xliff2.core.Unit
-
- All Implemented Interfaces:
Iterable<Part>,IWithChangeTrack,IWithExtAttributes,IWithExtElements,IWithInheritedData,IWithMetadata,IWithNotes,IWithStore,IWithValidation,IWithITSAttributes,IWithITSGroups
public class Unit extends InheritedData implements Iterable<Part>, IWithStore, IWithITSAttributes, IWithITSGroups
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DataCategoryGroup<?>addITSGroup(DataCategoryGroup<?> group)Adds an ITS group to this object.voidaddNote(Note note)Adds a note to the list of notes.PartappendIgnorable()Appends an empty ignorable part to this unit.SegmentappendSegment()Appends a new empty segment to this unit.booleandoNonEmptySourcesHaveNonEmptyTargets()Indicates if all parts (segments and ignorables) of this unit that have a non-empty source have also a non-empty target.List<Match>getAllExactMatches()Retrieves all exact matches from this unit, i.e.List<AnnotatedSpan>getAnnotatedSpans(boolean target)Creates a list of the annotated spans in this unit.ChangeTrackgetChangeTrack()Gets theChangeTrackobject for this element, creates an empty of if there is none.ExtAttributesgetExtAttributes()Gets theExtAttributesobject for the parent object.StringgetExtAttributeValue(String namespaceURI, String localName)Gets the value for a given extension attribute.ExtElementsgetExtElements()Gets theExtElementsobject associated with this object.GlossarygetGlossary()Gets theGlossaryobject for this unit, creates an empty of if there is none.StringgetId()Gets the id for this object.List<DataCategoryGroup<?>>getITSGroups()Gets the ITS groups associated with this object.ITSItemsgetITSItems()Gets the ITS items associated with this object.MatchesgetMatches()Gets theMatchesobject for this unit, creates an empty of if there is none.List<Match>getMatchesByMinimumSimilarity(double minSim)Retrieves all matches from this unit with the given minimum similarity.List<Match>getMatchesByRef(String ref)Retrieves all matches from this unit for the given ref attribute value.List<Match>getMatchesBySimilarityRange(double minSim, double maxSim)Retrieves all matches from this unit in the given similarity range.List<Match>getMatchesForSegment(int segIdx)Retrieves all matches from this unit for the given segment (zero-based index)MetadatagetMetadata()Gets theMetadataobject for the parent, creates an empty of if there is none.StringgetName()Gets the name for this group.intgetNoteCount()Gets the number of notes for this object.NotesgetNotes()Gets theNotesobject for this object.ObjectgetObjectFromId(String id)Gets the object associated with a given span-class id in this unit.List<CTag>getOrderedCTags(boolean target)Creates the list ofCTagin the source or target content, in their respective order.PartgetPart(int partIndex)Gets the part at a given index.intgetPartCount()Gets the number of parts in this unit.StringgetPlainText(boolean target, boolean useSourceForMissingTargetIgnorables)Gets the plain text version of the full content of this unit, for either the source or the target.booleangetPreserveWS()Indicates if the whitespace of this part's content must be preserved.SegmentgetSegment(int segIndex)Gets the segment at a given index.intgetSegmentCount()Gets the number of segments in this unit.Iterable<Segment>getSegments()Creates anIterableobject for the segments in this unit.ObjectgetSourceOrTargetReference(String ref)Gets the span-like object associated with a given id in this unit.StoregetStore()Gets the store for this unit.List<Part>getTargetOrderedParts()Gets the list of the parts for this unit in the order specified for the target content.List<Boolean>getTranslateStateEndings(boolean doSource)Creates a list of booleans corresponding to the translate state at the end of each part in this unit.StringgetType()Gets the type for this group.ValidationgetValidation()Gets theValidationobject for the parent, creates an empty of if there is none.booleanhasChangeTrack()Check if there is at least a revision for this element.booleanhasExtAttribute()Indicates if at least one extension attribute is present.booleanhasExtElements()Indicates if there is aExtElementsobject associated with this object.booleanhasGlossEntry()Indicates if this unit has at least one glossary entry.booleanhasITSGroup()Indicates if the object has at least one ITS group.booleanhasITSItem()Indicates if the object has at least one ITS item.booleanhasMatch()Indicates if this unit has at least one match.booleanhasMetadata()Indicates if the object has metadata.booleanhasTargetOrder()Indicates if this unit has at least one target part not in the same order as the source.booleanhasValidation()Indicates if the object has validation data.voidhideProtectedContent()Hides all protected spans of this unit intoPContobjects.booleanisIdUsed(String id)Iterator<Part>iterator()Creates a new iterator to loop through the segments and ignorables of this unit.voidjoin(int startPartIndex, int endPartIndex, boolean restrictedJoin, boolean adjustTargetIgnorable)Joins two or more parts together into the first one.voidjoinAll(boolean adjustTargetIgnorable)voidremoveMarkers()Removes all the annotation markers from the source and target (if a target is available) in this unit.voidsetChangeTrack(ChangeTrack changeTrack)Sets theChangeTrackobject for this element.voidsetExtAttributes(ExtAttributes attributes)Sets theExtAttributesobject associated with this object.ExtElementssetExtElements(ExtElements elements)Sets theExtElementsobject associated with this object.voidsetGlossary(Glossary glossary)sets theGlossaryobject for this unit.voidsetId(String id)Sets the id for this object.voidsetITSItems(ITSItems itsItems)Sets the list of the ITS items associated with this object.voidsetMatches(Matches matches)sets theMatchesobject for this unit.voidsetMetadata(Metadata metadata)sets theMetadataobject for the parent.voidsetName(String name)Sets the name for this group.voidsetPreserveWS(boolean preserveWS)Sets the flag indicating if the whitespace of this part's content must be preserved.voidsetType(String type)Sets the type for this group.voidsetValidation(Validation validation)sets theValidationobject for the parent.voidshowProtectedContent()Show allPContreferences in this unit into content.voidsplit(int partIndex, int srcStart, int srcEnd, int trgStart, int trgEnd, boolean changeState)Splits a segment.voidverifyOpeningsBeforeClosings(boolean target)Verifies that all opening tags in the source or targt content of this unit are located before their closing counterparts.voidverifyReadOnlyTags()Verifies if the non-removable tags in the source content of this unit are present in the target.-
Methods inherited from class net.sf.okapi.lib.xliff2.core.InheritedData
getAnnotatorsRef, getCanResegment, getSourceDir, getTargetDir, getTranslate, setAnnotatorsRef, setCanResegment, setInheritableData, setSourceDir, setTargetDir, setTranslate
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface net.sf.okapi.lib.xliff2.its.IWithITSAttributes
getId
-
Methods inherited from interface net.sf.okapi.lib.xliff2.core.IWithStore
getSourceDir, getTargetDir, setSourceDir, setTargetDir
-
-
-
-
Constructor Detail
-
Unit
public Unit(Unit original)
Copy constructor.Important: Most of the time you MUST change the id of the resulting unit as a unit id must be unique within its parent file element.
- Parameters:
original- the original unit to duplicate.
-
Unit
public Unit(String id, StartFileData startFileData)
Creates a newUnitobject with a given id and file context.- Parameters:
id- the id of the unit.startFileData- the file context (can be null).
-
-
Method Detail
-
iterator
public Iterator<Part> iterator()
Creates a new iterator to loop through the segments and ignorables of this unit.
-
getPartCount
public int getPartCount()
Gets the number of parts in this unit.- Returns:
- the number of parts in this unit.
-
getSegmentCount
public int getSegmentCount()
Gets the number of segments in this unit.- Returns:
- the number of segments in this unit.
-
appendSegment
public Segment appendSegment()
Appends a new empty segment to this unit.- Returns:
- the new segment created.
-
appendIgnorable
public Part appendIgnorable()
Appends an empty ignorable part to this unit.- Returns:
- the new ignorable part created.
-
getPart
public Part getPart(int partIndex)
Gets the part at a given index.- Parameters:
partIndex- the index of the part to retrieve (between 0 andgetPartCount()-1).- Returns:
- the
Partobject at the given index position, the part may or may not be aSegment. - Throws:
IndexOutOfBoundsException- if the index is invalid.
-
getSegment
public Segment getSegment(int segIndex)
Gets the segment at a given index. Note that the retrieval of the object is not direct, but rely on looping through the parts.- Parameters:
segIndex- the index of the segment to retrieve (between 0 andgetSegmentCount()-1).- Returns:
- the
Segmentobject at the given index position. - Throws:
IndexOutOfBoundsException- if the index is invalid.
-
getStore
public Store getStore()
Gets the store for this unit.- Specified by:
getStorein interfaceIWithStore- Returns:
- the
Storeobject for this unit.
-
isIdUsed
public boolean isIdUsed(String id)
Description copied from interface:IWithStore- Specified by:
isIdUsedin interfaceIWithStore- Parameters:
id- the id value to lookup.- Returns:
- true if the value is already used, false otherwise.
-
getObjectFromId
public Object getObjectFromId(String id)
Gets the object associated with a given span-class id in this unit.The objects checked are: the parts (including segments) and all the tags except the
PContobjects.- Parameters:
id- the id to look for.- Returns:
- the object found, or null if not found.
-
split
public void split(int partIndex, int srcStart, int srcEnd, int trgStart, int trgEnd, boolean changeState)Splits a segment.- Parameters:
partIndex- the part index of the segment to split.srcStart- the start position of the middle new segment for the source (inclusive, in coded text).srcEnd- the end position of the middle new segment for the source (exclusive, in coded text), use -1 for the end of the current segment.trgStart- the start position of the middle new segment for the target (inclusive, in coded text).trgEnd- the end position of the middle new segment for the target (exclusive, in coded text), use -1 for the end of the current segment.changeState- true to change the state and possibly the subState attributes for the modified or added segments if the initial segment as a target and its state is other than "initial" and "translated". Use false to keep the same state and subState.
-
hasTargetOrder
public boolean hasTargetOrder()
Indicates if this unit has at least one target part not in the same order as the source.- Returns:
- true if there is a target-specific order for this unit, false otherwise
-
join
public void join(int startPartIndex, int endPartIndex, boolean restrictedJoin, boolean adjustTargetIgnorable)Joins two or more parts together into the first one.- Parameters:
startPartIndex- the index of the first part to join (in the target order)endPartIndex- the index of the last part to join (in the target order)restrictedJoin- true to throw an exception if one of the segment cannot be merged, false to allow to merge regardless of the canResegment values (merger mode)adjustTargetIgnorable- TODO
-
joinAll
public void joinAll(boolean adjustTargetIgnorable)
-
getSegments
public Iterable<Segment> getSegments()
Creates anIterableobject for the segments in this unit.Use
getTargetOrderedParts()to get a list of the parts in target order.- Returns:
- a new
Iterableobject for the segments in this unit.
-
getTargetOrderedParts
public List<Part> getTargetOrderedParts()
Gets the list of the parts for this unit in the order specified for the target content.- Returns:
- a list of all the parts, in the target order.
-
getPlainText
public String getPlainText(boolean target, boolean useSourceForMissingTargetIgnorables)
Gets the plain text version of the full content of this unit, for either the source or the target.- Parameters:
target- true to generate the text for the target, false for the source.useSourceForMissingTargetIgnorables- true to use the source content when generating the target text and a target ignorable is missing. this parameter is ignored when generating the source text.- Returns:
- the plain text full content requested.
-
hideProtectedContent
public void hideProtectedContent()
Hides all protected spans of this unit intoPContobjects. No target parts are created.- See Also:
showProtectedContent()
-
getTranslateStateEndings
public List<Boolean> getTranslateStateEndings(boolean doSource)
Creates a list of booleans corresponding to the translate state at the end of each part in this unit.- Parameters:
doSource- true to generates the list from the source viewpoint, false to do it for the target.- Returns:
- a list of booleans where true means the state of translate at the end of the corresponding part is 'yes', and false means it is 'no'.
-
showProtectedContent
public void showProtectedContent()
Show allPContreferences in this unit into content.- See Also:
hideProtectedContent()
-
verifyOpeningsBeforeClosings
public void verifyOpeningsBeforeClosings(boolean target)
Verifies that all opening tags in the source or targt content of this unit are located before their closing counterparts.- Parameters:
target- true to verify the target, false to verify the source.
-
verifyReadOnlyTags
public void verifyReadOnlyTags()
Verifies if the non-removable tags in the source content of this unit are present in the target. Note that only the source tags that are in parts with an existing target are verified (as the absence of target is allowed).
-
getSourceOrTargetReference
public Object getSourceOrTargetReference(String ref)
Gets the span-like object associated with a given id in this unit.- Parameters:
ref- the reference of the object.- Returns:
- the object or null if not found.
-
getOrderedCTags
public List<CTag> getOrderedCTags(boolean target)
Creates the list ofCTagin the source or target content, in their respective order. This method does not check content inside protected text.- Parameters:
target- true to create the target list (in target order).- Returns:
- the list of codes.
-
doNonEmptySourcesHaveNonEmptyTargets
public boolean doNonEmptySourcesHaveNonEmptyTargets()
Indicates if all parts (segments and ignorables) of this unit that have a non-empty source have also a non-empty target.- Returns:
- true if all source parts that are not empty have a non-empty target too, false otherwise.
-
removeMarkers
public void removeMarkers()
Removes all the annotation markers from the source and target (if a target is available) in this unit.
-
addITSGroup
public DataCategoryGroup<?> addITSGroup(DataCategoryGroup<?> group)
Description copied from interface:IWithITSGroupsAdds an ITS group to this object.- Specified by:
addITSGroupin interfaceIWithITSGroups- Parameters:
group- the group to add.- Returns:
- the group added.
-
hasITSGroup
public boolean hasITSGroup()
Description copied from interface:IWithITSGroupsIndicates if the object has at least one ITS group.- Specified by:
hasITSGroupin interfaceIWithITSGroups- Returns:
- true if the object has at least one ITS group.
-
getITSGroups
public List<DataCategoryGroup<?>> getITSGroups()
Description copied from interface:IWithITSGroupsGets the ITS groups associated with this object.- Specified by:
getITSGroupsin interfaceIWithITSGroups- Returns:
- the ITS groups associated with this object (can be empty, but never null).
-
hasITSItem
public boolean hasITSItem()
Description copied from interface:IWithITSAttributesIndicates if the object has at least one ITS item.- Specified by:
hasITSItemin interfaceIWithITSAttributes- Returns:
- true if the object has at least one ITS item.
-
getITSItems
public ITSItems getITSItems()
Description copied from interface:IWithITSAttributesGets the ITS items associated with this object.- Specified by:
getITSItemsin interfaceIWithITSAttributes- Returns:
- the ITS items associated with this object (can be empty, but never null).
-
setITSItems
public void setITSItems(ITSItems itsItems)
Description copied from interface:IWithITSAttributesSets the list of the ITS items associated with this object.- Specified by:
setITSItemsin interfaceIWithITSAttributes- Parameters:
itsItems- the new items to set.
-
hasMatch
public boolean hasMatch()
Indicates if this unit has at least one match.- Returns:
- true if this unit has at least one match, false if not.
-
getMatches
public Matches getMatches()
Gets theMatchesobject for this unit, creates an empty of if there is none.- Returns:
- the
Matchesobject for this unit (can be empty, but never null).
-
setMatches
public void setMatches(Matches matches)
sets theMatchesobject for this unit.- Parameters:
matches- the newMatchesobject for this unit.
-
hasGlossEntry
public boolean hasGlossEntry()
Indicates if this unit has at least one glossary entry.- Returns:
- true if this unit has at least one glossary entry, false if not.
-
getGlossary
public Glossary getGlossary()
Gets theGlossaryobject for this unit, creates an empty of if there is none.- Returns:
- the
Glossaryobject for this unit (can be empty, but never null).
-
setGlossary
public void setGlossary(Glossary glossary)
sets theGlossaryobject for this unit.- Parameters:
glossary- the newGlossaryobject for this unit.
-
getAnnotatedSpans
public List<AnnotatedSpan> getAnnotatedSpans(boolean target)
Creates a list of the annotated spans in this unit.The
PartandMTagobjects in the list are live references, but any static data (e.g. the start and end position of the span) are a snapshot of the information at the moment of the call, any change to the content may make that information out-of-date and invalid.The spans are listed in the order their opening markers appears in the coded text.
The call is expected to be made on a unit with no hidden protected content.
- Parameters:
target- true to lookup the target, false to lookup the source.- Returns:
- a list of
AnnotatedSpanobjects for this unit.
-
getAllExactMatches
public List<Match> getAllExactMatches()
Retrieves all exact matches from this unit, i.e. similarity >= 100.0.- Returns:
- An array list with the relevant matches
-
getMatchesByMinimumSimilarity
public List<Match> getMatchesByMinimumSimilarity(double minSim)
Retrieves all matches from this unit with the given minimum similarity.- Parameters:
minSim- The minimum similarity as a percentage, e.g. 75.0 for all matches with 75% or higher- Returns:
- An array list with the relevant matches
-
getMatchesBySimilarityRange
public List<Match> getMatchesBySimilarityRange(double minSim, double maxSim)
Retrieves all matches from this unit in the given similarity range.- Parameters:
minSim- The minimum similarity as a percentage, e.g. 75.0 for all matches with 75% or highermaxSim- The maximum similarity- Returns:
- An array list with the relevant matches
-
getMatchesForSegment
public List<Match> getMatchesForSegment(int segIdx)
Retrieves all matches from this unit for the given segment (zero-based index)- Parameters:
segIdx- The index of the segment (zero-based)- Returns:
- An array list with the relevant matches
-
getMatchesByRef
public List<Match> getMatchesByRef(String ref)
Retrieves all matches from this unit for the given ref attribute value.- Parameters:
ref- The value of the ref attribute, with or without the "#" prefix- Returns:
- An array list with the relevant matches
-
getPreserveWS
public boolean getPreserveWS()
Indicates if the whitespace of this part's content must be preserved.- Returns:
- true if the whitespace must be preserved, false if the whitespace can be modified.
-
setPreserveWS
public void setPreserveWS(boolean preserveWS)
Sets the flag indicating if the whitespace of this part's content must be preserved.- Parameters:
preserveWS- true to preserve the whitespace, false otherwise.
-
getExtElements
public ExtElements getExtElements()
Description copied from interface:IWithExtElementsGets theExtElementsobject associated with this object. If none exists one is created.- Specified by:
getExtElementsin interfaceIWithExtElements- Returns:
- the
ExtElementsobject associated with this object. If none exists one is created.
-
hasExtElements
public boolean hasExtElements()
Description copied from interface:IWithExtElementsIndicates if there is aExtElementsobject associated with this object.- Specified by:
hasExtElementsin interfaceIWithExtElements- Returns:
- true if there is a
ExtElementsobject associated with this object, false otherwise.
-
setExtElements
public ExtElements setExtElements(ExtElements elements)
Description copied from interface:IWithExtElementsSets theExtElementsobject associated with this object.- Specified by:
setExtElementsin interfaceIWithExtElements- Parameters:
elements- theExtElementsobject associated with this object. If null, a newExtElementsobject is created.- Returns:
- the
ExtElementsobject associated with this object, never null.
-
addNote
public void addNote(Note note)
Description copied from interface:IWithNotesAdds a note to the list of notes.- Specified by:
addNotein interfaceIWithNotes- Parameters:
note- the note to add.
-
getNotes
public Notes getNotes()
Description copied from interface:IWithNotesGets theNotesobject for this object.- Specified by:
getNotesin interfaceIWithNotes- Returns:
- the notes for this object. Never returns null.
-
getNoteCount
public int getNoteCount()
Description copied from interface:IWithNotesGets the number of notes for this object.- Specified by:
getNoteCountin interfaceIWithNotes- Returns:
- the number of notes available.
-
getName
public String getName()
Gets the name for this group.- Returns:
- the name for this group (can be null).
-
setName
public void setName(String name)
Sets the name for this group.- Parameters:
name- the new name to set (can be null).
-
getType
public String getType()
Gets the type for this group.- Returns:
- the type for this group (can be null).
-
setType
public void setType(String type)
Sets the type for this group. The value must have a prefix (for both unit and group elements)- Parameters:
type- the new type to set (can be null).
-
hasMetadata
public boolean hasMetadata()
Description copied from interface:IWithMetadataIndicates if the object has metadata.- Specified by:
hasMetadatain interfaceIWithMetadata- Returns:
- true if the object has metadata, false otherwise.
-
getMetadata
public Metadata getMetadata()
Description copied from interface:IWithMetadataGets theMetadataobject for the parent, creates an empty of if there is none.- Specified by:
getMetadatain interfaceIWithMetadata- Returns:
- the
Metadataobject for the parent (can be empty, but never null).
-
setMetadata
public void setMetadata(Metadata metadata)
Description copied from interface:IWithMetadatasets theMetadataobject for the parent.- Specified by:
setMetadatain interfaceIWithMetadata- Parameters:
metadata- the newMetadataobject for the parent.
-
hasValidation
public boolean hasValidation()
Description copied from interface:IWithValidationIndicates if the object has validation data.- Specified by:
hasValidationin interfaceIWithValidation- Returns:
- true if the object has validataion data, false otherwise.
-
getValidation
public Validation getValidation()
Description copied from interface:IWithValidationGets theValidationobject for the parent, creates an empty of if there is none.- Specified by:
getValidationin interfaceIWithValidation- Returns:
- the
Validationobject for the parent (can be empty, but never null).
-
setValidation
public void setValidation(Validation validation)
Description copied from interface:IWithValidationsets theValidationobject for the parent.- Specified by:
setValidationin interfaceIWithValidation- Parameters:
validation- the newValidationobject for the parent.
-
getChangeTrack
public ChangeTrack getChangeTrack()
Description copied from interface:IWithChangeTrackGets theChangeTrackobject for this element, creates an empty of if there is none.- Specified by:
getChangeTrackin interfaceIWithChangeTrack- Returns:
- the
ChangeTrackobject for this element (can be empty, never null).
-
setChangeTrack
public void setChangeTrack(ChangeTrack changeTrack)
Description copied from interface:IWithChangeTrackSets theChangeTrackobject for this element.- Specified by:
setChangeTrackin interfaceIWithChangeTrack- Parameters:
changeTrack- theChangeTrackobject for this element.
-
hasChangeTrack
public boolean hasChangeTrack()
Description copied from interface:IWithChangeTrackCheck if there is at least a revision for this element.- Specified by:
hasChangeTrackin interfaceIWithChangeTrack- Returns:
trueif there is at least a revision for this element;falseotherwise.
-
setId
public void setId(String id)
Sets the id for this object.- Parameters:
id- the id for this object.
-
getId
public String getId()
Gets the id for this object.- Returns:
- the id for this object.
-
setExtAttributes
public void setExtAttributes(ExtAttributes attributes)
Description copied from interface:IWithExtAttributesSets theExtAttributesobject associated with this object.- Specified by:
setExtAttributesin interfaceIWithExtAttributes- Parameters:
attributes- theExtAttributesobject associated with this object. If null, a newExtAttributesobject is created.
-
getExtAttributes
public ExtAttributes getExtAttributes()
Description copied from interface:IWithExtAttributesGets theExtAttributesobject for the parent object. If none exists, one is created.- Specified by:
getExtAttributesin interfaceIWithExtAttributes- Returns:
- the
ExtAttributesobject for the parent object, never null.
-
hasExtAttribute
public boolean hasExtAttribute()
Description copied from interface:IWithExtAttributesIndicates if at least one extension attribute is present.- Specified by:
hasExtAttributein interfaceIWithExtAttributes- Returns:
- true if at least one extension attribute is present; false otherwise.
-
getExtAttributeValue
public String getExtAttributeValue(String namespaceURI, String localName)
Description copied from interface:IWithExtAttributesGets the value for a given extension attribute.- Specified by:
getExtAttributeValuein interfaceIWithExtAttributes- Parameters:
namespaceURI- the URI of the namespace for the attribute.localName- the name of the attribute.- Returns:
- the value of the extension attribute, or null if it does not exist.
-
-