MsaOwner usable
[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 classname = libclassdefs.addNewLibraryClass();
62 classname.setKeyword(name);
63 classname.setUsage(usage);
64 return true;
65 }
66
67 public final boolean addGuid (String guidname, UsageTypes.Enum usage) {
68 if (guids == null) {
69 guids = msa.addNewGuids();
70 }
71
72 Iterator<GuidsDocument.Guids.GuidCNames> guidit = guids.getGuidCNamesList().iterator();
73 while (guidit.hasNext()) {
74 if (guidit.next().getGuidCName() == guidname) {
75 MigrationTool.ui.println ("Warning: Duplicate Guid");
76 return false;
77 }
78 }
79
80 GuidsDocument.Guids.GuidCNames guid;
81 guid = guids.addNewGuidCNames();
82 guid.setGuidCName(guidname);
83 guid.setUsage(usage);
84 return true;
85 }
86
87
88 public final boolean addPpi (String ppiname, UsageTypes.Enum usage) {
89 if (ppis == null) {
90 ppis = msa.addNewPPIs();
91 }
92
93 Iterator<PPIsDocument.PPIs.Ppi> ppiit = ppis.getPpiList().iterator();
94 while (ppiit.hasNext()) {
95 if (ppiit.next().getPpiCName() == ppiname) {
96 MigrationTool.ui.println ("Warning: Duplicate Ppi");
97 return false;
98 }
99 }
100
101 PPIsDocument.PPIs.Ppi ppi;
102 ppi = ppis.addNewPpi();
103 ppi.setPpiCName(ppiname);
104 ppi.setUsage(usage);
105 return true;
106 }
107
108 /*
109 private final boolean installProtocols () {
110 if (mapprotocols.isEmpty()) {
111 return false;
112 }
113 Set<String> setprotocols = mapprotocols.keySet();
114 ProtocolsDocument.Protocols.Protocol protocol;
115 Iterator<String> it = setprotocols.iterator();
116 while (it.hasNext()) {
117 protocol = protocols.addNewProtocol();
118 protocol.setProtocolCName(it.next());
119 protocol.setUsage(mapprotocols.get(protocol.getProtocolCName()));
120 }
121 return true;
122 }
123
124 public final boolean addProtocols (String protocol, UsageTypes.Enum usage) {
125 if (mapprotocols.containsKey(protocol)) {
126 return false;
127 } else {
128 mapprotocols.put(protocol, usage);
129 return true;
130 }
131 }
132 */
133 public final boolean addProtocol (String proname, UsageTypes.Enum usage) {
134 if (protocols == null) {
135 protocols = msa.addNewProtocols();
136 }
137
138 Iterator<ProtocolsDocument.Protocols.Protocol> proit = protocols.getProtocolList().iterator();
139 while (proit.hasNext()) {
140 if (proit.next().getProtocolCName() == proname) {
141 MigrationTool.ui.println ("Warning: Duplicate Protocol");
142 return false;
143 }
144 }
145
146 ProtocolsDocument.Protocols.Protocol protocol;
147 protocol = protocols.addNewProtocol();
148 protocol.setProtocolCName(proname);
149 protocol.setUsage(usage);
150 return true;
151 }
152
153 /*
154 private final boolean installHashFilename () {
155 if (mapfilenames.isEmpty()) {
156 return false;
157 }
158 Set<String> setfilename = mapfilenames.keySet();
159 FilenameDocument.Filename filename;
160 List<Enum> arch = new ArrayList<Enum>();
161 Iterator<String> it = setfilename.iterator();
162 while (it.hasNext()) {
163 filename = sourcefiles.addNewFilename();
164 filename.setStringValue(it.next());
165 arch.add(mapfilenames.get(filename.getStringValue()));
166 filename.setSupArchList(arch);
167 }
168 return true;
169 }
170
171 public final boolean addSourceFile (String filename, Enum arch) { // dummy & null how to imply?
172 if (mapfilenames.containsKey(filename)) {
173 return false;
174 } else {
175 mapfilenames.put(filename, arch);
176 return true;
177 }
178 }
179 */
180 public final boolean addSourceFile (String name, Enum en) {
181 Iterator<FilenameDocument.Filename> fileit = sourcefiles.getFilenameList().iterator();
182 while (fileit.hasNext()) {
183 if (fileit.next().getStringValue() == name) {
184 MigrationTool.ui.println ("Warning: Duplicate SourceFileName");
185 return false;
186 }
187 }
188
189 FilenameDocument.Filename filename;
190 List<Enum> arch = new ArrayList<Enum>();
191 filename = sourcefiles.addNewFilename();
192 filename.setStringValue(name);
193 arch.add(en);
194 filename.setSupArchList(arch);
195 return true;
196 }
197
198 // entry point todo
199
200 public final boolean setupExternSpecification () {
201 addExternSpecification("EFI_SPECIFICATION_VERSION 0x00020000");
202 addExternSpecification("EDK_RELEASE_VERSION 0x00020000");
203 return true;
204 }
205
206 public final boolean addExternSpecification (String specification) {
207 if (externs.getSpecificationList().contains(specification)) {
208 return false;
209 } else {
210 externs.addSpecification(specification);
211 return true;
212 }
213 }
214
215 public final boolean setupPackageDependencies() {
216 addPackage("5e0e9358-46b6-4ae2-8218-4ab8b9bbdcec");
217 addPackage("68169ab0-d41b-4009-9060-292c253ac43d");
218 return true;
219 }
220
221 public final boolean addPackage (String guid) {
222 if (packagedependencies.getPackageList().contains(guid)) {
223 return false;
224 } else {
225 packagedependencies.addNewPackage().setPackageGuid(guid);
226 return true;
227 }
228 }
229
230 public final boolean setupModuleDefinitions () { //????????? give this job to moduleinfo
231 moduledefinitions.setBinaryModule(false);
232 moduledefinitions.setOutputFileBasename(msaheader.getModuleName());
233 return true;
234 }
235 public final boolean addSupportedArchitectures (Enum arch) {
236 if (listarch.contains(arch)) {
237 return false;
238 } else {
239 listarch.add(arch);
240 return true;
241 }
242 }
243
244 public final boolean addSpecification (String specification) {
245 if (msaheader.getSpecification() == null) {
246 if (specification == null) {
247 msaheader.setSpecification(SPECIFICATION);
248 } else {
249 msaheader.setSpecification(specification);
250 }
251 return true;
252 } else {
253 MigrationTool.ui.println ("Warning: Duplicate Specification");
254 return false;
255 }
256 }
257
258 public final boolean addLicense (String licensecontent) {
259 if (msaheader.getLicense() == null) {
260 license = msaheader.addNewLicense();
261 if (licensecontent == null) {
262 license.setStringValue(LICENSE);
263 } else {
264 license.setStringValue(licensecontent);
265 }
266 return true;
267 } else {
268 MigrationTool.ui.println ("Warning: Duplicate License");
269 return false;
270 }
271 }
272
273 public final boolean addDescription (String description) {
274 if (msaheader.getDescription() == null) {
275 if (description == null) {
276 msaheader.setDescription(DESCRIPTION);
277 } else {
278 msaheader.setDescription(description);
279 }
280 return true;
281 } else {
282 MigrationTool.ui.println ("Warning: Duplicate Description");
283 return false;
284 }
285 }
286
287 public final boolean addAbstract (String abs) {
288 if (msaheader.getAbstract() == null) {
289 if (abs == null) {
290 msaheader.setAbstract(ABSTRACT + msaheader.getModuleName());
291 } else {
292 msaheader.setVersion(abs);
293 }
294 return true;
295 } else {
296 MigrationTool.ui.println ("Warning: Duplicate Abstract");
297 return false;
298 }
299 }
300
301 public final boolean addVersion (String version) {
302 if (msaheader.getVersion() == null) {
303 if (version == null) {
304 msaheader.setVersion(VERSION);
305 } else {
306 msaheader.setVersion(version);
307 }
308 return true;
309 } else {
310 MigrationTool.ui.println ("Warning: Duplicate Version");
311 return false;
312 }
313 }
314
315 public final boolean addCopyRight (String copyright) {
316 if (msaheader.getCopyright() == null) {
317 if (copyright == null) {
318 msaheader.setCopyright(COPYRIGHT);
319 } else {
320 msaheader.setCopyright(copyright);
321 }
322 return true;
323 } else {
324 MigrationTool.ui.println ("Warning: Duplicate CopyRight");
325 return false;
326 }
327 }
328
329 public final boolean addModuleType (String moduletype) {
330 if (msaheader.getModuleType() == null) {
331 msaheader.setModuleType(ModuleTypeDef.Enum.forString(moduletype));
332 return true;
333 } else {
334 MigrationTool.ui.println ("Warning: Duplicate ModuleType");
335 return false;
336 }
337 }
338
339 public final boolean addGuidValue (String guidvalue) {
340 if (msaheader.getGuidValue() == null) {
341 msaheader.setGuidValue(guidvalue);
342 return true;
343 } else {
344 MigrationTool.ui.println ("Warning: Duplicate GuidValue");
345 return false;
346 }
347 }
348
349 public final boolean addModuleName (String modulename) {
350 if (msaheader.getModuleName() == null) {
351 msaheader.setModuleName(modulename);
352 return true;
353 } else {
354 MigrationTool.ui.println ("Warning: Duplicate ModuleName");
355 return false;
356 }
357 }
358 //-----------------------------msaheader-------------------------------------//
359
360 public final void flush(String outputpath) throws Exception {
361 XmlOptions options = new XmlOptions();
362
363 options.setCharacterEncoding("UTF-8");
364 options.setSavePrettyPrint();
365 options.setSavePrettyPrintIndent(2);
366 options.setUseDefaultNamespace();
367
368 BufferedWriter bw = new BufferedWriter(new FileWriter(outputpath));
369 msadoc.save(bw, options);
370 bw.flush();
371 bw.close();
372 }
373
374 private final MsaOwner init () {
375 msa = msadoc.addNewModuleSurfaceArea();
376 msaheader = msa.addNewMsaHeader();
377 moduledefinitions = msa.addNewModuleDefinitions();
378 moduledefinitions.setSupportedArchitectures(listarch);
379
380 sourcefiles = msa.addNewSourceFiles();
381 packagedependencies = msa.addNewPackageDependencies();
382 libclassdefs = msa.addNewLibraryClassDefinitions();
383 externs = msa.addNewExterns();
384 return this;
385 }
386
387 public static final MsaOwner initNewMsaOwner() {
388 return new MsaOwner().init();
389 }
390 }