All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pyx4me.maven.j2me.AbstractJadWtkMojo Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
/**
 * Pyx4me framework
 * Copyright (C) 2006-2007 pyx4.com.
 * 
 * @author vlads
 * @version $Id: AbstractJadWtkMojo.java 1498 2007-10-17 02:05:35Z vlads $
 */
package com.pyx4me.maven.j2me;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import de.pleumann.antenna.WtkJad;
import de.pleumann.antenna.misc.JadFile;

public abstract class AbstractJadWtkMojo extends AbstractWtkMojo {

	/**
	 * The name of the JAD file to create or update. classifier would be
	 * appended.
	 * 
	 * @parameter expression="${project.build.finalName}"
	 * @required
	 */
	protected String jadfile;

	/**
	 * The name of the JAR file that accompanies the JAD file. classifier would
	 * be appended.
	 * 
	 * @parameter expression="${project.build.finalName}"
	 * @required
	 */
	protected String jarfile;

	/**
	 * Classifier to add to the artifact generated. The artifact will be an
	 * attachment to project jar. Can be "none", See appendClassifier
	 * 
	 * @parameter expression="me"
	 * @required
	 */
	protected String classifier;

	/**
	 * Set to false to exclude the classifier from the Artifact final name.
	 * Default value is true.
	 * 
	 * @parameter default-value="true"
	 */
	protected boolean appendClassifier;

	/**
	 * Specifies whether or not create test JAR and JAD artifacts
	 * 
	 * @parameter default-value="false"
	 */
	protected boolean test = false;

	/**
	 * Classifier to add to the test artifact generated. The artifact will be an
	 * attachment to project jar. See appendClassifier
	 * 
	 * @parameter expression="test"
	 * @required
	 */
	protected String testClassifier;

	/**
	 * The task also allows to specify an arbitrary number of attributes.
	 * 
	 * @parameter
	 */
	protected Map jadAttributes;

	/**
	 * Arbitrary number of MIDlets using a nested element "MIDlet".
	 * 
	 * @parameter
	 */
	protected MIDlet[] midlets;

	public static String JAD_ATR_PROFILE = "MicroEdition-Profile";

	public static String JAD_ATR_CONFIGURATION = "MicroEdition-Configuration";

	public static String JAD_ATR_MIDLET_NAME = "MIDlet-Name";

	public static String JAD_ATR_MIDLET_ICON = "MIDlet-Icon";

	public static String JAD_ATR_MIDLET_VENDOR = "MIDlet-Vendor";

	public static String JAD_ATR_MIDLET_VERSION = "MIDlet-Version";

	// http://developers.sun.com/techtopics/mobility/midp/questions/version/
	private static Pattern versionRx = Pattern.compile("(\\d{1,2}[.]\\d{1,2}([.]\\d{1,2})?)");

	private static boolean useArtifactClassifier(String classifier, boolean appendClassifier) {
		return appendClassifier && ((classifier != null) && (classifier.length() > 0) && (!"none".equals(classifier)));
	}

	protected static String getArtifactName(String finalName, String classifier, boolean appendClassifier) {
		if (!useArtifactClassifier(classifier, appendClassifier)) {
			return finalName;
		}
		return finalName + "-" + classifier;
	}

	protected static File getJarFile(File basedir, String finalName, String classifier, boolean appendClassifier) {
		return new File(basedir, getArtifactName(finalName, classifier, appendClassifier) + ".jar");
	}

	protected static File getJadFile(File basedir, String finalName, String classifier, boolean appendClassifier) {
		return new File(basedir, getArtifactName(finalName, classifier, appendClassifier) + ".jad");
	}

	protected File getJarFile(String packageClassifier) {
		return getJarFile(outputDirectory, jarfile, packageClassifier, appendClassifier);
	}

	protected File getJadFile(String packageClassifier) {
		return getJadFile(outputDirectory, jadfile, packageClassifier, appendClassifier);
	}

	protected void populateJadAttributes() {
		if (jadAttributes == null) {
			jadAttributes = new HashMap();
		}
		if (!jadAttributes.containsKey(JAD_ATR_PROFILE)) {
			jadAttributes.put(JAD_ATR_PROFILE, j2meProfile);
		}
		if (!jadAttributes.containsKey(JAD_ATR_CONFIGURATION)) {
			jadAttributes.put(JAD_ATR_CONFIGURATION, j2meConfiguration);
		}
		if (!jadAttributes.containsKey(JAD_ATR_MIDLET_ICON)) {
			if ((midlets != null) && (midlets.length > 0)) {
				jadAttributes.put(JAD_ATR_MIDLET_ICON, midlets[0].icon);
			}
		}
	}

	protected String properMidletVersion() {
		return properMidletVersion(midletVersion);
	}

	protected String properMidletVersion(String version) {
		if (version.endsWith("-SNAPSHOT")) {
			version = version.substring(0, version.indexOf('-'));
		}
		Matcher mp = versionRx.matcher(version);
		if (mp.find()) {
			return mp.group(0);
		} else {
			throw new Error("Invalid midlet version " + version);
		}
	}

	protected WtkJad createWtkJadTask(String packageClassifier, boolean isTest) throws MojoExecutionException,
			MojoFailureException {

		if ((midletVendor == null) || (midletVendor.length() == 0)) {
			throw new MojoFailureException("Missing the required attribute: MIDletVendor");
		}

		WtkJad task = new WtkJad();
		initTask(task);

		task.setJadfile(getJadFile(packageClassifier));
		task.setJarfile(getJarFile(packageClassifier));

		task.setName(midletName);
		task.setVendor(midletVendor);
		task.setVersion(properMidletVersion());

		populateJadAttributes();
		for (Iterator i = jadAttributes.entrySet().iterator(); i.hasNext();) {
			Map.Entry jadEntry = (Map.Entry) i.next();
			WtkJad.Attribute a = task.createAttribute();
			a.setName((String) jadEntry.getKey());
			a.setValue((String) jadEntry.getValue());
		}

		if (midlets != null) {
			for (int i = 0; i < midlets.length; i++) {
				MIDlet mp = midlets[i];
				if (mp.test && (!isTest)) {
					continue;
				}
				WtkJad.MIDlet m = task.createMidlet();
				m.setClass(mp.cls);
				m.setIcon(mp.icon);
				m.setName(mp.name);
			}
		}

		return task;
	}

	protected void executeCreateJad(String packageClassifier, boolean isTest) throws MojoExecutionException,
			MojoFailureException {

		JadFile jad = new JadFile();
		File jadFile = getJadFile(packageClassifier);
		File jarFile = getJarFile(packageClassifier);

		String url = jarFile.getName();
		String target = (String) jadAttributes.get("MIDlet-Jar-URL");
		if (target != null && target.length() != 0) {
			url = (target.startsWith("http://") ? "" : "http://") + target + "/" + url;
		}
		jad.setValue("MIDlet-Jar-URL", url);

		jad.setValue("MIDlet-Jar-Size", "" + jarFile.length());

		if (midletName != null) {
			jad.setValue("MIDlet-Name", midletName);
		}
		if (midletVendor != null) {
			jad.setValue("MIDlet-Vendor", midletVendor);
		}

		jad.setValue("MIDlet-Version", properMidletVersion());

		populateJadAttributes();
		for (Iterator i = jadAttributes.entrySet().iterator(); i.hasNext();) {
			Map.Entry jadAttrEntry = (Map.Entry) i.next();
			jad.setValue((String) jadAttrEntry.getKey(), (String) jadAttrEntry.getValue());
		}

		if (midlets != null) {
			for (int i = 0; i < midlets.length; i++) {
				MIDlet m = midlets[i];
				if (m.test && (!isTest)) {
					continue;
				}
				jad.setValue("MIDlet-" + (i + 1), m.name + ", " + m.icon + ", " + m.cls);
			}
		}

		try {
			jad.save(jadFile.getPath(), null);
		} catch (IOException e) {
			throw new MojoExecutionException("JAD save error", e);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy