|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectFormat
TreeFormat
public class TreeFormat
A parser and formatter for a tree of nodes. This class can format any of the following types:
TreeModel (more info)TreeNode as the root node of a tree (more info)Iterable as a collection of children nodes (not including the root)
(more info)
The result is a tree in String form like the example below:
If the table format is enabled and the tree nodes are instances ofNode #1 ├───Node #2 │ └───Node #4 └───Node #3
TreeTableNode, then the output format will be like the example below:
This representation can be printed to the standard output stream (for example) if it uses a monospaced font and supports unicode. Indentation and position of the vertical line can be modified by calls to the setter methods.Node #1……………………… More #1 ├───Node #2…………… More #2 │ └───Node #4… More #4 └───Node #3…………… More #3
| utility/geotk-utility (download) | View source code for this class |
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class Format |
|---|
Format.Field |
| Constructor Summary | |
|---|---|
TreeFormat()
Creates a new format. |
|
| Method Summary | |
|---|---|
void |
format(Iterable<?> nodes,
Appendable toAppendTo)
Writes a graphical representation of the specified elements in the given buffer. |
void |
format(Iterable<?> nodes,
StringBuilder toAppendTo)
Convenience method which delegate to the above format(Iterable, Appendable)
method, but without throwing IOException. |
StringBuffer |
format(Object tree,
StringBuffer toAppendTo,
FieldPosition pos)
Writes a graphical representation of the specified tree in the given buffer. |
void |
format(TreeModel tree,
Appendable toAppendTo)
Writes a graphical representation of the specified tree model in the given buffer. |
void |
format(TreeModel tree,
StringBuilder toAppendTo)
Convenience method which delegate to the above format(TreeModel, Appendable)
method, but without throwing IOException. |
void |
format(TreeNode node,
Appendable toAppendTo)
Writes a graphical representation of the specified tree in the given buffer. |
void |
format(TreeNode node,
StringBuilder toAppendTo)
Convenience method which delegate to the above format(TreeNode, Appendable)
method, but without throwing IOException. |
char |
getColumnSeparator()
Returns the character used as column separator. |
int |
getIndentation()
Returns the number of spaces to add on the left margin for each indentation level. |
String |
getLineSeparator()
Returns the current line separator. |
int |
getVerticalLinePosition()
Returns the position of the vertical line, relative to the position of the root label. |
boolean |
isTableFormatEnabled()
Returns true if this TreeFormat is allowed to format the tree using many
columns. |
MutableTreeNode |
parse(LineReader input)
Creates a tree from the lines read from the given input. |
MutableTreeNode |
parseObject(String text)
Creates a tree from the given string representation. |
MutableTreeNode |
parseObject(String text,
ParsePosition pos)
Creates a tree from the given string representation, or returns null if an
error occurred while parsing the tree. |
void |
setColumnSeparator(char separator)
Sets the column character to insert between the columns in a TreeTableNode. |
void |
setIndentation(int indentation)
Sets the number of spaces to add on the left margin for each indentation level. |
void |
setLineSeparator(String separator)
Sets the line separator. |
void |
setTableFormatEnabled(boolean enabled)
Sets whatever this TreeFormat is allowed to format the tree as a table. |
void |
setVerticalLinePosition(int verticalLinePosition)
Sets the position of the vertical line, relative to the position of the root label. |
| Methods inherited from class Format |
|---|
clone, format, formatToCharacterIterator |
| Methods inherited from class Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public TreeFormat()
| Method Detail |
|---|
public int getIndentation()
public void setIndentation(int indentation)
throws IllegalArgumentException
indentation - The new indentation.
IllegalArgumentException - If the given value is negative.public int getVerticalLinePosition()
public void setVerticalLinePosition(int verticalLinePosition)
throws IllegalArgumentException
verticalLinePosition - The new vertical line position.
IllegalArgumentException - If the given value is negative or greater than the indentation.public String getLineSeparator()
public void setLineSeparator(String separator)
separator - The new line separator.public char getColumnSeparator()
TreeTableNode elements.
The default value if '…'.
public void setColumnSeparator(char separator)
TreeTableNode.
separator - The column separator.public boolean isTableFormatEnabled()
true if this TreeFormat is allowed to format the tree using many
columns. The default value is false. Setting this property to true is
useful only if the tree to format contains TreeTableNode elements.
true if this TreeFormat object is allowed to format many columns.public void setTableFormatEnabled(boolean enabled)
TreeFormat is allowed to format the tree as a table.
NOTE: parsing of table format is not yet implemented.
enabled - true for enabling the table format.
public MutableTreeNode parseObject(String text,
ParsePosition pos)
null if an
error occurred while parsing the tree.
The default implementation delegates to parseObject(String).
parseObject in class Formattext - The string representation of the tree to parse.pos - The position when to start the parsing.
null if the given tree can not be parsed.
public MutableTreeNode parseObject(String text)
throws ParseException
format(...) methods defined in this class.
The default implementation delegates to parse(LineReader).
parseObject in class Formattext - The string representation of the tree to parse.
ParseException - If an error occurred while parsing the tree.
public MutableTreeNode parse(LineReader input)
throws IOException
format(...) methods defined in this class.
Parsing rules
Each node must have at least one '─' character (unicode 2500) in front of it.
The number of spaces and drawing characters ('│', '├' or '└')
before the node determines its indentation, and the indentation determines the parent
of each node.
input - A LineReader for reading the lines.
IOException - If an error occurred while parsing the tree.
public void format(TreeModel tree,
Appendable toAppendTo)
throws IOException
TreeModel.getChild(Object, int) and its string representation
(expected to uses a single line) is created by a call to String.valueOf(Object).
tree - The tree to format.toAppendTo - Where to format the tree.
IOException - If an error occurred while writing in the given appender.Trees.toString(TreeModel)
public final void format(TreeModel tree,
StringBuilder toAppendTo)
format(TreeModel, Appendable)
method, but without throwing IOException. The I/O exception should never occur
since we are writing in a StringBuilder.
Note: Strictly speaking, anIOExceptioncould still occur in the user overrides the aboveformatmethod and performs some I/O operation outside the givenStringBuilder. However this is not the intended usage of this class and implementors should avoid such unexpected I/O operation.
tree - The tree to format.toAppendTo - Where to format the tree.
public void format(TreeNode node,
Appendable toAppendTo)
throws IOException
format(TreeModel, Appendable).
node - The root node of the tree to format.toAppendTo - Where to format the tree.
IOException - If an error occurred while writing in the given appender.Trees.toString(TreeNode)
public final void format(TreeNode node,
StringBuilder toAppendTo)
format(TreeNode, Appendable)
method, but without throwing IOException. The I/O exception should never occur
since we are writing in a StringBuilder.
Note: Strictly speaking, anIOExceptioncould still occur in the user overrides the aboveformatmethod and performs some I/O operation outside the givenStringBuilder. However this is not the intended usage of this class and implementors should avoid such unexpected I/O operation.
node - The root node of the tree to format.toAppendTo - Where to format the tree.
public void format(Iterable<?> nodes,
Appendable toAppendTo)
throws IOException
String.valueOf(Object) method for
each element. The String value can span multiple lines.
Root label
This method formats only the given child elements. It does not format anything for the
root. It is up to the caller to format a root label on its own line before to invoke
this method.
Recursivity
This method does not perform any check on the element types. In particular, elements of type
TreeModel, TreeNode or inner Iterable are not processed recursively.
It is up to the toString() implementation of each element to invoke this
format method recursively if they wish (this method is safe for this purpose).
nodes - A collection of nodes to format.toAppendTo - Where to format the tree.
IOException - If an error occurred while writing in the given appender.Trees.toString(String, Iterable)
public final void format(Iterable<?> nodes,
StringBuilder toAppendTo)
format(Iterable, Appendable)
method, but without throwing IOException. The I/O exception should never occur
since we are writing in a StringBuilder.
Note: Strictly speaking, anIOExceptioncould still occur in the user overrides the aboveformatmethod and performs some I/O operation outside the givenStringBuilder. However this is not the intended usage of this class and implementors should avoid such unexpected I/O operation.
nodes - A collection of nodes to format.toAppendTo - Where to format the tree.
public StringBuffer format(Object tree,
StringBuffer toAppendTo,
FieldPosition pos)
format in class Formattree - The tree to format.toAppendTo - Where to format the tree.pos - Ignored in current implementation.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||