Go to the documentation of this file.00001 package instruction.syntaxparser;
00002
00003 import instruction.syntaxparser.SyntaxElement;
00004
00005 import java.io.FileWriter;
00006 import java.io.IOException;
00007 import java.util.ArrayList;
00008 import java.util.Iterator;
00009
00010 public class SyntaxTree {
00011
00012 SyntaxElement element;
00013
00014 SyntaxTree parent;
00015 ArrayList<SyntaxTree> children = new ArrayList<SyntaxTree>();
00016
00017
00018 private int tree_depth;
00019 private int curr_depth;
00020
00021 public ArrayList<SyntaxTree> getChildren() {
00022
00023 return children;
00024 }
00025
00026 public SyntaxElement getElement() {
00027
00028 return element;
00029 }
00030
00031 public void setElement( SyntaxElement element ) {
00032
00033 this.element = element;
00034 }
00035
00036 public void addChild( SyntaxTree child ) {
00037
00038 children.add( child );
00039 }
00040
00041 public void setParent( SyntaxTree parent ) {
00042
00043 this.parent = parent;
00044 }
00045
00046 public void printTree() {
00047
00048 printTreeInternal( 0, this );
00049 }
00050
00051 public SyntaxTree getParent() {
00052
00053 return parent;
00054 }
00055
00056 public ArrayList<SyntaxTree> getSiblings() {
00057
00058 if ( parent == null ) {
00059 ArrayList<SyntaxTree> tmp = new ArrayList<SyntaxTree>();
00060 tmp.add( this );
00061 return tmp;
00062 }
00063 else
00064 return parent.getChildren();
00065 }
00066
00067 private void _getdepth( SyntaxTree node ) {
00068
00069 if ( node != null ) {
00070 curr_depth++;
00071 if ( curr_depth > tree_depth )
00072 tree_depth = curr_depth;
00073 for ( Iterator<SyntaxTree> i = node.getChildren().iterator(); i.hasNext(); )
00074 _getdepth( i.next() );
00075 curr_depth--;
00076 }
00077 }
00078
00079 public int getdepth( ) {
00080
00081 tree_depth = 0;
00082 _getdepth( this );
00083 return tree_depth;
00084 }
00085
00086 private void printTreeInternal( int level, SyntaxTree tree ) {
00087
00088 for ( int i = 0; i < level; i++ )
00089 System.out.print( "\t" );
00090
00091
00092
00093
00094 for ( Iterator i = tree.getChildren().iterator(); i.hasNext(); ) {
00095 printTreeInternal( level + 1, (SyntaxTree) i.next() );
00096 }
00097 }
00098
00099 public String toString() {
00100
00101 StringBuilder str = new StringBuilder();
00102 toStringInternal( 0, this, str );
00103 return str.toString();
00104 }
00105
00106 private void toStringInternal( int level, SyntaxTree tree, StringBuilder str ) {
00107
00108 for ( int i = 0; i < level; i++ )
00109 str.append( "\t" );
00110
00111 if ( tree.getElement() != null )
00112 str.append( "(" + tree.getElement().getType() + ", " + tree.getElement().getName() + ")" + "\n" );
00113
00114 for ( Iterator i = tree.getChildren().iterator(); i.hasNext(); ) {
00115 toStringInternal( level + 1, (SyntaxTree) i.next(), str );
00116 }
00117 }
00118
00119 public void writeToFile( FileWriter writer ) throws IOException {
00120
00121
00122 writeToFileInternal( 0, this, writer );
00123 }
00124
00125 private void writeToFileInternal( int level, SyntaxTree tree, FileWriter writer ) throws IOException {
00126
00127 for ( int i = 0; i < level; i++ )
00128 writer.append( "\t" );
00129
00130 if ( tree.getElement() != null )
00131 writer.append( "<" + tree.getElement().getType() + " " + tree.getElement().getName() + ">\n" );
00132
00133 for ( Iterator i = tree.getChildren().iterator(); i.hasNext(); ) {
00134 writeToFileInternal( level + 1, (SyntaxTree) i.next(), writer );
00135 }
00136
00137
00138 }
00139
00140 public String buildSentence() {
00141
00142 StringBuffer str = new StringBuffer();
00143 buildSentenceInternal( str );
00144 return str.toString();
00145 }
00146
00147 private void buildSentenceInternal( StringBuffer str ) {
00148
00149 if ( getElement() != null && ! getElement().getName().equals( "" ) )
00150 str.append( getElement().getName() + " " );
00151
00152 if ( getChildren().size() > 0 ) {
00153 for ( Iterator i = getChildren().iterator(); i.hasNext(); ) {
00154 SyntaxTree t = (SyntaxTree) i.next();
00155 t.buildSentenceInternal( str );
00156 }
00157 }
00158 }
00159
00160 }