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