// UML Model Transformation Tool (UMT)
// Copyright (C) 2003, 2004, 2005 SINTEF
// Authors: jon.oldevik at sintef.no | roy.gronmo at sintef.no | tor.neple at sintef.no | fredrik.vraalsen at sintef.no
// Webpage: http://umt.sourceforge.net
// Deloped in the projects: ACEGIS (EU project - IST-2002-37724),
// CAFE (EUREKA/ITEA - ip00004), FAMILIES (ITEA project ip02009)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2.1
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307 USA
package org.sintef.umt.umtmain;
/**
* Provides a command line interface to transformation functionality
* @author Jon Oldevik, (jon.oldevik@sintef.no)
* @copyright (c) SINTEF 2002 (www.sintef.no)
*
*/
import java.io.File;
import java.util.Iterator;
import org.sintef.umt.propertyeditor.PropertyGroup;
import org.sintef.umt.propertyeditor.TransformationEditor;
import org.sintef.umt.transformer.TransformationResultListener;
import org.sintef.umt.transformer.TransformerEngine;
import org.sintef.umt.transformer.TransformerEngineFactory;
public class UMTCmdMain {
private PIMViewer pimviewer;
// private PropertyManager transformationmanager;
private static TransformationEditor transformationeditor = null;
private static OutputWindow output;
private TransformationResultListener transformationresultlistener;
private final String default_outputdir = "." + System.getProperty("file.separator") + "gen";
private final String default_outputfile = "umt_output.txt";
/**
* Default constructor
*
*/
public UMTCmdMain (String[] args) {
/*
* check args
*/
if (args.length == 0) {
printUsage ();
}
int i = 0;
String input_file = null;
String output_dir = default_outputdir;
String output_file = default_outputfile;
String source = "xmi";
String transformation = null;
boolean list_transformations = false;
while (i < args.length) {
String arg = args [i];
if (arg.startsWith("-i") || arg.startsWith("-I")) {
input_file = arg.substring(2, arg.length());
} else if (arg.startsWith("-o") || arg.startsWith("-O")) {
output_file = arg.substring(2, arg.length());
} else if (arg.startsWith("-d") || arg.startsWith("-D")) {
output_dir = arg.substring(2, arg.length());
} else if (arg.equalsIgnoreCase("-xmi") || arg.equalsIgnoreCase("-xmil")) {
source = arg.substring(1, arg.length());
} else if (arg.equalsIgnoreCase("-l") || arg.equalsIgnoreCase("-list")) {
/* List all transformations */
list_transformations = true;
break;
} else if (arg.startsWith("-t") || arg.startsWith("-T")) {
transformation = arg.substring(2, arg.length());
} else {
printUsage ();
break;
}
i++;
}
init ();
if (list_transformations) {
/* Show a list of the available transformations */
Iterator it = transformationeditor.getPropertyManager().getPropertyGroups();
System.out.println ("\n\n Available transformations:\n");
System.out.println ("--------------------------------");
// String [] names = transformationeditor.getTransformationNames();
// System.out.println ("Transformation names: " + names.length);
while(it.hasNext()) {
PropertyGroup pg = (PropertyGroup)it.next();
System.out.println("\t" + pg.getName());
}
System.out.println ("--------------------------------");
} else {
doTransformation (source, input_file, output_dir, output_file, transformation, false);
}
System.exit(0);
}
/**
* Init
*
*/
private void init () {
UMTMain.setRootDirectory ("..");
output = new OutputWindow (true);
pimviewer = new PIMViewer (output);
transformationeditor = new TransformationEditor ();
transformationresultlistener = new TransformationResultListener(){
public void notifyNewResult (Object result)
{
if (result instanceof String && result != null)
{
}
}
public void notifyStarting ()
{
}
public void notifyFinished ()
{
System.out.println ("Finished the transformation.");
}
};
}
/**
* doTransformation
*
*/
private void doTransformation (String inputtype, String inputfile, String outputdir, String outputfile, String transformation, boolean save_pim) {
System.out.println ("UMTCmdMain::doTransformation (" + inputtype + ", " + inputfile + ", " + outputdir + ", " + outputfile +", " + transformation + ")\n");
if (inputfile == null || inputfile.equals("")) {
printError ("No input file given");
} else if (transformation == null || transformation.equals("")) {
printError ("No transformation given");
} else {
if (outputdir == null || outputdir.equals(""))
outputdir = default_outputdir;
File infile = new File (inputfile);
String xmil = "";
if (inputtype.equalsIgnoreCase("xmil")) {
/* xmi light file */
xmil = pimviewer.parseHutnFile(infile);
} else {
xmil = pimviewer.xmiTransform(infile);
}
PropertyGroup group = transformationeditor.getPropertyManager().getPropertyGroupForItem(transformation);
if (group == null) {
printError ("Non-existing transformation given ....: " + transformation);
} else {
String type = group.getProperty("Type");
String outputtype = group.getProperty("Outputtype");
String implementation = group.getProperty("Implementation");
String direction = group.getProperty("Direction");
TransformerEngineFactory trefactory = new TransformerEngineFactory (output, false);
TransformerEngine tre = trefactory.createTransformer(type, outputtype, implementation, direction);
System.out.println ("Created transformer " + tre);
if (tre != null) {
tre.setOutputDir(outputdir);
if (xmil == null || xmil.equalsIgnoreCase ("")){
printError ("No model loaded. Cannot transform.");
return;
}
tre.setInputSource (new java.io.StringReader(xmil));
tre.setTransformationImpl (implementation);
tre.setOutputDir(outputdir);
tre.setOutputFile(outputfile);
tre.addTransformationResultListener(transformationresultlistener);
System.out.println ("Doing transformation - with class" + tre.getClass().getName());
tre.doTransformation ();
}
}
}
System.exit(0);
}
/**
*
* printError
*
*/
private void printError (String msg) {
System.out.println ("\t ***");
System.out.println ("\t *** Error: " + msg);
System.out.println ("\t ***\n");
printUsage ();
}
/**
*
* Main
*
*/
public static void main(String[] args) {
new UMTCmdMain (args);
}
private void printUsage () {
System.out.println ("Correct usage is: ");
System.out.print("\t" + "umtcmd ");
System.out.println("-xmi|-xmil -i<input_file> -t<transformation> [-o<output_file>][-d<output_dir>] | -l|list");
System.out.print("\n\n");
System.out.println("\t-s = source type xmi or xmi light.");
System.out.println("\t-t = transformation name.");
System.out.println("\t-i = input file.");
System.out.println("\t-o = output file (optional)");
System.out.println("\t-d = output directory (optional) (Default is ./gen)");
System.out.println("\t-l = list available transformations.");
System.exit (-1);
}
}