7d907690e902b51cc9f28631f45a7aef8e3de674
[mirror_edk2.git] / Tools / 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(String path, ModuleInfo moduleinfo, Database database) {
24 modulepath = path;
25 mi = moduleinfo;
26 db = database;
27 }
28
29 private String modulepath;
30 private ModuleInfo mi;
31 private Database db;
32
33 private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();
34
35 private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = msadoc.addNewModuleSurfaceArea();
36 private MsaHeaderDocument.MsaHeader msaheader = msa.addNewMsaHeader();
37 private ModuleDefinitionsDocument.ModuleDefinitions md = msa.addNewModuleDefinitions();
38 private SourceFilesDocument.SourceFiles sourcefiles = msa.addNewSourceFiles(); //found local .h files are not written
39 private GuidsDocument.Guids guids;
40 private ProtocolsDocument.Protocols protocols;
41 private PPIsDocument.PPIs ppis;
42 private PackageDependenciesDocument.PackageDependencies pd = msa.addNewPackageDependencies();
43 private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = msa.addNewLibraryClassDefinitions();
44 private ExternsDocument.Externs externs = msa.addNewExterns();
45
46 private String Query (String requirement) throws Exception {
47 String answer;
48 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
49 System.out.println(requirement);
50 while ((answer = rd.readLine()).length() == 0) ;
51 return answer;
52 }
53
54 private ModuleSurfaceAreaDocument fulfillMsadoc() throws Exception {
55 Iterator<String> it;
56 String temp;
57
58 if (mi.modulename != null) {
59 msaheader.setModuleName(mi.modulename);
60 } else {
61 msaheader.setModuleName(mi.modulename = Query("Module Name Not Found! Please Input ModuleName"));
62 }
63 if (mi.guidvalue != null) {
64 msaheader.setGuidValue(mi.guidvalue);
65 } else {
66 msaheader.setGuidValue(mi.guidvalue = Query("Guid Value Not Found! Please Input Guid Value"));
67 }
68 if (mi.moduletype != null) {
69 if (mi.moduletype.contains("PEI")) {
70 msaheader.setModuleType(ModuleTypeDef.Enum.forString("PEIM"));
71 } else {
72 msaheader.setModuleType(ModuleTypeDef.Enum.forString("DXE_DRIVER"));
73 }
74 } else {
75 msaheader.setModuleType(ModuleTypeDef.Enum.forString(mi.moduletype = Query("Guid Value Not Found! Please Input Guid Value")));
76 }
77
78 msaheader.setCopyright("Copyright (c) 2006, Intel Corporation");
79 msaheader.setVersion("1.0");
80 msaheader.setAbstract("Component name for module " + mi.modulename);
81 msaheader.setDescription("FIX ME!"); //???
82 msaheader.addNewLicense().setStringValue("All rights reserved.\n" +
83 " This software and associated documentation (if any) is furnished\n" +
84 " under a license and may only be used or copied in accordance\n" +
85 " with the terms of the license. Except as permitted by such\n" +
86 " license, no part of this software or documentation may be\n" +
87 " reproduced, stored in a retrieval system, or transmitted in any\n" +
88 " form or by any means without the express written consent of\n" +
89 " Intel Corporation.");
90 msaheader.setSpecification("FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052");
91
92 List<Enum> arch = new ArrayList<Enum>();
93 arch.add(SupportedArchitectures.IA_32);
94 arch.add(SupportedArchitectures.X_64);
95 arch.add(SupportedArchitectures.IPF);
96 arch.add(SupportedArchitectures.EBC);
97 md.setSupportedArchitectures(arch);
98 md.setBinaryModule(false);
99 md.setOutputFileBasename(mi.modulename);
100
101 pd.addNewPackage().setPackageGuid("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
102 externs.addNewSpecification().setStringValue("EFI_SPECIFICATION_VERSION 0x00020000");
103 externs.addNewSpecification().setStringValue("EDK_RELEASE_VERSION 0x00020000");
104 externs.addNewExtern().setModuleEntryPoint(mi.entrypoint);
105
106 it = mi.localmodulesources.iterator();
107 while (it.hasNext()) {
108 sourcefiles.addNewFilename().setStringValue(it.next());
109 }
110 if (!mi.protocol.isEmpty()) {
111 protocols = msa.addNewProtocols();
112 it = mi.protocol.iterator();
113 while (it.hasNext()) {
114 if ((temp = it.next()) != null) {
115 ProtocolsDocument.Protocols.Protocol pr = protocols.addNewProtocol();
116 pr.setProtocolCName(temp);
117 pr.setUsage(UsageTypes.ALWAYS_CONSUMED);
118 }
119 }
120 }
121 if (!mi.ppi.isEmpty()) {
122 ppis = msa.addNewPPIs();
123 it = mi.ppi.iterator();
124 while (it.hasNext()) {
125 if ((temp = it.next()) != null) {
126 PPIsDocument.PPIs.Ppi pp = ppis.addNewPpi();
127 pp.setPpiCName(temp);
128 pp.setUsage(UsageTypes.ALWAYS_CONSUMED);
129 }
130 }
131 }
132 if (!mi.guid.isEmpty()) {
133 guids = msa.addNewGuids();
134 it = mi.guid.iterator();
135 while (it.hasNext()) {
136 if ((temp = it.next()) != null) {
137 GuidsDocument.Guids.GuidCNames gcn = guids.addNewGuidCNames();
138 gcn.setGuidCName(temp);
139 gcn.setUsage(UsageTypes.ALWAYS_CONSUMED);
140 }
141 }
142 }
143 it = mi.hashrequiredr9libs.iterator();
144 while (it.hasNext()) {
145 if ((temp = it.next()) != null && !temp.matches("%")) {
146 LibraryClassDocument.LibraryClass lc = libclassdefs.addNewLibraryClass();
147 lc.setKeyword(temp);
148 lc.setUsage(UsageTypes.ALWAYS_CONSUMED);
149 }
150 }
151
152 return msadoc;
153 }
154
155 public void flush() throws Exception {
156 XmlOptions options = new XmlOptions();
157
158 options.setCharacterEncoding("UTF-8");
159 options.setSavePrettyPrint();
160 options.setSavePrettyPrintIndent(2);
161 options.setUseDefaultNamespace();
162
163 BufferedWriter bw = new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + mi.modulename + ".msa"));
164 fulfillMsadoc().save(bw, options);
165 bw.flush();
166 bw.close();
167 }
168 }