KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ant > taskdefs > codegen > JMergerTask


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JMergerTask.java,v 1.4 2005/06/17 13:40:04 marcelop Exp $
16  */

17 package org.eclipse.emf.ant.taskdefs.codegen;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23
24 import org.eclipse.emf.ant.taskdefs.EMFTask;
25 import org.eclipse.emf.ant.util.Util;
26 import org.eclipse.emf.codegen.jmerge.JMerger;
27 import org.eclipse.emf.common.util.URI;
28
29
30 /**
31  * <p>
32  * Exposes some functionalities available on the
33  * {@link org.eclipse.emf.codegen.jmerge.JMerger JMerger} class.
34  * </p>
35  * <p>
36  * This task is supposed to be executed by a Eclipse driver with the
37  * <b>org.eclipse.emf.ant</b> plugin. It is neither necessary to use Ant's task
38  * <tt>TaskDef</tt> to declare this task in a script nor to change the Ant's runtime
39  * classpath.
40  * </p>
41  * <p>
42  * The following command line will start a headless Eclipse instance and run the specified Ant script.
43  * </p>
44  * <p>
45  * java -classpath <i>eclipseDir</i>/startup.jar org.eclipse.core.launcher.Main
46  * -data <i>worspaceDir</i>
47  * -application org.eclipse.ant.core.antRunner
48  * -buildfile <i>antScript</i>
49  * </p>
50  * <p>
51  * Usage examples:
52  * </p>
53  * <pre>
54  * &lt;emf.JMerger mergeXMLURI=&quot;http://www.example.com/merge.xml&quot;
55  * sourceFile=&quot;c:/java/MyClass.java&quot;
56  * targetURI=&quot;http://www.example.com/target/MyNewClass.java&quot;/&gt;
57  * </pre>
58  * <pre>
59  * &lt;emf.JMerger mergeXMLFile=&quot;merge.xml&quot;
60  * sourceURI=&quot;http://www.example.com/source/MyClass.java&quot;
61  * targetFile=&quot;MyNewClass.java&quot;;
62  * newFile=&quot;c:\MyClass.java&quot;/&gt;
63  * </pre>
64  * <pre>
65  * &lt;emf.JMerger mergeXMLFile=&quot;c:\mergefiles\merge.xml&quot;
66  * sourceFile=&quot;d:\old\MyClass.java&quot;
67  * targetFile=&quot;d:\new\MyNewClass.java&quot;;
68  * newFile=&quot;MyMergedClass.java&quot;/&gt;
69  * </pre>
70  *
71  * @since 2.1.0
72  */

73 public class JMergerTask extends EMFTask
74 {
75   private String JavaDoc mergeXMLURI;
76   private File JavaDoc mergeXMLFile;
77   private String JavaDoc sourceURI;
78   private File JavaDoc sourceFile;
79   private String JavaDoc targetURI;
80   private File JavaDoc targetFile;
81   private File JavaDoc newFile;
82
83   public void setMergeXMLURI(String JavaDoc mergeXMLURI)
84   {
85     this.mergeXMLURI = mergeXMLURI;
86   }
87
88   public void setMergeXMLFile(File JavaDoc mergeXMLFile)
89   {
90     this.mergeXMLFile = mergeXMLFile;
91   }
92
93   public void setSourceURI(String JavaDoc sourceURI)
94   {
95     this.sourceURI = sourceURI;
96   }
97
98   public void setSourceFile(File JavaDoc sourceFile)
99   {
100     this.sourceFile = sourceFile;
101   }
102
103   public void setTargetURI(String JavaDoc targetURI)
104   {
105     this.targetURI = targetURI;
106   }
107
108   public void setTargetFile(File JavaDoc targetFile)
109   {
110     this.targetFile = targetFile;
111   }
112
113   public void setNewFile(File JavaDoc newFile)
114   {
115     this.newFile = newFile;
116   }
117
118   protected void checkAttributes() throws BuildException
119   {
120     assertTrue("Either 'mergeXMLURI' or 'mergeXMLFile' must be specified.", mergeXMLURI != null || mergeXMLFile != null);
121     assertTrue("Either 'sourceURI' or 'sourceFile' must be specified.", sourceURI != null || sourceFile != null);
122     assertTrue("Either 'targetURI' or 'targetFile' must be specified.", targetURI != null || targetFile != null);
123   }
124
125   protected void doExecute() throws Exception JavaDoc
126   {
127     invokeMerger(createJMerger());
128   }
129
130   protected JMerger createJMerger()
131   {
132     return new JMerger();
133   }
134
135   protected void invokeMerger(JMerger merger) throws IOException JavaDoc, BuildException
136   {
137     String JavaDoc mergeXML = mergeXMLURI != null ? mergeXMLURI : mergeXMLFile.getAbsolutePath();
138     String JavaDoc source = sourceURI != null ? sourceURI : sourceFile.getAbsolutePath();
139
140     String JavaDoc target = null;
141     if (targetURI != null)
142     {
143       target = targetURI;
144       if (newFile == null)
145       {
146         String JavaDoc file = URI.createFileURI(target).toFileString();
147         if (file != null)
148         {
149           newFile = new File JavaDoc(file);
150         }
151       }
152     }
153     else
154     {
155       target = targetFile.getAbsolutePath();
156       if (newFile == null)
157       {
158         newFile = targetFile;
159       }
160     }
161
162     assertTrue("Cannot write to target", newFile != null);
163
164     String JavaDoc contents = merger.execute(getProgressMonitor(), new String JavaDoc []{ mergeXML, source, target });
165     Util.writeFile(newFile, contents);
166   }
167 }
168
Popular Tags