1 19 20 package org.netbeans.modules.java.guards; 21 22 import java.nio.CharBuffer ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import java.util.regex.Matcher ; 30 import java.util.regex.Pattern ; 31 import javax.swing.text.BadLocationException ; 32 import org.netbeans.api.editor.guards.GuardedSection; 33 import org.netbeans.modules.java.guards.GuardTag; 34 import org.netbeans.modules.java.guards.SectionDescriptor; 35 36 40 final class JavaGuardedReader { 41 42 43 final static String MAGIC_PREFIX = "//GEN-"; 45 Pattern magicsAsRE; 46 47 private static final int LONGEST_ITEM = 10; 48 49 50 private final LinkedList <SectionDescriptor> list; 51 52 private final JavaGuardedSectionsProvider provider; 53 54 55 public JavaGuardedReader(JavaGuardedSectionsProvider provider) { 56 list = new LinkedList <SectionDescriptor>(); 57 this.provider = provider; 58 } 59 60 public List <GuardedSection> getGuardedSections() { 61 return fillSections(list); 62 } 63 64 public char[] translateToCharBuff(char[] readBuff) { 65 char[] charBuff = new char[readBuff.length]; 66 int charBuffPtr = 0; 68 int stop = readBuff.length - 1; 69 70 int c; 72 int i = 0; 74 int lastNewLine = 0; 76 77 int fatpos = 0; 79 final int MAGICLEN = MAGIC_PREFIX.length(); 80 81 82 while (i < stop) { 85 c = readBuff[i]; 86 if (c == '\n') { 87 lastNewLine = charBuffPtr; 88 } 89 charBuff[charBuffPtr++] = readBuff[i++]; 90 91 switch (fatpos) { 92 case 0: 93 if (c == '/') { 94 fatpos++; 95 } else { 96 fatpos = 0; 97 } 98 break; 99 100 case 1: 101 if (c == '/') { 102 fatpos++; 103 } else { 104 fatpos = 0; 105 } 106 break; 107 108 case 2: 109 if (c == 'G') { 110 fatpos++; 111 } else if (c == '/') { 112 fatpos = 2; } else { 114 fatpos = 0; 115 } 116 break; 117 118 case 3: 119 if (c == 'E') { 120 fatpos++; 121 } else { 122 fatpos = 0; 123 } 124 break; 125 126 case 4: 127 if (c == 'N') { 128 fatpos++; 129 } else { 130 fatpos = 0; 131 } 132 break; 133 134 case 5: 135 if (c == '-') { 136 fatpos++; 137 } else { 138 fatpos = 0; 139 } 140 break; 141 142 default: 143 fatpos = 0; 144 } 145 146 if (fatpos == MAGICLEN) { 148 fatpos = 0; 149 Pattern magics = getMagicsAsRE(); 150 int searchLen = Math.min(LONGEST_ITEM, readBuff.length - i); 151 CharBuffer chi = CharBuffer.wrap(readBuff, i, searchLen); 152 Matcher matcher = magics.matcher(chi); 153 if (matcher.find()) { 154 String match = matcher.group(); 155 156 charBuffPtr -= MAGICLEN; 157 i += match.length(); 158 int toNl = toNewLine(i, readBuff); 159 int sectionSize = MAGICLEN+match.length()+toNl; 160 161 SectionDescriptor desc = new SectionDescriptor( 164 GuardTag.valueOf(match.substring(0, match.length() - 1)), String.valueOf(readBuff, i, toNl), 166 lastNewLine + 1, 167 charBuffPtr + sectionSize 168 ); 169 list.add(desc); 174 i += toNl; 176 Arrays.fill(charBuff,charBuffPtr,charBuffPtr+sectionSize,' '); 177 charBuffPtr+=sectionSize; 178 } 179 } 180 } 181 182 if (i == stop) { 183 charBuff[charBuffPtr++] = readBuff[i++]; 202 } 204 205 if ((list.size() > 0)) { 207 SectionDescriptor desc = (SectionDescriptor) list.getLast(); 208 if (desc.getEnd() > charBuffPtr) { 209 desc.setEnd(charBuffPtr); 210 } 211 } 212 213 char[] res; 214 if (charBuffPtr != charBuff.length) { 215 res = new char[charBuffPtr]; 216 System.arraycopy(charBuff, 0, res, 0, charBuffPtr); 217 } else { 218 res = charBuff; 219 } 220 return charBuff; 221 } 222 223 224 final Pattern getMagicsAsRE() { 225 if (magicsAsRE == null) { 226 magicsAsRE = Pattern.compile(makeOrRegexp()); 227 } 228 return magicsAsRE; 229 } 230 231 232 final String makeOrRegexp() { 233 StringBuilder sb = new StringBuilder (100); 234 for (GuardTag t: GuardTag.values()) { 236 sb.append(t.name() + ':'); 237 sb.append('|'); 238 } 239 240 return sb.substring(0, sb.length() - 1); 241 } 242 243 244 static int toNewLine(int i, char[] readBuff) { 245 int c; 246 int counter = i; 247 final int len = readBuff.length; 248 while (counter < len) { 249 c = readBuff[counter++]; 250 if (c == '\r' || c == '\n') { 251 counter--; 252 break; 253 } 254 } 255 256 return counter - i; 257 } 258 259 265 List <GuardedSection> fillSections(List <SectionDescriptor> descs) { 266 SectionDescriptor descBegin = null; 267 List <GuardedSection> sections = new ArrayList <GuardedSection>(descs.size()); 268 269 for (SectionDescriptor descCurrent: descs) { 270 try { 271 GuardedSection sect = null; 272 switch (descCurrent.getType()) { 273 case LINE: 274 sect = provider.createSimpleSection( 275 descCurrent.getName(), 276 descCurrent.getBegin(), 277 descCurrent.getEnd() 278 ); 279 break; 280 281 case BEGIN: 282 case HEADER: 283 case FIRST: 284 descBegin = descCurrent; 285 break; 286 287 case HEADEREND: 288 if ((descBegin != null) && 289 ((descBegin.getType() == GuardTag.HEADER) || (descBegin.getType() == GuardTag.FIRST)) && 290 (descCurrent.getName().equals(descBegin.getName())) 291 ) { 292 descBegin.setEnd(descCurrent.getEnd()); 293 } 294 else { 295 descBegin = null; 297 } 298 break; 299 300 case END: 301 case LAST: 302 if ((descBegin != null) && (descBegin.getName().equals(descCurrent.getName()))) { 303 if ((descBegin.getType() == GuardTag.BEGIN) && (descCurrent.getType() == GuardTag.END)) { 304 sect = provider.createSimpleSection( 306 descCurrent.getName(), 307 descBegin.getBegin(), descCurrent.getEnd() 308 ); 309 break; 310 } 311 if (((descBegin.getType() == GuardTag.FIRST) && (descCurrent.getType() == GuardTag.LAST)) || 312 ((descBegin.getType() == GuardTag.HEADER) && (descCurrent.getType() == GuardTag.END))) { 313 sect = provider.createInteriorSection( 315 descCurrent.getName(), 316 descBegin.getBegin(), descBegin.getEnd(), 317 descCurrent.getBegin(), descCurrent.getEnd() 318 ); 319 break; 320 } 321 } 322 descBegin = null; 324 break; 325 } 326 327 if (sect != null) { 328 sections.add(sect); 329 } 330 } catch (BadLocationException ex) { 331 Logger.getLogger(JavaGuardedReader.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); 332 } 333 } 334 335 return sections; 336 } 337 338 } 342 | Popular Tags |