]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
Delete the midterm temporary file which created by GenFFSFile task.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / Tool.java
1 /** @file
2 This file is to define nested element which is meant for specifying a tool
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14 package org.tianocore.framework.tasks;
15
16 import java.io.DataInputStream;
17 import java.io.DataOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.tools.ant.BuildException;
27 import org.tianocore.common.logger.EdkLog;
28
29 /**
30 Class Tool is to define an external tool to be used for genffsfile
31 **/
32 public class Tool implements EfiDefine, Section {
33
34 private String toolName = "";
35 private ToolArg toolArgList = new ToolArg();
36 private Input inputFiles = new Input();
37 private Input tempInputFile = new Input();
38 private String outputPath;
39 private File outputFile ;
40 private List<Section> gensectList = new ArrayList<Section>();
41 /**
42 Call extern tool
43
44 @param buffer The buffer to put the result with alignment
45 **/
46 public void toBuffer (DataOutputStream buffer){
47 ///
48 /// call extern tool
49 ///
50 try {
51 executeTool ();
52 } catch (Exception e) {
53 throw new BuildException("Call to executeTool failed!\n" + e.getMessage());
54 }
55
56 ///
57 /// check if file exist
58 ///
59 //File OutputFile = new File (this.outPutFileName);
60 if (!outputFile.exists()) {
61 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");
62 }
63
64 ///
65 /// Read output file and write it's cotains to buffer
66 ///
67 FileInputStream fs = null;
68 DataInputStream in = null;
69 try {
70 fs = new FileInputStream (outputFile);
71 in = new DataInputStream (fs);
72
73
74 int fileLen = (int)outputFile.length();
75 byte[] data = new byte[fileLen];
76 in.read(data);
77 buffer.write(data, 0, fileLen);
78
79 ///
80 /// 4 byte alignment
81 ///
82 while ((fileLen & 0x03) != 0) {
83 fileLen++;
84 buffer.writeByte(0);
85 }
86 } catch (Exception e) {
87 EdkLog.log(e.getMessage());
88 throw new BuildException("Tool call, toBuffer failed!\n");
89 } finally {
90 try {
91 if (in != null) {
92 in.close();
93 }
94 if (fs != null) {
95 fs.close();
96 }
97 outputFile.delete();
98 } catch (Exception e) {
99 EdkLog.log("WARNING: Cannot close " + outputFile.getPath());
100 }
101 }
102 }
103
104 ///
105 /// execute external tool for genffsfile
106 ///
107 private void executeTool () {
108 String command = "";
109 String argument = "";
110 command = toolName;
111
112 //
113 // Get each section which under the compress {};
114 // And add it is contains to File;
115 //
116 Section sect;
117 try{
118 Iterator SectionIter = this.gensectList.iterator();
119 while (SectionIter.hasNext()){
120 sect = (Section)SectionIter.next();
121 //
122 // Parse <genSection> element
123 //
124 File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));
125 FileOutputStream bo = new FileOutputStream(outputFile);
126 DataOutputStream Do = new DataOutputStream (bo);
127 //
128 // Call each section class's toBuffer function.
129 //
130 try {
131 sect.toBuffer(Do);
132 }
133 catch (BuildException e) {
134 EdkLog.log(e.getMessage());
135 throw new BuildException ("GenSection failed at Tool!");
136 }
137 Do.close();
138 this.tempInputFile.insFile(outputFile.getPath());
139 }
140 } catch (IOException e){
141 throw new BuildException ("Gensection failed at tool!");
142 }
143
144 try {
145 outputFile = File.createTempFile("temp", null, new File(outputPath));
146 argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ")
147 + tempInputFile.toString(" ")+ " -o " + outputFile.getPath();
148 EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);
149 ///
150 /// execute command line
151 ///
152 Process process = Runtime.getRuntime().exec(command + " " + argument);
153 process.waitFor();
154 Iterator tempFile = tempInputFile.getNameList().iterator();
155 while (tempFile.hasNext()){
156 File file = new File((String)tempFile.next());
157 if (file.exists()) {
158 file.delete();
159 }
160 }
161 } catch (Exception e) {
162 EdkLog.log(e.getMessage());
163 throw new BuildException("Execution of externalTool task failed!\n");
164 }
165 }
166
167 /**
168 Add method of ANT task/datatype for nested ToolArg type of element
169
170 @param toolArg The ToolArg object containing arguments for the tool
171 **/
172 public void addConfiguredToolArg (ToolArg toolArg) {
173 toolArgList.insert(toolArg);
174 }
175
176 /**
177 Get method of ANT task/datatype for attribute "OutputPath"
178
179 @returns The name of output path
180 **/
181 public String getOutputPath() {
182 return outputPath;
183 }
184
185 /**
186 Set method of ANT task/datatype for attribute "OutputPath"
187
188 @param outputPath The name of output path
189 **/
190 public void setOutputPath(String outPutPath) {
191 this.outputPath = outPutPath;
192 }
193
194 /**
195 Get method of ANT task/datatype for attribute "ToolName"
196
197 @returns The name of the tool.
198 **/
199 public String getToolName() {
200 return toolName;
201 }
202
203 /**
204 Set method of ANT task/datatype for attribute "ToolName"
205
206 @param toolName The name of the tool
207 **/
208 public void setToolName(String toolName) {
209 this.toolName = toolName;
210 }
211
212 /**
213 Add method of ANT task/datatype for nested Input type of element
214
215 @param file The Input objec which represents a file
216 **/
217 public void addConfiguredInput(Input file) {
218 inputFiles.insert(file);
219 }
220
221 // /**
222 // addTool
223 //
224 // This function is to add instance of Tool to list.
225 //
226 // @param tool instance of Tool.
227 // **/
228 // public void addTool(Tool tool){
229 // this.toolList.add(tool);
230 // }
231
232 public void addGenSection(GenSectionTask genSect){
233 this.gensectList.add(genSect);
234 }
235 }
236
237