af8646f792d0de4b946f7d3a7ffe22021da535c2
[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 pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
125 pd.addNewPackage().setPackageGuid("68169ab0-d41b-4009-9060-292c253ac43d");
126 externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
127 externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
128 if (mi.entrypoint != null) {
129 externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);
130 org.tianocore.ModuleTypeDef.Enum moduleType = msaheader.getModuleType();
131 if (moduleType == ModuleTypeDef.PEIM) {
132 mi.hashrequiredr9libs.add("PeimEntryPoint");
133 } else {
134 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
135 }
136 }
137
138 it = mi.localmodulesources.iterator();
139 while (it.hasNext()) {
140 addSourceFiles(it.next());
141 }
142 if (!mi.protocols.isEmpty()) {
143 protocols = msa.addNewProtocols();
144 it = mi.protocols.iterator();
145 while (it.hasNext()) {
146 if ((temp = it.next()) != null) {
147 ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();
148 pr.setProtocolCName(temp);
149 pr.setUsage(UsageTypes.ALWAYS_CONSUMED);
150 }
151 }
152 }
153 if (!mi.ppis.isEmpty()) {
154 ppis = msa.addNewPPIs();
155 it = mi.ppis.iterator();
156 while (it.hasNext()) {
157 if ((temp = it.next()) != null) {
158 PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();
159 pp.setPpiCName(temp);
160 pp.setUsage(UsageTypes.ALWAYS_CONSUMED);
161 }
162 }
163 }
164 if (!mi.guids.isEmpty()) {
165 guids = msa.addNewGuids();
166 it = mi.guids.iterator();
167 while (it.hasNext()) {
168 if ((temp = it.next()) != null) {
169 GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();
170 gcn.setGuidCName(temp);
171 gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);
172 }
173 }
174 }
175 it = mi.hashrequiredr9libs.iterator();
176 while (it.hasNext()) {
177 if ((temp = it.next()) != null && !temp.matches("%") && !temp.matches("n/a")) {
178 LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();
179 lc.setKeyword(temp);
180 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
181 }
182 }
183 addWrapper();
184 msadoc.setModuleSurfaceArea(msa);
185 return msadoc;
186 }
187
188 public void flush() throws Exception {
189 XmlOptions options = new XmlOptions();
190
191 options.setCharacterEncoding("UTF-8");
192 options.setSavePrettyPrint();
193 options.setSavePrettyPrintIndent(2);
194 options.setUseDefaultNamespace();
195
196 BufferedWriter bw = new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + mi.modulename + ".msa"));
197 fulfillMsadoc().save(bw, options);
198 //MsaTreeEditor.init(mi, ui, msadoc);
199 bw.flush();
200 bw.close();
201 }
202
203 private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {
204 XmlOptions options = new XmlOptions();
205
206 options.setCharacterEncoding("UTF-8");
207 options.setSavePrettyPrint();
208 options.setSavePrettyPrintIndent(2);
209 options.setUseDefaultNamespace();
210
211 BufferedWriter bw = new BufferedWriter(new FileWriter(path));
212 msadoc.save(bw, options);
213 bw.flush();
214 bw.close();
215 }
216
217 public static final void parse(String msafile) throws Exception {
218 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);
219 flush("c:\\temp.msa", msadoc);
220 }
221 }