1 17 package org.eclipse.emf.ant.taskdefs.codegen; 18 19 import java.io.File ; 20 import java.io.IOException ; 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 73 public class JMergerTask extends EMFTask 74 { 75 private String mergeXMLURI; 76 private File mergeXMLFile; 77 private String sourceURI; 78 private File sourceFile; 79 private String targetURI; 80 private File targetFile; 81 private File newFile; 82 83 public void setMergeXMLURI(String mergeXMLURI) 84 { 85 this.mergeXMLURI = mergeXMLURI; 86 } 87 88 public void setMergeXMLFile(File mergeXMLFile) 89 { 90 this.mergeXMLFile = mergeXMLFile; 91 } 92 93 public void setSourceURI(String sourceURI) 94 { 95 this.sourceURI = sourceURI; 96 } 97 98 public void setSourceFile(File sourceFile) 99 { 100 this.sourceFile = sourceFile; 101 } 102 103 public void setTargetURI(String targetURI) 104 { 105 this.targetURI = targetURI; 106 } 107 108 public void setTargetFile(File targetFile) 109 { 110 this.targetFile = targetFile; 111 } 112 113 public void setNewFile(File 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 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 , BuildException 136 { 137 String mergeXML = mergeXMLURI != null ? mergeXMLURI : mergeXMLFile.getAbsolutePath(); 138 String source = sourceURI != null ? sourceURI : sourceFile.getAbsolutePath(); 139 140 String target = null; 141 if (targetURI != null) 142 { 143 target = targetURI; 144 if (newFile == null) 145 { 146 String file = URI.createFileURI(target).toFileString(); 147 if (file != null) 148 { 149 newFile = new File (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 contents = merger.execute(getProgressMonitor(), new String []{ mergeXML, source, target }); 165 Util.writeFile(newFile, contents); 166 } 167 } 168 | Popular Tags |