String source = StringUtils.replace(dest, UML2EXT, MDEXT1);
URL sourceUrl = null;
try {
sourceUrl = new URL(ResourceUtils.normalizePath(source));
} catch (MalformedURLException e) {
throw new MojoExecutionException("Invalid source model file name [" + source + "]: " + e.getMessage());
}
//check for for second MD extension
File sourceFile = new File(sourceUrl.getFile());
if (sourceFile == null || !sourceFile.exists())
{
source = StringUtils.replace(dest, UML2EXT, MDEXT2);
try {
sourceUrl = new URL(ResourceUtils.normalizePath(source));
} catch (MalformedURLException e) {
throw new MojoExecutionException("Invalid source model file name [" + source + "]: " + e.getMessage());
}
}
sourceFile = new File(sourceUrl.getFile());
if (sourceFile == null || !sourceFile.exists())
{
throw new MojoExecutionException("Model file [" + source + "] does not exist");
}
//check for destination (emf) file
URL destUrl = null;
try {
destUrl = new URL(ResourceUtils.normalizePath(dest));
} catch (MalformedURLException e) {
throw new MojoExecutionException("Invalid destination model file name [" + dest + "]: " + e.getMessage());
}
File destFile = new File(destUrl.getFile());
if (destFile == null || !destFile.exists())
{
getLog().debug("No old model file [" + dest + "] existing");
}
else
{
if (getLog().isDebugEnabled())
{
Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
getLog().debug("- MagicDraw model file ["+sourceFile.getName()+"] date = " + formatter.format(new Date(sourceFile.lastModified())));
getLog().debug("- EMF model file ["+destFile.getName()+"] date = " + formatter.format(new Date(destFile.lastModified())));
}
if (destFile.lastModified() >= sourceFile.lastModified())
{
getLog().info("Model file [" + dest + "] is up-to-date");
return;
}
}
//check for valid magic draw installation
checkForMagicDraw();
//perform the export via MagicDraw
getLog().info("Exporting model file [" + source + "] ...");
String command = "\"" + exporterPath + "\""
+ " project_file=\"" + sourceFile.getPath() + "\""
+ " destination_dir=\"" + sourceFile.getParent() + "\""
+ " load_all_modules=true";
Process process = Runtime.getRuntime().exec(command);
//since at least the windows version forks the magicdraw process,
//we have to synchronize via input stream reading
InputStream is = process.getInputStream();
final byte[] buf = new byte[128];
int length;
while ((length = is.read(buf)) > 0)
{
getLog().info(new String(buf, 0, length));
}
process.waitFor();
process.destroy();
int err = process.exitValue();
if (err != 0)
{
throw new MojoExecutionException("MagicDraw export returned error code " + err);
}
getLog().info("Successfully exported model file.");
}