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