1 7 8 package de.java2html.javasource; 9 10 import java.util.Iterator ; 11 import java.util.NoSuchElementException ; 12 13 public class JavaSourceIterator implements Iterator { 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 47 48 while(true){ 49 if (endIndex==javaSource.getCode().length()){ 50 break; 52 } 53 if (endIndex<=javaSource.getCode().length()-1 && javaSource.getCode().charAt(endIndex)=='\n'){ 54 --endIndex; 55 56 isNewLine = true; 57 break; 59 } 60 if (javaSource.getClassification()[endIndex]!=javaSource.getClassification()[startIndex] && 61 javaSource.getClassification()[endIndex]!=JavaSourceType.BACKGROUND){ 62 break; 64 } 65 67 ++endIndex; 68 } 69 } 71 72 public boolean hasNext(){ 73 return !finished; 74 } 75 76 public JavaSourceRun getNext() throws NoSuchElementException { 77 if (finished){ 78 throw new NoSuchElementException (); 79 } 80 seekToNext(); 81 82 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 next() throws NoSuchElementException { 93 return getNext(); 94 } 95 96 public void remove() { 97 } 99 } | Popular Tags |