3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 package org
.tianocore
.migration
;
15 import java
.io
.BufferedReader
;
16 import java
.io
.BufferedWriter
;
18 import java
.io
.FileWriter
;
19 import java
.io
.InputStreamReader
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Iterator
;
22 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import org
.apache
.xmlbeans
.XmlCursor
;
26 import org
.apache
.xmlbeans
.XmlOptions
;
27 import org
.tianocore
.ExternsDocument
;
28 import org
.tianocore
.FilenameDocument
;
29 import org
.tianocore
.GuidsDocument
;
30 import org
.tianocore
.LibraryClassDefinitionsDocument
;
31 import org
.tianocore
.LibraryClassDocument
;
32 import org
.tianocore
.ModuleDefinitionsDocument
;
33 import org
.tianocore
.ModuleSurfaceAreaDocument
;
34 import org
.tianocore
.ModuleTypeDef
;
35 import org
.tianocore
.MsaHeaderDocument
;
36 import org
.tianocore
.PPIsDocument
;
37 import org
.tianocore
.PackageDependenciesDocument
;
38 import org
.tianocore
.ProtocolsDocument
;
39 import org
.tianocore
.SourceFilesDocument
;
40 import org
.tianocore
.SupportedArchitectures
;
41 import org
.tianocore
.UsageTypes
;
42 import org
.tianocore
.SupportedArchitectures
.Enum
;
44 public class MsaWriter
{
45 MsaWriter(ModuleInfo moduleinfo
) {
49 private ModuleInfo mi
;
51 private ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
54 private ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= msadoc
55 .addNewModuleSurfaceArea();
57 private MsaHeaderDocument
.MsaHeader msaheader
= msa
.addNewMsaHeader();
59 private ModuleDefinitionsDocument
.ModuleDefinitions md
= msa
60 .addNewModuleDefinitions();
62 private SourceFilesDocument
.SourceFiles sourcefiles
= msa
63 .addNewSourceFiles(); // found local .h files are not written
65 private GuidsDocument
.Guids guids
;
67 private ProtocolsDocument
.Protocols protocols
;
69 private PPIsDocument
.PPIs ppis
;
71 private PackageDependenciesDocument
.PackageDependencies pd
= msa
72 .addNewPackageDependencies();
74 private LibraryClassDefinitionsDocument
.LibraryClassDefinitions libclassdefs
= msa
75 .addNewLibraryClassDefinitions();
77 private ExternsDocument
.Externs externs
= msa
.addNewExterns();
79 private String
Query(String requirement
) throws Exception
{
81 BufferedReader rd
= new BufferedReader(new InputStreamReader(System
.in
));
82 System
.out
.println(requirement
);
83 while ((answer
= rd
.readLine()).length() == 0)
88 private void addSourceFiles(String name
) { // furthur modification needed
89 List
<Enum
> arch
= new ArrayList
<Enum
>();
90 FilenameDocument
.Filename filename
;
91 filename
= sourcefiles
.addNewFilename();
92 filename
.setStringValue(name
);
94 if (name
.contains("x64" + File
.separator
)) { // filename ???
95 arch
.add(SupportedArchitectures
.X_64
);
96 System
.out
.println("x64" + File
.separator
);
97 filename
.setSupArchList(arch
);
98 } else if (name
.contains("Ia32" + File
.separator
)) { // filename ???
99 arch
.add(SupportedArchitectures
.IA_32
);
100 System
.out
.println("Ia32" + File
.separator
);
101 filename
.setSupArchList(arch
);
102 } else if (name
.contains("Ipf" + File
.separator
)) { // filename ???
103 arch
.add(SupportedArchitectures
.IPF
);
104 System
.out
.println("Ipf" + File
.separator
);
105 filename
.setSupArchList(arch
);
106 } else if (name
.contains("Ebc" + File
.separator
)) { // filename ???
107 arch
.add(SupportedArchitectures
.EBC
);
108 System
.out
.println("Ebc" + File
.separator
);
109 filename
.setSupArchList(arch
);
113 private void addWrapper() {
114 XmlCursor cursor
= msa
.newCursor();
115 String uri
= "http://www.TianoCore.org/2006/Edk2.0";
117 cursor
.toNextToken();
118 cursor
.insertNamespace("", uri
);
119 cursor
.insertNamespace("xsi",
120 "http://www.w3.org/2001/XMLSchema-instance");
122 msa
= (ModuleSurfaceAreaDocument
.ModuleSurfaceArea
) cursor
.getObject();
125 private ModuleSurfaceAreaDocument
fulfillMsadoc() throws Exception
{
129 if (mi
.modulename
!= null) {
130 msaheader
.setModuleName(mi
.modulename
);
133 .setModuleName(mi
.modulename
= Query("Module Name Not Found! Please Input ModuleName"));
135 if (mi
.guidvalue
== null) {
136 mi
.guidvalue
= UUID
.randomUUID().toString();
138 .println("Guid value can not be retrieved from inf file. Generate "
139 + mi
.guidvalue
+ " at random!");
141 msaheader
.setGuidValue(mi
.guidvalue
);
142 if (mi
.moduletype
!= null) {
143 msaheader
.setModuleType(ModuleTypeDef
.Enum
.forString(mi
147 .setModuleType(ModuleTypeDef
.Enum
148 .forString(mi
.moduletype
= Query("Guid Value Not Found! Please Input Guid Value")));
152 .setCopyright("Copyright (c) 2007, Intel Corporation. All rights reserved.");
153 msaheader
.setVersion("1.0");
154 msaheader
.setAbstract("Component name for module " + mi
.modulename
);
155 msaheader
.setDescription("FIX ME!");
157 if (mi
.license
== null) {
158 mi
.license
= "FIX ME!";
160 .println("Fail to extract license info in inf file");
162 msaheader
.addNewLicense().setStringValue(mi
.license
);
164 .setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");
166 List
<Enum
> arch
= new ArrayList
<Enum
>();
167 arch
.add(SupportedArchitectures
.IA_32
);
168 arch
.add(SupportedArchitectures
.X_64
);
169 arch
.add(SupportedArchitectures
.IPF
);
170 arch
.add(SupportedArchitectures
.EBC
);
171 md
.setSupportedArchitectures(arch
);
172 md
.setBinaryModule(false);
173 md
.setOutputFileBasename(mi
.modulename
);
175 // For now, simply add all package guids in the database.
177 it
= MigrationTool
.db
.dumpAllPkgGuid();
178 while (it
.hasNext()) {
179 pd
.addNewPackage().setPackageGuid(it
.next());
181 externs
.addNewSpecification().setStringValue(
182 "EFI_SPECIFICATION_VERSION 0x00020000");
183 externs
.addNewSpecification().setStringValue(
184 "EDK_RELEASE_VERSION 0x00020000");
185 if (mi
.entrypoint
!= null) {
186 externs
.addNewExtern().setModuleEntryPoint(mi
.entrypoint
);
187 org
.tianocore
.ModuleTypeDef
.Enum moduleType
= msaheader
189 if (moduleType
== ModuleTypeDef
.PEIM
) {
190 mi
.hashrequiredr9libs
.add("PeimEntryPoint");
192 mi
.hashrequiredr9libs
.add("UefiDriverEntryPoint");
196 it
= mi
.localmodulesources
.iterator();
197 while (it
.hasNext()) {
198 addSourceFiles(it
.next());
200 if (!mi
.protocols
.isEmpty()) {
201 protocols
= msa
.addNewProtocols();
202 it
= mi
.protocols
.iterator();
203 while (it
.hasNext()) {
204 if ((temp
= it
.next()) != null) {
205 ProtocolsDocument
.Protocols
.Protocol pr
= protocols
207 pr
.setProtocolCName(temp
);
208 pr
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
212 if (!mi
.ppis
.isEmpty()) {
213 ppis
= msa
.addNewPPIs();
214 it
= mi
.ppis
.iterator();
215 while (it
.hasNext()) {
216 if ((temp
= it
.next()) != null) {
217 PPIsDocument
.PPIs
.Ppi pp
= ppis
.addNewPpi();
218 pp
.setPpiCName(temp
);
219 pp
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
223 if (!mi
.guids
.isEmpty()) {
224 guids
= msa
.addNewGuids();
225 it
= mi
.guids
.iterator();
226 while (it
.hasNext()) {
227 if ((temp
= it
.next()) != null) {
228 GuidsDocument
.Guids
.GuidCNames gcn
= guids
230 gcn
.setGuidCName(temp
);
231 gcn
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
235 it
= mi
.hashrequiredr9libs
.iterator();
236 while (it
.hasNext()) {
237 if ((temp
= it
.next()) != null && !temp
.matches("%")
238 && !temp
.matches("n/a")) {
239 LibraryClassDocument
.LibraryClass lc
= libclassdefs
240 .addNewLibraryClass();
242 lc
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
246 msadoc
.setModuleSurfaceArea(msa
);
250 public void flush() throws Exception
{
251 XmlOptions options
= new XmlOptions();
253 options
.setCharacterEncoding("UTF-8");
254 options
.setSavePrettyPrint();
255 options
.setSavePrettyPrintIndent(2);
256 options
.setUseDefaultNamespace();
258 BufferedWriter bw
= new BufferedWriter(new FileWriter(
259 MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
260 + "Migration_" + mi
.modulename
+ File
.separator
261 + mi
.modulename
+ ".msa"));
262 fulfillMsadoc().save(bw
, options
);
263 // MsaTreeEditor.init(mi, ui, msadoc);
268 private static void flush(String path
, ModuleSurfaceAreaDocument msadoc
)
270 XmlOptions options
= new XmlOptions();
272 options
.setCharacterEncoding("UTF-8");
273 options
.setSavePrettyPrint();
274 options
.setSavePrettyPrintIndent(2);
275 options
.setUseDefaultNamespace();
277 BufferedWriter bw
= new BufferedWriter(new FileWriter(path
));
278 msadoc
.save(bw
, options
);
283 public static final void parse(String msafile
) throws Exception
{
284 ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
286 flush("c:\\temp.msa", msadoc
);