Fix several bugs & enhancement.
[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");
104 msaheader.setVersion("1.0");
105 msaheader.setAbstract("Component name for module " + mi.modulename);
106 msaheader.setDescription("FIX ME!");
107 msaheader.addNewLicense().setStringValue("All rights reserved.\n" +
108 " This software and associated documentation (if any) is furnished\n" +
109 " under a license and may only be used or copied in accordance\n" +
110 " with the terms of the license. Except as permitted by such\n" +
111 " license, no part of this software or documentation may be\n" +
112 " reproduced, stored in a retrieval system, or transmitted in any\n" +
113 " form or by any means without the express written consent of\n" +
114 " Intel Corporation.");
115 msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");
116
117 List<Enum> arch = new ArrayList<Enum>();
118 arch.add(SupportedArchitectures.IA_32);
119 arch.add(SupportedArchitectures.X_64);
120 arch.add(SupportedArchitectures.IPF);
121 arch.add(SupportedArchitectures.EBC);
122 md.setSupportedArchitectures(arch);
123 md.setBinaryModule(false);
124 md.setOutputFileBasename(mi.modulename);
125
126 pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
127 pd.addNewPackage().setPackageGuid("68169ab0-d41b-4009-9060-292c253ac43d");
128 externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
129 externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
130 if (mi.entrypoint != null) {
131 externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);
132 org.tianocore.ModuleTypeDef.Enum moduleType = msaheader.getModuleType();
133 if (moduleType == ModuleTypeDef.PEIM) {
134 mi.hashrequiredr9libs.add("PeimEntryPoint");
135 } else {
136 mi.hashrequiredr9libs.add("UefiDriverEntryPoint");
137 }
138 }
139
140 it = mi.localmodulesources.iterator();
141 while (it.hasNext()) {
142 addSourceFiles(it.next());
143 }
144 if (!mi.protocols.isEmpty()) {
145 protocols = msa.addNewProtocols();
146 it = mi.protocols.iterator();
147 while (it.hasNext()) {
148 if ((temp = it.next()) != null) {
149 ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();
150 pr.setProtocolCName(temp);
151 pr.setUsage(UsageTypes.ALWAYS_CONSUMED);
152 }
153 }
154 }
155 if (!mi.ppis.isEmpty()) {
156 ppis = msa.addNewPPIs();
157 it = mi.ppis.iterator();
158 while (it.hasNext()) {
159 if ((temp = it.next()) != null) {
160 PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();
161 pp.setPpiCName(temp);
162 pp.setUsage(UsageTypes.ALWAYS_CONSUMED);
163 }
164 }
165 }
166 if (!mi.guids.isEmpty()) {
167 guids = msa.addNewGuids();
168 it = mi.guids.iterator();
169 while (it.hasNext()) {
170 if ((temp = it.next()) != null) {
171 GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();
172 gcn.setGuidCName(temp);
173 gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);
174 }
175 }
176 }
177 it = mi.hashrequiredr9libs.iterator();
178 while (it.hasNext()) {
179 if ((temp = it.next()) != null && !temp.matches("%") && !temp.matches("n/a")) {
180 LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();
181 lc.setKeyword(temp);
182 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
183 }
184 }
185 addWrapper();
186 msadoc.setModuleSurfaceArea(msa);
187 return msadoc;
188 }
189
190 public void flush() throws Exception {
191 XmlOptions options = new XmlOptions();
192
193 options.setCharacterEncoding("UTF-8");
194 options.setSavePrettyPrint();
195 options.setSavePrettyPrintIndent(2);
196 options.setUseDefaultNamespace();
197
198 BufferedWriter bw = new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + mi.modulename + ".msa"));
199 fulfillMsadoc().save(bw, options);
200 //MsaTreeEditor.init(mi, ui, msadoc);
201 bw.flush();
202 bw.close();
203 }
204
205 private static void flush(String path, ModuleSurfaceAreaDocument msadoc) throws Exception {
206 XmlOptions options = new XmlOptions();
207
208 options.setCharacterEncoding("UTF-8");
209 options.setSavePrettyPrint();
210 options.setSavePrettyPrintIndent(2);
211 options.setUseDefaultNamespace();
212
213 BufferedWriter bw = new BufferedWriter(new FileWriter(path));
214 msadoc.save(bw, options);
215 bw.flush();
216 bw.close();
217 }
218
219 public static final void parse(String msafile) throws Exception {
220 ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.parse(msafile);
221 flush("c:\\temp.msa", msadoc);
222 }
223 }