2 This file is for surface area information retrieval.
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
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.
14 package org
.tianocore
.frameworkwizard
.platform
.ui
.global
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import java
.util
.Stack
;
21 import java
.util
.Vector
;
22 import java
.util
.regex
.Matcher
;
23 import java
.util
.regex
.Pattern
;
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
;
33 import org
.tianocore
.frameworkwizard
.common
.GlobalData
;
34 import org
.tianocore
.frameworkwizard
.module
.Identifications
.ModuleIdentification
;
35 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
38 * SurfaceAreaQuery class is used to query Surface Area information from msa,
39 * mbd, spd and fpd files.
41 * This class should not instantiated. All the public interfaces is static.
45 public class SurfaceAreaQuery
{
47 public static String prefix
= "http://www.TianoCore.org/2006/Edk2.0";
50 // / Contains name/value pairs of Surface Area document object. The name is
51 // / always the top level element name.
53 private static Map
<String
, XmlObject
> map
= null;
56 // / mapStack is used to do nested query
58 private static Stack
<Map
<String
, XmlObject
>> mapStack
= new Stack
<Map
<String
, XmlObject
>>();
61 // / prefix of name space
63 private static String nsPrefix
= "sans";
66 // / xmlbeans needs a name space for each Xpath element
68 private static String ns
= null;
71 // / keep the namep declaration for xmlbeans Xpath query
73 private static String queryDeclaration
= null;
76 * Set a Surface Area document for query later
79 * A Surface Area document in TopLevelElementName/XmlObject
82 public static void setDoc(Map
<String
, XmlObject
> map
) {
84 queryDeclaration
= "declare namespace " + nsPrefix
+ "='" + ns
+ "'; ";
85 SurfaceAreaQuery
.map
= map
;
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.
94 * The TopLevelElementName/XmlObject format of a Surface Area
97 public static void push(Map
<String
, XmlObject
> newMap
) {
98 mapStack
.push(SurfaceAreaQuery
.map
);
99 SurfaceAreaQuery
.map
= newMap
;
103 * Discard current used Surface Area document and use the top document in
106 public static void pop() {
107 SurfaceAreaQuery
.map
= mapStack
.pop();
111 // / Convert xPath to be namespace qualified, which is necessary for
113 // / selectPath(). For example, converting /MsaHeader/ModuleType to
114 // / /ns:MsaHeader/ns:ModuleType
116 private static String
normalizeQueryString(String
[] exp
, String from
) {
117 StringBuffer normQueryString
= new StringBuffer(4096);
120 while (i
< exp
.length
) {
121 String newExp
= from
+ exp
[i
];
122 Pattern pattern
= Pattern
.compile("([^/]*)(/|//)([^/]+)");
123 Matcher matcher
= pattern
.matcher(newExp
);
125 while (matcher
.find()) {
126 String starter
= newExp
.substring(matcher
.start(1), matcher
128 String seperator
= newExp
.substring(matcher
.start(2), matcher
130 String token
= newExp
.substring(matcher
.start(3), matcher
133 normQueryString
.append(starter
);
134 normQueryString
.append(seperator
);
135 normQueryString
.append(nsPrefix
);
136 normQueryString
.append(":");
137 normQueryString
.append(token
);
141 if (i
< exp
.length
) {
142 normQueryString
.append(" | ");
146 return normQueryString
.toString();
150 * Search all XML documents stored in "map" for the specified xPath, using
151 * relative path (starting with '$this')
154 * xpath query string array
155 * @returns An array of XmlObject if elements are found at the specified
157 * @returns NULL if nothing is at the specified xpath
159 public static XmlObject
[] get(String
[] xPath
) {
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) {
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
) {
180 int size
= result
.size();
185 return (XmlObject
[]) result
.toArray(new XmlObject
[size
]);
189 * Search XML documents named by "rootName" for the given xPath, using
190 * relative path (starting with '$this')
193 * The top level element name
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
199 public static XmlObject
[] get(String rootName
, String
[] xPath
) {
204 XmlObject root
= (XmlObject
) map
.get(rootName
);
209 String query
= queryDeclaration
210 + normalizeQueryString(xPath
, "$this/" + rootName
);
211 XmlObject
[] result
= root
.selectPath(query
);
212 if (result
.length
> 0) {
216 query
= queryDeclaration
+ normalizeQueryString(xPath
, "/" + rootName
);
217 result
= root
.selectPath(query
);
218 if (result
.length
> 0) {
226 * Retrieve SourceFiles/Filename for specified ARCH type
230 * @returns An 2 dimension string array if elements are found at the known
232 * @returns NULL if nothing is found at the known xpath
234 public static String
[][] getSourceFiles(String arch
) {
238 if (arch
== null || arch
.equals("")) {
239 xPath
= new String
[] { "/Filename" };
241 xPath
= new String
[] { "/Filename[not(@SupArchList) or @SupArchList='"
245 returns
= get("SourceFiles", xPath
);
247 if (returns
== null || returns
.length
== 0) {
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();
261 * Retrieve /PlatformDefinitions/OutputDirectory from FPD
263 * @returns Directory names array if elements are found at the known xpath
264 * @returns Empty if nothing is found at the known xpath
266 public static String
getFpdOutputDirectory() {
267 String
[] xPath
= new String
[] { "/PlatformDefinitions/OutputDirectory" };
269 XmlObject
[] returns
= get("FrameworkPlatformDescription", xPath
);
270 if (returns
!= null && returns
.length
> 0) {
277 public static String
getFpdIntermediateDirectories() {
278 String
[] xPath
= new String
[] { "/PlatformDefinitions/IntermediateDirectories" };
280 XmlObject
[] returns
= get("FrameworkPlatformDescription", xPath
);
281 if (returns
!= null && returns
.length
> 0) {
287 public static String
getBuildTarget() {
288 String
[] xPath
= new String
[] { "/PlatformDefinitions/BuildTargets" };
290 XmlObject
[] returns
= get("FrameworkPlatformDescription", xPath
);
291 if (returns
!= null && returns
.length
> 0) {
292 return ((BuildTargetList
) returns
[0]).getStringValue();
299 * Retrieve <xxxHeader>/ModuleType
301 * @returns The module type name if elements are found at the known xpath
302 * @returns null if nothing is there
304 public static String
getModuleType(ModuleIdentification mi
) {
305 ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= WorkspaceProfile
.getModuleXmlObject(mi
);
307 return msa
.getMsaHeader().getModuleType()+"";
311 * Retrieve PackageDependencies/Package
316 * @returns package name list if elements are found at the known xpath
317 * @returns null if nothing is there
320 public static PackageIdentification
[] getDependencePkg(String arch
, ModuleIdentification mi
) throws Exception
{
322 String packageGuid
= null;
323 String packageVersion
= null;
325 ModuleSurfaceAreaDocument
.ModuleSurfaceArea msa
= (ModuleSurfaceAreaDocument
.ModuleSurfaceArea
) WorkspaceProfile
.getModuleXmlObject(mi
);
326 if (msa
.getPackageDependencies() == null) {
327 return new PackageIdentification
[0];
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
);
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();
341 Iterator
<PackageIdentification
> ispi
= GlobalData
.vPackageList
.iterator();
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
;
352 if (pi
.getGuid().equalsIgnoreCase(packageGuid
)) {
353 if (pi
.getVersion() != null && pi
.getVersion().compareTo(ver
) > 0){
354 ver
= pi
.getVersion();
355 packageIdList
[i
] = pi
;
357 else if (packageIdList
[i
] == null){
358 packageIdList
[i
] = pi
;
365 return packageIdList
;
369 * Retrieve LibraryClassDefinitions/LibraryClass for specified usage
372 * Library class usage
374 * @returns LibraryClass objects list if elements are found at the known
376 * @returns null if nothing is there
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
;
385 int size
= msa
.getLibraryClassDefinitions().getLibraryClassList().size();
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());
394 return libraryClassName
;
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
);