0ef2a6ea7c22941d7bbb7709e49a7cd2ccc443bd
[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 String outputPath;
38 private File outputFile ;
39 private List<Section> gensectList = new ArrayList<Section>();
40 /**
41 Call extern tool
42
43 @param buffer The buffer to put the result with alignment
44 **/
45 public void toBuffer (DataOutputStream buffer){
46 ///
47 /// call extern tool
48 ///
49 try {
50 executeTool ();
51 } catch (Exception e) {
52 throw new BuildException("Call to executeTool failed!\n" + e.getMessage());
53 }
54
55 ///
56 /// check if file exist
57 ///
58 //File OutputFile = new File (this.outPutFileName);
59 if (!outputFile.exists()) {
60 throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");
61 }
62
63 ///
64 /// Read output file and write it's cotains to buffer
65 ///
66 FileInputStream fs = null;
67 DataInputStream in = null;
68 try {
69 fs = new FileInputStream (outputFile);
70 in = new DataInputStream (fs);
71
72
73 int fileLen = (int)outputFile.length();
74 byte[] data = new byte[fileLen];
75 in.read(data);
76 buffer.write(data, 0, fileLen);
77
78 ///
79 /// 4 byte alignment
80 ///
81 while ((fileLen & 0x03) != 0) {
82 fileLen++;
83 buffer.writeByte(0);
84 }
85 } catch (Exception e) {
86 EdkLog.log(e.getMessage());
87 throw new BuildException("Tool call, toBuffer failed!\n");
88 } finally {
89 try {
90 if (in != null) {
91 in.close();
92 }
93 if (fs != null) {
94 fs.close();
95 }
96 } catch (Exception e) {
97 EdkLog.log("WARNING: Cannot close " + outputFile.getPath());
98 }
99 }
100 }
101
102 ///
103 /// execute external tool for genffsfile
104 ///
105 private void executeTool () {
106 String command = "";
107 String argument = "";
108 command = toolName;
109
110 //
111 // Get each section which under the compress {};
112 // And add it is contains to File;
113 //
114 Section sect;
115 try{
116 Iterator SectionIter = this.gensectList.iterator();
117 while (SectionIter.hasNext()){
118 sect = (Section)SectionIter.next();
119 //
120 // Parse <genSection> element
121 //
122 File outputFile = File.createTempFile("temp", "sec1", new File(outputPath));
123 FileOutputStream bo = new FileOutputStream(outputFile);
124 DataOutputStream Do = new DataOutputStream (bo);
125 //
126 // Call each section class's toBuffer function.
127 //
128 try {
129 sect.toBuffer(Do);
130 }
131 catch (BuildException e) {
132 EdkLog.log(e.getMessage());
133 throw new BuildException ("GenSection failed at Tool!");
134 }
135 Do.close();
136 this.inputFiles.insFile(outputFile.getPath());
137 }
138 } catch (IOException e){
139 throw new BuildException ("Gensection failed at tool!");
140 }
141
142 try {
143 outputFile = File.createTempFile("temp", null, new File(outputPath));
144 argument = toolArgList + inputFiles.toStringWithSinglepPrefix(" -i ")
145 + " -o " + outputFile.getPath();
146 EdkLog.log(this, EdkLog.EDK_VERBOSE, command + " " + argument);
147 ///
148 /// execute command line
149 ///
150 Process process = Runtime.getRuntime().exec(command + " " + argument);
151 process.waitFor();
152 } catch (Exception e) {
153 EdkLog.log(e.getMessage());
154 throw new BuildException("Execution of externalTool task failed!\n");
155 }
156 }
157
158 /**
159 Add method of ANT task/datatype for nested ToolArg type of element
160
161 @param toolArg The ToolArg object containing arguments for the tool
162 **/
163 public void addConfiguredToolArg (ToolArg toolArg) {
164 toolArgList.insert(toolArg);
165 }
166
167 /**
168 Get method of ANT task/datatype for attribute "OutputPath"
169
170 @returns The name of output path
171 **/
172 public String getOutputPath() {
173 return outputPath;
174 }
175
176 /**
177 Set method of ANT task/datatype for attribute "OutputPath"
178
179 @param outputPath The name of output path
180 **/
181 public void setOutputPath(String outPutPath) {
182 this.outputPath = outPutPath;
183 }
184
185 /**
186 Get method of ANT task/datatype for attribute "ToolName"
187
188 @returns The name of the tool.
189 **/
190 public String getToolName() {
191 return toolName;
192 }
193
194 /**
195 Set method of ANT task/datatype for attribute "ToolName"
196
197 @param toolName The name of the tool
198 **/
199 public void setToolName(String toolName) {
200 this.toolName = toolName;
201 }
202
203 /**
204 Add method of ANT task/datatype for nested Input type of element
205
206 @param file The Input objec which represents a file
207 **/
208 public void addConfiguredInput(Input file) {
209 inputFiles.insert(file);
210 }
211
212 // /**
213 // addTool
214 //
215 // This function is to add instance of Tool to list.
216 //
217 // @param tool instance of Tool.
218 // **/
219 // public void addTool(Tool tool){
220 // this.toolList.add(tool);
221 // }
222
223 public void addGenSection(GenSectionTask genSect){
224 this.gensectList.add(genSect);
225 }
226 }
227
228