]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/SaveFile.java
1. Fix bug for wrong Variable name in msa file
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / SaveFile.java
1 /** @file
2
3 The file provides interface to save xml file.
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15 package org.tianocore.frameworkwizard.common;
16
17 import java.io.File;
18
19 import org.apache.xmlbeans.XmlCursor;
20 import org.apache.xmlbeans.XmlObject;
21 import org.apache.xmlbeans.XmlOptions;
22 import org.tianocore.FrameworkDatabaseDocument;
23 import org.tianocore.ModuleSurfaceAreaDocument;
24 import org.tianocore.PackageSurfaceAreaDocument;
25 import org.tianocore.PlatformSurfaceAreaDocument;
26 import org.tianocore.FrameworkDatabaseDocument.FrameworkDatabase;
27 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
28 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
29 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
30
31 public class SaveFile {
32
33 /**
34
35 @param args
36
37 **/
38 public static void main(String[] args) {
39
40 }
41
42 private static void createDirectory(String path) throws Exception {
43 File f = new File(path);
44 path = f.getParent();
45 FileOperation.newFolder(path);
46 }
47
48 /**
49 Save file as msa
50
51 **/
52 public static void saveMsaFile(String path, ModuleSurfaceArea msa) throws Exception {
53 //
54 // Create the file's directory first
55 //
56 createDirectory(path);
57
58 //
59 // Remove all empty top level elements
60 //
61 XmlObject o = msa.getLibraryClassDefinitions();
62 if (o != null) {
63 if (msa.getLibraryClassDefinitions().getLibraryClassList().size() <= 0) {
64 XmlCursor xmlCursor = o.newCursor();
65 xmlCursor.removeXml();
66 xmlCursor.dispose();
67 }
68 }
69
70 o = msa.getSourceFiles();
71 if (o != null) {
72 if (msa.getSourceFiles().getFilenameList().size() <= 0) {
73 XmlCursor xmlCursor = o.newCursor();
74 xmlCursor.removeXml();
75 xmlCursor.dispose();
76 }
77 }
78
79 o = msa.getPackageDependencies();
80 if (o != null) {
81 if (msa.getPackageDependencies().getPackageList().size() <= 0) {
82 XmlCursor xmlCursor = o.newCursor();
83 xmlCursor.removeXml();
84 xmlCursor.dispose();
85 }
86 }
87
88 o = msa.getProtocols();
89 if (o != null) {
90 if (msa.getProtocols().getProtocolList().size() <= 0
91 && msa.getProtocols().getProtocolNotifyList().size() <= 0) {
92 XmlCursor xmlCursor = o.newCursor();
93 xmlCursor.removeXml();
94 xmlCursor.dispose();
95 }
96 }
97
98 o = msa.getEvents();
99 if (o != null) {
100 if (msa.getEvents().getCreateEvents() != null || msa.getEvents().getSignalEvents() != null) {
101 if (msa.getEvents().getCreateEvents() != null && msa.getEvents().getCreateEvents().getEventTypesList().size() <= 0) {
102 XmlCursor xmlCursor = o.newCursor();
103 xmlCursor.removeXml();
104 xmlCursor.dispose();
105 }
106 if (msa.getEvents().getSignalEvents() != null && msa.getEvents().getSignalEvents().getEventTypesList().size() <= 0) {
107 XmlCursor xmlCursor = o.newCursor();
108 xmlCursor.removeXml();
109 xmlCursor.dispose();
110 }
111 } else {
112 XmlCursor xmlCursor = o.newCursor();
113 xmlCursor.removeXml();
114 xmlCursor.dispose();
115 }
116 }
117
118 o = msa.getHobs();
119 if (o != null) {
120 if (msa.getHobs().getHobTypesList().size() <= 0) {
121 XmlCursor xmlCursor = o.newCursor();
122 xmlCursor.removeXml();
123 xmlCursor.dispose();
124 }
125 }
126
127 o = msa.getPPIs();
128 if (o != null) {
129 if (msa.getPPIs().getPpiList().size() <= 0 && msa.getPPIs().getPpiNotifyList().size() <= 0) {
130 XmlCursor xmlCursor = o.newCursor();
131 xmlCursor.removeXml();
132 xmlCursor.dispose();
133 }
134 }
135
136 o = msa.getVariables();
137 if (o != null) {
138 if (msa.getVariables().getVariableList().size() <= 0) {
139 XmlCursor xmlCursor = o.newCursor();
140 xmlCursor.removeXml();
141 xmlCursor.dispose();
142 }
143 }
144
145 o = msa.getBootModes();
146 if (o != null) {
147 if (msa.getBootModes().getBootModeList().size() <= 0) {
148 XmlCursor xmlCursor = o.newCursor();
149 xmlCursor.removeXml();
150 xmlCursor.dispose();
151 }
152 }
153
154 o = msa.getSystemTables();
155 if (o != null) {
156 if (msa.getSystemTables().getSystemTableCNamesList().size() <= 0) {
157 XmlCursor xmlCursor = o.newCursor();
158 xmlCursor.removeXml();
159 xmlCursor.dispose();
160 }
161 }
162
163 o = msa.getDataHubs();
164 if (o != null) {
165 if (msa.getDataHubs().getDataHubRecordList().size() <= 0) {
166 XmlCursor xmlCursor = o.newCursor();
167 xmlCursor.removeXml();
168 xmlCursor.dispose();
169 }
170 }
171
172 o = msa.getHiiPackages();
173 if (o != null) {
174 if (msa.getHiiPackages().getHiiPackageList().size() <= 0) {
175 XmlCursor xmlCursor = o.newCursor();
176 xmlCursor.removeXml();
177 xmlCursor.dispose();
178 }
179 }
180
181 o = msa.getGuids();
182 if (o != null) {
183 if (msa.getGuids().getGuidCNamesList().size() <= 0) {
184 XmlCursor xmlCursor = o.newCursor();
185 xmlCursor.removeXml();
186 xmlCursor.dispose();
187 }
188 }
189
190 o = msa.getExterns();
191 if (o != null) {
192 if (msa.getExterns().getExternList().size() <= 0 && msa.getExterns().getSpecificationList().size() <= 0
193 && msa.getExterns().getPcdIsDriver() == null) {
194 XmlCursor xmlCursor = o.newCursor();
195 xmlCursor.removeXml();
196 xmlCursor.dispose();
197 }
198 }
199
200 o = msa.getPcdCoded();
201 if (o != null) {
202 if (msa.getPcdCoded().getPcdEntryList().size() <= 0) {
203 XmlCursor xmlCursor = o.newCursor();
204 xmlCursor.removeXml();
205 xmlCursor.dispose();
206 }
207 }
208
209 ModuleSurfaceAreaDocument msaDoc = ModuleSurfaceAreaDocument.Factory.newInstance();
210 File f = new File(path);
211
212 //
213 //Init namespace
214 //
215 XmlCursor cursor = XmlConfig.setupXmlCursor(msa.newCursor());
216
217 //
218 //Config file format
219 //
220 XmlOptions options = XmlConfig.setupXmlOptions();
221
222 //
223 //Create finial doc
224 //
225 msaDoc.addNewModuleSurfaceArea();
226 msaDoc.setModuleSurfaceArea((ModuleSurfaceArea) cursor.getObject());
227 //
228 //Save the file
229 //
230 msaDoc.save(f, options);
231 cursor.dispose();
232 }
233
234 /**
235 Save file as spd
236
237 **/
238 public static void saveSpdFile(String path, PackageSurfaceArea spd) throws Exception {
239 //
240 // Create the file's directory first
241 //
242 createDirectory(path);
243
244 PackageSurfaceAreaDocument spdDoc = PackageSurfaceAreaDocument.Factory.newInstance();
245 File f = new File(path);
246
247 //
248 //Init namespace
249 //
250 XmlCursor cursor = XmlConfig.setupXmlCursor(spd.newCursor());
251
252 //
253 //Config file format
254 //
255 XmlOptions options = XmlConfig.setupXmlOptions();
256
257 //
258 //Create finial doc
259 //
260 spdDoc.addNewPackageSurfaceArea();
261 spdDoc.setPackageSurfaceArea((PackageSurfaceArea) cursor.getObject());
262 //
263 //Save the file
264 //
265 spdDoc.save(f, options);
266 cursor.dispose();
267 }
268
269 /**
270 Save file as fpd
271
272 **/
273 public static void saveFpdFile(String path, PlatformSurfaceArea fpd) throws Exception {
274 //
275 // Create the file's directory first
276 //
277 createDirectory(path);
278
279 PlatformSurfaceAreaDocument fpdDoc = PlatformSurfaceAreaDocument.Factory.newInstance();
280 File f = new File(path);
281
282 //
283 //Init namespace
284 //
285 XmlCursor cursor = XmlConfig.setupXmlCursor(fpd.newCursor());
286
287 //
288 //Config file format
289 //
290 XmlOptions options = XmlConfig.setupXmlOptions();
291
292 //
293 //Create finial doc
294 //
295 fpdDoc.addNewPlatformSurfaceArea();
296 fpdDoc.setPlatformSurfaceArea((PlatformSurfaceArea) cursor.getObject());
297 //
298 //Save the file
299 //
300 fpdDoc.save(f, options);
301 cursor.dispose();
302 }
303
304 /**
305 Save file as framework db
306
307 **/
308 public static void saveDbFile(String path, FrameworkDatabase db) throws Exception {
309 FrameworkDatabaseDocument dbDoc = FrameworkDatabaseDocument.Factory.newInstance();
310 File f = new File(path);
311
312 //
313 //Init namespace
314 //
315 XmlCursor cursor = XmlConfig.setupXmlCursor(db.newCursor());
316
317 //
318 //Config file format
319 //
320 XmlOptions options = XmlConfig.setupXmlOptions();
321
322 //
323 //Create finial doc
324 //
325 dbDoc.addNewFrameworkDatabase();
326 dbDoc.setFrameworkDatabase((FrameworkDatabase) cursor.getObject());
327
328 //
329 //Save the file
330 //
331 dbDoc.save(f, options);
332 cursor.dispose();
333 }
334 }