Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.openide.util; |
44 |
|
45 |
import java.io.IOException; |
46 |
import java.io.InputStream; |
47 |
import java.io.OutputStream; |
48 |
import java.io.PrintWriter; |
49 |
import java.io.Writer; |
50 |
import java.util.ArrayList; |
51 |
import java.util.Collections; |
52 |
import java.util.HashMap; |
53 |
import java.util.HashSet; |
54 |
import java.util.List; |
55 |
import java.util.Map; |
56 |
import java.util.Set; |
57 |
import java.util.TreeMap; |
58 |
import java.util.regex.Matcher; |
59 |
import java.util.regex.Pattern; |
60 |
import javax.annotation.processing.AbstractProcessor; |
61 |
import javax.annotation.processing.Processor; |
62 |
import javax.annotation.processing.RoundEnvironment; |
63 |
import javax.annotation.processing.SupportedSourceVersion; |
64 |
import javax.lang.model.SourceVersion; |
65 |
import javax.lang.model.element.Element; |
66 |
import javax.lang.model.element.PackageElement; |
67 |
import javax.lang.model.element.TypeElement; |
68 |
import javax.tools.Diagnostic.Kind; |
69 |
import javax.tools.StandardLocation; |
70 |
import org.openide.util.EditableProperties; |
71 |
import org.openide.util.NbBundle; |
72 |
import org.openide.util.Utilities; |
73 |
import org.openide.util.lookup.ServiceProvider; |
74 |
|
75 |
@ServiceProvider(service = Processor.class) |
76 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
77 |
public class NbBundleProcessor extends AbstractProcessor { |
78 |
|
79 |
public @Override Set<String> getSupportedAnnotationTypes() { |
80 |
return Collections.singleton(NbBundle.Messages.class.getCanonicalName()); |
81 |
} |
82 |
|
83 |
public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
84 |
if (roundEnv.processingOver()) { |
85 |
return false; |
86 |
} |
87 |
Map</*package*/String,Map</*key*/String,/*value*/String>> pairs = new HashMap<String,Map<String,String>>(); |
88 |
Map</*package*/String,Set</*identifier*/String>> identifiers = new HashMap<String,Set<String>>(); |
89 |
Map</*package*/String,List<Element>> originatingElements = new HashMap<String,List<Element>>(); |
90 |
Map</*package*/String,Map</*key*/String,/*line*/String[]>> comments = new HashMap<String,Map<String,String[]>>(); |
91 |
for (Element e : roundEnv.getElementsAnnotatedWith(NbBundle.Messages.class)) { |
92 |
String pkg = findPackage(e); |
93 |
Map<String, String> pairsByPackage = pairs.get(pkg); |
94 |
if (pairsByPackage == null) { |
95 |
pairsByPackage = new HashMap<String, String>(); |
96 |
pairs.put(pkg, pairsByPackage); |
97 |
} |
98 |
Set<String> identifiersByPackage = identifiers.get(pkg); |
99 |
if (identifiersByPackage == null) { |
100 |
identifiersByPackage = new HashSet<String>(); |
101 |
identifiers.put(pkg, identifiersByPackage); |
102 |
} |
103 |
List<Element> originatingElementsByPackage = originatingElements.get(pkg); |
104 |
if (originatingElementsByPackage == null) { |
105 |
originatingElementsByPackage = new ArrayList<Element>(); |
106 |
originatingElements.put(pkg, originatingElementsByPackage); |
107 |
} |
108 |
Map<String,String[]> commentsByPackage = comments.get(pkg); |
109 |
if (commentsByPackage == null) { |
110 |
commentsByPackage = new HashMap<String,String[]>(); |
111 |
comments.put(pkg, commentsByPackage); |
112 |
} |
113 |
List<String> runningComments = new ArrayList<String>(); |
114 |
for (String keyValue : e.getAnnotation(NbBundle.Messages.class).value()) { |
115 |
if (keyValue.startsWith("#")) { |
116 |
runningComments.add(keyValue); |
117 |
continue; |
118 |
} |
119 |
int i = keyValue.indexOf('='); |
120 |
if (i == -1) { |
121 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Bad key=value: " + keyValue, e); |
122 |
continue; |
123 |
} |
124 |
String key = keyValue.substring(0, i); |
125 |
if (key.isEmpty() || !key.equals(key.trim())) { |
126 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Whitespace not permitted in key: " + keyValue, e); |
127 |
continue; |
128 |
} |
129 |
if (!identifiersByPackage.add(toIdentifier(key))) { |
130 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Duplicate key: " + key, e); |
131 |
continue; |
132 |
} |
133 |
String value = keyValue.substring(i + 1); |
134 |
pairsByPackage.put(key, value); |
135 |
originatingElementsByPackage.add(e); |
136 |
if (!runningComments.isEmpty()) { |
137 |
commentsByPackage.put(key, runningComments.toArray(new String[runningComments.size()])); |
138 |
runningComments.clear(); |
139 |
} |
140 |
} |
141 |
if (!runningComments.isEmpty()) { |
142 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Comments must precede keys", e); |
143 |
} |
144 |
} |
145 |
for (Map.Entry<String,Map<String,String>> entry : pairs.entrySet()) { |
146 |
String pkg = entry.getKey(); |
147 |
Map<String,String> keysAndValues = entry.getValue(); |
148 |
Element[] elements = originatingElements.get(pkg).toArray(new Element[0]); |
149 |
try { |
150 |
EditableProperties p = new EditableProperties(true); |
151 |
// Load any preexisting bundle so we can just add our keys. |
152 |
try { |
153 |
InputStream is = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, pkg, "Bundle.properties").openInputStream(); |
154 |
try { |
155 |
p.load(is); |
156 |
} finally { |
157 |
is.close(); |
158 |
} |
159 |
} catch (IOException x) { |
160 |
// OK, not there |
161 |
} |
162 |
for (String key : p.keySet()) { |
163 |
if (keysAndValues.containsKey(key)) { |
164 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Key " + key + " is a duplicate of one from Bundle.properties", elements[0]); |
165 |
} |
166 |
} |
167 |
// Also check class output for (1) incremental builds, (2) preexisting bundles from Maven projects. |
168 |
try { |
169 |
InputStream is = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, "Bundle.properties").openInputStream(); |
170 |
try { |
171 |
// do not use p.load(is) as the impl in EditableProperties does not currently handle duplicates properly |
172 |
EditableProperties p2 = new EditableProperties(true); |
173 |
p2.load(is); |
174 |
p.putAll(p2); |
175 |
} finally { |
176 |
is.close(); |
177 |
} |
178 |
} catch (IOException x) { |
179 |
// OK, not there |
180 |
} |
181 |
p.putAll(keysAndValues); |
182 |
for (Map.Entry<String,String[]> entry2 : comments.get(pkg).entrySet()) { |
183 |
p.setComment(entry2.getKey(), entry2.getValue(), false); |
184 |
} |
185 |
OutputStream os = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, pkg, "Bundle.properties", elements).openOutputStream(); |
186 |
try { |
187 |
p.store(os); |
188 |
} finally { |
189 |
os.close(); |
190 |
} |
191 |
Map</*identifier*/String,/*method body*/String> methods = new TreeMap<String,String>(); |
192 |
try { |
193 |
Matcher m = Pattern.compile(" /[*][*]\r?\n(?: [*].+\r?\n)+ [*]/\r?\n static String (\\w+).+\r?\n .+\r?\n [}]\r?\n").matcher(processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, pkg, "Bundle.java").getCharContent(false)); |
194 |
while (m.find()) { |
195 |
methods.put(m.group(1), m.group()); |
196 |
} |
197 |
} catch (IOException x) { |
198 |
// OK, not there |
199 |
} |
200 |
for (Map.Entry<String, String> entry2 : keysAndValues.entrySet()) { |
201 |
String key = entry2.getKey(); |
202 |
String value = entry2.getValue(); |
203 |
StringBuilder method = new StringBuilder(); |
204 |
method.append(" /**\n"); |
205 |
List<String> params = new ArrayList<String>(); |
206 |
int i = 0; |
207 |
while (value.contains("{" + i)) { |
208 |
params.add("arg" + i++); |
209 |
} |
210 |
String[] commentLines = comments.get(pkg).get(key); |
211 |
if (commentLines != null) { |
212 |
for (String comment : commentLines) { |
213 |
Matcher m = Pattern.compile("# [{](\\d+)[}] - (.+)").matcher(comment); |
214 |
if (m.matches()) { |
215 |
i = Integer.parseInt(m.group(1)); |
216 |
String desc = m.group(2); |
217 |
params.set(i, toIdentifier(desc)); |
218 |
method.append(" * @param ").append(params.get(i)).append(" ").append(toJavadoc(desc)).append("\n"); |
219 |
} |
220 |
} |
221 |
} |
222 |
method.append(" * @return ").append(toJavadoc(value)).append("\n"); |
223 |
method.append(" */\n"); |
224 |
String name = toIdentifier(key); |
225 |
method.append(" static String ").append(name).append("("); |
226 |
boolean first = true; |
227 |
for (String param : params) { |
228 |
if (first) { |
229 |
first = false; |
230 |
} else { |
231 |
method.append(", "); |
232 |
} |
233 |
method.append("Object ").append(param); |
234 |
} |
235 |
method.append(") {\n"); |
236 |
method.append(" return org.openide.util.NbBundle.getMessage(Bundle.class, \"").append(key).append("\""); |
237 |
for (String param : params) { |
238 |
method.append(", ").append(param); |
239 |
} |
240 |
method.append(");\n"); |
241 |
method.append(" }\n"); |
242 |
methods.put(name, method.toString()); |
243 |
} |
244 |
String fqn = pkg + ".Bundle"; |
245 |
Writer w = processingEnv.getFiler().createSourceFile(fqn, elements).openWriter(); |
246 |
try { |
247 |
PrintWriter pw = new PrintWriter(w); |
248 |
pw.println("package " + pkg + ";"); |
249 |
pw.println("/** Localizable strings for {@link " + pkg + "}. */"); |
250 |
pw.println("class Bundle {"); |
251 |
for (String method : methods.values()) { |
252 |
pw.print(method); |
253 |
} |
254 |
pw.println(" private void Bundle() {}"); |
255 |
pw.println("}"); |
256 |
pw.flush(); |
257 |
pw.close(); |
258 |
} finally { |
259 |
w.close(); |
260 |
} |
261 |
} catch (IOException x) { |
262 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Could not generate files: " + x, elements[0]); |
263 |
} |
264 |
} |
265 |
return true; |
266 |
} |
267 |
|
268 |
private String findPackage(Element e) { |
269 |
switch (e.getKind()) { |
270 |
case PACKAGE: |
271 |
return ((PackageElement) e).getQualifiedName().toString(); |
272 |
default: |
273 |
return findPackage(e.getEnclosingElement()); |
274 |
} |
275 |
} |
276 |
|
277 |
private String toIdentifier(String key) { |
278 |
if (Utilities.isJavaIdentifier(key)) { |
279 |
return key; |
280 |
} else { |
281 |
String i = key.replaceAll("[^\\p{javaJavaIdentifierPart}]+", "_"); |
282 |
if (Utilities.isJavaIdentifier(i)) { |
283 |
return i; |
284 |
} else { |
285 |
return "_" + i; |
286 |
} |
287 |
} |
288 |
} |
289 |
|
290 |
private String toJavadoc(String text) { |
291 |
return text.replace("&", "&").replace("<", "<").replace("*/", "*/").replace("\n", "<br>").replace("@", "@"); |
292 |
} |
293 |
|
294 |
} |