View Javadoc

1   /**
2    * Pyx4me framework
3    * Copyright (C) 2006-2008 pyx4j.com.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing,
12   * software distributed under the License is distributed on an
13   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   * KIND, either express or implied.  See the License for the
15   * specific language governing permissions and limitations
16   * under the License.
17   * 
18   * @author vlads
19   * @version $Id: AbstractJadWtkMojo.java 3347 2009-04-27 14:43:50Z vlads $
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       * The name of the JAD file to create or update. classifier would be appended.
41       * 
42       * @parameter expression="${project.build.finalName}"
43       * @required
44       */
45      protected String jadfile;
46  
47      /**
48       * The name of the JAR file that accompanies the JAD file. classifier would be
49       * appended.
50       * 
51       * @parameter expression="${project.build.finalName}"
52       * @required
53       */
54      protected String jarfile;
55  
56      /**
57       * Classifier to add to the artifact generated. The artifact will be an attachment to
58       * project jar. Can be "none", See appendClassifier
59       * 
60       * @parameter expression="me"
61       * @required
62       */
63      protected String classifier;
64  
65      /**
66       * Set to false to exclude the classifier from the Artifact final name. Default value
67       * is true.
68       * 
69       * @parameter default-value="true"
70       */
71      protected boolean appendClassifier;
72  
73      /**
74       * Specifies whether or not create test JAR and JAD artifacts
75       * 
76       * @parameter default-value="false"
77       */
78      protected boolean test = false;
79  
80      /**
81       * Classifier to add to the test artifact generated. The artifact will be an
82       * attachment to project jar. See appendClassifier
83       * 
84       * @parameter expression="test"
85       * @required
86       */
87      protected String testClassifier;
88  
89      /**
90       * The task also allows to specify an arbitrary number of attributes.
91       * 
92       * @parameter
93       */
94      protected Map jadAttributes;
95  
96      /**
97       * Arbitrary number of MIDlets using a nested element "MIDlet".
98       * 
99       * @parameter
100      */
101     protected MIDlet[] midlets;
102 
103     /**
104      * Select specific ProGuard version from plugin dependencies
105      * 
106      * @parameter
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     // http://developers.sun.com/techtopics/mobility/midp/questions/version/
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      * Cleanup JAD Attributes. 1) Scan through available jadAttribute values and ensure
171      * that none contain excess whitespace and linefeeds
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 }