]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/global/SurfaceAreaQuery.java
85b406621c47f8f3cf2dd3250d9387d0034c86c0
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / global / SurfaceAreaQuery.java
1 /** @file
2 This file is for surface area information retrieval.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14 package org.tianocore.frameworkwizard.platform.ui.global;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Stack;
21 import java.util.Vector;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.xmlbeans.XmlObject;
26 import org.tianocore.BuildTargetList;
27 import org.tianocore.LibraryClassDocument;
28 import org.tianocore.ModuleSurfaceAreaDocument;
29 import org.tianocore.PackageDependenciesDocument;
30 import org.tianocore.PackageSurfaceAreaDocument;
31 import org.tianocore.FilenameDocument.Filename;
32
33 import org.tianocore.frameworkwizard.common.GlobalData;
34 import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;
35 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
36
37 /**
38 * SurfaceAreaQuery class is used to query Surface Area information from msa,
39 * mbd, spd and fpd files.
40 *
41 * This class should not instantiated. All the public interfaces is static.
42 *
43 * @since GenBuild 1.0
44 */
45 public class SurfaceAreaQuery {
46
47 public static String prefix = "http://www.TianoCore.org/2006/Edk2.0";
48
49 // /
50 // / Contains name/value pairs of Surface Area document object. The name is
51 // / always the top level element name.
52 // /
53 private static Map<String, XmlObject> map = null;
54
55 // /
56 // / mapStack is used to do nested query
57 // /
58 private static Stack<Map<String, XmlObject>> mapStack = new Stack<Map<String, XmlObject>>();
59
60 // /
61 // / prefix of name space
62 // /
63 private static String nsPrefix = "sans";
64
65 // /
66 // / xmlbeans needs a name space for each Xpath element
67 // /
68 private static String ns = null;
69
70 // /
71 // / keep the namep declaration for xmlbeans Xpath query
72 // /
73 private static String queryDeclaration = null;
74
75 /**
76 * Set a Surface Area document for query later
77 *
78 * @param map
79 * A Surface Area document in TopLevelElementName/XmlObject
80 * format.
81 */
82 public static void setDoc(Map<String, XmlObject> map) {
83 ns = prefix;
84 queryDeclaration = "declare namespace " + nsPrefix + "='" + ns + "'; ";
85 SurfaceAreaQuery.map = map;
86 }
87
88 /**
89 * Push current used Surface Area document into query stack. The given new
90 * document will be used for any immediately followed getXXX() callings,
91 * untill pop() is called.
92 *
93 * @param newMap
94 * The TopLevelElementName/XmlObject format of a Surface Area
95 * document.
96 */
97 public static void push(Map<String, XmlObject> newMap) {
98 mapStack.push(SurfaceAreaQuery.map);
99 SurfaceAreaQuery.map = newMap;
100 }
101
102 /**
103 * Discard current used Surface Area document and use the top document in
104 * stack instead.
105 */
106 public static void pop() {
107 SurfaceAreaQuery.map = mapStack.pop();
108 }
109
110 // /
111 // / Convert xPath to be namespace qualified, which is necessary for
112 // XmlBeans
113 // / selectPath(). For example, converting /MsaHeader/ModuleType to
114 // / /ns:MsaHeader/ns:ModuleType
115 // /
116 private static String normalizeQueryString(String[] exp, String from) {
117 StringBuffer normQueryString = new StringBuffer(4096);
118
119 int i = 0;
120 while (i < exp.length) {
121 String newExp = from + exp[i];
122 Pattern pattern = Pattern.compile("([^/]*)(/|//)([^/]+)");
123 Matcher matcher = pattern.matcher(newExp);
124
125 while (matcher.find()) {
126 String starter = newExp.substring(matcher.start(1), matcher
127 .end(1));
128 String seperator = newExp.substring(matcher.start(2), matcher
129 .end(2));
130 String token = newExp.substring(matcher.start(3), matcher
131 .end(3));
132
133 normQueryString.append(starter);
134 normQueryString.append(seperator);
135 normQueryString.append(nsPrefix);
136 normQueryString.append(":");
137 normQueryString.append(token);
138 }
139
140 ++i;
141 if (i < exp.length) {
142 normQueryString.append(" | ");
143 }
144 }
145
146 return normQueryString.toString();
147 }
148
149 /**
150 * Search all XML documents stored in "map" for the specified xPath, using
151 * relative path (starting with '$this')
152 *
153 * @param xPath
154 * xpath query string array
155 * @returns An array of XmlObject if elements are found at the specified
156 * xpath
157 * @returns NULL if nothing is at the specified xpath
158 */
159 public static XmlObject[] get(String[] xPath) {
160 if (map == null) {
161 return null;
162 }
163
164 String[] keys = (String[]) map.keySet().toArray(new String[map.size()]);
165 List<XmlObject> result = new ArrayList<XmlObject>();
166 for (int i = 0; i < keys.length; ++i) {
167 XmlObject rootNode = (XmlObject) map.get(keys[i]);
168 if (rootNode == null) {
169 continue;
170 }
171
172 String query = queryDeclaration
173 + normalizeQueryString(xPath, "$this/" + keys[i]);
174 XmlObject[] tmp = rootNode.selectPath(query);
175 for (int j = 0; j < tmp.length; ++j) {
176 result.add(tmp[j]);
177 }
178 }
179
180 int size = result.size();
181 if (size <= 0) {
182 return null;
183 }
184
185 return (XmlObject[]) result.toArray(new XmlObject[size]);
186 }
187
188 /**
189 * Search XML documents named by "rootName" for the given xPath, using
190 * relative path (starting with '$this')
191 *
192 * @param rootName
193 * The top level element name
194 * @param xPath
195 * The xpath query string array
196 * @returns An array of XmlObject if elements are found at the given xpath
197 * @returns NULL if nothing is found at the given xpath
198 */
199 public static XmlObject[] get(String rootName, String[] xPath) {
200 if (map == null) {
201 return null;
202 }
203
204 XmlObject root = (XmlObject) map.get(rootName);
205 if (root == null) {
206 return null;
207 }
208
209 String query = queryDeclaration
210 + normalizeQueryString(xPath, "$this/" + rootName);
211 XmlObject[] result = root.selectPath(query);
212 if (result.length > 0) {
213 return result;
214 }
215
216 query = queryDeclaration + normalizeQueryString(xPath, "/" + rootName);
217 result = root.selectPath(query);
218 if (result.length > 0) {
219 return result;
220 }
221
222 return null;
223 }
224
225 /**
226 * Retrieve SourceFiles/Filename for specified ARCH type
227 *
228 * @param arch
229 * architecture name
230 * @returns An 2 dimension string array if elements are found at the known
231 * xpath
232 * @returns NULL if nothing is found at the known xpath
233 */
234 public static String[][] getSourceFiles(String arch) {
235 String[] xPath;
236 XmlObject[] returns;
237
238 if (arch == null || arch.equals("")) {
239 xPath = new String[] { "/Filename" };
240 } else {
241 xPath = new String[] { "/Filename[not(@SupArchList) or @SupArchList='"
242 + arch + "']" };
243 }
244
245 returns = get("SourceFiles", xPath);
246
247 if (returns == null || returns.length == 0) {
248 return null;
249 }
250
251 Filename[] sourceFileNames = (Filename[]) returns;
252 String[][] outputString = new String[sourceFileNames.length][2];
253 for (int i = 0; i < sourceFileNames.length; i++) {
254 outputString[i][0] = sourceFileNames[i].getToolCode();
255 outputString[i][1] = sourceFileNames[i].getStringValue();
256 }
257 return outputString;
258 }
259
260 /**
261 * Retrieve /PlatformDefinitions/OutputDirectory from FPD
262 *
263 * @returns Directory names array if elements are found at the known xpath
264 * @returns Empty if nothing is found at the known xpath
265 */
266 public static String getFpdOutputDirectory() {
267 String[] xPath = new String[] { "/PlatformDefinitions/OutputDirectory" };
268
269 XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
270 if (returns != null && returns.length > 0) {
271 // String TBD
272 }
273
274 return null;
275 }
276
277 public static String getFpdIntermediateDirectories() {
278 String[] xPath = new String[] { "/PlatformDefinitions/IntermediateDirectories" };
279
280 XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
281 if (returns != null && returns.length > 0) {
282 // TBD
283 }
284 return "UNIFIED";
285 }
286
287 public static String getBuildTarget() {
288 String[] xPath = new String[] { "/PlatformDefinitions/BuildTargets" };
289
290 XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
291 if (returns != null && returns.length > 0) {
292 return ((BuildTargetList) returns[0]).getStringValue();
293 }
294
295 return null;
296 }
297
298 /**
299 * Retrieve <xxxHeader>/ModuleType
300 *
301 * @returns The module type name if elements are found at the known xpath
302 * @returns null if nothing is there
303 */
304 public static String getModuleType(ModuleIdentification mi) {
305 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = WorkspaceProfile.getModuleXmlObject(mi);
306
307 return msa.getMsaHeader().getModuleType()+"";
308 }
309
310 /**
311 * Retrieve PackageDependencies/Package
312 *
313 * @param arch
314 * Architecture name
315 *
316 * @returns package name list if elements are found at the known xpath
317 * @returns null if nothing is there
318 */
319
320 public static PackageIdentification[] getDependencePkg(String arch, ModuleIdentification mi) throws Exception{
321
322 String packageGuid = null;
323 String packageVersion = null;
324
325 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea) WorkspaceProfile.getModuleXmlObject(mi);
326 if (msa.getPackageDependencies() == null) {
327 return new PackageIdentification[0];
328 }
329 int size = msa.getPackageDependencies().getPackageList().size();
330 XmlObject[] returns = new XmlObject[size];
331 for (int i = 0; i < size; ++i) {
332 returns[i] = msa.getPackageDependencies().getPackageList().get(i);
333 }
334
335 PackageIdentification[] packageIdList = new PackageIdentification[returns.length];
336 for (int i = 0; i < returns.length; i++) {
337 PackageDependenciesDocument.PackageDependencies.Package item = (PackageDependenciesDocument.PackageDependencies.Package) returns[i];
338 packageGuid = item.getPackageGuid();
339 packageVersion = item.getPackageVersion();
340
341 Iterator<PackageIdentification> ispi = GlobalData.vPackageList.iterator();
342 String ver = "";
343 while(ispi.hasNext()) {
344 PackageIdentification pi = ispi.next();
345 if (packageVersion != null) {
346 if (pi.getGuid().equalsIgnoreCase(packageGuid) && pi.getVersion().equals(packageVersion)) {
347 packageIdList[i] = pi;
348 break;
349 }
350 }
351 else {
352 if (pi.getGuid().equalsIgnoreCase(packageGuid)) {
353 if (pi.getVersion() != null && pi.getVersion().compareTo(ver) > 0){
354 ver = pi.getVersion();
355 packageIdList[i] = pi;
356 }
357 else if (packageIdList[i] == null){
358 packageIdList[i] = pi;
359 }
360 }
361 }
362
363 }
364 }
365 return packageIdList;
366 }
367
368 /**
369 * Retrieve LibraryClassDefinitions/LibraryClass for specified usage
370 *
371 * @param usage
372 * Library class usage
373 *
374 * @returns LibraryClass objects list if elements are found at the known
375 * xpath
376 * @returns null if nothing is there
377 */
378 public static Vector<String> getLibraryClasses(String usage, ModuleIdentification mi) throws Exception{
379 ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)WorkspaceProfile.getModuleXmlObject(mi);
380 Vector<String> libraryClassName = new Vector<String>();
381 if (msa.getLibraryClassDefinitions() == null) {
382 return libraryClassName;
383 }
384
385 int size = msa.getLibraryClassDefinitions().getLibraryClassList().size();
386
387 for (int i = 0; i < size; i++) {
388 LibraryClassDocument.LibraryClass libClass = msa.getLibraryClassDefinitions().getLibraryClassList().get(i);
389 if (usage.equals(libClass.getUsage().toString())) {
390 libraryClassName.add(libClass.getKeyword());
391 }
392 }
393
394 return libraryClassName;
395 }
396
397 public static XmlObject[] getSpdPcdDeclarations(PackageIdentification pi) {
398 XmlObject[] returns = null;
399 PackageSurfaceAreaDocument.PackageSurfaceArea psa = WorkspaceProfile.getPackageXmlObject(pi);
400 if (psa.getPcdDeclarations() != null && psa.getPcdDeclarations().getPcdEntryList() != null) {
401 int size = psa.getPcdDeclarations().getPcdEntryList().size();
402 returns = new XmlObject[size];
403 for (int i = 0; i < size; ++i) {
404 returns[i] = psa.getPcdDeclarations().getPcdEntryList().get(i);
405 }
406 }
407
408 return returns;
409 }
410
411 }