|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--editor.AbstractDocument | +--editor.EditorDocument
A linked list based implementation of Document interface. This implementation uses a java.util.LinkedList of java.lang.Character representation to store document content.
The search process is based on the use of simple patterns. These are essentially made of constant symbols, like a, b, c..., taken from the alphabet, and a distinguished symbol, *, called the wild card. Examples of patterns are abc*d*e, < TITLE>*</TITLE>. Patterns are matched against documents in a natural way: each alphabet symbol matches itself, and wild cards match any string. When searching a document for strings matching a pattern, in case of alternatives, the leftmost shortest match is selected. This is defined as the shortest matching region among the ones starting at the earliest position. For example, given a pattern a*b and a document "ccaabb", the matching chosen by search method is ccaabb.
Patterns have been extended with some special symbol:
Symbol | Matches with | |||||
---|---|---|---|---|---|---|
\n | new-line | |||||
\t | tabulator | |||||
\f | form-feed | |||||
\r | carriage-return | |||||
\. | any character | |||||
\d | any digit | |||||
\s |
|
A construct [...] to specify user-defined character classes is
provided: [0-9] is equivalent to \d,
\s is equivalent to [ \n\t\r], [0-9a-z$]
matches with any digit, lower case letter or $,
[^0-9] matches with any character that is not a digit.
To specify ],^ or - as one of characters
of a class special care is needed:
] must be placed immediately after opening [
as in []].
^ must be placed not immediately after opening [
as in [a^].
- need to be placed where cannot be interpreted as
range separator as in [a-z-] or in [a-].
Another interesting feature of editor patterns is the possibility of using forms of look-ahead. Patterns with look-ahead are of the form p(?p1|...|pn), where p and each pi are look-ahead-free patterns, for example a*b(?b|ba). Patterns of this form match any document region matching pattern p and immediately followed by a document region matching at least one of the pi. For example, when matched against document ababb, pattern a*b(?b|ba) above matchesababb. We say that look-ahead patterns pi have zero-expansion, in the sense that they do not contribute directly to the matched region, but are used only to specify an additional constraint on the matching.
In order to resolve possible ambiguities, sintax is quite restrictive on symbols (,),[,],|, * (literally, not as wild card), that must be used inside a character class.
Field Summary | |
static char |
STAR
Wild card used in patterns. |
Constructor Summary | |
EditorDocument()
Constructs a new empty document. |
|
EditorDocument(Reader in)
Constructs a new document whose content is read from a characters-stream Reader. |
Method Summary | |
void |
clear()
Clears document content. |
boolean |
getCaseSensitive()
Returns search and match sensitivity to characters case. |
int |
getPosition()
Get current position. |
int |
getStartPoint()
Get current start position. |
static String |
getVersion()
Returns package version. |
boolean |
isEmpty()
Returns true if document is empty. |
protected ListIterator |
listIterator(int index)
Returns a list iterator over document's characters wrapped inside java.lang.Character objects |
protected void |
makeList(Reader in)
An utility method to instantiate the list with characters read from a characters-stream Reader. |
boolean |
match(String p)
Matches a pattern starting from current position. |
void |
open(Reader reader)
Reads document content from a characters-stream Reader. |
void |
remove()
Removes current selection. |
boolean |
search(String p)
Searches for the next occurrence of pattern starting. |
void |
setCaseSensitive(boolean cs)
Set search and match sensitivity to characters case. |
void |
setPosition(int index)
Set current position (right delimeter of current selection). |
void |
setStartPoint(int index)
Set current start position (left delimeter of current selection). |
int |
size()
Return the number of characters composing this document. |
Methods inherited from class editor.AbstractDocument |
copy,
copyAll,
cut,
cutAll,
loopSearch,
paste,
put,
replace,
replaceAll,
reset,
save,
toString |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
public static final char STAR
Constructor Detail |
public EditorDocument()
public EditorDocument(Reader in) throws IOException
open(java.io.Reader)
Method Detail |
public static String getVersion()
public void open(Reader reader) throws IOException
reader
- Reader from which new document's content is readAbstractDocument.save(java.io.Writer)
protected void makeList(Reader in) throws IOException
public void remove()
public void clear()
public boolean search(String p) throws ParseException
The following code search for the first occurrence of "mirror" or, if it does not exist, of "server" in document doc:
doc.reset(); if (doc.search("mirror") || doc.search("server")) { System.out.println("found: "+doc.clipboard); } else System.out.println("not found");Note that if the first search("mirror") fails, the second one is executed with current selected region set to (n,n) so that it starts again from the beginning of doc.
pattern
- the pattern to search forAbstractDocument.loopSearch(java.lang.String)
public boolean match(String p) throws ParseException
This method is useful for incrementally delimit a region starting from a given position. For example, given an empty document doc:
doc.put("abcde1234fg"); doc.search("[0-9]"); while (document.match("[0-9]")); System.out.println("Selected region: "+Document.clipboard);will print:
Selected region: 1234The pattern is
pattern
- the pattern to matchpublic void setPosition(int index) throws IndexOutOfBoundsException
index
- new current position to setpublic int getPosition()
public void setStartPoint(int index) throws IndexOutOfBoundsException
index
- new start positionpublic int getStartPoint()
public boolean isEmpty()
public int size()
public void setCaseSensitive(boolean cs)
Document.match(java.lang.String)
,
Document.search(java.lang.String)
public boolean getCaseSensitive()
Document.match(java.lang.String)
,
Document.search(java.lang.String)
protected ListIterator listIterator(int index)
Character
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |