]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
Support calling customized compression tool in FrameworkTask.
[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.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.tools.ant.BuildException;
28 import org.tianocore.common.logger.EdkLog;
29
30 /**
31 Class Tool is to define an external tool to be used for genffsfile
32 **/
33 public class Tool implements EfiDefine, Section {
34
35 String toolName = "";
36 List<Object> toolArgList = new ArrayList<Object>();
37 String outputPath;
38 File outputFile ;
39 List<Input> inputFiles = new ArrayList<Input>();
40 List<Section> gensectList = new ArrayList<Section>();
41 String inputArg = "-i ";
42 /**
43 Call extern tool
44
45 @param buffer The buffer to put the result with alignment
46 **/
47 public void toBuffer (DataOutputStream buffer){
48 ///
49 /// call extern tool
50 ///
51 try {
52 executeTool ();
53 } catch (Exception e) {
54 throw new BuildException("Call to executeTool failed!\n");
55 }
56
57 ///
58 /// check if file exist
59 ///
60 //File OutputFile = new File (this.outPutFileName);
61 if (!outputFile.exists()) {
62 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");
63 }
64
65 ///
66 /// Read output file and write it's cotains to buffer
67 ///
68 FileInputStream fs = null;
69 DataInputStream in = null;
70 try {
71 fs = new FileInputStream (outputFile);
72 in = new DataInputStream (fs);
73
74
75 int fileLen = (int)outputFile.length();
76 byte[] data = new byte[fileLen];
77 in.read(data);
78 buffer.write(data, 0, fileLen);
79
80 ///
81 /// 4 byte alignment
82 ///
83 while ((fileLen & 0x03) != 0) {
84 fileLen++;
85 buffer.writeByte(0);
86 }
87 } catch (Exception e) {
88 System.out.print(e.getMessage());
89 throw new BuildException("Tool call, toBuffer failed!\n");
90 } finally {
91 try {
92 if (in != null) {
93 in.close();
94 }
95 if (fs != null) {
96 fs.close();
97 }
98 } catch (Exception e) {
99 System.out.println("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 Iterator argIter = toolArgList.iterator();
112 Iterator inputIter = inputFiles.iterator();
113 ToolArg toolArg;
114 Input file = null;
115
116
117 //
118 // Get each section which under the compress {};
119 // And add it is contains to File;
120 //
121 Section sect;
122 try{
123 Iterator SectionIter = this.gensectList.iterator();
124 while (SectionIter.hasNext()){
125 sect = (Section)SectionIter.next();
126 //
127 // Parse <genSection> element
128 //
129 File outputFile = File.createTempFile("temp", "sec1",new File(outputPath));
130 FileOutputStream bo = new FileOutputStream(outputFile);
131 DataOutputStream Do = new DataOutputStream (bo);
132 //
133 // Call each section class's toBuffer function.
134 //
135 try {
136 sect.toBuffer(Do);
137 }
138 catch (BuildException e) {
139 System.out.print(e.getMessage());
140 throw new BuildException ("GenSection failed at Tool!");
141 }
142 Do.close();
143 this.inputArg += outputFile.getPath() + " ";
144 }
145 } catch (IOException e){
146 throw new BuildException ("Gensection failed at tool!");
147 }
148
149
150 ///
151 /// argument of tools
152 ///
153 while (argIter.hasNext()) {
154 toolArg = (ToolArg)argIter.next();
155 argument = argument + toolArg.getLine() + " ";
156
157 }
158
159 ///
160 /// input files for tools
161 ///
162 while (inputIter.hasNext()) {
163 file = (Input)inputIter.next();
164 inputArg += file.toString(" ");
165 }
166 try {
167 outputFile = File.createTempFile("temp", null, new File(outputPath));
168 argument = argument + inputArg + " -o " + outputFile.getPath();
169 EdkLog.log(EdkLog.EDK_INFO, argument);
170 ///
171 /// execute command line
172 ///
173 Process process = Runtime.getRuntime().exec(command + " " + argument);
174 process.waitFor();
175 } catch (Exception e) {
176 System.out.print (e.getMessage());
177 throw new BuildException("Execution of externalTool task failed!\n");
178 }
179 }
180
181 /**
182 Add method of ANT task/datatype for nested ToolArg type of element
183
184 @param toolArg The ToolArg object containing arguments for the tool
185 **/
186 public void addToolArg (ToolArg toolArg) {
187 toolArgList.add (toolArg);
188 }
189
190 /**
191 Get method of ANT task/datatype for attribute "OutputPath"
192
193 @returns The name of output path
194 **/
195 public String getOutputPath() {
196 return outputPath;
197 }
198
199 /**
200 Set method of ANT task/datatype for attribute "OutputPath"
201
202 @param outputPath The name of output path
203 **/
204 public void setOutputPath(String outPutPath) {
205 this.outputPath = outPutPath;
206 }
207
208 /**
209 Get method of ANT task/datatype for attribute "ToolName"
210
211 @returns The name of the tool.
212 **/
213 public String getToolName() {
214 return toolName;
215 }
216
217 /**
218 Set method of ANT task/datatype for attribute "ToolName"
219
220 @param toolName The name of the tool
221 **/
222 public void setToolName(String toolName) {
223 this.toolName = toolName;
224 }
225
226 /**
227 Add method of ANT task/datatype for nested Input type of element
228
229 @param file The Input objec which represents a file
230 **/
231 public void addInput(Input file) {
232 inputFiles.add(file);
233 }
234
235 // /**
236 // addTool
237 //
238 // This function is to add instance of Tool to list.
239 //
240 // @param tool instance of Tool.
241 // **/
242 // public void addTool(Tool tool){
243 // this.toolList.add(tool);
244 // }
245
246 public void addGenSection(GenSectionTask genSect){
247 this.gensectList.add(genSect);
248 }
249
250 }
251
252