1 19 20 package org.netbeans.modules.java.guards; 21 22 import java.io.CharArrayWriter ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import org.netbeans.api.editor.guards.GuardedSection; 30 import org.netbeans.api.editor.guards.InteriorSection; 31 import org.netbeans.api.editor.guards.SimpleSection; 32 33 37 final class JavaGuardedWriter { 38 39 private Iterator <SectionDescriptor> descs; 40 41 private CharArrayWriter writer; 42 43 46 private SectionDescriptor current; 47 48 51 private int offsetCounter; 52 53 54 boolean wasNewLine; 55 56 57 int spaces; 58 59 60 public JavaGuardedWriter() { 61 } 62 63 public void setGuardedSection(List <GuardedSection> sections) { 64 assert this.descs == null; this.descs = prepareSections(sections).iterator(); 66 } 67 68 public char[] translate(char[] writeBuff) { 69 if (this.descs == null || !this.descs.hasNext()) { 70 return writeBuff; 71 } 72 this.writer = new CharArrayWriter (writeBuff.length); 73 this.offsetCounter = 0; 74 this.wasNewLine = false; 75 76 nextSection(); 77 78 try { 79 for (char c : writeBuff) { 80 writeOneChar(c); 81 } 82 return this.writer.toCharArray(); 83 } catch (IOException ex) { 84 throw new IllegalStateException (ex); 86 } finally { 87 this.writer = null; 88 this.current = null; 89 } 90 91 } 92 93 97 void writeOneChar(int b) throws IOException { 98 if (b == '\r') 99 return; 100 101 if (current != null) { 102 if (offsetCounter == current.getBegin()) { 103 wasNewLine = false; 104 } 105 if ((b == '\n') && (current.getBegin() <= offsetCounter)) { 106 switch(current.getType()) { 107 case LINE: 108 109 if (!wasNewLine) { 110 if (offsetCounter + 1 >= current.getEnd()) { 111 writeMagic(GuardTag.LINE, current.getName()); 112 nextSection(); 113 } 114 else { 115 writeMagic(GuardTag.BEGIN, current.getName()); 116 wasNewLine = true; 117 } 118 } 119 else { 120 if (offsetCounter + 1 >= current.getEnd()) { 121 writeMagic(GuardTag.END, current.getName()); 122 nextSection(); 123 } 124 } 125 126 break; 127 case FIRST: 128 case HEADER: 129 130 if (!wasNewLine) { 131 if (offsetCounter + 1 >= current.getEnd()) { 132 writeMagic(GuardTag.FIRST, current.getName()); 133 nextSection(); 134 } 135 else { 136 writeMagic(GuardTag.FIRST, current.getName()); 137 wasNewLine = true; 138 } 139 } 140 else { 141 if (offsetCounter + 1 >= current.getEnd()) { 142 writeMagic(GuardTag.HEADEREND, current.getName()); 143 nextSection(); 144 } 145 } 146 147 break; 148 case LAST: 149 case END: 150 151 writeMagic(GuardTag.LAST, current.getName()); 152 153 nextSection(); 154 155 break; 156 } 157 } 158 } 159 if (b==' ') 160 spaces++; 161 else { 162 if (spaces > 0) { 163 char[] sp = new char[spaces]; 164 Arrays.fill(sp,' '); 165 writer.write(sp); 166 spaces=0; 167 } 168 writer.write(b); 169 } 170 offsetCounter++; 171 } 172 173 176 private void nextSection() { 177 current = descs.hasNext() ? descs.next() : null; 178 } 179 180 184 private void writeMagic(GuardTag type, String name) throws IOException { 185 spaces = 0; 190 String magic = JavaGuardedReader.MAGIC_PREFIX + type.name() + ':'; 191 writer.write(magic, 0, magic.length()); 192 writer.write(name, 0, name.length()); 193 } 194 195 199 private List <SectionDescriptor> prepareSections(List <? extends GuardedSection> list) { 200 List <SectionDescriptor> dest = new ArrayList <SectionDescriptor>(list.size()); 201 202 for (GuardedSection o: list) { 203 if (o instanceof SimpleSection) { 204 SectionDescriptor desc = new SectionDescriptor( 205 GuardTag.LINE, 206 o.getName(), 207 o.getStartPosition().getOffset(), 208 o.getEndPosition().getOffset() 209 ); 210 dest.add(desc); 211 } else { 212 SectionDescriptor desc = new SectionDescriptor( 213 GuardTag.HEADER, 214 o.getName(), 215 o.getStartPosition().getOffset(), 216 ((InteriorSection) o).getBodyStartPosition().getOffset() - 1 217 ); 218 dest.add(desc); 219 220 desc = new SectionDescriptor( 221 GuardTag.END, 222 o.getName(), 223 ((InteriorSection) o).getBodyEndPosition().getOffset() + 1, 224 o.getEndPosition().getOffset() 225 ); 226 dest.add(desc); 227 } 228 } 229 return dest; 230 } 231 232 } 233 | Popular Tags |