KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > javasource > JavaSourceIterator


1 /**
2  * An object for iterating a {@link JavaSource} object by getting connected pieces of Java source
3  * code having the same type. Line breaks are omitted, but empty lines will be covered.
4  *
5  * @see JavaSourceRun
6  */

7
8 package de.java2html.javasource;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12
13 public class JavaSourceIterator implements Iterator JavaDoc{
14   private int startIndex;
15   private int endIndex;
16   private JavaSource javaSource;
17   private boolean finished;
18   private boolean isNewLine;
19   
20   public JavaSourceIterator(JavaSource javaSource) {
21     this.javaSource = javaSource;
22     finished = false;
23     isNewLine=false;
24     startIndex = 0;
25     endIndex = 0;
26     if (javaSource.getCode().length()==0){
27       finished = true;
28     }
29   }
30
31   private void seekToNext() {
32     if (isNewLine){
33       startIndex = endIndex+2;
34       endIndex = startIndex+1;
35       isNewLine=false;
36     }else{
37       startIndex = endIndex;
38       endIndex = startIndex+1;
39     }
40     
41     if (endIndex>javaSource.getCode().length()){
42       --endIndex;
43     }
44     
45 // System.out.println(startIndex+".."+endIndex);
46

47     
48     while(true){
49       if (endIndex==javaSource.getCode().length()){
50 //System.out.println("1");
51
break;
52       }
53       if (endIndex<=javaSource.getCode().length()-1 && javaSource.getCode().charAt(endIndex)=='\n'){
54         --endIndex;
55  
56         isNewLine = true;
57 //System.out.println("2");
58
break;
59       }
60       if (javaSource.getClassification()[endIndex]!=javaSource.getClassification()[startIndex] &&
61           javaSource.getClassification()[endIndex]!=JavaSourceType.BACKGROUND){
62 //System.out.println("3");
63
break;
64       }
65 //System.out.println("+");
66

67       ++endIndex;
68     }
69 // System.out.println("=>"+startIndex+".."+endIndex);
70
}
71     
72   public boolean hasNext(){
73     return !finished;
74   }
75   
76   public JavaSourceRun getNext() throws NoSuchElementException JavaDoc{
77     if (finished){
78       throw new NoSuchElementException JavaDoc();
79     }
80     seekToNext();
81     
82     //Sonderfall: Hinter abschliessendem Newline in letzer Zeile ("\r\n")
83
if (startIndex>=javaSource.getCode().length()){
84       --startIndex;
85       --endIndex;
86     }
87     JavaSourceRun run = new JavaSourceRun(javaSource, startIndex, endIndex);
88     finished = endIndex==javaSource.getCode().length();
89     return run;
90   }
91   
92   public Object JavaDoc next() throws NoSuchElementException JavaDoc{
93     return getNext();
94   }
95
96   public void remove() {
97     //nothing to do
98
}
99 }
Popular Tags