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 |
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 |
42 | public void toBuffer (DataOutputStream buffer){\r |
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 |
75 | i ++;\r |
76 | }\r |
77 | \r |
78 | ///\r |
79 | /// 4 byte alignment\r |
80 | ///\r |
81 | while ((fileLen & 0x03) != 0) {\r |
82 | fileLen++;\r |
83 | buffer.writeByte(0);\r |
84 | }\r |
85 | In.close();\r |
86 | \r |
87 | } catch (Exception e) {\r |
88 | System.out.print(e.getMessage());\r |
89 | throw new BuildException("Call tool2buffer failed!\n");\r |
90 | }\r |
91 | }\r |
92 | \r |
93 | ///\r |
94 | /// execute external tool for genffsfile\r |
95 | ///\r |
96 | private void executeTool () {\r |
97 | String command = "";\r |
98 | String argument = "";\r |
99 | command = toolName;\r |
100 | Iterator argIter = toolArgList.iterator();\r |
101 | Iterator inputIter = inputFiles.iterator();\r |
102 | ToolArg toolArg;\r |
103 | Input file = null;\r |
104 | \r |
105 | ///\r |
106 | /// argument of tools\r |
107 | ///\r |
108 | while (argIter.hasNext()) {\r |
109 | toolArg = (ToolArg)argIter.next();\r |
110 | argument = argument + toolArg.getLine() + " ";\r |
111 | \r |
112 | }\r |
113 | \r |
114 | ///\r |
115 | /// input files for tools\r |
116 | ///\r |
117 | argument = argument + "-i ";\r |
118 | while (inputIter.hasNext()) {\r |
119 | file = (Input)inputIter.next();\r |
120 | argument = argument + file.getFile() + " ";\r |
121 | }\r |
122 | \r |
123 | outPutFileName = outputPath + File.separatorChar + (new File(file.getFile())).getName() + ".crc";\r |
124 | argument = argument + " -o " + outPutFileName; \r |
125 | \r |
126 | try {\r |
127 | \r |
128 | ///\r |
129 | /// execute command line\r |
130 | ///\r |
131 | Process crcProcess = Runtime.getRuntime().exec(command + " " + argument);\r |
132 | crcProcess.waitFor();\r |
133 | } catch (Exception e) {\r |
134 | System.out.print (e.getMessage());\r |
135 | throw new BuildException("Execute tools fails!\n");\r |
136 | }\r |
137 | }\r |
138 | \r |
139 | /**\r |
140 | Add method of ANT task/datatype for nested ToolArg type of element\r |
141 | \r |
142 | @param toolArg The ToolArg object containing arguments for the tool\r |
143 | **/\r |
144 | public void addToolArg (ToolArg toolArg) {\r |
145 | toolArgList.add (toolArg);\r |
146 | }\r |
147 | \r |
148 | /**\r |
149 | Get method of ANT task/datatype for attribute "OutputPath"\r |
150 | \r |
151 | @returns The name of output path\r |
152 | **/\r |
153 | public String getOutputPath() {\r |
154 | return outputPath;\r |
155 | }\r |
156 | \r |
157 | /**\r |
158 | Set method of ANT task/datatype for attribute "OutputPath"\r |
159 | \r |
160 | @param outputPath The name of output path\r |
161 | **/\r |
162 | public void setOutputPath(String outPutPath) {\r |
163 | this.outputPath = outPutPath;\r |
164 | }\r |
165 | \r |
166 | /**\r |
167 | Get method of ANT task/datatype for attribute "ToolName"\r |
168 | \r |
169 | @returns The name of the tool.\r |
170 | **/\r |
171 | public String getToolName() {\r |
172 | return toolName;\r |
173 | }\r |
174 | \r |
175 | /**\r |
176 | Set method of ANT task/datatype for attribute "ToolName"\r |
177 | \r |
178 | @param toolName The name of the tool\r |
179 | **/\r |
180 | public void setToolName(String toolName) {\r |
181 | this.toolName = toolName;\r |
182 | }\r |
183 | \r |
184 | /**\r |
185 | Add method of ANT task/datatype for nested Input type of element\r |
186 | \r |
187 | @param file The Input objec which represents a file\r |
188 | **/\r |
189 | public void addInput(Input file) {\r |
190 | inputFiles.add(file);\r |
191 | }\r |
192 | }\r |
193 | \r |
194 | \r |