2 This file is to define nested element which is meant for specifying a tool
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
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.
14 package org
.tianocore
.framework
.tasks
;
16 import java
.io
.DataInputStream
;
17 import java
.io
.DataOutputStream
;
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 import java
.util
.Random
;
27 import org
.apache
.tools
.ant
.BuildException
;
28 import org
.tianocore
.common
.logger
.EdkLog
;
31 Class Tool is to define an external tool to be used for genffsfile
33 public class Tool
implements EfiDefine
, Section
{
35 private String toolName
= "";
36 private ToolArg toolArgList
= new ToolArg();
37 private Input inputFiles
= new Input();
38 private Input tempInputFile
= new Input();
39 private String outputPath
;
40 private String outputFileName
;
41 private static Random ran
= new Random(9999);
42 private List
<Section
> gensectList
= new ArrayList
<Section
>();
46 @param buffer The buffer to put the result with alignment
48 public void toBuffer (DataOutputStream buffer
){
54 } catch (Exception e
) {
55 throw new BuildException("Call to executeTool failed!\n" + e
.getMessage());
59 /// check if file exist
61 File outputFile
= new File (this.outputFileName
);
62 if (!outputFile
.exists()) {
63 throw new BuildException("The file " + outputFile
.getPath() + " does not exist!\n");
67 /// Read output file and write it's cotains to buffer
69 FileInputStream fs
= null;
70 DataInputStream in
= null;
72 fs
= new FileInputStream (outputFile
);
73 in
= new DataInputStream (fs
);
76 int fileLen
= (int)outputFile
.length();
77 byte[] data
= new byte[fileLen
];
79 buffer
.write(data
, 0, fileLen
);
84 while ((fileLen
& 0x03) != 0) {
88 } catch (Exception e
) {
89 EdkLog
.log(e
.getMessage());
90 throw new BuildException("Tool call, toBuffer failed!\n");
100 } catch (Exception e
) {
101 EdkLog
.log("WARNING: Cannot close " + outputFile
.getPath());
107 /// execute external tool for genffsfile
109 private void executeTool () {
111 String argument
= "";
115 // Get each section which under the compress {};
116 // And add it is contains to File;
120 Iterator SectionIter
= this.gensectList
.iterator();
121 while (SectionIter
.hasNext()){
122 sect
= (Section
)SectionIter
.next();
124 // Parse <genSection> element
126 File outputFile
= File
.createTempFile("temp", "sec1", new File(outputPath
));
127 FileOutputStream bo
= new FileOutputStream(outputFile
);
128 DataOutputStream Do
= new DataOutputStream (bo
);
130 // Call each section class's toBuffer function.
135 catch (BuildException e
) {
136 EdkLog
.log(e
.getMessage());
137 throw new BuildException ("GenSection failed at Tool!");
144 this.tempInputFile
.insFile(outputFile
.getPath());
146 } catch (IOException e
){
147 throw new BuildException ("Gensection failed at tool!");
151 this.outputFileName
= "Temp" + ran
.nextInt();
152 argument
= toolArgList
+ inputFiles
.toStringWithSinglepPrefix(" -i ")
153 + tempInputFile
.toString(" ")+ " -o " + outputFileName
;
154 EdkLog
.log(this, EdkLog
.EDK_VERBOSE
, command
+ " " + argument
);
156 /// execute command line
158 Process process
= Runtime
.getRuntime().exec(command
+ " " + argument
);
160 Iterator tempFile
= tempInputFile
.getNameList().iterator();
161 while (tempFile
.hasNext()){
162 File file
= new File((String
)tempFile
.next());
167 } catch (Exception e
) {
EdkLog
.log(e
.getMessage());
168 throw new BuildException("Execution of externalTool task failed!\n");
173 Add method of ANT task/datatype for nested ToolArg type of element
175 @param toolArg The ToolArg object containing arguments for the tool
177 public void addConfiguredToolArg (ToolArg toolArg
) {
178 toolArgList
.insert(toolArg
);
182 Get method of ANT task/datatype for attribute "OutputPath"
184 @returns The name of output path
186 public String
getOutputPath() {
191 Set method of ANT task/datatype for attribute "OutputPath"
193 @param outputPath The name of output path
195 public void setOutputPath(String outPutPath
) {
196 this.outputPath
= outPutPath
;
200 Get method of ANT task/datatype for attribute "ToolName"
202 @returns The name of the tool.
204 public String
getToolName() {
209 Set method of ANT task/datatype for attribute "ToolName"
211 @param toolName The name of the tool
213 public void setToolName(String toolName
) {
214 this.toolName
= toolName
;
218 Add method of ANT task/datatype for nested Input type of element
220 @param file The Input objec which represents a file
222 public void addConfiguredInput(Input file
) {
223 inputFiles
.insert(file
);
229 // This function is to add instance of Tool to list.
231 // @param tool instance of Tool.
233 // public void addTool(Tool tool){
234 // this.toolList.add(tool);
237 public void addGenSection(GenSectionTask genSect
){
238 this.gensectList
.add(genSect
);