1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.pyx4me.maven.j2me;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33
34 import de.pleumann.antenna.WtkJad;
35 import de.pleumann.antenna.misc.JadFile;
36
37 public abstract class AbstractJadWtkMojo extends AbstractWtkMojo {
38
39
40
41
42
43
44
45 protected String jadfile;
46
47
48
49
50
51
52
53
54 protected String jarfile;
55
56
57
58
59
60
61
62
63 protected String classifier;
64
65
66
67
68
69
70
71 protected boolean appendClassifier;
72
73
74
75
76
77
78 protected boolean test = false;
79
80
81
82
83
84
85
86
87 protected String testClassifier;
88
89
90
91
92
93
94 protected Map jadAttributes;
95
96
97
98
99
100
101 protected MIDlet[] midlets;
102
103
104
105
106
107
108 protected String proguardVersion;
109
110 public static String JAD_ATR_PROFILE = "MicroEdition-Profile";
111
112 public static String JAD_ATR_CONFIGURATION = "MicroEdition-Configuration";
113
114 public static String JAD_ATR_MIDLET_NAME = "MIDlet-Name";
115
116 public static String JAD_ATR_MIDLET_ICON = "MIDlet-Icon";
117
118 public static String JAD_ATR_MIDLET_VENDOR = "MIDlet-Vendor";
119
120 public static String JAD_ATR_MIDLET_VERSION = "MIDlet-Version";
121
122
123 private static Pattern versionRx = Pattern.compile("(\\d{1,2}[.]\\d{1,2}([.]\\d{1,2})?)");
124
125 private static boolean useArtifactClassifier(String classifier, boolean appendClassifier) {
126 return appendClassifier && ((classifier != null) && (classifier.length() > 0) && (!"none".equals(classifier)));
127 }
128
129 protected static String getArtifactName(String finalName, String classifier, boolean appendClassifier) {
130 if (!useArtifactClassifier(classifier, appendClassifier)) {
131 return finalName;
132 }
133 return finalName + "-" + classifier;
134 }
135
136 protected static File getJarFile(File basedir, String finalName, String classifier, boolean appendClassifier) {
137 return new File(basedir, getArtifactName(finalName, classifier, appendClassifier) + ".jar");
138 }
139
140 protected static File getJadFile(File basedir, String finalName, String classifier, boolean appendClassifier) {
141 return new File(basedir, getArtifactName(finalName, classifier, appendClassifier) + ".jad");
142 }
143
144 protected File getJarFile(String packageClassifier) {
145 return getJarFile(outputDirectory, jarfile, packageClassifier, appendClassifier);
146 }
147
148 protected File getJadFile(String packageClassifier) {
149 return getJadFile(outputDirectory, jadfile, packageClassifier, appendClassifier);
150 }
151
152 protected void populateJadAttributes() {
153 if (jadAttributes == null) {
154 jadAttributes = new HashMap();
155 }
156 if (!jadAttributes.containsKey(JAD_ATR_PROFILE)) {
157 jadAttributes.put(JAD_ATR_PROFILE, j2meProfile);
158 }
159 if (!jadAttributes.containsKey(JAD_ATR_CONFIGURATION)) {
160 jadAttributes.put(JAD_ATR_CONFIGURATION, j2meConfiguration);
161 }
162 if (!jadAttributes.containsKey(JAD_ATR_MIDLET_ICON)) {
163 if ((midlets != null) && (midlets.length > 0)) {
164 jadAttributes.put(JAD_ATR_MIDLET_ICON, midlets[0].icon);
165 }
166 }
167 }
168
169
170
171
172
173 protected void cleanupJadAttributes() {
174 for (Iterator i = jadAttributes.entrySet().iterator(); i.hasNext();) {
175 Map.Entry jadEntry = (Map.Entry) i.next();
176
177 String rawvalue = (String) jadEntry.getValue();
178
179 String value = rawvalue.replace('\n', ' ');
180 value = value.replace('\r', ' ');
181 value = value.replaceAll(" *", " ");
182 value = value.trim();
183
184 jadEntry.setValue(value);
185 }
186 }
187
188 protected String properMidletVersion() {
189 return properMidletVersion(midletVersion);
190 }
191
192 protected String properMidletVersion(String version) {
193 if (version.endsWith("-SNAPSHOT")) {
194 version = version.substring(0, version.indexOf('-'));
195 }
196 Matcher mp = versionRx.matcher(version);
197 if (mp.find()) {
198 return mp.group(0);
199 } else {
200 throw new Error("Invalid midlet version " + version);
201 }
202 }
203
204 protected WtkJad createWtkJadTask(String packageClassifier, boolean isTest) throws MojoExecutionException, MojoFailureException {
205
206 if ((midletVendor == null) || (midletVendor.length() == 0)) {
207 throw new MojoFailureException("Missing the required attribute: MIDletVendor");
208 }
209
210 WtkJad task = new WtkJad();
211 initTask(task);
212
213 task.setJadfile(getJadFile(packageClassifier));
214 task.setJarfile(getJarFile(packageClassifier));
215
216 task.setName(midletName);
217 task.setVendor(midletVendor);
218 task.setVersion(properMidletVersion());
219
220 populateJadAttributes();
221 cleanupJadAttributes();
222 for (Iterator i = jadAttributes.entrySet().iterator(); i.hasNext();) {
223 Map.Entry jadEntry = (Map.Entry) i.next();
224 WtkJad.Attribute a = task.createAttribute();
225 a.setName((String) jadEntry.getKey());
226 a.setValue((String) jadEntry.getValue());
227 }
228
229 if (midlets != null) {
230 for (int i = 0; i < midlets.length; i++) {
231 MIDlet mp = midlets[i];
232 if (mp.test && (!isTest)) {
233 continue;
234 }
235 WtkJad.MIDlet m = task.createMidlet();
236 m.setClass(mp.cls);
237 m.setIcon(mp.icon);
238 m.setName(mp.name);
239 }
240 }
241
242 return task;
243 }
244
245 protected void executeCreateJad(String packageClassifier, boolean isTest) throws MojoExecutionException, MojoFailureException {
246
247 JadFile jad = new JadFile();
248 File jadFile = getJadFile(packageClassifier);
249 File jarFile = getJarFile(packageClassifier);
250
251 String url = jarFile.getName();
252 String target = (String) jadAttributes.get("MIDlet-Jar-URL");
253 if (target != null && target.length() != 0) {
254 url = (target.startsWith("http://") ? "" : "http://") + target + "/" + url;
255 }
256 jad.setValue("MIDlet-Jar-URL", url);
257
258 jad.setValue("MIDlet-Jar-Size", "" + jarFile.length());
259
260 if (midletName != null) {
261 jad.setValue("MIDlet-Name", midletName);
262 }
263 if (midletVendor != null) {
264 jad.setValue("MIDlet-Vendor", midletVendor);
265 }
266
267 jad.setValue("MIDlet-Version", properMidletVersion());
268
269 populateJadAttributes();
270 for (Iterator i = jadAttributes.entrySet().iterator(); i.hasNext();) {
271 Map.Entry jadAttrEntry = (Map.Entry) i.next();
272 jad.setValue((String) jadAttrEntry.getKey(), (String) jadAttrEntry.getValue());
273 }
274 cleanupJadAttributes();
275
276 if (midlets != null) {
277 for (int i = 0; i < midlets.length; i++) {
278 MIDlet m = midlets[i];
279 if (m.test && (!isTest)) {
280 continue;
281 }
282 jad.setValue("MIDlet-" + (i + 1), m.name + ", " + m.icon + ", " + m.cls);
283 }
284 }
285
286 try {
287 jad.save(jadFile.getPath(), null);
288 } catch (IOException e) {
289 throw new MojoExecutionException("JAD save error", e);
290 }
291
292 }
293
294 }