Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 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 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.util.annotations; |
43 |
|
44 |
import java.io.IOException; |
45 |
import java.io.InputStream; |
46 |
import java.io.OutputStream; |
47 |
import java.io.PrintWriter; |
48 |
import java.io.Writer; |
49 |
import java.util.ArrayList; |
50 |
import java.util.Arrays; |
51 |
import java.util.HashMap; |
52 |
import java.util.List; |
53 |
import java.util.Map; |
54 |
import java.util.Map.Entry; |
55 |
import java.util.Set; |
56 |
import java.util.TreeMap; |
57 |
import java.util.TreeSet; |
58 |
import java.util.regex.Matcher; |
59 |
import java.util.regex.Pattern; |
60 |
import javax.annotation.processing.AbstractProcessor; |
61 |
import javax.annotation.processing.RoundEnvironment; |
62 |
import javax.lang.model.element.Element; |
63 |
import javax.lang.model.element.TypeElement; |
64 |
import javax.tools.Diagnostic.Kind; |
65 |
import javax.tools.StandardLocation; |
66 |
import org.netbeans.modules.openide.util.NbBundleProcessor; |
67 |
import org.openide.util.EditableProperties; |
68 |
|
69 |
/** |
70 |
* |
71 |
* @author lahvac |
72 |
*/ |
73 |
public abstract class AbstractBundleProcessor extends AbstractProcessor { |
74 |
|
75 |
private Map</*package*/String, Map<String, Value>> package2Keys2Values = new HashMap<String, Map<String, Value>>(); |
76 |
|
77 |
@Override |
78 |
public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
79 |
if (!roundEnv.processingOver()) { |
80 |
return handleProcess(annotations, roundEnv); |
81 |
} |
82 |
|
83 |
List<Element> totalElements = new ArrayList<Element>(); |
84 |
|
85 |
for (Entry<String, Map<String, Value>> e : package2Keys2Values.entrySet()) { |
86 |
String pkg = e.getKey(); |
87 |
Map<String, Value> key2Value = e.getValue(); |
88 |
|
89 |
try { |
90 |
EditableProperties p = new EditableProperties(true); |
91 |
// Load any preexisting bundle so we can just add our keys. |
92 |
try { |
93 |
InputStream is = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, pkg, "Bundle.properties").openInputStream(); |
94 |
try { |
95 |
p.load(is); |
96 |
} finally { |
97 |
is.close(); |
98 |
} |
99 |
} catch (IOException x) { |
100 |
// OK, not there |
101 |
} |
102 |
for (String key : p.keySet()) { |
103 |
if (key2Value.containsKey(key)) { |
104 |
Element[] originatingElements = key2Value.get(key).originatingElements; |
105 |
if (originatingElements.length == 0) { |
106 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Key " + key + " is a duplicate of one from Bundle.properties"); |
107 |
} else { |
108 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Key " + key + " is a duplicate of one from Bundle.properties", originatingElements[0]); |
109 |
} |
110 |
} |
111 |
} |
112 |
// Also check class output for (1) incremental builds, (2) preexisting bundles from Maven projects. |
113 |
try { |
114 |
InputStream is = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, "Bundle.properties").openInputStream(); |
115 |
try { |
116 |
// do not use p.load(is) as the impl in EditableProperties does not currently handle duplicates properly |
117 |
EditableProperties p2 = new EditableProperties(true); |
118 |
p2.load(is); |
119 |
p.putAll(p2); |
120 |
} finally { |
121 |
is.close(); |
122 |
} |
123 |
} catch (IOException x) { |
124 |
// OK, not there |
125 |
} |
126 |
|
127 |
for (Entry<String, Value> valEntry : key2Value.entrySet()) { |
128 |
p.put(valEntry.getKey(), valEntry.getValue().value); |
129 |
p.setComment(valEntry.getKey(), valEntry.getValue().comments, false); |
130 |
totalElements.addAll(Arrays.asList(valEntry.getValue().originatingElements)); |
131 |
} |
132 |
OutputStream os = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, pkg, "Bundle.properties", totalElements.toArray(new Element[0])).openOutputStream(); |
133 |
try { |
134 |
p.store(os); |
135 |
} finally { |
136 |
os.close(); |
137 |
} |
138 |
} catch (IOException x) { |
139 |
if (totalElements.isEmpty()) { |
140 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Could not generate files: " + x); |
141 |
} else { |
142 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Could not generate files: " + x, totalElements.get(0)); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
return true; |
148 |
} |
149 |
|
150 |
protected abstract boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv); |
151 |
|
152 |
protected final void setBundleKey(String pkg, String key, Value value) { |
153 |
Map<String, Value> key2Value = package2Keys2Values.get(pkg); |
154 |
|
155 |
if (key2Value == null) { |
156 |
package2Keys2Values.put(pkg, key2Value = new HashMap<String, Value>()); |
157 |
} |
158 |
|
159 |
if (key2Value.containsKey(key)) { |
160 |
throw new IllegalStateException("Key already exists: " + key); |
161 |
} |
162 |
|
163 |
key2Value.put(key, value); |
164 |
} |
165 |
|
166 |
public static final class Value { |
167 |
private final String value; |
168 |
private final String[] comments; |
169 |
private final Element[] originatingElements; |
170 |
|
171 |
public Value(String value, String[] comments, Element[] originatingElements) { |
172 |
this.value = value; |
173 |
this.comments = comments; |
174 |
this.originatingElements = originatingElements; |
175 |
} |
176 |
|
177 |
} |
178 |
|
179 |
} |