org.lsmp.djep.xjep
Class PrintVisitor

java.lang.Object
  extended byorg.lsmp.djep.xjep.DoNothingVisitor
      extended byorg.lsmp.djep.xjep.ErrorCatchingVisitor
          extended byorg.lsmp.djep.xjep.PrintVisitor
All Implemented Interfaces:
ParserVisitor
Direct Known Subclasses:
DPrintVisitor

public class PrintVisitor
extends ErrorCatchingVisitor

Prints an expression. Prints the expression with lots of brackets. ((-1.0)/sqrt((1.0-(x^2.0)))). To use

 JEP j = ...; Node in = ...;
 TreeUtils tu = new TreeUtils(j);
 PrintVisitor pv = new PrintVisitor(tu);
 pv.print(in,"x");
 

Author:
Rich Morris Created on 20-Jun-2003

Nested Class Summary
static interface PrintVisitor.PrintRulesI
          This interface specifies the method needed to implement a special print rule.
 
Field Summary
static int FULL_BRACKET
          All brackets are printed.
protected  int mode
          The current mode for printing.
protected  java.lang.StringBuffer sb
           
 
Constructor Summary
PrintVisitor()
          Creates a visitor to create and print string representations of an expression tree.
 
Method Summary
 void addSpecialRule(Operator op, PrintVisitor.PrintRulesI rules)
          Adds a special print rule to be added for a given operator.
 void append(java.lang.String s)
          Add a string to buffer.
 int getMaxLen()
           
 int getMode()
          Return the current print mode.
 void print(Node node)
          Prints on System.out.
 void print(Node node, java.io.PrintStream out)
          Prints the tree decending from node with lots of brackets or specified stream.
 void println(Node node)
          Prints on System.out.
 void println(Node node, java.io.PrintStream out)
          Prints the tree decending from node with a newline at end.
 void setMaxLen(int i)
          Sets the maximium length printed per line.
 void setMode(int mode, boolean flag)
          Set printing mode.
 java.lang.String toString(Node node)
          returns a String representation of the equation.
 java.lang.Object visit(ASTConstant node, java.lang.Object data)
           
 java.lang.Object visit(ASTFunNode node, java.lang.Object data)
           
 java.lang.Object visit(ASTVarNode node, java.lang.Object data)
           
 
Methods inherited from class org.lsmp.djep.xjep.ErrorCatchingVisitor
acceptCatchingErrors, addError, clearErrors, getError, getErrorsMessage, hasErrors
 
Methods inherited from class org.lsmp.djep.xjep.DoNothingVisitor
acceptChildrenAsArray, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FULL_BRACKET

public static final int FULL_BRACKET
All brackets are printed. Removes all ambiguity.

See Also:
Constant Field Values

sb

protected java.lang.StringBuffer sb

mode

protected int mode
The current mode for printing.

Constructor Detail

PrintVisitor

public PrintVisitor()
Creates a visitor to create and print string representations of an expression tree.

Method Detail

print

public void print(Node node,
                  java.io.PrintStream out)
Prints the tree decending from node with lots of brackets or specified stream.


print

public void print(Node node)
Prints on System.out.


println

public void println(Node node,
                    java.io.PrintStream out)
Prints the tree decending from node with a newline at end.


println

public void println(Node node)
Prints on System.out.


toString

public java.lang.String toString(Node node)
returns a String representation of the equation.


append

public void append(java.lang.String s)
Add a string to buffer. Classes implementing PrintRulesI should call this add the


addSpecialRule

public void addSpecialRule(Operator op,
                           PrintVisitor.PrintRulesI rules)
Adds a special print rule to be added for a given operator. TODO Allow special rules for other functions, i.e. not operators.


visit

public java.lang.Object visit(ASTFunNode node,
                              java.lang.Object data)
                       throws ParseException
Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DoNothingVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTVarNode node,
                              java.lang.Object data)
                       throws ParseException
Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DoNothingVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTConstant node,
                              java.lang.Object data)
Specified by:
visit in interface ParserVisitor
Overrides:
visit in class DoNothingVisitor

getMode

public int getMode()
Return the current print mode.


setMode

public void setMode(int mode,
                    boolean flag)
Set printing mode. In full bracket mode the brackets each element in the tree will be suronded by brackets to indicate the tree structure. In the default mode, (full bracket off) the number of brackets is minimised so (x+y)+z will be printed as x+y+z.

Parameters:
mode - which flags to change, typically FULL_BRACKET
flag - whether to switch this mode on or off

getMaxLen

public int getMaxLen()
Returns:
the maximum length printed per line

setMaxLen

public void setMaxLen(int i)
Sets the maximium length printed per line. If the value is not -1 then the string will be broken into chunks each of which is less than the max lenght.

Parameters:
i - the maximum length


http://www.singularsys.com/jep Copyright © 2004 Singular Systems