org.wurbelizer
Class SourceDocument

java.lang.Object
  extended by org.wurbelizer.SourceDocument

public class SourceDocument
extends Object

A SourceDocument represents the logical structure of a source file in memory. Source documents are divided into sections, so-called SourceElements. Each element can be manipulated independently. Especially new elements, so-called guarded blocks, can be inserted, replaced and removed. They are identified by a unique name, a tag, which is stored along with the section within the document. Elements can also be addressed by their logical index (starting at 0).


Nested Class Summary
static class SourceDocument.FoldType
          editor folds (currently only supported for GUARDTYPE_NETBEANS
 
Constructor Summary
SourceDocument(StringBuilder text, String name, Properties otherProps)
          Constructs a SourceDocument from a text buffer.
SourceDocument(String text, String name, Properties otherProps)
          Constructs a SourceDocument from a String.
 
Method Summary
 void deleteElement(int elemNdx)
          Removes an element at given index.
 int deleteGuarded(String guardedName)
          Removes a guarded section with a given name.
 int deleteGuarded(String guardedName, int startIndex)
          Removes a guarded section with a given name starting at a given logical index.
 List<SourceElement> getElements()
          Gets the list of all elements of this document.
 int getLineNumber(int pos)
          Gets the line number for a given character position.
 String getPackageName()
          Gets the package name of the source document.
 Properties getProperties()
          Gets the optional properties for this document
 StringBuilder getText()
          Gets the contents of the whole document.
 void insertGuarded(SourceElement.Type guardType, SourceDocument.FoldType foldType, String guardedName, String str, int elemNdx)
          Inserts a guarded section by creating a new SourceElement.
 int replaceGuarded(SourceElement.Type guardType, SourceDocument.FoldType foldType, String guardedName, String str)
          Replaces a guarded section.
 void replaceInline(String str, int elemNdx)
          Replaces an inline wurblet section.
 void setText(StringBuilder text)
          Sets the text of the document.
 String toString()
          Prints the name of the document without the package name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SourceDocument

public SourceDocument(StringBuilder text,
                      String name,
                      Properties otherProps)
               throws SourceException
Constructs a SourceDocument from a text buffer.

Parameters:
text - the contents of the document
name - the name of the document (usually the filename)
otherProps - optional properties for wurblets
Throws:
SourceException - if parsing the document failed

SourceDocument

public SourceDocument(String text,
                      String name,
                      Properties otherProps)
               throws SourceException
Constructs a SourceDocument from a String.

Parameters:
text - the contents of the document
name - the name of the document (usually the filename)
otherProps - optional properties for wurblets
Throws:
SourceException - if parsing the document failed
Method Detail

insertGuarded

public void insertGuarded(SourceElement.Type guardType,
                          SourceDocument.FoldType foldType,
                          String guardedName,
                          String str,
                          int elemNdx)
                   throws SourceException
Inserts a guarded section by creating a new SourceElement.

Parameters:
guardType - the element type
foldType - the editor fold type (currently only supported for Netbeans), null if none
guardedName - the name of the guarded section
str - the text to insert (the output of the wurblet)
elemNdx - the element index where to insert the new element
Throws:
SourceException - if creating the element failed

deleteGuarded

public int deleteGuarded(String guardedName,
                         int startIndex)
Removes a guarded section with a given name starting at a given logical index.

Parameters:
guardedName - the name of the guarded section to remove
startIndex - start index in elements where to start the search
Returns:
the index >= 0 if removed, -1 if no such section

deleteGuarded

public int deleteGuarded(String guardedName)
Removes a guarded section with a given name.

Parameters:
guardedName - the name of the guarded section to remove
Returns:
the index >= 0 if removed, -1 if no such section

replaceGuarded

public int replaceGuarded(SourceElement.Type guardType,
                          SourceDocument.FoldType foldType,
                          String guardedName,
                          String str)
                   throws SourceException
Replaces a guarded section.

Parameters:
guardType - the element type
foldType - the editor fold type (currently only supported for Netbeans), null if none
guardedName - the name of the guarded section to replace
str - the new text
Returns:
the index >=0 of the replaced element, -1 if not found
Throws:
SourceException - if operation failed

replaceInline

public void replaceInline(String str,
                          int elemNdx)
                   throws SourceException
Replaces an inline wurblet section. Generated inline sections are enclosed between two empty block comments within a CODE-element. Notice that empty block comments are not treated as regular comments but as code! Furthermore, inline sections are not guarded, i.e. any manual change will be overwritten by the next wurblet invocation.

Parameters:
str - the new text
elemNdx - the index of the SourceElement
Throws:
SourceException - if operaton failed

deleteElement

public void deleteElement(int elemNdx)
Removes an element at given index.

Parameters:
elemNdx - the index to elements

getLineNumber

public int getLineNumber(int pos)
Gets the line number for a given character position. For performance reasons line number or not calculated when parsing the document but only when printing an error message.

Parameters:
pos - the character position in the document.
Returns:
the line number.

toString

public String toString()
Prints the name of the document without the package name.

Overrides:
toString in class Object
Returns:
the name of the document

getText

public StringBuilder getText()
Gets the contents of the whole document.

Returns:
the documents contents.

getProperties

public Properties getProperties()
Gets the optional properties for this document

Returns:
the properties

getPackageName

public String getPackageName()
Gets the package name of the source document.

Returns:
the packagename of null, if no package name found

setText

public void setText(StringBuilder text)
             throws SourceException
Sets the text of the document. The text will be parsed and separated into elements.

Parameters:
text - the contents of the document
Throws:
SourceException - if parsing the contents failed

getElements

public List<SourceElement> getElements()
Gets the list of all elements of this document.

Returns:
the elements


Copyright © 2001-2008 Harald Krake, Bergstr. 48, 78098 Triberg, Germany, harald@krake.de