1 55 56 package org.apache.bsf.dbline; 57 58 import java.io.*; 59 import java.util.*; 60 import org.apache.bsf.*; 61 62 79 80 public class Buffer { 81 82 String m_uri; String m_name; String m_filename; 85 86 String m_lines[]; Vector m_breakpoints; 89 int m_currentLine; 92 94 public Buffer(String uri,String filename,File file) { 95 int index; 96 m_filename = filename; 97 m_lines = loadAndParse2(file); 98 index = m_filename.lastIndexOf(File.separatorChar); 99 m_name = m_filename.substring(index+1); 100 m_uri= uri; 101 102 m_breakpoints = new Vector(); 104 } 105 106 public void addBreakpoint(BreakPoint bp) { 107 m_breakpoints.addElement(bp); 108 } 109 110 StringBuffer buildFnOrScript(int start,int end) { 112 113 StringBuffer buf = new StringBuffer (); 114 String line; 115 116 if (start<0) start = 0; 117 if (end>m_lines.length) end = m_lines.length; 118 119 for (int l = start; l < end ; l++) { 120 line = m_lines[l]; 121 buf.append(line+"\n"); 122 } 123 return buf; 124 } 125 139 public static Buffer factory(String filename, String uri) { 141 int index; 142 Buffer buffer=null; 143 char sep = File.separatorChar; 144 145 filename = filename.replace('/',sep); 146 filename = filename.trim(); 147 148 File file = new File(filename); 149 if (file.exists()) { 150 buffer = new Buffer(uri,filename,file); 151 } else { 152 System.out.println("File "+filename+" does not exist."); 153 } 154 return buffer; 155 } 156 public BreakPoint getBreakpoint(int id) { 158 BreakPoint bp; 159 Enumeration e; 160 e = m_breakpoints.elements(); 161 while (e.hasMoreElements()) { 162 bp = (BreakPoint)e.nextElement(); 163 if (bp.m_id==id) return bp; 164 } 165 return null; 166 } 167 public BreakPoint removeBreakpoint(int id) { 169 BreakPoint bp = getBreakpoint(id); 170 m_breakpoints.removeElement(bp); 171 return bp; 172 } 173 public Enumeration getBreakpoints() { 175 return m_breakpoints.elements(); 176 } 177 public int getCurrentLine() { 179 return m_currentLine; 180 } 181 public void setCurrentLine(int lineno) { 182 m_currentLine = lineno; 183 } 184 public String getFileName() { return m_filename; } 186 public int getLineCount() { 188 return m_lines.length; 189 } 190 public String getLine(int lineno) { 192 try { 193 return m_lines[lineno]; 194 } catch (Throwable t) { 195 return null; 196 } 197 } 198 public String getName() { return m_name; } 200 public String getURI() { return m_uri; } 202 203 204 private String [] loadAndParse2(File file) { 206 int b, size, bytes_read = 0; 207 int count; 208 FileInputStream in; 209 byte bytes[]; 210 String buf, line; 211 Vector lines; 212 StringTokenizer lineTokenizer; 213 214 lines = new Vector(); 215 216 try { 217 size = (int) file.length(); 219 in = new FileInputStream(file); 220 bytes = new byte[size]; 221 count = 0; 222 while (bytes_read < size) { 223 b = in.read(); 224 bytes[count++] = (byte)b; 225 bytes_read++; 226 if (b==(int)'\n') { 227 line = new String (bytes,0,count-1); 228 lines.addElement(line); 229 count=0; 230 } 231 } 232 } catch (Throwable t) { 233 t.printStackTrace(); 234 return null; 235 } 236 String tmp[] = new String [lines.size()]; 237 System.arraycopy(lines.toArray(),0,tmp,0,tmp.length); 238 return tmp; 239 } 240 241 } 242 | Popular Tags |