public class TextContainer extends BaseNameable implements Iterable<TextPart>
The TextContainer is made of a collection of parts: Some are simple TextPart objects,
others are special TextPart objects called Segment.
A TextContainer has always at least one Segment part.
id, isTranslatable, mimeType, name, preserveWS, typeCOPY_ALL, COPY_CONTENT, COPY_PROPERTIES, COPY_SEGMENTATION, COPY_SEGMENTED_CONTENT, CREATE_EMPTY| Constructor and Description |
|---|
TextContainer()
Creates a new empty TextContainer object.
|
TextContainer(Segment segment)
Creates a new TextContainer object with an initial segment.
|
TextContainer(String text)
Creates a new TextContainer object with some initial text.
|
TextContainer(TextFragment fragment)
Creates a new TextContainer object with an initial TextFragment.
|
TextContainer(TextPart... parts)
Creates a new TextContainer object with initial
TextParts (segment or non-segment) appended. |
| Modifier and Type | Method and Description |
|---|---|
void |
append(String text)
Appends a part with a given text at the end of this container.
|
void |
append(String text,
boolean collapseIfPreviousEmpty)
Appends a part with a given text at the end of this container.
|
void |
append(TextFragment fragment)
Appends a part at the end of this container.
|
void |
append(TextFragment fragment,
boolean collapseIfPreviousEmpty)
Appends a part at the end of this container.
|
void |
append(TextFragment fragment,
boolean collapseIfPreviousEmpty,
boolean keepCodeIds)
Appends a part at the end of this container.
|
void |
append(TextPart part)
Appends a
TextPart (segment or non-segment) at the end of this container. |
void |
append(TextPart part,
boolean collapseIfPreviousEmpty)
Appends a
TextPart (segment or non-segment) at the end of this container. |
void |
changePart(int partIndex)
Changes the type of a given part.
|
void |
clear()
Clears this TextContainer, removes any existing segments.
|
TextContainer |
clone()
Clones this TextContainer, including the properties.
|
TextContainer |
clone(boolean cloneProperties)
Clones this container, with or without its properties.
|
int |
compareTo(TextContainer cont,
boolean codeSensitive)
Compares this container with another one.
|
boolean |
contentIsOneSegment()
Indicates if this container is made of a single segment that holds the
whole content (i.e.
|
static String[] |
contentToSplitStorage(TextContainer tc)
Create two storage strings to serialize a given
TextContainer. |
static String |
contentToString(TextContainer tc)
Creates a string that stores the content of a given container.
|
int |
count()
Gets the number of parts (segments and non-segments) in this container.
|
TextFragment |
createJoinedContent() |
TextPart |
get(int index)
Gets the part (segment or non-segment) for a given part index.
|
<A extends IAnnotation> |
getAnnotation(Class<A> type)
Gets the annotation object for a given class for this resource.
|
Annotations |
getAnnotations() |
String |
getCodedText()
Gets the coded text of the whole content (segmented or not).
|
TextFragment |
getFirstContent()
Gets the content of the first part (segment or non-segment) of this container.
|
Segment |
getFirstSegment()
Returns the first
Segment of this container. |
TextFragment |
getLastContent()
Gets the content of the last part (segment or non-segment) of this container.
|
List<TextPart> |
getParts() |
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.
|
ISegments |
getSegments()
Creates a new
ISegments object to access the segments of this container. |
TextFragment |
getUnSegmentedContentCopy()
Gets a new TextFragment representing the un-segmented content of this container.
|
boolean |
hasBeenSegmented()
Indicates if a segmentation has been applied to this container.
|
boolean |
hasProperty(String name)
Indicates if a resource-level property exists for a given name.
|
boolean |
hasText()
Indicates if this fragment contains at least one character that is 'text'
(inline codes, segment markers, and annotation markers do not count as 'text' characters).
|
boolean |
hasText(boolean whiteSpacesAreText)
Indicates if this container contains at least one character that is not a whitespace.
|
boolean |
hasText(boolean lookInSegments,
boolean whiteSpacesAreText)
Indicates if this container contains at least one character.
|
void |
insert(int partIndex,
TextPart part)
Inserts a given part (segment or non-segment) at a given position.
|
boolean |
isEmpty()
Indicates if this container is empty (no text and no codes).
|
Iterator<TextPart> |
iterator()
Creates an iterator to loop through the parts (segments and
non-segments) of this container.
|
void |
joinAll()
Merges back together all parts (segments and non-segments) of this container,
and clear the list of segments.
|
int |
joinWithNext(int partIndex,
int partCount)
Joins a given part with a specified number of its following parts.
|
void |
remove(int partIndex)
Removes the part at s given position.
|
void |
removeProperty(String name)
Removes a resource-level property of a given name.
|
void |
setAnnotation(IAnnotation annotation)
Sets an annotation object for this resource.
|
void |
setContent(TextFragment content)
Sets the content of this TextContainer.
|
TextContainer |
setContentFromString(String data)
Sets content of this TextContainer from a string created by
contentToString(TextContainer). |
void |
setHasBeenSegmentedFlag(boolean hasBeenSegmented)
Sets the flag indicating if the content of this container has been segmented.
|
void |
setParts(TextPart... parts) |
Property |
setProperty(Property property)
Sets a resource-level property.
|
void |
split(int partIndex,
int start,
int end,
boolean spannedPartIsSegment)
Splits a given part into two or three parts.
|
static TextContainer |
splitStorageToContent(String ctext,
String codes)
Creates a new
TextContainer object from two strings generated
with contentToSplitStorage(TextContainer). |
static TextContainer |
stringToContent(String data)
Converts a string created by
contentToString(TextContainer)
back into a TextContainer. |
String |
toString()
Gets the string representation of this container.
|
void |
unwrap(boolean trimEnds,
boolean collapseMode)
Unwraps the content of this container.
|
getId, getMimeType, getName, getProperties, getSkeleton, getType, isTranslatable, preserveWhitespaces, setId, setIsTranslatable, setMimeType, setName, setPreserveWhitespaces, setSkeleton, setTypeequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitforEach, spliteratorcopy, propertyIteratorannotationIterator, copy, getAnnotationsTypesAsSet, hasAnnotation, hasAnnotations, removepublic TextContainer()
public TextContainer(String text)
text - the initial text.public TextContainer(TextFragment fragment)
fragment - the initial TextFragment.public TextContainer(TextPart... parts)
TextParts (segment or non-segment) appended.parts - the given initial parts.public TextContainer(Segment segment)
segment - the initial segment.public ISegments getSegments()
ISegments object to access the segments of this container.ISegments object.public static String contentToString(TextContainer tc)
stringToContent(String) to create the container back from the string.
IMPORTANT: Only the content is saved (not the properties, annotations, etc.).
tc - the container holding the content to store.public static TextContainer stringToContent(String data)
contentToString(TextContainer)
back into a TextContainer.data - the string to process.public TextContainer setContentFromString(String data)
contentToString(TextContainer).data - the string to process.public static String[] contentToSplitStorage(TextContainer tc)
TextContainer.
Use splitStorageToContent(String, String) to create the container back from the strings.
IMPORTANT: Only the content is saved (not the properties, annotations, etc.).
tc - the text container to store.String objects: The first one contains the coded text
parts, the second one contains the codes.splitStorageToContent(String, String)public static TextContainer splitStorageToContent(String ctext, String codes)
TextContainer object from two strings generated
with contentToSplitStorage(TextContainer).ctext - the string holding the coded text parts.codes - the string holding the codes.TextContainer object created from the strings.contentToSplitStorage(TextContainer)public String toString()
toString in class BaseNameablepublic Iterator<TextPart> iterator()
public int compareTo(TextContainer cont, boolean codeSensitive)
cont - the other container to compare this one with.codeSensitive - true if the codes need to be compared as well.public boolean hasBeenSegmented()
contentIsOneSegment() to
check if the container counts only one segment (whether is is the result of a segmentation
or simply the default single segment).
This method return true if any method that may cause the content to be segmented has been called, and no operation has resulted in un-segmenting the content since that call, or if the content has more than one part.
setHasBeenSegmentedFlag(boolean)public void setHasBeenSegmentedFlag(boolean hasBeenSegmented)
hasBeenSegmented - true to flag the content has having been segmented, false to set it
has not having been segmented.hasBeenSegmented()public boolean contentIsOneSegment()
When this method returns true, the methods getFirstContent(),
ISegments.getFirstContent(), getLastContent() and
ISegments.getLastContent() return the same result.
count(),
ISegments.count()public void changePart(int partIndex)
partIndex - the index of the part to change. Note that even if the part is a segment
this index must be the part index not the segment index.public void insert(int partIndex,
TextPart part)
If the part to insert is a segment, its id is validated.
partIndex - the position where to insert the new part.part - the part to insert.public void remove(int partIndex)
If the selected part is the last segment in the content, the part is only cleared, not removed.
partIndex - the position of the part to remove.public void append(TextFragment fragment, boolean collapseIfPreviousEmpty)
If collapseIfPreviousEmpty and if the current last part (segment or non-segment) is empty, the text fragment is appended to the last part. Otherwise the text fragment is appended to the content as a new non-segment part.
Important: If the container is empty, the appended part becomes a segment, as the container has always at least one segment.
fragment - the text fragment to append.collapseIfPreviousEmpty - true to collapse the previous part if it is empty.public void append(TextFragment fragment, boolean collapseIfPreviousEmpty, boolean keepCodeIds)
If collapseIfPreviousEmpty and if the current last part (segment or non-segment) is empty, the text fragment is appended to the last part. Otherwise the text fragment is appended to the content as a new non-segment part.
Important: If the container is empty, the appended part becomes a segment, as the container has always at least one segment.
fragment - the text fragment to append.collapseIfPreviousEmpty - true to collapse the previous part if it is empty.keepCodeIds - true to block code balancing.public void append(TextFragment fragment)
This call is the same as calling append(TextFragment, boolean) with collapseIfPreviousEmpty
set to true.
fragment - the text fragment to append.public void append(String text, boolean collapseIfPreviousEmpty)
If collapseIfPreviousEmpty is true and if the current last part (segment or non-segment) is empty, the new text is appended to the last part part. Otherwise the text is appended to the content as a new non-segment part.
text - the text to append.collapseIfPreviousEmpty - true to collapse the previous part if it is empty.public void append(String text)
This call is the same as calling append(String, boolean)
with collapseIfPreviousEmpty set to true.
text - the text to append.public void append(TextPart part, boolean collapseIfPreviousEmpty)
TextPart (segment or non-segment) at the end of this container.
If collapseiIfPreviousEmpty is true and if the current last part (segment or non-segment) is empty, the new part replaces the last part. Otherwise the part is appended to the container as it. If the result of the operation would result in a container without segment, the first part is automatically converted to a fragment.
part - the TextPart to append.collapseIfPreviousEmpty - true to collapse the previous part if it is empty.public void append(TextPart part)
TextPart (segment or non-segment) at the end of this container.
This call is the same as calling append(TextPart, boolean)
with collapseIfPreviousEmpty set to true.
part - the TextPart to append.public String getCodedText()
ISegments.create(int, int) or ISegments.create(List) or other methods.ISegments.create(int, int),
ISegments.create(List)public void split(int partIndex,
int start,
int end,
boolean spannedPartIsSegment)
partIndex - index of the part to split.start - start of the middle part to create.end - position just after the last character of the middle part to create.spannedPartIsSegment - true if the new middle part should be a segment,
false if it should be a non-segment.public void unwrap(boolean trimEnds,
boolean collapseMode)
This method replaces any sequences of white-spaces by a single space character. It also removes leading and trailing white-spaces if the parameter trimEnds is set to true.
White spaces in this context are #x9, #xA and #x20. #xD is not considered a whitespace as the content of a text container must have its line-breaks normalized to #xA.
If the container has more than one segment and if collapseMode mode is set: non-segments parts are normalized and removed if they end up empty. If the option is not set: the method preserve at least one space between segments, even if the segments are empty.
Empty segments are always left.
Currently there is no provision to not unwrap a given span of the content.
trimEnds - true to remove leading and trailing white-spaces.collapseMode - true to remove non-segments parts that end up empty after the unwrapping.public TextFragment getFirstContent()
This method always returns the same result as ISegments.getFirstContent()
if contentIsOneSegment() is true.
ISegments.getFirstContent(),
getLastContent(),
ISegments.getLastContent()public TextFragment getLastContent()
This method always returns the same result as ISegments.getLastContent() if contentIsOneSegment().
ISegments.getLastContent(),
getFirstContent(),
ISegments.getFirstContent()public TextContainer clone()
public TextContainer clone(boolean cloneProperties)
cloneProperties - indicates if the properties should be cloned.public TextFragment getUnSegmentedContentCopy()
Important: This is an expensive method.
public void setContent(TextFragment content)
content - the new content to set.public void setParts(TextPart... parts)
public void clear()
clear in interface IWithAnnotationspublic boolean hasText(boolean lookInSegments,
boolean whiteSpacesAreText)
lookInSegments - indicates if the possible segments in this containers should be
looked at. If this parameter is set to false, the segment marker are treated as codes.whiteSpacesAreText - indicates if whitespaces should be considered
text characters or not.public boolean hasText(boolean whiteSpacesAreText)
whiteSpacesAreText - indicates if whitespaces should be considered
text characters or not.public boolean hasText()
hasText(boolean, boolean)
with the parameters true and false.public boolean isEmpty()
public boolean hasProperty(String name)
IWithPropertieshasProperty in interface IWithPropertiesname - The name of the resource-level property to query.public Property getProperty(String name)
IWithPropertiesgetProperty in interface IWithPropertiesname - Name of the property to retrieve.public Property setProperty(Property property)
IWithPropertiessetProperty in interface IWithPropertiesproperty - The new property to set.public void removeProperty(String name)
IWithPropertiesremoveProperty in interface IWithPropertiesname - The name of the property to remove.public Set<String> getPropertyNames()
IWithPropertiesgetPropertyNames in interface IWithPropertiespublic Annotations getAnnotations()
getAnnotations in interface IWithAnnotationsgetAnnotations in class BaseNameableAnnotations for the implementer of interfacepublic <A extends IAnnotation> A getAnnotation(Class<A> type)
IWithAnnotationsgetAnnotation in interface IWithAnnotationspublic void setAnnotation(IAnnotation annotation)
IWithAnnotationssetAnnotation in interface IWithAnnotationsannotation - the annotation object to set.public TextPart get(int index)
index - the index of the part to retrieve. the first part has the index 0,
the second has the index 1, etc.IndexOutOfBoundsException - if the index is out of bounds.ISegments.get(int)public int count()
ISegments.count()public TextFragment createJoinedContent()
public void joinAll()
public int joinWithNext(int partIndex,
int partCount)
If the resulting part is the only part in the container and is not a segment, it is set automatically changed into a segment.
joinWithNext(0, -1) has the same effect as joinAll();
partIndex - the index of the part where to append the following parts.partCount - the number of parts to join. You can use -1 to indicate all the parts
after the initial one.Copyright © 2021. All rights reserved.