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