a8e26c573f590814dbfd06a0639dd4e768ddcf76
[mirror_edk2.git] / Tools / Java / Source / MigrationTools / org / tianocore / migration / MsaWriter.java
1 /** @file
2
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
8
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.
11
12 **/
13 package org.tianocore.migration;
14
15 import java.io.*;
16 import java.util.*;
17
18 import org.tianocore.*;
19 import org.tianocore.SupportedArchitectures.Enum;
20 import org.apache.xmlbeans.*;
21
22 public class MsaWriter {
23 MsaWriter(ModuleInfo moduleinfo) {
24 mi = moduleinfo;
25 }
26
27 private ModuleInfo mi;
28 private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();
29
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();
40
41 private String Query (String requirement) throws Exception {
42 String answer;
43 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
44 System.out.println(requirement);
45 while ((answer = rd.readLine()).length() == 0) ;
46 return answer;
47 }
48
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);
54
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);
71 }
72 }
73 private void addWrapper() {
74 XmlCursor cursor = msa.newCursor();
75 String uri = "http://www.TianoCore.org/2006/Edk2.0";
76 cursor.push();
77 cursor.toNextToken();
78 cursor.insertNamespace("", uri);
79 cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
80 cursor.pop();
81 msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)cursor.getObject();
82 }
83 private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {
84 Iterator<String> it;
85 String temp;
86
87 if (mi.modulename != null) {
88 msaheader.setModuleName(mi.modulename);
89 } else {
90 msaheader.setModuleName(mi.modulename = Query("Module Name Not Found! Please Input ModuleName"));
91 }
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!");
95 }
96 msaheader.setGuidValue(mi.guidvalue);
97 if (mi.moduletype != null) {
98 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.getModuleType()));
99 } else {
100 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("Guid Value Not Found! Please Input Guid Value")));
101 }
102
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!");
107
108 if (mi.license == null) {
109 mi.license = "FIX ME!";
110 MigrationTool.ui.println ("Fail to extract license info in inf file");
111 }
112 msaheader.addNewLicense().setStringValue(mi.license);
113 msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");
114
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);
123 //
124 // For now, simply add all package guids in the database.
125 //
126 it = MigrationTool.db.dumpAllPkgGuid();
127 while (it.hasNext()) {
128 pd.addNewPackage().setPackageGuid(it.next());
129 }
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");
137 } else {
138 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
139 }
140 }
141
142 it = mi.localmodulesources.iterator();
143 while (it.hasNext()) {
144 addSourceFiles(it.next());
145 }
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);
154 }
155 }
156 }
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);
165 }
166 }
167 }
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);
176 }
177 }
178 }
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();
183 lc.setKeyword(temp);
184 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
185 }
186 }
187 addWrapper();
188 msadoc.setModuleSurfaceArea(msa);
189 return msadoc;
190 }
191
192 public void flush() throws Exception {
193 XmlOptions options = new XmlOptions();
194
195 options.setCharacterEncoding("UTF-8");
196 options.setSavePrettyPrint();
197 options.setSavePrettyPrintIndent(2);
198 options.setUseDefaultNamespace();
199
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);
203 bw.flush();
204 bw.close();
205 }
206
207 private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {
208 XmlOptions options = new XmlOptions();
209
210 options.setCharacterEncoding("UTF-8");
211 options.setSavePrettyPrint();
212 options.setSavePrettyPrintIndent(2);
213 options.setUseDefaultNamespace();
214
215 BufferedWriter bw = new BufferedWriter(new FileWriter(path));
216 msadoc.save(bw, options);
217 bw.flush();
218 bw.close();
219 }
220
221 public static final void parse(String msafile) throws Exception {
222 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);
223 flush("c:\\temp.msa", msadoc);
224 }
225 }