editor
Class EditorDocument

java.lang.Object
  |
  +--editor.AbstractDocument
        |
        +--editor.EditorDocument

public class EditorDocument
extends AbstractDocument
implements Document

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:

SymbolMatches with
\nnew-line
\ttabulator
\fform-feed
\rcarriage-return
\.any character
\dany digit
\s
new-line,
tabulator,
carriage-return,
form-feed or
blank
For example a pattern like 19\d\d matches any sequence of four digits, the first ones being 1 and 9.

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.

Author:
Valter Crescenzi, Alessandro Masci, Gianni Mecca

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

STAR

public static final char STAR
Wild card used in patterns.
Constructor Detail

EditorDocument

public EditorDocument()
Constructs a new empty document.

EditorDocument

public EditorDocument(Reader in)
               throws IOException
Constructs a new document whose content is read from a characters-stream Reader.
Throws:
IOException - if an I/O occurred
See Also:
open(java.io.Reader)
Method Detail

getVersion

public static String getVersion()
Returns package version.
Returns:
a string indicating package version.

open

public void open(Reader reader)
          throws IOException
Reads document content from a characters-stream Reader. Old document content is discarded and new document's content is read from a characters stream Reader.
Specified by:
open in interface Document
Parameters:
reader - Reader from which new document's content is read
Throws:
IOException - if an I/O error occurred
Overrides:
open in class AbstractDocument
See Also:
AbstractDocument.save(java.io.Writer)

makeList

protected void makeList(Reader in)
                 throws IOException
An utility method to instantiate the list with characters read from a characters-stream Reader.

remove

public void remove()
Removes current selection. Clipboard is unaffected by this method and current selection becomes empty.
Specified by:
remove in interface Document
Overrides:
remove in class AbstractDocument

clear

public void clear()
Clears document content. Current selection becomes (0,0)
Specified by:
clear in interface Document
Overrides:
clear in class AbstractDocument

search

public boolean search(String p)
               throws ParseException
Searches for the next occurrence of pattern starting. The search is started from current position or from 0 if current position is n (n = size of document) so that a document is searched in a circular way. If the pattern occurrence is found, then it becomes the new selected region and true is returned; otherwise false is returned and the final empty region (n,n) is selected.

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.
Specified by:
search in interface Document
Parameters:
pattern - the pattern to search for
Returns:
true it the pattern occurrence has been found and selected as current region; false if it has not been found and the final empty region is selected.
See Also:
AbstractDocument.loopSearch(java.lang.String)

match

public boolean match(String p)
              throws ParseException
Matches a pattern starting from current position. Returns true when an occurrence of a pattern is found starting from current position. If the match succeeded, the current position is moved just after the match: if the match failed, no side-effects are produced.

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: 1234
 
The pattern is
Specified by:
match in interface Document
Parameters:
pattern - the pattern to match
Returns:
true if the match succeeded.

setPosition

public void setPosition(int index)
                 throws IndexOutOfBoundsException
Set current position (right delimeter of current selection).
Specified by:
setPosition in interface Document
Parameters:
index - new current position to set

getPosition

public int getPosition()
Get current position.
Specified by:
getPosition in interface Document
Returns:
the current position

setStartPoint

public void setStartPoint(int index)
                   throws IndexOutOfBoundsException
Set current start position (left delimeter of current selection).
Specified by:
setStartPoint in interface Document
Parameters:
index - new start position

getStartPoint

public int getStartPoint()
Get current start position.
Specified by:
getStartPoint in interface Document
Returns:
the current start position

isEmpty

public boolean isEmpty()
Returns true if document is empty.
Specified by:
isEmpty in interface Document
Returns:
true if document is empty; false otherwise.
Overrides:
isEmpty in class AbstractDocument

size

public int size()
Return the number of characters composing this document.
Specified by:
size in interface Document
Returns:
the document's size, that is the number of characters composing the document
Overrides:
size in class AbstractDocument

setCaseSensitive

public void setCaseSensitive(boolean cs)
Set search and match sensitivity to characters case.
Specified by:
setCaseSensitive in interface Document
See Also:
Document.match(java.lang.String), Document.search(java.lang.String)

getCaseSensitive

public boolean getCaseSensitive()
Returns search and match sensitivity to characters case.
Specified by:
getCaseSensitive in interface Document
Returns:
true if searching is case-sensitive; false otherwise
See Also:
Document.match(java.lang.String), Document.search(java.lang.String)

listIterator

protected ListIterator listIterator(int index)
Returns a list iterator over document's characters wrapped inside java.lang.Character objects
Overrides:
listIterator in class AbstractDocument
See Also:
Character