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
;
18 import org
.tianocore
.*;
19 import org
.tianocore
.SupportedArchitectures
.Enum
;
20 import org
.apache
.xmlbeans
.*;
22 public class MsaWriter
{
23 MsaWriter(ModuleInfo moduleinfo
) {
27 private ModuleInfo mi
;
28 private ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
.newInstance();
30 private ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= msadoc
.addNewModuleSurfaceArea();
31 private MsaHeaderDocument
.MsaHeader msaheader
= msa
.addNewMsaHeader();
32 private ModuleDefinitionsDocument
.ModuleDefinitions md
= msa
.addNewModuleDefinitions();
33 private SourceFilesDocument
.SourceFiles sourcefiles
= msa
.addNewSourceFiles(); //found local .h files are not written
34 private GuidsDocument
.Guids guids
;
35 private ProtocolsDocument
.Protocols protocols
;
36 private PPIsDocument
.PPIs ppis
;
37 private PackageDependenciesDocument
.PackageDependencies pd
= msa
.addNewPackageDependencies();
38 private LibraryClassDefinitionsDocument
.LibraryClassDefinitions libclassdefs
= msa
.addNewLibraryClassDefinitions();
39 private ExternsDocument
.Externs externs
= msa
.addNewExterns();
41 private String
Query (String requirement
) throws Exception
{
43 BufferedReader rd
= new BufferedReader(new InputStreamReader(System
.in
));
44 System
.out
.println(requirement
);
45 while ((answer
= rd
.readLine()).length() == 0) ;
49 private void addSourceFiles (String name
) { // furthur modification needed
50 List
<Enum
> arch
= new ArrayList
<Enum
>();
51 FilenameDocument
.Filename filename
;
52 filename
= sourcefiles
.addNewFilename();
53 filename
.setStringValue(name
);
55 if (name
.contains("x64" + File
.separator
)) { // filename ???
56 arch
.add(SupportedArchitectures
.X_64
);
57 System
.out
.println("x64" + File
.separator
);
58 filename
.setSupArchList(arch
);
59 } else if (name
.contains("Ia32" + File
.separator
)) { // filename ???
60 arch
.add(SupportedArchitectures
.IA_32
);
61 System
.out
.println("Ia32" + File
.separator
);
62 filename
.setSupArchList(arch
);
63 } else if (name
.contains("Ipf" + File
.separator
)) { // filename ???
64 arch
.add(SupportedArchitectures
.IPF
);
65 System
.out
.println("Ipf" + File
.separator
);
66 filename
.setSupArchList(arch
);
67 } else if (name
.contains("Ebc" + File
.separator
)) { // filename ???
68 arch
.add(SupportedArchitectures
.EBC
);
69 System
.out
.println("Ebc" + File
.separator
);
70 filename
.setSupArchList(arch
);
73 private void addWrapper() {
74 XmlCursor cursor
= msa
.newCursor();
75 String uri
= "http://www.TianoCore.org/2006/Edk2.0";
78 cursor
.insertNamespace("", uri
);
79 cursor
.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
81 msa
= (ModuleSurfaceAreaDocument
.ModuleSurfaceArea
)cursor
.getObject();
83 private ModuleSurfaceAreaDocument
fulfillMsadoc() throws Exception
{
87 if (mi
.modulename
!= null) {
88 msaheader
.setModuleName(mi
.modulename
);
90 msaheader
.setModuleName(mi
.modulename
= Query("Module Name Not Found! Please Input ModuleName"));
92 if (mi
.guidvalue
== null) {
93 mi
.guidvalue
= UUID
.randomUUID().toString();
94 MigrationTool
.ui
.println ("Guid value can not be retrieved from inf file. Generate " + mi
.guidvalue
+ " at random!");
96 msaheader
.setGuidValue(mi
.guidvalue
);
97 if (mi
.moduletype
!= null) {
98 msaheader
.setModuleType(ModuleTypeDef
.Enum
.forString(mi
.getModuleType()));
100 msaheader
.setModuleType(ModuleTypeDef
.Enum
.forString(mi
.moduletype
= Query("Guid Value Not Found! Please Input Guid Value")));
103 msaheader
.setCopyright("Copyright (c) 2006, Intel Corporation. All right reserved.");
104 msaheader
.setVersion("1.0");
105 msaheader
.setAbstract("Component name for module " + mi
.modulename
);
106 msaheader
.setDescription("FIX ME!");
108 if (mi
.license
== null) {
109 mi
.license
= "FIX ME!";
110 MigrationTool
.ui
.println ("Fail to extract license info in inf file");
112 msaheader
.addNewLicense().setStringValue(mi
.license
);
113 msaheader
.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");
115 List
<Enum
> arch
= new ArrayList
<Enum
>();
116 arch
.add(SupportedArchitectures
.IA_32
);
117 arch
.add(SupportedArchitectures
.X_64
);
118 arch
.add(SupportedArchitectures
.IPF
);
119 arch
.add(SupportedArchitectures
.EBC
);
120 md
.setSupportedArchitectures(arch
);
121 md
.setBinaryModule(false);
122 md
.setOutputFileBasename(mi
.modulename
);
124 // For now, simply add all package guids in the database.
126 it
= MigrationTool
.db
.dumpAllPkgGuid();
127 while (it
.hasNext()) {
128 pd
.addNewPackage().setPackageGuid(it
.next());
130 externs
.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
131 externs
.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
132 if (mi
.entrypoint
!= null) {
133 externs
.addNewExtern().setModuleEntryPoint(mi
.entrypoint
);
134 org
.tianocore
.ModuleTypeDef
.Enum moduleType
= msaheader
.getModuleType();
135 if (moduleType
== ModuleTypeDef
.PEIM
) {
136 mi
.hashrequiredr9libs
.add("PeimEntryPoint");
138 mi
.hashrequiredr9libs
.add("UefiDriverEntryPoint");
142 it
= mi
.localmodulesources
.iterator();
143 while (it
.hasNext()) {
144 addSourceFiles(it
.next());
146 if (!mi
.protocols
.isEmpty()) {
147 protocols
= msa
.addNewProtocols();
148 it
= mi
.protocols
.iterator();
149 while (it
.hasNext()) {
150 if ((temp
= it
.next()) != null) {
151 ProtocolsDocument
.Protocols
.Protocol pr
= protocols
.addNewProtocol();
152 pr
.setProtocolCName(temp
);
153 pr
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
157 if (!mi
.ppis
.isEmpty()) {
158 ppis
= msa
.addNewPPIs();
159 it
= mi
.ppis
.iterator();
160 while (it
.hasNext()) {
161 if ((temp
= it
.next()) != null) {
162 PPIsDocument
.PPIs
.Ppi pp
= ppis
.addNewPpi();
163 pp
.setPpiCName(temp
);
164 pp
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
168 if (!mi
.guids
.isEmpty()) {
169 guids
= msa
.addNewGuids();
170 it
= mi
.guids
.iterator();
171 while (it
.hasNext()) {
172 if ((temp
= it
.next()) != null) {
173 GuidsDocument
.Guids
.GuidCNames gcn
= guids
.addNewGuidCNames();
174 gcn
.setGuidCName(temp
);
175 gcn
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
179 it
= mi
.hashrequiredr9libs
.iterator();
180 while (it
.hasNext()) {
181 if ((temp
= it
.next()) != null && !temp
.matches("%") && !temp
.matches("n/a")) {
182 LibraryClassDocument
.LibraryClass lc
= libclassdefs
.addNewLibraryClass();
184 lc
.setUsage(UsageTypes
.ALWAYS_CONSUMED
);
188 msadoc
.setModuleSurfaceArea(msa
);
192 public void flush() throws Exception
{
193 XmlOptions options
= new XmlOptions();
195 options
.setCharacterEncoding("UTF-8");
196 options
.setSavePrettyPrint();
197 options
.setSavePrettyPrintIndent(2);
198 options
.setUseDefaultNamespace();
200 BufferedWriter bw
= new BufferedWriter(new FileWriter(MigrationTool
.ModuleInfoMap
.get(mi
) + File
.separator
+ "Migration_" + mi
.modulename
+ File
.separator
+ mi
.modulename
+ ".msa"));
201 fulfillMsadoc().save(bw
, options
);
202 //MsaTreeEditor.init(mi, ui, msadoc);
207 private static void flush(String path
, ModuleSurfaceAreaDocument msadoc
) throws Exception
{
208 XmlOptions options
= new XmlOptions();
210 options
.setCharacterEncoding("UTF-8");
211 options
.setSavePrettyPrint();
212 options
.setSavePrettyPrintIndent(2);
213 options
.setUseDefaultNamespace();
215 BufferedWriter bw
= new BufferedWriter(new FileWriter(path
));
216 msadoc
.save(bw
, options
);
221 public static final void parse(String msafile
) throws Exception
{
222 ModuleSurfaceAreaDocument msadoc
= ModuleSurfaceAreaDocument
.Factory
.parse(msafile
);
223 flush("c:\\temp.msa", msadoc
);