1 11 package org.eclipse.jdt.internal.ui.text; 12 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.jface.text.BadLocationException; 17 import org.eclipse.jface.text.IDocument; 18 import org.eclipse.jface.text.rules.ICharacterScanner; 19 20 import org.eclipse.jdt.internal.ui.JavaPlugin; 21 22 23 24 28 29 public final class BufferedDocumentScanner implements ICharacterScanner { 30 31 32 private IDocument fDocument; 33 34 private int fRangeOffset; 35 36 private int fRangeLength; 37 38 private char[][] fDelimiters; 39 40 41 private final char[] fBuffer; 42 43 private int fBufferOffset; 44 45 private int fBufferLength; 46 47 private int fOffset; 48 49 50 56 public BufferedDocumentScanner(int size) { 57 Assert.isTrue(size >= 1); 58 fBuffer= new char[size]; 59 } 60 61 66 private final void updateBuffer(int offset) { 67 68 fBufferOffset= offset; 69 70 if (fBufferOffset + fBuffer.length > fRangeOffset + fRangeLength) 71 fBufferLength= fRangeLength - (fBufferOffset - fRangeOffset); 72 else 73 fBufferLength= fBuffer.length; 74 75 try { 76 final String content= fDocument.get(fBufferOffset, fBufferLength); 77 content.getChars(0, fBufferLength, fBuffer, 0); 78 } catch (BadLocationException e) { 79 } 80 } 81 82 89 public final void setRange(IDocument document, int offset, int length) { 90 91 fDocument= document; 92 fRangeOffset= offset; 93 fRangeLength= length; 94 95 String [] delimiters= document.getLegalLineDelimiters(); 96 fDelimiters= new char[delimiters.length][]; 97 for (int i= 0; i < delimiters.length; i++) 98 fDelimiters[i]= delimiters[i].toCharArray(); 99 100 updateBuffer(offset); 101 fOffset= 0; 102 } 103 104 107 public final int read() { 108 109 if (fOffset == fBufferLength) { 110 int end= fBufferOffset + fBufferLength; 111 if (end == fDocument.getLength() || end == fRangeOffset + fRangeLength) 112 return EOF; 113 else { 114 updateBuffer(fBufferOffset + fBufferLength); 115 fOffset= 0; 116 } 117 } 118 119 try { 120 return fBuffer[fOffset++]; 121 } catch (ArrayIndexOutOfBoundsException ex) { 122 StringBuffer buf= new StringBuffer (); 123 buf.append("Detailed state of 'BufferedDocumentScanner:'"); buf.append("\n\tfOffset= "); buf.append(fOffset); 126 buf.append("\n\tfBufferOffset= "); buf.append(fBufferOffset); 128 buf.append("\n\tfBufferLength= "); buf.append(fBufferLength); 130 buf.append("\n\tfRangeOffset= "); buf.append(fRangeOffset); 132 buf.append("\n\tfRangeLength= "); buf.append(fRangeLength); 134 JavaPlugin.logErrorMessage(buf.toString()); 135 throw ex; 136 } 137 } 138 139 142 public final void unread() { 143 144 if (fOffset == 0) { 145 if (fBufferOffset == fRangeOffset) { 146 } else { 148 updateBuffer(fBufferOffset - fBuffer.length); 149 fOffset= fBuffer.length - 1; 150 } 151 } else { 152 --fOffset; 153 } 154 } 155 156 159 public final int getColumn() { 160 161 try { 162 final int offset= fBufferOffset + fOffset; 163 final int line= fDocument.getLineOfOffset(offset); 164 final int start= fDocument.getLineOffset(line); 165 return offset - start; 166 } catch (BadLocationException e) { 167 } 168 169 return -1; 170 } 171 172 175 public final char[][] getLegalLineDelimiters() { 176 return fDelimiters; 177 } 178 } 179 | Popular Tags |