MsaOwner.java & ModuleInfo.java
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / MsaOwner.java
1 package org.tianocore.migration;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.util.*;
6
7 import org.apache.xmlbeans.XmlOptions;
8 import org.tianocore.*;
9 import org.tianocore.SupportedArchitectures.Enum;
10
11 public class MsaOwner {
12 public static final String COPYRIGHT = "Copyright (c) 2006, Intel Corporation";
13 public static final String VERSION = "1.0";
14 public static final String ABSTRACT = "Component name for module ";
15 public static final String DESCRIPTION = "FIX ME!";
16 public static final String LICENSE = "All rights reserved.\n" +
17 " This software and associated documentation (if any) is furnished\n" +
18 " under a license and may only be used or copied in accordance\n" +
19 " with the terms of the license. Except as permitted by such\n" +
20 " license, no part of this software or documentation may be\n" +
21 " reproduced, stored in a retrieval system, or transmitted in any\n" +
22 " form or by any means without the express written consent of\n" +
23 " Intel Corporation.";
24 public static final String SPECIFICATION = "FRAMEWORK_BUILD_PACKAGING_SPECIFICATION 0x00000052";
25
26 public static final Enum IA32 = SupportedArchitectures.IA_32;
27 public static final Enum X64 = SupportedArchitectures.X_64;
28 public static final Enum IPF = SupportedArchitectures.IPF;
29 public static final Enum EBC = SupportedArchitectures.EBC;
30
31 private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance();
32
33 private ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = null;
34 private MsaHeaderDocument.MsaHeader msaheader = null;
35 private LicenseDocument.License license = null;
36 private ModuleDefinitionsDocument.ModuleDefinitions moduledefinitions = null;
37 private SourceFilesDocument.SourceFiles sourcefiles = null; //found local .h files are not written
38 private GuidsDocument.Guids guids = null;
39 private ProtocolsDocument.Protocols protocols = null;
40 private PPIsDocument.PPIs ppis = null;
41 private PackageDependenciesDocument.PackageDependencies packagedependencies = null;
42 private LibraryClassDefinitionsDocument.LibraryClassDefinitions libclassdefs = null;
43 private ExternsDocument.Externs externs = null;
44
45 private List<Enum> listarch = new ArrayList<Enum>();
46 //private Map<String, Enum> mapfilenames = new HashMap<String, Enum>(); //this need to be installed manually when msa is to be written
47 //private Map<String, UsageTypes.Enum> mapprotocols = new HashMap<String, UsageTypes.Enum>();
48
49 //-----------------------------msaheader-------------------------------------//
50
51 public final boolean addLibraryClass (String name, UsageTypes.Enum usage) {
52 Iterator<LibraryClassDocument.LibraryClass> classit = libclassdefs.getLibraryClassList().iterator();
53 while (classit.hasNext()) {
54 if (classit.next().getKeyword() == name) {
55 MigrationTool.ui.println ("Warning: Duplicate LibraryClass");
56 return false;
57 }
58 }
59
60 LibraryClassDocument.LibraryClass classname;
61 List<UsageTypes.Enum> arch = new ArrayList<UsageTypes.Enum>();
62 classname = libclassdefs.addNewLibraryClass();
63 classname.setKeyword(name);
64 arch.add(usage);
65 classname.setSupArchList(arch);
66 return true;
67 }
68
69 public final boolean addGuid (String guidname, UsageTypes.Enum usage) {
70 if (guids == null) {
71 guids = msa.addNewGuids();
72 }
73
74 Iterator<GuidsDocument.Guids.GuidCNames> guidit = guids.getGuidCNamesList().iterator();
75 while (guidit.hasNext()) {
76 if (guidit.next().getGuidCName() == guidname) {
77 MigrationTool.ui.println ("Warning: Duplicate Guid");
78 return false;
79 }
80 }
81
82 GuidsDocument.Guids.GuidCNames guid;
83 List<UsageTypes.Enum> arch = new ArrayList<UsageTypes.Enum>();
84 guid = guids.addNewGuidCNames();
85 guid.setGuidCName(guidname);
86 arch.add(usage);
87 guid.setSupArchList(arch);
88 return true;
89 }
90
91
92 public final boolean addPpi (String ppiname, UsageTypes.Enum usage) {
93 if (ppis == null) {
94 ppis = msa.addNewPPIs();
95 }
96
97 Iterator<PPIsDocument.PPIs.Ppi> ppiit = ppis.getPpiList().iterator();
98 while (ppiit.hasNext()) {
99 if (ppiit.next().getPpiCName() == ppiname) {
100 MigrationTool.ui.println ("Warning: Duplicate Ppi");
101 return false;
102 }
103 }
104
105 PPIsDocument.PPIs.Ppi ppi;
106 List<UsageTypes.Enum> arch = new ArrayList<UsageTypes.Enum>();
107 ppi = ppis.addNewPpi();
108 ppi.setPpiCName(ppiname);
109 arch.add(usage);
110 ppi.setSupArchList(arch);
111 return true;
112 }
113
114 /*
115 private final boolean installProtocols () {
116 if (mapprotocols.isEmpty()) {
117 return false;
118 }
119 Set<String> setprotocols = mapprotocols.keySet();
120 ProtocolsDocument.Protocols.Protocol protocol;
121 Iterator<String> it = setprotocols.iterator();
122 while (it.hasNext()) {
123 protocol = protocols.addNewProtocol();
124 protocol.setProtocolCName(it.next());
125 protocol.setUsage(mapprotocols.get(protocol.getProtocolCName()));
126 }
127 return true;
128 }
129
130 public final boolean addProtocols (String protocol, UsageTypes.Enum usage) {
131 if (mapprotocols.containsKey(protocol)) {
132 return false;
133 } else {
134 mapprotocols.put(protocol, usage);
135 return true;
136 }
137 }
138 */
139 public final boolean addProtocol (String proname, UsageTypes.Enum usage) {
140 if (protocols == null) {
141 protocols = msa.addNewProtocols();
142 }
143
144 Iterator<ProtocolsDocument.Protocols.Protocol> proit = protocols.getProtocolList().iterator();
145 while (proit.hasNext()) {
146 if (proit.next().getProtocolCName() == proname) {
147 MigrationTool.ui.println ("Warning: Duplicate Protocol");
148 return false;
149 }
150 }
151
152 ProtocolsDocument.Protocols.Protocol protocol;
153 List<UsageTypes.Enum> arch = new ArrayList<UsageTypes.Enum>();
154 protocol = protocols.addNewProtocol();
155 protocol.setProtocolCName(proname);
156 arch.add(usage);
157 protocol.setSupArchList(arch);
158 return true;
159 }
160
161 /*
162 private final boolean installHashFilename () {
163 if (mapfilenames.isEmpty()) {
164 return false;
165 }
166 Set<String> setfilename = mapfilenames.keySet();
167 FilenameDocument.Filename filename;
168 List<Enum> arch = new ArrayList<Enum>();
169 Iterator<String> it = setfilename.iterator();
170 while (it.hasNext()) {
171 filename = sourcefiles.addNewFilename();
172 filename.setStringValue(it.next());
173 arch.add(mapfilenames.get(filename.getStringValue()));
174 filename.setSupArchList(arch);
175 }
176 return true;
177 }
178
179 public final boolean addSourceFile (String filename, Enum arch) { // dummy & null how to imply?
180 if (mapfilenames.containsKey(filename)) {
181 return false;
182 } else {
183 mapfilenames.put(filename, arch);
184 return true;
185 }
186 }
187 */
188 public final boolean addSourceFile (String name, Enum en) {
189 Iterator<FilenameDocument.Filename> fileit = sourcefiles.getFilenameList().iterator();
190 while (fileit.hasNext()) {
191 if (fileit.next().getStringValue() == name) {
192 MigrationTool.ui.println ("Warning: Duplicate SourceFileName");
193 return false;
194 }
195 }
196
197 FilenameDocument.Filename filename;
198 List<Enum> arch = new ArrayList<Enum>();
199 filename = sourcefiles.addNewFilename();
200 filename.setStringValue(name);
201 arch.add(en);
202 filename.setSupArchList(arch);
203 return true;
204 }
205
206 // entry point todo
207
208 public final boolean setupExternSpecification () {
209 addExternSpecification("EFI_SPECIFICATION_VERSION 0x00020000");
210 addExternSpecification("EDK_RELEASE_VERSION 0x00020000");
211 return true;
212 }
213
214 public final boolean addExternSpecification (String specification) {
215 if (externs.getSpecificationList().contains(specification)) {
216 return false;
217 } else {
218 externs.addSpecification(specification);
219 return true;
220 }
221 }
222
223 public final boolean setupPackageDependencies() {
224 addPackage("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
225 addPackage("68169ab0-d41b-4009-9060-292c253ac43d");
226 return true;
227 }
228
229 public final boolean addPackage (String guid) {
230 if (packagedependencies.getPackageList().contains(guid)) {
231 return false;
232 } else {
233 packagedependencies.addNewPackage().setPackageGuid(guid);
234 return true;
235 }
236 }
237
238 public final boolean setupModuleDefinitions () { //????????? give this job to moduleinfo
239 moduledefinitions.setBinaryModule(false);
240 moduledefinitions.setOutputFileBasename(msaheader.getModuleName());
241 return true;
242 }
243 public final boolean addSupportedArchitectures (Enum arch) {
244 if (listarch.contains(arch)) {
245 return false;
246 } else {
247 listarch.add(arch);
248 return true;
249 }
250 }
251
252 public final boolean addSpecification (String specification) {
253 if (msaheader.getSpecification() == null) {
254 if (specification == null) {
255 msaheader.setSpecification(SPECIFICATION);
256 } else {
257 msaheader.setSpecification(specification);
258 }
259 return true;
260 } else {
261 MigrationTool.ui.println ("Warning: Duplicate Specification");
262 return false;
263 }
264 }
265
266 public final boolean addLicense (String licensecontent) {
267 if (msaheader.getLicense() == null) {
268 license = msaheader.addNewLicense();
269 if (licensecontent == null) {
270 license.setStringValue(LICENSE);
271 } else {
272 license.setStringValue(licensecontent);
273 }
274 return true;
275 } else {
276 MigrationTool.ui.println ("Warning: Duplicate License");
277 return false;
278 }
279 }
280
281 public final boolean addDescription (String description) {
282 if (msaheader.getDescription() == null) {
283 if (description == null) {
284 msaheader.setDescription(DESCRIPTION);
285 } else {
286 msaheader.setDescription(description);
287 }
288 return true;
289 } else {
290 MigrationTool.ui.println ("Warning: Duplicate Description");
291 return false;
292 }
293 }
294
295 public final boolean addAbstract (String abs) {
296 if (msaheader.getAbstract() == null) {
297 if (abs == null) {
298 msaheader.setAbstract(ABSTRACT + msaheader.getModuleName());
299 } else {
300 msaheader.setVersion(abs);
301 }
302 return true;
303 } else {
304 MigrationTool.ui.println ("Warning: Duplicate Abstract");
305 return false;
306 }
307 }
308
309 public final boolean addVersion (String version) {
310 if (msaheader.getVersion() == null) {
311 if (version == null) {
312 msaheader.setVersion(VERSION);
313 } else {
314 msaheader.setVersion(version);
315 }
316 return true;
317 } else {
318 MigrationTool.ui.println ("Warning: Duplicate Version");
319 return false;
320 }
321 }
322
323 public final boolean addCopyRight (String copyright) {
324 if (msaheader.getCopyright() == null) {
325 if (copyright == null) {
326 msaheader.setCopyright(COPYRIGHT);
327 } else {
328 msaheader.setCopyright(copyright);
329 }
330 return true;
331 } else {
332 MigrationTool.ui.println ("Warning: Duplicate CopyRight");
333 return false;
334 }
335 }
336
337 public final boolean addModuleType (String moduletype) {
338 if (msaheader.getModuleType() == null) {
339 msaheader.setModuleType(ModuleTypeDef.Enum.forString(moduletype));
340 return true;
341 } else {
342 MigrationTool.ui.println ("Warning: Duplicate ModuleType");
343 return false;
344 }
345 }
346
347 public final boolean addGuidValue (String guidvalue) {
348 if (msaheader.getGuidValue() == null) {
349 msaheader.setGuidValue(guidvalue);
350 return true;
351 } else {
352 MigrationTool.ui.println ("Warning: Duplicate GuidValue");
353 return false;
354 }
355 }
356
357 public final boolean addModuleName (String modulename) {
358 if (msaheader.getModuleName() == null) {
359 msaheader.setModuleName(modulename);
360 return true;
361 } else {
362 MigrationTool.ui.println ("Warning: Duplicate ModuleName");
363 return false;
364 }
365 }
366 //-----------------------------msaheader-------------------------------------//
367
368 public final void flush(String outputpath) throws Exception {
369 XmlOptions options = new XmlOptions();
370
371 options.setCharacterEncoding("UTF-8");
372 options.setSavePrettyPrint();
373 options.setSavePrettyPrintIndent(2);
374 options.setUseDefaultNamespace();
375
376 BufferedWriter bw = new BufferedWriter(new FileWriter(outputpath));
377 msadoc.save(bw, options);
378 bw.flush();
379 bw.close();
380 }
381
382 private final MsaOwner init () {
383 msa = msadoc.addNewModuleSurfaceArea();
384 msaheader = msa.addNewMsaHeader();
385 moduledefinitions = msa.addNewModuleDefinitions();
386 moduledefinitions.setSupportedArchitectures(listarch);
387
388 sourcefiles = msa.addNewSourceFiles();
389 packagedependencies = msa.addNewPackageDependencies();
390 libclassdefs = msa.addNewLibraryClassDefinitions();
391 externs = msa.addNewExterns();
392 return this;
393 }
394
395 public static final MsaOwner initNewMsaOwner() {
396 return new MsaOwner().init();
397 }
398 }