KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > NLSUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.nls;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.List JavaDoc;
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     //no instances
39
private NLSUtil() {
40     }
41
42     /**
43      * Returns null if an error occurred.
44      * closes the stream
45      */

46     public static String JavaDoc readString(InputStream JavaDoc is, String JavaDoc encoding) {
47         if (is == null)
48             return null;
49         BufferedReader JavaDoc reader= null;
50         try {
51             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
52             char[] part= new char[2048];
53             int read= 0;
54             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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 JavaDoc ex) {
62         } finally {
63             if (reader != null) {
64                 try {
65                     reader.close();
66                 } catch (IOException JavaDoc ex) {
67                 }
68             }
69         }
70         return null;
71     }
72
73     /**
74      * Creates and returns an NLS tag edit for a string that is at the specified position in
75      * a compilation unit. Returns <code>null</code> if the string is already NLSed
76      * or the edit could not be created for some other reason.
77      * @throws CoreException
78      */

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 JavaDoc editText= ' ' + NLSElement.createTagText(indexInElementList + 1); //tags are 1-based
90
return new InsertEdit(editOffset, editText);
91     }
92     
93     /**
94      * Creates and returns NLS tag edits for strings that are at the specified positions in
95      * a compilation unit. Returns <code>null</code> if all the strings are already NLSed
96      * or the edits could not be created for some other reason.
97      * @throws CoreException
98      */

99     public static TextEdit[] createNLSEdits(ICompilationUnit cu, int[] positions) throws CoreException {
100         List JavaDoc result= new ArrayList JavaDoc();
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 JavaDoc editText= ' ' + NLSElement.createTagText(j + 1); //tags are 1-based
123
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     //we try to find a good place to put the nls tag
169
//first, try to find the previous nlsed-string and try putting after its tag
170
//if no such string exists, try finding the next nlsed-string try putting before its tag
171
//otherwise, find the line end and put the tag there
172
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