Add Package-Guid Map.
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / MsaWriter.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17\r
18import org.tianocore.*;\r
19import org.tianocore.SupportedArchitectures.Enum;\r
20import org.apache.xmlbeans.*;\r
21\r
5c55f71c 22public class MsaWriter {\r
1ea59929 23 MsaWriter(ModuleInfo moduleinfo) {\r
24 mi = moduleinfo;\r
25 }\r
26 \r
27e0221a 27 private ModuleInfo mi;\r
8554faeb 28 private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();\r
27e0221a 29 \r
30 private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.addNewModuleSurfaceArea();\r
31 private MsaHeaderDocument.MsaHeader msaheader = msa.addNewMsaHeader();\r
32 private ModuleDefinitionsDocument.ModuleDefinitions md = msa.addNewModuleDefinitions();\r
33 private SourceFilesDocument.SourceFiles sourcefiles = msa.addNewSourceFiles(); //found local .h files are not written\r
34 private GuidsDocument.Guids guids;\r
35 private ProtocolsDocument.Protocols protocols;\r
36 private PPIsDocument.PPIs ppis;\r
37 private PackageDependenciesDocument.PackageDependencies pd = msa.addNewPackageDependencies();\r
38 private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = msa.addNewLibraryClassDefinitions();\r
39 private ExternsDocument.Externs externs = msa.addNewExterns();\r
40 \r
41 private String Query (String requirement) throws Exception {\r
42 String answer;\r
43 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));\r
44 System.out.println(requirement);\r
45 while ((answer = rd.readLine()).length() == 0) ;\r
46 return answer;\r
47 }\r
48 \r
49 private void addSourceFiles (String name) { // furthur modification needed\r
50 List<Enum> arch = new ArrayList<Enum>();\r
51 FilenameDocument.Filename filename;\r
52 filename = sourcefiles.addNewFilename();\r
53 filename.setStringValue(name);\r
54 \r
55 if (name.contains("x64" + File.separator)) { // filename ???\r
56 arch.add(SupportedArchitectures.X_64);\r
57 System.out.println("x64" + File.separator);\r
58 filename.setSupArchList(arch);\r
59 } else if (name.contains("Ia32" + File.separator)) { // filename ???\r
60 arch.add(SupportedArchitectures.IA_32);\r
61 System.out.println("Ia32" + File.separator);\r
62 filename.setSupArchList(arch);\r
63 } else if (name.contains("Ipf" + File.separator)) { // filename ???\r
64 arch.add(SupportedArchitectures.IPF);\r
65 System.out.println("Ipf" + File.separator);\r
66 filename.setSupArchList(arch);\r
67 } else if (name.contains("Ebc" + File.separator)) { // filename ???\r
68 arch.add(SupportedArchitectures.EBC);\r
69 System.out.println("Ebc" + File.separator);\r
70 filename.setSupArchList(arch);\r
71 }\r
72 }\r
49324055 73 private void addWrapper() {\r
74 XmlCursor cursor = msa.newCursor();\r
75 String uri = "http://www.TianoCore.org/2006/Edk2.0";\r
76 cursor.push();\r
77 cursor.toNextToken();\r
78 cursor.insertNamespace("", uri);\r
79 cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");\r
80 cursor.pop();\r
81 msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)cursor.getObject();\r
82 }\r
27e0221a 83 private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {\r
84 Iterator<String> it;\r
85 String temp;\r
49324055 86 \r
27e0221a 87 if (mi.modulename != null) {\r
88 msaheader.setModuleName(mi.modulename);\r
89 } else {\r
90 msaheader.setModuleName(mi.modulename = Query("Module Name Not Found! Please Input ModuleName"));\r
91 }\r
92 if (mi.guidvalue == null) {\r
93 mi.guidvalue = UUID.randomUUID().toString();\r
94 MigrationTool.ui.println ("Guid value can not be retrieved from inf file. Generate " + mi.guidvalue + " at random!"); \r
95 } \r
96 msaheader.setGuidValue(mi.guidvalue);\r
97 if (mi.moduletype != null) {\r
98 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.getModuleType()));\r
27e0221a 99 } else {\r
100 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("Guid Value Not Found! Please Input Guid Value")));\r
101 }\r
fb840aa8 102\r
db0e6906 103 msaheader.setCopyright("Copyright (c) 2006, Intel Corporation. All right reserved.");\r
27e0221a 104 msaheader.setVersion("1.0");\r
105 msaheader.setAbstract("Component name for module " + mi.modulename);\r
106 msaheader.setDescription("FIX ME!");\r
db0e6906 107\r
108 if (mi.license == null) {\r
109 mi.license = "FIX ME!";\r
110 MigrationTool.ui.println ("Fail to extract license info in inf file"); \r
111 }\r
112 msaheader.addNewLicense().setStringValue(mi.license);\r
27e0221a 113 msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");\r
114 \r
115 List<Enum> arch = new ArrayList<Enum>();\r
116 arch.add(SupportedArchitectures.IA_32);\r
117 arch.add(SupportedArchitectures.X_64);\r
118 arch.add(SupportedArchitectures.IPF);\r
119 arch.add(SupportedArchitectures.EBC);\r
120 md.setSupportedArchitectures(arch);\r
121 md.setBinaryModule(false);\r
122 md.setOutputFileBasename(mi.modulename);\r
9c0e70cb 123 //\r
124 // For now, simply add all package guids in the database. \r
125 // \r
126 it = MigrationTool.db.dumpAllPkgGuid();\r
127 while (it.hasNext()) {\r
128 pd.addNewPackage().setPackageGuid(it.next());\r
129 }\r
27e0221a 130 externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");\r
131 externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");\r
37e5cd65 132 if (mi.entrypoint != null) {\r
fd16b4dd 133 externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);\r
134 org.tianocore.ModuleTypeDef.Enum moduleType = msaheader.getModuleType();\r
135 if (moduleType == ModuleTypeDef.PEIM) {\r
136 mi.hashrequiredr9libs.add("PeimEntryPoint");\r
137 } else {\r
138 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");\r
139 }\r
37e5cd65 140 }\r
141 \r
27e0221a 142 it = mi.localmodulesources.iterator();\r
143 while (it.hasNext()) {\r
144 addSourceFiles(it.next());\r
145 }\r
323e2ffc 146 if (!mi.protocols.isEmpty()) {\r
27e0221a 147 protocols = msa.addNewProtocols();\r
323e2ffc 148 it = mi.protocols.iterator();\r
27e0221a 149 while (it.hasNext()) {\r
150 if ((temp = it.next()) != null) {\r
151 ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();\r
152 pr.setProtocolCName(temp);\r
153 pr.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
154 }\r
155 }\r
156 }\r
323e2ffc 157 if (!mi.ppis.isEmpty()) {\r
27e0221a 158 ppis = msa.addNewPPIs();\r
323e2ffc 159 it = mi.ppis.iterator();\r
27e0221a 160 while (it.hasNext()) {\r
161 if ((temp = it.next()) != null) {\r
162 PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();\r
163 pp.setPpiCName(temp);\r
164 pp.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
165 }\r
166 }\r
167 }\r
323e2ffc 168 if (!mi.guids.isEmpty()) {\r
27e0221a 169 guids = msa.addNewGuids();\r
323e2ffc 170 it = mi.guids.iterator();\r
27e0221a 171 while (it.hasNext()) {\r
172 if ((temp = it.next()) != null) {\r
173 GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();\r
174 gcn.setGuidCName(temp);\r
175 gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
176 }\r
177 }\r
178 }\r
179 it = mi.hashrequiredr9libs.iterator();\r
180 while (it.hasNext()) {\r
55bfa0f1 181 if ((temp = it.next()) != null && !temp.matches("%") && !temp.matches("n/a")) {\r
27e0221a 182 LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();\r
183 lc.setKeyword(temp);\r
184 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
185 }\r
186 }\r
49324055 187 addWrapper();\r
188 msadoc.setModuleSurfaceArea(msa);\r
27e0221a 189 return msadoc;\r
190 }\r
191 \r
192 public void flush() throws Exception {\r
0dc8c589 193 XmlOptions options = new XmlOptions();\r
194\r
195 options.setCharacterEncoding("UTF-8");\r
196 options.setSavePrettyPrint();\r
197 options.setSavePrettyPrintIndent(2);\r
198 options.setUseDefaultNamespace();\r
49324055 199\r
27e0221a 200 BufferedWriter bw = new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + mi.modulename + ".msa"));\r
201 fulfillMsadoc().save(bw, options);\r
202 //MsaTreeEditor.init(mi, ui, msadoc);\r
203 bw.flush();\r
204 bw.close();\r
205 }\r
6f55be9f 206\r
27e0221a 207 private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {\r
6f55be9f 208 XmlOptions options = new XmlOptions();\r
209\r
210 options.setCharacterEncoding("UTF-8");\r
211 options.setSavePrettyPrint();\r
212 options.setSavePrettyPrintIndent(2);\r
213 options.setUseDefaultNamespace();\r
214\r
27e0221a 215 BufferedWriter bw = new BufferedWriter(new FileWriter(path));\r
216 msadoc.save(bw, options);\r
217 bw.flush();\r
218 bw.close();\r
219 }\r
6ef5feb5 220\r
27e0221a 221 public static final void parse(String msafile) throws Exception {\r
222 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);\r
223 flush("c:\\temp.msa", msadoc);\r
224 }\r
0dc8c589 225}\r