878ddf1f |
1 | /** @file\r |
2 | This file is to define nested element which is meant for specifying a tool\r |
3 | \r |
4 | Copyright (c) 2006, Intel Corporation\r |
5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r |
8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
12 | \r |
13 | **/\r |
14 | package org.tianocore.framework.tasks;\r |
15 | \r |
16 | import java.io.DataInputStream;\r |
17 | import java.io.DataOutputStream;\r |
18 | import java.io.File;\r |
19 | import java.io.FileInputStream;\r |
20 | import java.util.ArrayList;\r |
21 | import java.util.Iterator;\r |
22 | import java.util.List;\r |
23 | \r |
24 | import 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 |
29 | public class Tool implements EfiDefine, Section {\r |
30 | \r |
31 | String toolName = "";\r |
32 | List<Object> toolArgList = new ArrayList<Object>();\r |
33 | String outputPath;\r |
82810f3b |
34 | File outputFile ;\r |
878ddf1f |
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 |
87379bbe |
42 | public void toBuffer (DataOutputStream buffer){\r |
878ddf1f |
43 | ///\r |
44 | /// call extern tool\r |
45 | ///\r |
46 | try {\r |
47 | executeTool ();\r |
48 | } catch (Exception e) {\r |
3f7b510e |
49 | throw new BuildException("Call to executeTool failed!\n");\r |
878ddf1f |
50 | }\r |
51 | \r |
52 | ///\r |
53 | /// check if file exist\r |
54 | ///\r |
82810f3b |
55 | //File OutputFile = new File (this.outPutFileName);\r |
56 | if (!outputFile.exists()) {\r |
57 | throw new BuildException("The file " + outputFile.getPath() + " does not exist!\n");\r |
878ddf1f |
58 | }\r |
59 | \r |
60 | ///\r |
61 | /// Read output file and write it's cotains to buffer\r |
62 | ///\r |
82810f3b |
63 | FileInputStream fs = null;\r |
64 | DataInputStream in = null;\r |
878ddf1f |
65 | try {\r |
82810f3b |
66 | fs = new FileInputStream (outputFile);\r |
67 | in = new DataInputStream (fs);\r |
68 | \r |
69 | \r |
70 | int fileLen = (int)outputFile.length();\r |
71 | byte[] data = new byte[fileLen];\r |
72 | in.read(data);\r |
73 | buffer.write(data, 0, fileLen);\r |
878ddf1f |
74 | \r |
75 | ///\r |
76 | /// 4 byte alignment\r |
77 | ///\r |
78 | while ((fileLen & 0x03) != 0) {\r |
79 | fileLen++;\r |
80 | buffer.writeByte(0);\r |
81 | }\r |
878ddf1f |
82 | } catch (Exception e) {\r |
83 | System.out.print(e.getMessage());\r |
3f7b510e |
84 | throw new BuildException("Tool call, toBuffer failed!\n");\r |
82810f3b |
85 | } finally {\r |
86 | try {\r |
87 | if (in != null) {\r |
88 | in.close();\r |
89 | }\r |
90 | if (fs != null) {\r |
91 | fs.close();\r |
92 | }\r |
93 | } catch (Exception e) {\r |
94 | System.out.println("WARNING: Cannot close " + outputFile.getPath());\r |
95 | }\r |
878ddf1f |
96 | }\r |
97 | }\r |
98 | \r |
99 | ///\r |
100 | /// execute external tool for genffsfile\r |
101 | ///\r |
102 | private void executeTool () {\r |
103 | String command = "";\r |
104 | String argument = "";\r |
105 | command = toolName;\r |
106 | Iterator argIter = toolArgList.iterator();\r |
107 | Iterator inputIter = inputFiles.iterator();\r |
108 | ToolArg toolArg;\r |
109 | Input file = null;\r |
110 | \r |
111 | ///\r |
112 | /// argument of tools\r |
113 | ///\r |
114 | while (argIter.hasNext()) {\r |
115 | toolArg = (ToolArg)argIter.next();\r |
116 | argument = argument + toolArg.getLine() + " ";\r |
117 | \r |
118 | }\r |
119 | \r |
120 | ///\r |
121 | /// input files for tools\r |
122 | ///\r |
82810f3b |
123 | argument += " -i ";\r |
878ddf1f |
124 | while (inputIter.hasNext()) {\r |
125 | file = (Input)inputIter.next();\r |
82810f3b |
126 | argument += file.toString(" ");\r |
878ddf1f |
127 | }\r |
128 | \r |
878ddf1f |
129 | try {\r |
82810f3b |
130 | outputFile = File.createTempFile("temp", ".crc", new File(outputPath));\r |
131 | argument = argument + " -o " + outputFile.getPath();\r |
878ddf1f |
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 |
3f7b510e |
140 | throw new BuildException("Execution of externalTool task failed!\n");\r |
878ddf1f |
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 |