]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/Tool.java
Add judgement if filepath is . or not.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / Tool.java
CommitLineData
878ddf1f 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.DataInputStream;\r
17import java.io.DataOutputStream;\r
18import java.io.File;\r
19import java.io.FileInputStream;\r
20import java.util.ArrayList;\r
21import java.util.Iterator;\r
22import java.util.List;\r
23\r
24import org.apache.tools.ant.BuildException;\r
25\r
26/**\r
27 Class Tool is to define an external tool to be used for genffsfile\r
28 **/\r
29public class Tool implements EfiDefine, Section {\r
30\r
31 String toolName = "";\r
32 List<Object> toolArgList = new ArrayList<Object>();\r
33 String outputPath;\r
34 String outPutFileName ;\r
35 List<Input> inputFiles = new ArrayList<Input>();\r
36\r
37 /**\r
38 Call extern tool\r
39\r
40 @param buffer The buffer to put the result with alignment\r
41 **/\r
2da8968b 42 public void toBuffer (DataOutputStream buffer, DataOutputStream orgBuffer){\r
878ddf1f 43 File OutputFile;\r
44 byte data;\r
45\r
46 ///\r
47 /// call extern tool\r
48 ///\r
49 try {\r
50 executeTool ();\r
51 } catch (Exception e) {\r
52 throw new BuildException("Call tools failed!\n");\r
53 }\r
54\r
55 ///\r
56 /// check if file exist\r
57 ///\r
58 OutputFile = new File (this.outPutFileName);\r
59 long fileLen = OutputFile.length();\r
60 if (!OutputFile.exists()) {\r
61 throw new BuildException("The file " + outPutFileName + " is 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 try {\r
68 FileInputStream fs = new FileInputStream (this.outPutFileName);\r
69 DataInputStream In = new DataInputStream (fs);\r
70\r
71 int i = 0;\r
72 while (i < fileLen) {\r
73 data = In.readByte();\r
74 buffer.writeByte(data);\r
2da8968b 75 //\r
76 // Add data to org file\r
77 //\r
78 orgBuffer.writeByte(data);\r
878ddf1f 79 i ++;\r
80 }\r
81\r
82 ///\r
83 /// 4 byte alignment\r
84 ///\r
85 while ((fileLen & 0x03) != 0) {\r
86 fileLen++;\r
87 buffer.writeByte(0);\r
2da8968b 88 orgBuffer.writeByte(0);\r
878ddf1f 89 }\r
90 In.close();\r
91\r
92 } catch (Exception e) {\r
93 System.out.print(e.getMessage());\r
94 throw new BuildException("Call tool2buffer failed!\n");\r
95 }\r
96 }\r
97\r
98 ///\r
99 /// execute external tool for genffsfile\r
100 ///\r
101 private void executeTool () {\r
102 String command = "";\r
103 String argument = "";\r
104 command = toolName;\r
105 Iterator argIter = toolArgList.iterator();\r
106 Iterator inputIter = inputFiles.iterator();\r
107 ToolArg toolArg;\r
108 Input file = null;\r
109\r
110 ///\r
111 /// argument of tools\r
112 ///\r
113 while (argIter.hasNext()) {\r
114 toolArg = (ToolArg)argIter.next();\r
115 argument = argument + toolArg.getLine() + " ";\r
116\r
117 }\r
118\r
119 ///\r
120 /// input files for tools\r
121 ///\r
122 argument = argument + "-i ";\r
123 while (inputIter.hasNext()) {\r
124 file = (Input)inputIter.next();\r
125 argument = argument + file.getFile() + " ";\r
126 }\r
127\r
128 outPutFileName = outputPath + File.separatorChar + (new File(file.getFile())).getName() + ".crc";\r
129 argument = argument + " -o " + outPutFileName; \r
130\r
131 try {\r
132\r
133 ///\r
134 /// execute command line\r
135 ///\r
136 Process crcProcess = Runtime.getRuntime().exec(command + " " + argument);\r
137 crcProcess.waitFor();\r
138 } catch (Exception e) {\r
139 System.out.print (e.getMessage());\r
140 throw new BuildException("Execute tools fails!\n");\r
141 }\r
142 }\r
143\r
144 /**\r
145 Add method of ANT task/datatype for nested ToolArg type of element\r
146\r
147 @param toolArg The ToolArg object containing arguments for the tool\r
148 **/\r
149 public void addToolArg (ToolArg toolArg) {\r
150 toolArgList.add (toolArg);\r
151 }\r
152\r
153 /**\r
154 Get method of ANT task/datatype for attribute "OutputPath"\r
155\r
156 @returns The name of output path\r
157 **/\r
158 public String getOutputPath() {\r
159 return outputPath;\r
160 }\r
161\r
162 /**\r
163 Set method of ANT task/datatype for attribute "OutputPath"\r
164\r
165 @param outputPath The name of output path\r
166 **/\r
167 public void setOutputPath(String outPutPath) {\r
168 this.outputPath = outPutPath;\r
169 }\r
170\r
171 /**\r
172 Get method of ANT task/datatype for attribute "ToolName"\r
173\r
174 @returns The name of the tool.\r
175 **/\r
176 public String getToolName() {\r
177 return toolName;\r
178 }\r
179\r
180 /**\r
181 Set method of ANT task/datatype for attribute "ToolName"\r
182\r
183 @param toolName The name of the tool\r
184 **/\r
185 public void setToolName(String toolName) {\r
186 this.toolName = toolName;\r
187 }\r
188\r
189 /**\r
190 Add method of ANT task/datatype for nested Input type of element\r
191\r
192 @param file The Input objec which represents a file\r
193 **/\r
194 public void addInput(Input file) {\r
195 inputFiles.add(file);\r
196 }\r
197}\r
198\r
199\r