enhance MsaOwner.java
[mirror_edk2.git] / Tools / 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
6ef5feb5 23 MsaWriter(ModuleInfo moduleinfo) {\r
24 mi = moduleinfo;\r
6ef5feb5 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
73 \r
74 private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {\r
75 Iterator<String> it;\r
76 String temp;\r
77 \r
78 if (mi.modulename != null) {\r
79 msaheader.setModuleName(mi.modulename);\r
80 } else {\r
81 msaheader.setModuleName(mi.modulename = Query("Module Name Not Found! Please Input ModuleName"));\r
82 }\r
83 if (mi.guidvalue == null) {\r
84 mi.guidvalue = UUID.randomUUID().toString();\r
85 MigrationTool.ui.println ("Guid value can not be retrieved from inf file. Generate " + mi.guidvalue + " at random!"); \r
86 } \r
87 msaheader.setGuidValue(mi.guidvalue);\r
88 if (mi.moduletype != null) {\r
89 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.getModuleType()));\r
27e0221a 90 } else {\r
91 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("Guid Value Not Found! Please Input Guid Value")));\r
92 }\r
fb840aa8 93\r
27e0221a 94 msaheader.setCopyright("Copyright (c) 2006, Intel Corporation");\r
95 msaheader.setVersion("1.0");\r
96 msaheader.setAbstract("Component name for module " + mi.modulename);\r
97 msaheader.setDescription("FIX ME!");\r
98 msaheader.addNewLicense().setStringValue("All rights reserved.\n" +\r
99 " This software and associated documentation (if any) is furnished\n" +\r
100 " under a license and may only be used or copied in accordance\n" +\r
101 " with the terms of the license. Except as permitted by such\n" +\r
102 " license, no part of this software or documentation may be\n" +\r
103 " reproduced, stored in a retrieval system, or transmitted in any\n" +\r
104 " form or by any means without the express written consent of\n" +\r
105 " Intel Corporation.");\r
106 msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");\r
107 \r
108 List<Enum> arch = new ArrayList<Enum>();\r
109 arch.add(SupportedArchitectures.IA_32);\r
110 arch.add(SupportedArchitectures.X_64);\r
111 arch.add(SupportedArchitectures.IPF);\r
112 arch.add(SupportedArchitectures.EBC);\r
113 md.setSupportedArchitectures(arch);\r
114 md.setBinaryModule(false);\r
115 md.setOutputFileBasename(mi.modulename);\r
116 \r
117 pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");\r
77dd49a7 118 pd.addNewPackage().setPackageGuid("68169ab0-d41b-4009-9060-292c253ac43d");\r
27e0221a 119 externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");\r
120 externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");\r
37e5cd65 121 if (mi.entrypoint != null) {\r
fd16b4dd 122 externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);\r
123 org.tianocore.ModuleTypeDef.Enum moduleType = msaheader.getModuleType();\r
124 if (moduleType == ModuleTypeDef.PEIM) {\r
125 mi.hashrequiredr9libs.add("PeimEntryPoint");\r
126 } else {\r
127 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");\r
128 }\r
37e5cd65 129 }\r
130 \r
27e0221a 131 it = mi.localmodulesources.iterator();\r
132 while (it.hasNext()) {\r
133 addSourceFiles(it.next());\r
134 }\r
135 if (!mi.protocol.isEmpty()) {\r
136 protocols = msa.addNewProtocols();\r
137 it = mi.protocol.iterator();\r
138 while (it.hasNext()) {\r
139 if ((temp = it.next()) != null) {\r
140 ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();\r
141 pr.setProtocolCName(temp);\r
142 pr.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
143 }\r
144 }\r
145 }\r
146 if (!mi.ppi.isEmpty()) {\r
147 ppis = msa.addNewPPIs();\r
148 it = mi.ppi.iterator();\r
149 while (it.hasNext()) {\r
150 if ((temp = it.next()) != null) {\r
151 PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();\r
152 pp.setPpiCName(temp);\r
153 pp.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
154 }\r
155 }\r
156 }\r
157 if (!mi.guid.isEmpty()) {\r
158 guids = msa.addNewGuids();\r
159 it = mi.guid.iterator();\r
160 while (it.hasNext()) {\r
161 if ((temp = it.next()) != null) {\r
162 GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();\r
163 gcn.setGuidCName(temp);\r
164 gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
165 }\r
166 }\r
167 }\r
168 it = mi.hashrequiredr9libs.iterator();\r
169 while (it.hasNext()) {\r
55bfa0f1 170 if ((temp = it.next()) != null && !temp.matches("%") && !temp.matches("n/a")) {\r
27e0221a 171 LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();\r
172 lc.setKeyword(temp);\r
173 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);\r
174 }\r
175 }\r
176 \r
177 return msadoc;\r
178 }\r
179 \r
180 public void flush() throws Exception {\r
0dc8c589 181 XmlOptions options = new XmlOptions();\r
182\r
183 options.setCharacterEncoding("UTF-8");\r
184 options.setSavePrettyPrint();\r
185 options.setSavePrettyPrintIndent(2);\r
186 options.setUseDefaultNamespace();\r
187 \r
27e0221a 188 BufferedWriter bw = new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + mi.modulename + ".msa"));\r
189 fulfillMsadoc().save(bw, options);\r
190 //MsaTreeEditor.init(mi, ui, msadoc);\r
191 bw.flush();\r
192 bw.close();\r
193 }\r
6f55be9f 194\r
27e0221a 195 private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {\r
6f55be9f 196 XmlOptions options = new XmlOptions();\r
197\r
198 options.setCharacterEncoding("UTF-8");\r
199 options.setSavePrettyPrint();\r
200 options.setSavePrettyPrintIndent(2);\r
201 options.setUseDefaultNamespace();\r
202\r
27e0221a 203 BufferedWriter bw = new BufferedWriter(new FileWriter(path));\r
204 msadoc.save(bw, options);\r
205 bw.flush();\r
206 bw.close();\r
207 }\r
6ef5feb5 208\r
27e0221a 209 public static final void parse(String msafile) throws Exception {\r
210 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);\r
211 flush("c:\\temp.msa", msadoc);\r
212 }\r
0dc8c589 213}\r