]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
1) Changed ToolArg class to abstract generic arguments of a tool
[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 private String toolName = "";
36 private ToolArg toolArgList = new ToolArg();
37 private Input inputFiles = 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");
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 System.out.print(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 } catch (Exception e) {
98 System.out.println("WARNING: Cannot close " + outputFile.getPath());
99 }
100 }
101 }
102
103 ///
104 /// execute external tool for genffsfile
105 ///
106 private void executeTool () {
107 String command = "";
108 String argument = "";
109 command = toolName;
110
111 //
112 // Get each section which under the compress {};
113 // And add it is contains to File;
114 //
115 Section sect;
116 try{
117 Iterator SectionIter = this.gensectList.iterator();
118 while (SectionIter.hasNext()){
119 sect = (Section)SectionIter.next();
120 //
121 // Parse <genSection> element
122 //
123 File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));
124 FileOutputStream bo = new FileOutputStream(outputFile);
125 DataOutputStream Do = new DataOutputStream (bo);
126 //
127 // Call each section class's toBuffer function.
128 //
129 try {
130 sect.toBuffer(Do);
131 }
132 catch (BuildException e) {
133 System.out.print(e.getMessage());
134 throw new BuildException ("GenSection failed at Tool!");
135 }
136 Do.close();
137 this.inputFiles.insFile(outputFile.getPath());
138 }
139 } catch (IOException e){
140 throw new BuildException ("Gensection failed at tool!");
141 }
142
143 try {
144 outputFile = File.createTempFile("temp", null, new File(outputPath));
145 argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ")
146 + " -o " + outputFile.getPath();
147 EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);
148 ///
149 /// execute command line
150 ///
151 Process process = Runtime.getRuntime().exec(command + " " + argument);
152 process.waitFor();
153 } catch (Exception e) {
154 System.out.print (e.getMessage());
155 throw new BuildException("Execution of externalTool task failed!\n");
156 }
157 }
158
159 /**
160 Add method of ANT task/datatype for nested ToolArg type of element
161
162 @param toolArg The ToolArg object containing arguments for the tool
163 **/
164 public void addConfiguredToolArg (ToolArg toolArg) {
165 toolArgList.insert(toolArg);
166 }
167
168 /**
169 Get method of ANT task/datatype for attribute "OutputPath"
170
171 @returns The name of output path
172 **/
173 public String getOutputPath() {
174 return outputPath;
175 }
176
177 /**
178 Set method of ANT task/datatype for attribute "OutputPath"
179
180 @param outputPath The name of output path
181 **/
182 public void setOutputPath(String outPutPath) {
183 this.outputPath = outPutPath;
184 }
185
186 /**
187 Get method of ANT task/datatype for attribute "ToolName"
188
189 @returns The name of the tool.
190 **/
191 public String getToolName() {
192 return toolName;
193 }
194
195 /**
196 Set method of ANT task/datatype for attribute "ToolName"
197
198 @param toolName The name of the tool
199 **/
200 public void setToolName(String toolName) {
201 this.toolName = toolName;
202 }
203
204 /**
205 Add method of ANT task/datatype for nested Input type of element
206
207 @param file The Input objec which represents a file
208 **/
209 public void addConfiguredInput(Input file) {
210 inputFiles.insert(file);
211 }
212
213 // /**
214 // addTool
215 //
216 // This function is to add instance of Tool to list.
217 //
218 // @param tool instance of Tool.
219 // **/
220 // public void addTool(Tool tool){
221 // this.toolList.add(tool);
222 // }
223
224 public void addGenSection(GenSectionTask genSect){
225 this.gensectList.add(genSect);
226 }
227 }
228
229