1 46 47 package org.codehaus.groovy.control.io; 48 49 import java.io.BufferedReader ; 50 import java.io.IOException ; 51 52 import org.codehaus.groovy.control.CompilerConfiguration; 53 import org.codehaus.groovy.control.Janitor; 54 55 56 64 65 public abstract class AbstractReaderSource implements ReaderSource 66 { 67 protected CompilerConfiguration configuration; 69 70 73 74 public AbstractReaderSource( CompilerConfiguration configuration ) 75 { 76 this.configuration = configuration; 77 } 78 79 80 84 85 public boolean canReopenSource() 86 { 87 return true; 88 } 89 90 91 92 95 96 private BufferedReader lineSource = null; private String line = null; private int number = 0; 100 101 105 106 public String getLine( int lineNumber, Janitor janitor ) 107 { 108 112 if( lineSource != null && number > lineNumber ) 113 { 114 cleanup(); 115 } 116 117 118 121 if( lineSource == null ) 122 { 123 try { lineSource = new BufferedReader ( getReader() ); } catch( Exception e ) {} 124 number = 0; 125 } 126 127 128 131 if( lineSource != null ) 132 { 133 while( number < lineNumber ) 134 { 135 try 136 { 137 line = lineSource.readLine(); 138 number++; 139 } 140 catch( IOException e ) 141 { 142 cleanup(); 143 } 144 } 145 146 if( janitor == null ) 147 { 148 cleanup(); 149 } 150 else 151 { 152 janitor.register( this ); 153 } 154 } 155 156 return line; 157 } 158 159 160 161 164 165 public void cleanup() 166 { 167 if( lineSource != null ) 168 { 169 try { lineSource.close(); } catch( Exception e ) {} 170 } 171 172 lineSource = null; 173 line = null; 174 number = 0; 175 } 176 177 } 178 | Popular Tags |