se.biltmore.tools.fileeditor
Class TextFileEditor

java.lang.Object
  extended by se.biltmore.tools.fileeditor.TextFileEditor

public class TextFileEditor
extends Object

This is a text file editor that as the name suggests, edits text files. It is only a programatic editor that allows you to load a file, search, replace, delete, and insert stuff in the file, and then save it again or save to a new file. The show() method are a utility for debuging so that you can look at the result without saving it.

Please note that line and column numbers start at 1 while TextBuffer lines start at 0!


Field Summary
static Character END_OF_FILE_CHAR
          Means end of file in the buffer held by the editor.
 
Constructor Summary
TextFileEditor()
          Creates a new instance of TextFileEditor.
 
Method Summary
 void appendBuffer(TextBuffer buffer)
          Appends another buffer into this one at the end.
 void clearSelection()
          Clears the selection.
 void deleteCurrentLine()
          Removes the current line.
 void deleteCurrentWord()
          Deletes the current word.
 void deleteCurrentWord(char delimeter)
          Deletes the current word as denoted by the specified delimeter.
 void deleteSelection()
          Deletes the current selection and sets the current position to the position right after the deleted text.
 void endSelection()
          Marks the end of the selection at the current position.
 boolean find(String text)
          Finds the specified text and moves the current position.
 List<Occurence> findAllBlankLines()
          Finds all blank lines and returns them as a list of Occurence objects.
 List<Occurence> findAllLinesMatching(String regexp)
          Returns a list of Occurence objects for each match.
 List<Occurence> findAllOccurences(String text)
          Returns a list of Occurence objects for each match.
 boolean findBlankLine()
          Finds the next blank line starting at the current line.
 boolean findLast(String text)
          Finds the last occurence of the specifeid text and moves the current position.
 Occurence findLastOccurence(String text)
          Finds the last occurence of the specified text and returns it as an Occurence object.
 boolean findNext(String text)
          Finds the next occurence of the specified text and moves the current position.
 boolean findNextBlankLine()
          Finds the next blank line starting at the line after the current line.
 char getChar()
          Returns the character at the current position.
 String getLine()
          Returns the current line.
 String getLine(int line)
          Returns the specified line.
 Position getPosition()
          Returns the current position.
 TextBuffer getSelectedText()
          Returns the selection as a TextBuffer.
 String getSelectedTextAsString()
          Returns the selection as a String including '\n' character at end of line for multi line selections.
 Selection getSelection()
          Returns the current selection.
 String getSubString(int startColumn, int endColumn)
          Returns a substring of the current line.
 String getSubString(int line, int startColumn, int endColumn)
          Returns a substring of the specified line.
 TextBuffer getTextBuffer()
          Returns the currently edited buffer.
 String getWord()
          Returns the current word, and positions the current position at the beginning of the word.
 boolean hasValidSelection()
          Returns true if there is a valid selection set.
 void insertBuffer(TextBuffer buffer)
          Inserts another buffer into the buffer at the line below the current line.
 void insertLine(String line)
          Inserts a line after the current line.
 void insertLineAbove(String line)
          Inserts a line above the current line.
 void insertTextBuffer(TextBuffer insertBuffer)
          Inserts a TextBuffer at the current position and moves the current position to after the last inserted character.
 boolean isOnFirstColumn()
          Returns true if the current position is on the first column in the line.
 boolean isOnFirstLine()
          Returns true if the current position is on the first line.
 boolean isOnLastColumn()
          Returns true if the current position is on the last column of the line.
 boolean isOnLastLine()
          Returns true if the current position is on the last line.
 boolean lineMatches(String regexp)
          Returns true if the current line matches the specified regular expression.
 void load(File file)
          Loads a file into the buffer.
 void load(String path)
          Loads a file into the buffer.
 boolean moveDown(int size)
          Moves the current position down.
 boolean moveLeft(int size)
          Move the current position left.
 boolean moveRight(int size)
          Move the current position right.
 char moveToAndReturnNextChar()
          Moves to the right and returns the next character.
 String moveToAndReturnNextWord()
          Moves to the next word and returns it.
 String moveToAndReturnPrevWord()
          Moves to the previous word and returns it.
 void moveToBeginningOfLine()
          Moves the current position to the beginning of the line.
 void moveToBottomOfFile()
          Moves the current position to the bottom of the file at 0,last-line.
 void moveToEndOfFile()
          Moves the current position to the end of the file at last-column,last-line.
 void moveToEndOfLine()
          Moves the current position to the end of the line.
 void moveToMatching(char start, char end)
          Moves to the next "end" character at the 0 level.
 boolean moveToNextChar()
          Moves to the next character in the buffer being edited.
 boolean moveToPrevChar()
          Moves to the previous character in the buffer being edited.
 void moveToTopOfFile()
          Moves the current position to the top of the file at 0,0.
 boolean moveUp(int size)
          Moves the current positon up.
 boolean moveWordsLeft(int words)
          Moves the current position the specified number of words left and returns true if this was possible.
 boolean moveWordsRight(int words)
          Moves the current position the specified number of words right and returns true if this was possible.
 boolean nextLineMatches(String regexp)
          Moves down to the next line and returns true if that line matches the specified regular expression.
 void replaceAllInCurrentLine(String text, String replaceWithText)
          Replaces all occurances of the specified text with the new text in the current line.
 void replaceAllInFile(String text, String replaceWithText)
          Replaces all occurances of the specified text with the new text in the whole buffer.
 void replaceCurrentLine(String text)
          Replaces the current line completely wiht the specified text.
 void replaceFirstInCurrentLine(String text, String replaceWithText)
          Replaces the first occurence of the specified text with the new text in the current line.
 void replaceSelectionWithText(String replacementText)
          Replaces the current selection with the specified text.
 void replaceSelectionWithTextBuffer(TextBuffer replaceBuffer)
          Replaces the current selection with the contents of the specified TextBuffer.
 void save()
          Saves the current buffer.
 void saveAs(File file)
          Saves the current buffer to a new file.
 void saveAs(String path)
          Saves the current buffer to a new file.
 void selectToMatching(char start, char end)
          Selects to the next "end" character at the 0 level.
 void setAllowLoadSave(boolean allowLoadSave)
          Change the allowLoadSave state.
 void setLine(int lineNo, String line)
          Replaces a line with a new text.
 void setLine(String line)
          Replaces the current line with a new text.
 void setPosition(int column, int line)
          Sets the current position.
 void setPosition(Position position)
          Sets the current position.
 void setTextBuffer(TextBuffer textBuffer)
          Replaces the buffer to edit.
 void show()
          Displays the loaded file.
 void show(int start, int end)
          Displays the loaded file.
 void startSelection()
          Starts a new selection at the current position.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

END_OF_FILE_CHAR

public static final Character END_OF_FILE_CHAR
Means end of file in the buffer held by the editor.

Constructor Detail

TextFileEditor

public TextFileEditor()
Creates a new instance of TextFileEditor.

Method Detail

setAllowLoadSave

public void setAllowLoadSave(boolean allowLoadSave)
Change the allowLoadSave state.

Parameters:
allowLoadSave - true or false.

setTextBuffer

public void setTextBuffer(TextBuffer textBuffer)
Replaces the buffer to edit.


getTextBuffer

public TextBuffer getTextBuffer()
Returns the currently edited buffer.


load

public void load(File file)
          throws IOException
Loads a file into the buffer.

Parameters:
file - The file to load.
Throws:
IOException

load

public void load(String path)
          throws IOException
Loads a file into the buffer.

Parameters:
path - The path to the file to load.
Throws:
IOException

save

public void save()
          throws IOException
Saves the current buffer.

Throws:
IOException

saveAs

public void saveAs(File file)
            throws IOException
Saves the current buffer to a new file.

Parameters:
file - The file to save to.
Throws:
IOException

saveAs

public void saveAs(String path)
            throws IOException
Saves the current buffer to a new file.

Parameters:
path - The path to the file to save to.
Throws:
IOException

getLine

public String getLine(int line)
Returns the specified line.

Parameters:
line - The line to get.

getLine

public String getLine()
Returns the current line.


setLine

public void setLine(int lineNo,
                    String line)
Replaces a line with a new text.

Parameters:
lineNo - The line number to replace.
line - The new line text.

setLine

public void setLine(String line)
Replaces the current line with a new text.

Parameters:
line - The new text to set for the line.

getSubString

public String getSubString(int line,
                           int startColumn,
                           int endColumn)
Returns a substring of the specified line.

Parameters:
line - The line to get the substring from.
startColumn - The start of the substring.
endColumn - The end of the substring (inclusive! Not like java substring!!).

getSubString

public String getSubString(int startColumn,
                           int endColumn)
Returns a substring of the current line.

Parameters:
startColumn - The start of the substring.
endColumn - The end of the substring.

getWord

public String getWord()
Returns the current word, and positions the current position at the beginning of the word.

Returns:
The current word.

setPosition

public void setPosition(Position position)
Sets the current position.

Parameters:
position - The position to set.

setPosition

public void setPosition(int column,
                        int line)
Sets the current position.

Parameters:
column - The column of the position.
line - The line of the position.

getPosition

public Position getPosition()
Returns the current position.


moveDown

public boolean moveDown(int size)
Moves the current position down.

Returns false on failure to do so.

Parameters:
size - The number of lines to move the position down.

moveUp

public boolean moveUp(int size)
Moves the current positon up.

Returns false on failure to do so.

Parameters:
size - The number of lines to move the position up.

moveRight

public boolean moveRight(int size)
Move the current position right.

Returns false on failure to do so.

Parameters:
size - The number of columns to move the position right.

moveLeft

public boolean moveLeft(int size)
Move the current position left.

Returns false on failure to do so.

Parameters:
size - The number of columns to move the position left.

moveToNextChar

public boolean moveToNextChar()
Moves to the next character in the buffer being edited. This will move down to the next line if at the end of the line. It will never move past the last character of the buffer, but will stay at that character for successive calls.

Returns false on failure to do so.


moveToPrevChar

public boolean moveToPrevChar()
Moves to the previous character in the buffer being edited. This will move upp to the previous line if at the beginning of the line. It will never move before the beginning of the buffer, but will stay at the beginning for successive calls.

Returns false on failure to do so.


getChar

public char getChar()
Returns the character at the current position.


moveToAndReturnNextChar

public char moveToAndReturnNextChar()
Moves to the right and returns the next character. If end of line is reached it continues on the next line until end of buffer which will return END_OF_FILE_CHAR.

Returns:
The next character or END_OF_FILE_CHAR if end of buffer.

moveToAndReturnNextWord

public String moveToAndReturnNextWord()
Moves to the next word and returns it.


moveToAndReturnPrevWord

public String moveToAndReturnPrevWord()
Moves to the previous word and returns it.


moveWordsRight

public boolean moveWordsRight(int words)
Moves the current position the specified number of words right and returns true if this was possible.

Please note that this method only moves right on the current line, it will not wrap to the next line! When it returns false, do "moveDown(1);" and "moveToBeginningOfLine();" and then continue if you want to cover several lines.

Parameters:
words - The number of words to move.

moveWordsLeft

public boolean moveWordsLeft(int words)
Moves the current position the specified number of words left and returns true if this was possible.

Please note that this method only moves left on the current line, it will not wrap to the previous line! When it returns false, do "moveUp(1);" and "moveToBeginningOfLine();" and then continue if you want to cover several lines.

Parameters:
words - The number of words to move.

moveToEndOfLine

public void moveToEndOfLine()
Moves the current position to the end of the line.


moveToBeginningOfLine

public void moveToBeginningOfLine()
Moves the current position to the beginning of the line.


moveToTopOfFile

public void moveToTopOfFile()
Moves the current position to the top of the file at 0,0.


moveToBottomOfFile

public void moveToBottomOfFile()
Moves the current position to the bottom of the file at 0,last-line.


moveToEndOfFile

public void moveToEndOfFile()
Moves the current position to the end of the file at last-column,last-line.


isOnFirstLine

public boolean isOnFirstLine()
Returns true if the current position is on the first line.


isOnLastLine

public boolean isOnLastLine()
Returns true if the current position is on the last line.


isOnFirstColumn

public boolean isOnFirstColumn()
Returns true if the current position is on the first column in the line.


isOnLastColumn

public boolean isOnLastColumn()
Returns true if the current position is on the last column of the line.


find

public boolean find(String text)
Finds the specified text and moves the current position.

Parameters:
text - The text to find. This can be a regular expression.

findNext

public boolean findNext(String text)
Finds the next occurence of the specified text and moves the current position.

Parameters:
text - The text to find. This can be a regular expression.

findLast

public boolean findLast(String text)
Finds the last occurence of the specifeid text and moves the current position.

Parameters:
text - The text to find. This can be a regular expression.

findAllOccurences

public List<Occurence> findAllOccurences(String text)
Returns a list of Occurence objects for each match. If no match was found the list will be empty.

This search will not move the current position, and will always start at the top of the file.

Parameters:
text - The text to find all occurences of. This can be a reqular expression.

findLastOccurence

public Occurence findLastOccurence(String text)
Finds the last occurence of the specified text and returns it as an Occurence object. This returns null if no occurence is found!

This search will not move the current position, and will always start at the top of the file.

Parameters:
text - The text to look for the last occurence of. This can be a reqular expression.

findBlankLine

public boolean findBlankLine()
Finds the next blank line starting at the current line. Returns true if found in which case the poition will be updated to the beginning of the found line.


findNextBlankLine

public boolean findNextBlankLine()
Finds the next blank line starting at the line after the current line. Returns true if found in which case the position will be updated to the beginning of the found line.


findAllBlankLines

public List<Occurence> findAllBlankLines()
Finds all blank lines and returns them as a list of Occurence objects. If no blank lines were found an empty list will be returned.

This search will not move the current position, and will always start at the top of the file.


lineMatches

public boolean lineMatches(String regexp)
Returns true if the current line matches the specified regular expression.

Parameters:
regexp - A regular expression to match with the line.

nextLineMatches

public boolean nextLineMatches(String regexp)
Moves down to the next line and returns true if that line matches the specified regular expression. If it tries to move beyond last line of file then false is returned.

Parameters:
regexp - The regular expression to match with the next line.

findAllLinesMatching

public List<Occurence> findAllLinesMatching(String regexp)
Returns a list of Occurence objects for each match. If no match was found the list will be empty.

This search will not move the current position, and will always start at the top of the file.

Parameters:
regexp - The regular expression to find all occurences of.

startSelection

public void startSelection()
Starts a new selection at the current position. End with endSelection(). Once a complete selection have been made it can be replaced with replaceSelectionWithTextBuffer(...) and deleted with deleteSelection().

See Also:
endSelection(), replaceSelectionWithTextBuffer(TextBuffer), deleteSelection()

endSelection

public void endSelection()
Marks the end of the selection at the current position.


clearSelection

public void clearSelection()
Clears the selection.


hasValidSelection

public boolean hasValidSelection()
Returns true if there is a valid selection set.


moveToMatching

public void moveToMatching(char start,
                           char end)
Moves to the next "end" character at the 0 level. To look for a matching parentesis do moveToMatching('(', ')');, to a matching brace moveToMatching('{', '}');

Parameters:
start - This character increases the level.
end - This is what to look for and also decreses the level.

selectToMatching

public void selectToMatching(char start,
                             char end)
Selects to the next "end" character at the 0 level. To look for a matching parentesis do selectToMatching('(', ')');, to a matching brace selectToMatching('{', '}');

A few notes:
- You must have done startSelection() first!
- The selection will include the matching end character, but the current position will remain at the matching end character.

Parameters:
start - This character increases the level.
end - This is what to look for and also decreses the level.

getSelection

public Selection getSelection()
Returns the current selection.


replaceCurrentLine

public void replaceCurrentLine(String text)
Replaces the current line completely wiht the specified text.

Parameters:
text - The text to replace the current line with.

replaceFirstInCurrentLine

public void replaceFirstInCurrentLine(String text,
                                      String replaceWithText)
Replaces the first occurence of the specified text with the new text in the current line.

Parameters:
text - The text to replace. Can be a regular expression.
replaceWithText - The new text to replace the old with.

replaceAllInCurrentLine

public void replaceAllInCurrentLine(String text,
                                    String replaceWithText)
Replaces all occurances of the specified text with the new text in the current line.

Parameters:
text - The text to replace. Can be a regular expression.
replaceWithText - The new text to replace the old with.

replaceAllInFile

public void replaceAllInFile(String text,
                             String replaceWithText)
Replaces all occurances of the specified text with the new text in the whole buffer.

Parameters:
text - The text to replace. Can be a regular expression.
replaceWithText - The new text to replace the old with.

replaceSelectionWithText

public void replaceSelectionWithText(String replacementText)
Replaces the current selection with the specified text. The text should not contain any CR or NL characters!

Parameters:
replacementText - The text to replace with.

replaceSelectionWithTextBuffer

public void replaceSelectionWithTextBuffer(TextBuffer replaceBuffer)
Replaces the current selection with the contents of the specified TextBuffer.

Parameters:
replaceBuffer - The buffer whose content should replace current selection.

insertTextBuffer

public void insertTextBuffer(TextBuffer insertBuffer)
Inserts a TextBuffer at the current position and moves the current position to after the last inserted character.

Parameters:
insertBuffer - The buffer to insert.

deleteSelection

public void deleteSelection()
Deletes the current selection and sets the current position to the position right after the deleted text.


getSelectedText

public TextBuffer getSelectedText()
Returns the selection as a TextBuffer.


getSelectedTextAsString

public String getSelectedTextAsString()
Returns the selection as a String including '\n' character at end of line for multi line selections.


insertBuffer

public void insertBuffer(TextBuffer buffer)
Inserts another buffer into the buffer at the line below the current line.

Parameters:
buffer - The buffer to insert.

appendBuffer

public void appendBuffer(TextBuffer buffer)
Appends another buffer into this one at the end.


insertLine

public void insertLine(String line)
Inserts a line after the current line.


insertLineAbove

public void insertLineAbove(String line)
Inserts a line above the current line.


deleteCurrentLine

public void deleteCurrentLine()
Removes the current line. The new current line will be the line above the deleted line. A new insertLine() will insert in the position of the deleted line.


deleteCurrentWord

public void deleteCurrentWord(char delimeter)
Deletes the current word as denoted by the specified delimeter. Afterward the current position will be right after the deleted word.

Parameters:
delimeter - The delimeter that separates words.

deleteCurrentWord

public void deleteCurrentWord()
Deletes the current word.


show

public void show()
Displays the loaded file.


show

public void show(int start,
                 int end)
Displays the loaded file.



Copyright © 2009-2010 Biltmore Group AB. All Rights Reserved.