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 103 2007-04-21 01:33:52Z vlads $
 */
package com.pyx4me.maven.j2me;

import java.io.File;
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;

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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy