public class Fragment
extends java.lang.Object
implements java.lang.Iterable<java.lang.Object>, java.lang.Appendable
Modifier and Type | Field and Description |
---|---|
static char |
CODE_CLOSING
First character of the special pair indicating a reference to the closing tag of a code (a
CTag ). |
static char |
CODE_OPENING
First character of the special pair indicating a reference to the opening tag of a code (a
CTag ). |
static char |
CODE_STANDALONE
First character of the special pair indicating a reference to the standalone tag of a code (a
CTag ). |
static char |
MARKER_CLOSING
First character of the special pair indicating a reference to the closing tag of a marker (a
MTag ). |
static char |
MARKER_OPENING
First character of the special pair indicating a reference to the opening tag of a marker (a
MTag ). |
static char |
PCONT_STANDALONE
First character of the special pair indicating a reference to the standalone holder
for a protected content (a
PCont ). |
static int |
TAGREF_BASE
Base value for the tag reference index.
|
static int |
TAGREF_MAX
Maximum number of tag possible per unit for a given type of tag: 6127.
|
static java.util.regex.Pattern |
TAGREF_REGEX
Compiled regular expression for all possible kinds of tag reference (the two characters) in a coded text.
|
Constructor and Description |
---|
Fragment(Fragment original,
Store store,
boolean target)
Copy constructor.
|
Fragment(Store store,
boolean target)
Creates a new source or target
Fragment object. |
Fragment(Store store,
boolean target,
java.lang.String plainText)
Creates a new source or target
Fragment object with a content. |
Modifier and Type | Method and Description |
---|---|
int |
annotate(int start,
int end,
MTag opening)
Annotates a span of content in this fragment.
|
int |
annotate(int start,
int end,
java.lang.String type,
java.lang.String value,
java.lang.String ref)
Annotates a span of content in this fragment.
|
Note |
annotateWithNote(int start,
int end,
java.lang.String noteContent)
Annotates a span of content in this fragment with a
Note object and add the new note to the container. |
Fragment |
append(char ch)
Appends a character at the end of this fragment.
|
Fragment |
append(java.lang.CharSequence plainText)
Appends a plain text string to this fragment.
|
Fragment |
append(java.lang.CharSequence plainText,
int start,
int end)
Appends a sub-sequence of a given plain text string to this fragment.
|
Fragment |
append(Fragment fragment)
Appends a fragment to this fragment.
|
Tag |
append(Tag tag)
Appends a tag to this fragment.
|
CTag |
append(TagType tagType,
java.lang.String id,
java.lang.String data,
boolean canOverlap)
Appends a code tag.
|
CTag |
appendCode(java.lang.String id,
java.lang.String data)
Appends a standalone code to this fragment.
|
void |
checkPosition(int position)
Verifies if a given position in the coded text is on the second special
character of a tag reference.
|
void |
clear()
Clears the fragment: removes all tags and text.
|
CTag |
closeCodeSpan(java.lang.String id,
java.lang.String data)
Creates at the end of this fragment a closing tag for an existing code.
|
MTag |
closeMarkerSpan(java.lang.String id)
Appends a tag to close the marker of a given id.
|
Fragment |
delete(int start,
int end)
Deletes a section of this fragment (including any protected content within the section).
|
boolean |
equals(java.lang.Object object)
Indicates if this fragment equals another object.
|
int |
getClosingPosition(Tag opening)
Gets the closing position of a span of content in this fragment for a given opening tag.
|
Tag |
getClosingTag(Tag tag)
Gets the closing tag corresponding to a given opening tag within a unit.
|
java.lang.String |
getCodedText()
Gets the content of this fragment in coded text format.
|
static int |
getCodedTextPosition(java.lang.CharSequence codedText,
int plainTextPosition,
boolean leftOfTag)
Gets the coded text position in a given coded text string for a given plain text position.
|
int |
getCodedTextPosition(int plainTextPosition,
boolean leftOfTag)
Helper methods calling
getCodedTextPosition(CharSequence, int, boolean) with the coded
text of this fragment as the first parameter. |
CTag |
getCTag(java.lang.CharSequence ctext,
int pos)
Gets the
CTag for a given reference in a coded text. |
Directionality |
getDir(boolean resolved) |
<T> java.lang.Iterable<T> |
getIterable(java.lang.Class<T> type)
Creates a new instance of an iterable interface for a given class of objects.
|
MTag |
getMTag(java.lang.CharSequence ctext,
int pos)
Gets the
MTag for a given reference in a coded text. |
Tag |
getOpeningTag(Tag tag)
Gets the opening tag corresponding to a given closing tag within a unit.
|
MTag |
getOrCreateMarker(int start,
int end,
java.lang.String matchingType,
java.lang.String typeForNew)
Get or create an annotation marker for a given span of content.
|
java.util.List<Tag> |
getOwnTags()
Creates a list of all the source or target tags in this fragment.
|
java.util.Map<Tag,java.lang.Integer> |
getOwnTagsStatus()
Creates a map of the opening and closing tags in this fragment and their corresponding
status as tag: (0=isolated, 1=not-well-formed or 2=well-formed).
|
java.lang.String |
getPlainText()
Gets a plain text version (all tag references stripped out) of the content of this fragment.
|
Store |
getStore()
Gets the store associated with this fragment.
|
Tag |
getTag(java.lang.CharSequence ctext,
int pos)
|
Tag |
getTag(int key)
Gets the tag for a given key.
|
Tags |
getTags()
Gets the source or target tags for the unit where this segment is located.
|
Tag |
getWellFormedClosing(Tag opening,
int from)
Gets the well-formed closing tag for a given opening one, starting at a given character position.
|
static boolean |
hasContentAfter(java.lang.String codedText,
int position)
Indicates if a given coded text has any non-marker tags at or after a given position.
|
int |
hashCode() |
boolean |
hasTag()
Indicates if this fragment contains at least one tag.
|
Fragment |
insert(java.lang.CharSequence plainText,
int offset)
Inserts a plain text at a given position.
|
CTag |
insert(TagType tagType,
java.lang.String type,
java.lang.String id,
java.lang.String data,
int offset,
boolean connect,
boolean allowOrphan)
Inserts a code at a given position (including the end) of this fragment.
|
static boolean |
isChar1(char value)
Helper method that checks if a given character is the first special character
of a tag reference.
|
static boolean |
isCTag(char value)
Indicates if a given character is the first special character of a
CTag reference. |
boolean |
isEmpty()
Indicates if this fragment is empty.
|
boolean |
isTarget()
Indicates if this fragment is a target content.
|
java.util.Iterator<java.lang.Object> |
iterator()
Creates an iterator for all the different objects composing this fragment.
|
CTag |
openCodeSpan(java.lang.String id,
java.lang.String data)
Creates at the end of this fragment an opening tag for a new code.
|
MTag |
openMarkerSpan(java.lang.String id,
java.lang.String type)
Appends a tag opening a new marker span.
|
static void |
printCommonAttributes(CTag code,
Tags tags,
java.lang.StringBuilder tmp,
CTag closing,
boolean outputDataRefEnd)
Output the common attributes for a given CTag object.
|
static void |
printExtAttributes(Tag tag,
java.lang.StringBuilder output,
java.util.Stack<NSContext> nsStack) |
Fragment |
remove(Tag tag)
Removes a tag from this fragment (including if the tag is in a protected content).
|
void |
setCodedText(java.lang.String codedText)
Sets the coded text content for this fragment.
|
void |
setDir(Directionality dir)
Sets the directionality for this fragment.
|
void |
showProtectedContent()
Expands all
PCont references in this fragment into normal content. |
static char |
toChar1(int key)
Gets the first character of a tag reference from a given tag key.
|
static char |
toChar2(int key)
Gets the second character of a tag reference from a given tag key.
|
static int |
toKey(int c1,
int c2)
Converts the first and second character of a tag reference to the key of the tag.
|
static java.lang.String |
toRef(int key)
Converts a tag key to a reference as used in the coded text
|
java.lang.String |
toString()
Returns a string representation of the fragment: the text in coded text format.
|
java.lang.String |
toXLIFF()
Creates an XLIFF output of this fragment, without handling the inline codes original data,
without namespace content and without inherited data context.
|
java.lang.String |
toXLIFF(java.util.Stack<NSContext> nsStack,
java.util.Stack<InheritedData> context,
boolean withOriginalData)
Creates an XLIFF output for this fragment.
|
boolean |
unitHasOpening(CTag ctag)
Tests if the unit to which this fragment belongs has an opening code
for a given closing code.
|
public static final char CODE_OPENING
CTag
).public static final char CODE_CLOSING
CTag
).public static final char CODE_STANDALONE
CTag
).public static final char MARKER_OPENING
MTag
).public static final char MARKER_CLOSING
MTag
).public static final char PCONT_STANDALONE
PCont
).public static final int TAGREF_BASE
public static final int TAGREF_MAX
public static final java.util.regex.Pattern TAGREF_REGEX
public Fragment(Fragment original, Store store, boolean target)
original
- the original fragment to duplicate.store
- the store to attach to the new fragment.target
- true if the new fragment is a target, false if it is a source.public Fragment(Store store, boolean target)
Fragment
object.store
- the shared Store
for this object.target
- true if this fragment is a target fragment, false if it is a source fragment.public Fragment(Store store, boolean target, java.lang.String plainText)
Fragment
object with a content.store
- the shared Store
for this object.target
- true if this fragment is a target fragment, false if it is a source fragment.plainText
- the content of this fragment.public static int toKey(int c1, int c2)
Note that both parameters are passed as integer for convenience, but they are characters.
c1
- the first character of the reference (the type of inline object and the type of tag).c2
- the second character of the reference (the encoded 'index' part of the key)toChar1(int)
,
toChar2(int)
,
toRef(int)
public static char toChar1(int key)
key
- the key to process.toChar2(int)
,
toRef(int)
,
toKey(int, int)
public static char toChar2(int key)
key
- the key to process.toChar1(int)
,
toRef(int)
,
toKey(int, int)
public static java.lang.String toRef(int key)
key
- the key to converttoKey(int, int)
,
toChar1(int)
,
toChar2(int)
public static boolean isChar1(char value)
toKey(int, int)
.value
- the character to check.public static boolean isCTag(char value)
CTag
reference.value
- the character to check.CTag
reference.public static boolean hasContentAfter(java.lang.String codedText, int position)
codedText
- the coded text to process.position
- the position where to start checking.public static int getCodedTextPosition(java.lang.CharSequence codedText, int plainTextPosition, boolean leftOfTag)
codedText
- the coded text character sequence to use as the base for the conversion.plainTextPosition
- the plain text position to convert.leftOfTag
- true to return the left side position of the tag reference
when the plain text position is on a tag reference (e.g. for end of range stopping
before the reference). Use false to get the right side.getPlainText()
,
getCodedTextPosition(int, boolean)
public java.lang.String toString()
the coded text format is made of normal content and a pair of special characters for each tag in the content.
The method has the same effect as calling getCodedText()
.
toString
in class java.lang.Object
public java.lang.String getPlainText()
public java.lang.String getCodedText()
the coded text format is made of normal content and a pair of special characters for each tag in the content.
public void setCodedText(java.lang.String codedText)
getCodedText()
for more information on coded text.codedText
- the new coded text content to set.public Tags getTags()
public java.util.List<Tag> getOwnTags()
public java.util.Map<Tag,java.lang.Integer> getOwnTagsStatus()
isolated means to counterpart tag within the parent unit.
(not-)well-formed means (not-)well-formed within this fragment.
public Tag getTag(java.lang.CharSequence ctext, int pos)
ctext
- the coded text (e.g. String or StringBuilder object).pos
- the position of the first character of the reference.public CTag getCTag(java.lang.CharSequence ctext, int pos)
CTag
for a given reference in a coded text.ctext
- the coded text (e.g. String or StringBuilder object).pos
- the position of the first character of the reference.public MTag getMTag(java.lang.CharSequence ctext, int pos)
MTag
for a given reference in a coded text.ctext
- the coded text (e.g. String or StringBuilder object).pos
- the position of the first character of the reference.public Store getStore()
public Tag getTag(int key)
key
- the key of the tag to retrieve.public java.lang.String toXLIFF(java.util.Stack<NSContext> nsStack, java.util.Stack<InheritedData> context, boolean withOriginalData)
nsStack
- the namespace context to use (can be null for out-of-context output).context
- context of the inherited data (can be null for out-of-context output).withOriginalData
- true to output references to original data, false otherwise.public Tag getClosingTag(Tag tag)
tag
- the opening tag.public Tag getOpeningTag(Tag tag)
tag
- the closing tag.public int getClosingPosition(Tag opening)
opening
- the opening tag.public boolean unitHasOpening(CTag ctag)
ctag
- the closing code to test.public static void printCommonAttributes(CTag code, Tags tags, java.lang.StringBuilder tmp, CTag closing, boolean outputDataRefEnd)
code
- the code to output.tags
- the tags collection to which the code belongs (can be null if closing is null).tmp
- the buffer where to output.closing
- the closing code if this is a paired-code (can be null).outputDataRefEnd
- true to output the dataRefEnd attribute if needed.public static void printExtAttributes(Tag tag, java.lang.StringBuilder output, java.util.Stack<NSContext> nsStack)
public java.lang.String toXLIFF()
public boolean isEmpty()
public boolean isTarget()
public boolean hasTag()
public Tag getWellFormedClosing(Tag opening, int from)
This will not find closing tag that are not well-formed (e.g. overlapping)
opening
- the opening tag.from
- the first character position to look at.public Fragment append(char ch)
append
in interface java.lang.Appendable
ch
- the character to append.public Fragment append(java.lang.CharSequence plainText)
append
in interface java.lang.Appendable
plainText
- the string to append.public Fragment append(java.lang.CharSequence plainText, int start, int end)
append
in interface java.lang.Appendable
plainText
- the source for the sub-sequence to append.start
- the index of the first character in the subsequence.end
- the index of the character following the last character in the subsequence.public Tag append(Tag tag)
tag
- the code or marker tag to append.public MTag openMarkerSpan(java.lang.String id, java.lang.String type)
id
- the ID of the new marker (or null to use an auto-generated ID).type
- the type of the new marker (or null for the default).closeMarkerSpan(String)
public MTag closeMarkerSpan(java.lang.String id)
id
- the id of the marker.XLIFFException
- if the opening tag for this marker cannot be found.openMarkerSpan(String, String)
public CTag append(TagType tagType, java.lang.String id, java.lang.String data, boolean canOverlap)
tagType
- the TagType
of the code tag (cannot be null).id
- the id of the tag.data
- the original data for this tag (can be null)canOverlap
- true if this code can be overlapped, false otherwise.
This parameter is ignored if the code is created from its counterpart.appendCode(String, String)
public Fragment append(Fragment fragment)
fragment
- the fragment to append (cannot be itself or null).public int annotate(int start, int end, java.lang.String type, java.lang.String value, java.lang.String ref)
start
- the start position (in the coded text)end
- the position just after the last character of the span (in the coded text).
You can use -1 to indicate the end of the fragment.type
- the type of the annotation.
If the type is term
or its:term-no
the marker created
is an instance of TermTag
rather than MTag
.value
- the value of the value
attribute (can be null).ref
- the value of the ref
attribute (can be null).getOrCreateMarker(int, int, String, String)
public int annotate(int start, int end, MTag opening)
start
- the start position (in the coded text)end
- the position just after the last character of the span (in the coded text).
You can use -1 to indicate the end of the fragment.opening
- the start tag of the marker. The end tag will be generated from this tag.getOrCreateMarker(int, int, String, String)
public Note annotateWithNote(int start, int end, java.lang.String noteContent)
Note
object and add the new note to the container.
Both the ID of the note and the ID of the marker are automatically created.start
- the start position (in the coded text)end
- the position just after the last character of the span (in the coded text).
You can use -1 to indicate the end of the fragment.noteContent
- the text of the note.public MTag getOrCreateMarker(int start, int end, java.lang.String matchingType, java.lang.String typeForNew)
start
- the start position (in the coded text)end
- the position just after the last character of the span (in the coded text).
You can use -1 to indicate the end of the fragment.matchingType
- the type of marker that can be reused. Use null to reuse any marker.typeForNew
- the type of the marker to create of none reusable is found (must not be null).public Fragment remove(Tag tag)
tag
- the tag to remove.public Fragment delete(int start, int end)
start
- the start index (inclusive)end
- the end index (exclusive)public java.util.Iterator<java.lang.Object> iterator()
String
, CTag
, MTag
and PCont
.
To iterate through only one type of object use getIterable(Class)
.iterator
in interface java.lang.Iterable<java.lang.Object>
getIterable(Class)
public <T> java.lang.Iterable<T> getIterable(java.lang.Class<T> type)
type
- the class of the object to iterate through. this can be
Object
, String
, Tag
, CTag
, MTag
or PCont
.iterator()
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object object)
==
to see if two fragments are the same object.equals
in class java.lang.Object
object
- the object to compare this fragment with.public Directionality getDir(boolean resolved)
public void setDir(Directionality dir)
dir
- public void showProtectedContent()
PCont
references in this fragment into normal content.
To hide the protected content use Unit.hideProtectedContent()
.public void checkPosition(int position)
position
- the position to check.InvalidPositionException
- when position points inside a tag reference.public void clear()
public int getCodedTextPosition(int plainTextPosition, boolean leftOfTag)
getCodedTextPosition(CharSequence, int, boolean)
with the coded
text of this fragment as the first parameter.plainTextPosition
- the plain text position to convert.leftOfTag
- true to return the left side position of the tag reference
when the plain text position is on a tag reference (e.g. for end of range stopping
before the reference). Use false to get the right side.getPlainText()
,
getCodedTextPosition(CharSequence, int, boolean)
public Fragment insert(java.lang.CharSequence plainText, int offset)
plainText
- the plain text to insert.offset
- the position where to insert.InvalidPositionException
- if the offset is on a tag reference.java.lang.IndexOutOfBoundsException
- if the offset is invalid.public CTag insert(TagType tagType, java.lang.String type, java.lang.String id, java.lang.String data, int offset, boolean connect, boolean allowOrphan)
tagType
- the type of tag of the code.type
- the type of the code (can be null).id
- the id of the code (if null an new ID is created automatically)
This parameter is ignored when the new tag is created by connecting it with another one.data
- the original data for the code (can be null).offset
- the position where to insert the code. Use -1 to append. Other negative values or values greater
then the length of the coded text also cause the code to be appended at the end of the fragment.connect
- true to connect a new opening code to its closing counterpart, or to connect
a new closing code to its opening counterpart (the counterpart may be in a different fragment).
Use false to create new opening or closing codes.
This option is ignored used if the code is standalone.allowOrphan
- true to allow the connect option to fail, that is: to not found the counterpart
of the new code. this option is ignore if connect is false or if the new code is a standalone code.public CTag appendCode(java.lang.String id, java.lang.String data)
id
- the id of the code tag (cannot be null).data
- the original data for this tag (can be null).public CTag openCodeSpan(java.lang.String id, java.lang.String data)
id
- the id to use for this code (use null to use an automatic ID).data
- the original data for the code, e.g. <B>
(can be null).public CTag closeCodeSpan(java.lang.String id, java.lang.String data)
id
- the id of the code to close (must not be null).data
- the original data for the code, e.g. <B>
(can be null).