001    package plugins.JavaRolePluginModule;
002    
003    public class ParseTree {
004      private Entity root;
005      private Entity current;
006      private boolean hasSyntaxError;
007    
008      public ParseTree(String file) {
009        hasSyntaxError=false;
010        hasSyntaxError=generateParseTree(file);
011      }
012    
013      public boolean hasSyntaxError() {
014        return hasSyntaxError;
015      }
016    
017      private boolean generateParseTree(String file) {
018        Entity currentParent = new Entity(file);
019        currentParent.type = Entity.FILE;
020        currentParent.startPos = 0;
021    
022        Parser parser = new Parser(file);
023    
024        current=parser.getNextEntity();
025    
026        while(current.type!=Entity.ENDOFFILE &&
027              current.type!=Entity.SYNTAXERROR) {
028    
029          //RMTester.print(current);
030    
031          currentParent.addChild(current);
032    
033          if(current.type==Entity.CLASSBODY) {
034            currentParent=current;
035          }else if(current.type==Entity.ENDOFCLASSBODY) {
036            if(currentParent.type==Entity.CLASSBODY) {
037              currentParent=currentParent.getParent();
038            } else {
039              return true;
040            }
041          }
042    
043          current=parser.getNextEntity();
044    
045        }
046    
047    
048        if(current.type==Entity.SYNTAXERROR) {
049          return true;
050        }
051    
052        if(current.type==Entity.ENDOFFILE && currentParent.type==Entity.FILE) {
053          currentParent.addChild(current);
054          root=currentParent;
055          return false;
056          
057        }else{
058          return true;
059        }
060      }
061    
062    
063      public Entity getFirstChildOfFile() {
064        current=root.getFirstChild();
065        return current;
066      }
067    
068      public Entity getFirstChild() {
069        if(current!=null) {
070          current=current.getFirstChild();
071        }
072        return current;
073      }
074    
075      public Entity getNext() {
076        if(current==null) {
077          return null;
078        }else if(current.type==Entity.CLASSBODY || current.type==Entity.FILE) {
079          current=current.getFirstChild(); // has always at least one child
080        }else if(current.type==Entity.ENDOFFILE) {
081          current=null;
082        }else if(current.type==Entity.ENDOFCLASSBODY) {
083          current=current.getParent().getParent().getNextChild();
084        }else{
085          current=current.getParent().getNextChild();
086        }
087        return current;
088      }
089    
090      /** */
091      public Entity getPrevious() {
092        if(current==null || current.type==Entity.FILE) {
093          current=null;
094          return current;
095        }
096    
097        Entity parent=current.getParent();
098        if(parent.isAtFirstChild()) {
099          current=parent;
100          return current;
101        }
102    
103        current=parent.getPreviousChild();
104        if(current.type==Entity.CLASSBODY) {
105          current=current.getLastChild();
106          return current;
107        } else {
108          return current;
109        }
110      }
111    
112      public Entity getParent() {
113        current=current.getParent();
114        return current;
115      }
116    
117      /** This method skips the first instance of a class
118       *  or method and the body.
119       *  If the next entity is different from *HEAD and *BODY
120       *  this entity is returned.
121       *  If the current entity is *HEAD the method skips the body
122       *  and returns the entity after the body.
123       *  If the current entity is *BODY the entity after the body
124       *  is returned.
125       */
126      public Entity getNextSkipHeadAndBody() {
127        if(current!=null) {
128          if(current.type==Entity.CLASSBODY ||
129             current.type==Entity.SEMICOLON) {
130            Entity parent=current.getParent();
131            current=parent.getNextChild();
132          } else if(current.type==Entity.CLASSHEAD ||
133                    current.type==Entity.METHODHEAD) {
134            Entity parent=current.getParent();
135            parent.getNextChild(); // body or semicolon
136            current=parent.getNextChild();
137          } else {
138            getNext();
139            if(current.type==Entity.CLASSHEAD ||
140               current.type==Entity.METHODHEAD) {
141              Entity parent=current.getParent();
142              parent.getNextChild(); // body or semicolon
143              current=parent.getNextChild();
144            }
145          }
146        }
147    
148        return current;
149      }
150    
151    
152      public Entity removeAndGetPrevious() {
153        Entity parent = current.getParent();
154        current=parent.removeCurrentChildAndGetPrevious();
155        if(current==null) {
156          current=parent;
157        }
158        return current;
159      }
160    
161      public Entity removeAndGetNext() {
162        Entity parent = current.getParent();
163        //System.out.print("Parent in removeAGN:");
164        //RMTester.print(parent);
165        current=parent.removeCurrentChildAndGetNext();
166        if(current==null) {
167          current=parent.getNextChild();
168        }
169        return current;
170      }
171    
172      public void insertBefore(Entity newEntity) {
173        Entity parent=current.getParent();
174        parent.insertChildBefore(newEntity);
175      }
176    
177      /*
178        public void insertAfter(Entity newEntity) {
179        Entity parent=current.getParent();
180        parent.insertChildAfter(newEntity);
181        }*/
182    
183    
184    
185    
186      public Entity getMatching(Entity matchingEntity) {
187        Entity parent=current.getParent();
188        Entity entity=parent.getFirstChild();
189       String temp1 = null, temp2 = null;
190       while(entity!=null) {
191           temp1 = entity.getContent();
192            temp2 = matchingEntity.getContent();
193          if(temp1.equals(temp2)) {
194              //System.out.println("Found match: "+'"'+temp1+'"'+" == "+'"'+temp2+'"');
195              current=entity;
196              return current;
197          }
198          //System.out.println('"'+temp1+'"'+" != "+'"'+temp2+'"');
199          entity=parent.getNextChild();
200        }
201    
202        /* No match */
203        //System.out.println("Found no match for: "+'"'+temp1+'"');
204        
205        return null;
206      }
207    
208      public String toString() {
209        StringBuffer sb=new StringBuffer();
210        getFirstChildOfFile();
211        while(current!=null) {
212          sb.append(current.getContentAndWhitespace());
213          getNext();
214        }
215        return sb.toString();
216      }
217    
218    
219    }
220    
221    
222    
223    
224    
225