1 11 package org.eclipse.jdt.internal.corext.refactoring.nls; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.List ; 20 21 import org.eclipse.text.edits.InsertEdit; 22 import org.eclipse.text.edits.TextEdit; 23 24 import org.eclipse.core.runtime.Assert; 25 import org.eclipse.core.runtime.CoreException; 26 27 import org.eclipse.jface.text.Region; 28 29 import org.eclipse.jdt.core.IBuffer; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.JavaModelException; 32 import org.eclipse.jdt.core.compiler.InvalidInputException; 33 import org.eclipse.jdt.core.formatter.IndentManipulation; 34 35 36 public class NLSUtil { 37 38 private NLSUtil() { 40 } 41 42 46 public static String readString(InputStream is, String encoding) { 47 if (is == null) 48 return null; 49 BufferedReader reader= null; 50 try { 51 StringBuffer buffer= new StringBuffer (); 52 char[] part= new char[2048]; 53 int read= 0; 54 reader= new BufferedReader (new InputStreamReader (is, encoding)); 55 56 while ((read= reader.read(part)) != -1) 57 buffer.append(part, 0, read); 58 59 return buffer.toString(); 60 61 } catch (IOException ex) { 62 } finally { 63 if (reader != null) { 64 try { 65 reader.close(); 66 } catch (IOException ex) { 67 } 68 } 69 } 70 return null; 71 } 72 73 79 public static TextEdit createNLSEdit(ICompilationUnit cu, int position) throws CoreException { 80 NLSLine nlsLine= scanCurrentLine(cu, position); 81 if (nlsLine == null) 82 return null; 83 NLSElement element= findElement(nlsLine, position); 84 if (element.hasTag()) 85 return null; 86 NLSElement[] elements= nlsLine.getElements(); 87 int indexInElementList= Arrays.asList(elements).indexOf(element); 88 int editOffset= computeInsertOffset(elements, indexInElementList, cu); 89 String editText= ' ' + NLSElement.createTagText(indexInElementList + 1); return new InsertEdit(editOffset, editText); 91 } 92 93 99 public static TextEdit[] createNLSEdits(ICompilationUnit cu, int[] positions) throws CoreException { 100 List result= new ArrayList (); 101 try { 102 NLSLine[] allLines= NLSScanner.scan(cu); 103 for (int i= 0; i < allLines.length; i++) { 104 NLSLine line= allLines[i]; 105 NLSElement[] elements= line.getElements(); 106 for (int j= 0; j < elements.length; j++) { 107 NLSElement element= elements[j]; 108 if (!element.hasTag()) { 109 for (int k= 0; k < positions.length; k++) { 110 if (isPositionInElement(element, positions[k])) { 111 int editOffset; 112 if (j==0) { 113 if (elements.length > j+1) { 114 editOffset= elements[j+1].getTagPosition().getOffset(); 115 } else { 116 editOffset= findLineEnd(cu, element.getPosition().getOffset()); 117 } 118 } else { 119 Region previousPosition= elements[j-1].getTagPosition(); 120 editOffset= previousPosition.getOffset() + previousPosition.getLength(); 121 } 122 String editText= ' ' + NLSElement.createTagText(j + 1); result.add(new InsertEdit(editOffset, editText)); 124 } 125 } 126 } 127 } 128 } 129 } catch (InvalidInputException e) { 130 return null; 131 } 132 if (result.isEmpty()) 133 return null; 134 135 return (TextEdit[])result.toArray(new TextEdit[result.size()]); 136 } 137 138 private static NLSLine scanCurrentLine(ICompilationUnit cu, int position) throws JavaModelException { 139 try { 140 Assert.isTrue(position >= 0 && position <= cu.getBuffer().getLength()); 141 NLSLine[] allLines= NLSScanner.scan(cu); 142 for (int i= 0; i < allLines.length; i++) { 143 NLSLine line= allLines[i]; 144 if (findElement(line, position) != null) 145 return line; 146 } 147 return null; 148 } catch (InvalidInputException e) { 149 return null; 150 } 151 } 152 153 private static boolean isPositionInElement(NLSElement element, int position) { 154 Region elementPosition= element.getPosition(); 155 return (elementPosition.getOffset() <= position && position <= elementPosition.getOffset() + elementPosition.getLength()); 156 } 157 158 private static NLSElement findElement(NLSLine line, int position) { 159 NLSElement[] elements= line.getElements(); 160 for (int i= 0; i < elements.length; i++) { 161 NLSElement element= elements[i]; 162 if (isPositionInElement(element, position)) 163 return element; 164 } 165 return null; 166 } 167 168 private static int computeInsertOffset(NLSElement[] elements, int index, ICompilationUnit cu) throws CoreException { 173 NLSElement previousTagged= findPreviousTagged(index, elements); 174 if (previousTagged != null) 175 return previousTagged.getTagPosition().getOffset() + previousTagged.getTagPosition().getLength(); 176 NLSElement nextTagged= findNextTagged(index, elements); 177 if (nextTagged != null) 178 return nextTagged.getTagPosition().getOffset(); 179 return findLineEnd(cu, elements[index].getPosition().getOffset()); 180 } 181 182 private static NLSElement findPreviousTagged(int startIndex, NLSElement[] elements) { 183 int i= startIndex - 1; 184 while (i >= 0) { 185 if (elements[i].hasTag()) 186 return elements[i]; 187 i--; 188 } 189 return null; 190 } 191 192 private static NLSElement findNextTagged(int startIndex, NLSElement[] elements) { 193 int i= startIndex + 1; 194 while (i < elements.length) { 195 if (elements[i].hasTag()) 196 return elements[i]; 197 i++; 198 } 199 return null; 200 } 201 202 private static int findLineEnd(ICompilationUnit cu, int position) throws JavaModelException { 203 IBuffer buffer= cu.getBuffer(); 204 int length= buffer.getLength(); 205 for (int i= position; i < length; i++) { 206 if (IndentManipulation.isLineDelimiterChar(buffer.getChar(i))) { 207 return i; 208 } 209 } 210 return length; 211 } 212 } 213 | Popular Tags |