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.OutputStream; |
47 |
import java.io.PrintWriter; |
48 |
import java.io.Writer; |
49 |
import java.util.ArrayList; |
50 |
import java.util.Collections; |
51 |
import java.util.HashMap; |
52 |
import java.util.HashSet; |
53 |
import java.util.List; |
54 |
import java.util.Map; |
55 |
import java.util.Set; |
56 |
import javax.annotation.processing.AbstractProcessor; |
57 |
import javax.annotation.processing.Processor; |
58 |
import javax.annotation.processing.RoundEnvironment; |
59 |
import javax.annotation.processing.SupportedSourceVersion; |
60 |
import javax.lang.model.SourceVersion; |
61 |
import javax.lang.model.element.Element; |
62 |
import javax.lang.model.element.PackageElement; |
63 |
import javax.lang.model.element.TypeElement; |
64 |
import javax.tools.Diagnostic.Kind; |
65 |
import javax.tools.StandardLocation; |
66 |
import org.openide.util.EditableProperties; |
67 |
import org.openide.util.NbBundle; |
68 |
import org.openide.util.Utilities; |
69 |
import org.openide.util.lookup.ServiceProvider; |
70 |
|
71 |
@ServiceProvider(service = Processor.class) |
72 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
73 |
public class NbBundleProcessor extends AbstractProcessor { |
74 |
|
75 |
public @Override Set<String> getSupportedAnnotationTypes() { |
76 |
return Collections.singleton(NbBundle.Messages.class.getCanonicalName()); |
77 |
} |
78 |
|
79 |
public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
80 |
if (roundEnv.processingOver()) { |
81 |
return false; |
82 |
} |
83 |
Map</*package*/String,Map</*key*/String,/*value*/String>> pairs = new HashMap<String,Map<String,String>>(); |
84 |
Map</*package*/String,Set</*identifier*/String>> identifiers = new HashMap<String,Set<String>>(); |
85 |
Map</*package*/String,List<Element>> originatingElements = new HashMap<String,List<Element>>(); |
86 |
for (Element e : roundEnv.getElementsAnnotatedWith(NbBundle.Messages.class)) { |
87 |
String pkg = findPackage(e); |
88 |
try { |
89 |
processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, pkg, "Bundle.properties").openInputStream().close(); |
90 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Bundle.properties already exists in " + pkg, e); |
91 |
continue; |
92 |
} catch (IOException x) { |
93 |
// fine, does not exist |
94 |
} |
95 |
for (String keyValue : e.getAnnotation(NbBundle.Messages.class).value()) { |
96 |
int i = keyValue.indexOf('='); |
97 |
if (i == -1) { |
98 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Bad key=value: " + keyValue, e); |
99 |
continue; |
100 |
} |
101 |
String key = keyValue.substring(0, i); |
102 |
if (key.isEmpty() || !key.equals(key.trim())) { |
103 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Whitespace not permitted in key: " + keyValue, e); |
104 |
continue; |
105 |
} |
106 |
Set<String> identifiersByPackage = identifiers.get(pkg); |
107 |
if (identifiersByPackage == null) { |
108 |
identifiersByPackage = new HashSet<String>(); |
109 |
identifiers.put(pkg, identifiersByPackage); |
110 |
} |
111 |
if (!identifiersByPackage.add(toIdentifier(key))) { |
112 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Duplicate key: " + key, e); |
113 |
continue; |
114 |
} |
115 |
String value = keyValue.substring(i + 1); |
116 |
Map<String,String> pairsByPackage = pairs.get(pkg); |
117 |
if (pairsByPackage == null) { |
118 |
pairsByPackage = new HashMap<String,String>(); |
119 |
pairs.put(pkg, pairsByPackage); |
120 |
} |
121 |
pairsByPackage.put(key, value); |
122 |
List<Element> originatingElementsByPackage = originatingElements.get(pkg); |
123 |
if (originatingElementsByPackage == null) { |
124 |
originatingElementsByPackage = new ArrayList<Element>(); |
125 |
originatingElements.put(pkg, originatingElementsByPackage); |
126 |
} |
127 |
originatingElementsByPackage.add(e); |
128 |
} |
129 |
} |
130 |
for (Map.Entry<String,Map<String,String>> entry : pairs.entrySet()) { |
131 |
String pkg = entry.getKey(); |
132 |
Map<String,String> keysAndValues = entry.getValue(); |
133 |
Element[] elements = originatingElements.get(pkg).toArray(new Element[0]); |
134 |
try { |
135 |
OutputStream os = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, pkg, "Bundle.properties", elements).openOutputStream(); |
136 |
try { |
137 |
EditableProperties p = new EditableProperties(true); |
138 |
p.putAll(keysAndValues); |
139 |
p.store(os); |
140 |
} finally { |
141 |
os.close(); |
142 |
} |
143 |
String fqn = pkg + ".Bundle"; |
144 |
Writer w = processingEnv.getFiler().createSourceFile(fqn, elements).openWriter(); |
145 |
try { |
146 |
PrintWriter pw = new PrintWriter(w); |
147 |
pw.println("package " + pkg + ";"); |
148 |
pw.println("/** Localizable strings for {@link " + pkg + "}. */"); |
149 |
pw.println("class Bundle {"); |
150 |
for (Map.Entry<String,String> entry2 : keysAndValues.entrySet()) { |
151 |
String key = entry2.getKey(); |
152 |
String value = entry2.getValue(); |
153 |
pw.println(" /** " + value.replace("&", "&").replace("<", "<").replace("*/", "*/").replace("\n", "<br>").replace("@", "@") + " */"); |
154 |
pw.print(" static String " + toIdentifier(key) + "("); |
155 |
int params = 0; |
156 |
while (value.contains("{" + params)) { |
157 |
params++; |
158 |
} |
159 |
for (int i = 0; i < params; i++) { |
160 |
if (i > 0) { |
161 |
pw.print(", "); |
162 |
} |
163 |
pw.print("Object arg" + i); |
164 |
} |
165 |
pw.println(") {"); |
166 |
pw.print(" return org.openide.util.NbBundle.getMessage(Bundle.class, \"" + key + "\""); |
167 |
for (int i = 0; i < params; i++) { |
168 |
pw.print(", arg" + i); |
169 |
} |
170 |
pw.println(");"); |
171 |
pw.println(" }"); |
172 |
} |
173 |
pw.println(" private void Bundle() {}"); |
174 |
pw.println("}"); |
175 |
pw.flush(); |
176 |
pw.close(); |
177 |
} finally { |
178 |
w.close(); |
179 |
} |
180 |
} catch (IOException x) { |
181 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Could not generate files: " + x, elements[0]); |
182 |
} |
183 |
} |
184 |
return true; |
185 |
} |
186 |
|
187 |
private String findPackage(Element e) { |
188 |
switch (e.getKind()) { |
189 |
case PACKAGE: |
190 |
return ((PackageElement) e).getQualifiedName().toString(); |
191 |
default: |
192 |
return findPackage(e.getEnclosingElement()); |
193 |
} |
194 |
} |
195 |
|
196 |
private String toIdentifier(String key) { |
197 |
if (Utilities.isJavaIdentifier(key)) { |
198 |
return key; |
199 |
} else { |
200 |
String i = key.replaceAll("[^\\p{javaJavaIdentifierPart}]", "_"); |
201 |
if (Utilities.isJavaIdentifier(i)) { |
202 |
return i; |
203 |
} else { |
204 |
return "_" + i; |
205 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
} |