View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package no.sintef.umt.ant;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  
26  import no.sintef.umt.XmiLightTransformer;
27  import no.sintef.umt.XmiReader;
28  import no.sintef.xml.XmlHelper;
29  
30  import org.apache.tools.ant.BuildException;
31  import org.apache.tools.ant.Task;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Node;
34  
35  /***
36   * Ant task for XmiLightTransformer.
37   * 
38   * @author Fredrik Vraalsen
39   */
40  public class XmiLightConverterTask extends Task {
41  
42  	private File src = null;
43  	private File dest = null;
44  
45  	/***
46  	 * @param srcFile the source file to transform from
47  	 */
48  	public void setSrcfile(File srcFile) {
49  		src = srcFile;
50  	}
51  	
52  	/***
53  	 * @param destFile the destination file to transform to
54  	 */
55  	public void setDestfile(File destFile) {
56  		dest = destFile;
57  	}
58  
59  	/***
60  	 * Execute the Ant task.
61  	 */
62  	public void execute() {
63  		super.execute();
64  		try {
65  			System.out.println("Converting XMI file " + src + " to XMI Light " + dest);
66  			Document xmiDoc = XmiReader.readXmi(src);
67  			XmiLightTransformer xlt = XmiLightTransformer.getTransformer();
68  			Node xmiLight = xlt.getXmiLight(xmiDoc);
69  			FileOutputStream os = new FileOutputStream(dest);
70  			XmlHelper.xmlToUtf8(xmiLight, os, true);
71  			os.close();
72  		} catch (Exception e) {
73  			throw new BuildException("Error while converting XMI to XMI Light", e);
74  		}
75  	}
76  
77  }