summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/platform/ui/global/SurfaceAreaQuery.java
blob: ad0e82ca3ff0d88e0da8832ffce10f80dddd3965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/** @file
 This file is for surface area information retrieval.

 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php

 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 **/
package org.tianocore.frameworkwizard.platform.ui.global;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.xmlbeans.XmlObject;
import org.tianocore.BuildTargetList;
import org.tianocore.LibraryClassDocument;
import org.tianocore.ModuleSurfaceAreaDocument;
import org.tianocore.PackageDependenciesDocument;
import org.tianocore.PackageSurfaceAreaDocument;
import org.tianocore.FilenameDocument.Filename;

import org.tianocore.frameworkwizard.common.GlobalData;
import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;
import org.tianocore.frameworkwizard.packaging.PackageIdentification;

/**
 * SurfaceAreaQuery class is used to query Surface Area information from msa,
 * mbd, spd and fpd files.
 * 
 * This class should not instantiated. All the public interfaces is static.
 * 
 * @since GenBuild 1.0
 */
public class SurfaceAreaQuery {

	public static String prefix = "http://www.TianoCore.org/2006/Edk2.0";

	// /
	// / Contains name/value pairs of Surface Area document object. The name is
	// / always the top level element name.
	// /
	private static Map<String, XmlObject> map = null;

	// /
	// / mapStack is used to do nested query
	// /
	private static Stack<Map<String, XmlObject>> mapStack = new Stack<Map<String, XmlObject>>();

	// /
	// / prefix of name space
	// /
	private static String nsPrefix = "sans";

	// /
	// / xmlbeans needs a name space for each Xpath element
	// /
	private static String ns = null;

	// /
	// / keep the namep declaration for xmlbeans Xpath query
	// /
	private static String queryDeclaration = null;

	/**
	 * Set a Surface Area document for query later
	 * 
	 * @param map
	 *            A Surface Area document in TopLevelElementName/XmlObject
	 *            format.
	 */
	public static void setDoc(Map<String, XmlObject> map) {
		ns = prefix;
		queryDeclaration = "declare namespace " + nsPrefix + "='" + ns + "'; ";
		SurfaceAreaQuery.map = map;
	}

	/**
	 * Push current used Surface Area document into query stack. The given new
	 * document will be used for any immediately followed getXXX() callings,
	 * untill pop() is called.
	 * 
	 * @param newMap
	 *            The TopLevelElementName/XmlObject format of a Surface Area
	 *            document.
	 */
	public static void push(Map<String, XmlObject> newMap) {
		mapStack.push(SurfaceAreaQuery.map);
		SurfaceAreaQuery.map = newMap;
	}

	/**
	 * Discard current used Surface Area document and use the top document in
	 * stack instead.
	 */
	public static void pop() {
		SurfaceAreaQuery.map = mapStack.pop();
	}

	// /
	// / Convert xPath to be namespace qualified, which is necessary for
	// XmlBeans
	// / selectPath(). For example, converting /MsaHeader/ModuleType to
	// / /ns:MsaHeader/ns:ModuleType
	// /
	private static String normalizeQueryString(String[] exp, String from) {
		StringBuffer normQueryString = new StringBuffer(4096);

		int i = 0;
		while (i < exp.length) {
			String newExp = from + exp[i];
			Pattern pattern = Pattern.compile("([^/]*)(/|//)([^/]+)");
			Matcher matcher = pattern.matcher(newExp);

			while (matcher.find()) {
				String starter = newExp.substring(matcher.start(1), matcher
						.end(1));
				String seperator = newExp.substring(matcher.start(2), matcher
						.end(2));
				String token = newExp.substring(matcher.start(3), matcher
						.end(3));

				normQueryString.append(starter);
				normQueryString.append(seperator);
				normQueryString.append(nsPrefix);
				normQueryString.append(":");
				normQueryString.append(token);
			}

			++i;
			if (i < exp.length) {
				normQueryString.append(" | ");
			}
		}

		return normQueryString.toString();
	}

	/**
	 * Search all XML documents stored in "map" for the specified xPath, using
	 * relative path (starting with '$this')
	 * 
	 * @param xPath
	 *            xpath query string array
	 * @returns An array of XmlObject if elements are found at the specified
	 *          xpath
	 * @returns NULL if nothing is at the specified xpath
	 */
	public static XmlObject[] get(String[] xPath) {
		if (map == null) {
			return null;
		}

		String[] keys = (String[]) map.keySet().toArray(new String[map.size()]);
		List<XmlObject> result = new ArrayList<XmlObject>();
		for (int i = 0; i < keys.length; ++i) {
			XmlObject rootNode = (XmlObject) map.get(keys[i]);
			if (rootNode == null) {
				continue;
			}

			String query = queryDeclaration
					+ normalizeQueryString(xPath, "$this/" + keys[i]);
			XmlObject[] tmp = rootNode.selectPath(query);
			for (int j = 0; j < tmp.length; ++j) {
				result.add(tmp[j]);
			}
		}

		int size = result.size();
		if (size <= 0) {
			return null;
		}

		return (XmlObject[]) result.toArray(new XmlObject[size]);
	}

	/**
	 * Search XML documents named by "rootName" for the given xPath, using
	 * relative path (starting with '$this')
	 * 
	 * @param rootName
	 *            The top level element name
	 * @param xPath
	 *            The xpath query string array
	 * @returns An array of XmlObject if elements are found at the given xpath
	 * @returns NULL if nothing is found at the given xpath
	 */
	public static XmlObject[] get(String rootName, String[] xPath) {
		if (map == null) {
			return null;
		}

		XmlObject root = (XmlObject) map.get(rootName);
		if (root == null) {
			return null;
		}

		String query = queryDeclaration
				+ normalizeQueryString(xPath, "$this/" + rootName);
		XmlObject[] result = root.selectPath(query);
		if (result.length > 0) {
			return result;
		}

		query = queryDeclaration + normalizeQueryString(xPath, "/" + rootName);
		result = root.selectPath(query);
		if (result.length > 0) {
			return result;
		}

		return null;
	}

	/**
	 * Retrieve SourceFiles/Filename for specified ARCH type
	 * 
	 * @param arch
	 *            architecture name
	 * @returns An 2 dimension string array if elements are found at the known
	 *          xpath
	 * @returns NULL if nothing is found at the known xpath
	 */
	public static String[][] getSourceFiles(String arch) {
		String[] xPath;
		XmlObject[] returns;

		if (arch == null || arch.equals("")) {
			xPath = new String[] { "/Filename" };
		} else {
			xPath = new String[] { "/Filename[not(@SupArchList) or @SupArchList='"
					+ arch + "']" };
		}

		returns = get("SourceFiles", xPath);

		if (returns == null || returns.length == 0) {
			return null;
		}

		Filename[] sourceFileNames = (Filename[]) returns;
		String[][] outputString = new String[sourceFileNames.length][2];
		for (int i = 0; i < sourceFileNames.length; i++) {
			outputString[i][0] = sourceFileNames[i].getToolCode();
			outputString[i][1] = sourceFileNames[i].getStringValue();
		}
		return outputString;
	}

	/**
	 * Retrieve /PlatformDefinitions/OutputDirectory from FPD
	 * 
	 * @returns Directory names array if elements are found at the known xpath
	 * @returns Empty if nothing is found at the known xpath
	 */
	public static String getFpdOutputDirectory() {
		String[] xPath = new String[] { "/PlatformDefinitions/OutputDirectory" };

		XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
		if (returns != null && returns.length > 0) {
			// String TBD
		}

		return null;
	}

	public static String getFpdIntermediateDirectories() {
		String[] xPath = new String[] { "/PlatformDefinitions/IntermediateDirectories" };

		XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
		if (returns != null && returns.length > 0) {
			// TBD
		}
		return "UNIFIED";
	}

	public static String getBuildTarget() {
		String[] xPath = new String[] { "/PlatformDefinitions/BuildTargets" };

		XmlObject[] returns = get("FrameworkPlatformDescription", xPath);
		if (returns != null && returns.length > 0) {
			return ((BuildTargetList) returns[0]).getStringValue();
		}

		return null;
	}

	/**
	 * Retrieve <xxxHeader>/ModuleType
	 * 
	 * @returns The module type name if elements are found at the known xpath
	 * @returns null if nothing is there
	 */
	public static String getModuleType(ModuleIdentification mi) {
		ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = WorkspaceProfile.getModuleXmlObject(mi);

		return msa.getMsaHeader().getModuleType()+"";
	}

	/**
	 * Retrieve PackageDependencies/Package
	 * 
	 * @param arch
	 *            Architecture name
	 * 
	 * @returns package name list if elements are found at the known xpath
	 * @returns null if nothing is there
	 */

	public static PackageIdentification[] getDependencePkg(String arch, ModuleIdentification mi){
		
		String packageGuid = null;
		String packageVersion = null;

        ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea) WorkspaceProfile.getModuleXmlObject(mi);
        if (msa.getPackageDependencies() == null) {
            return new PackageIdentification[0];
        }
        int size = msa.getPackageDependencies().getPackageList().size();
        XmlObject[] returns = new XmlObject[size];
        for (int i = 0; i < size; ++i) {
            returns[i] = msa.getPackageDependencies().getPackageList().get(i);
        }

		PackageIdentification[] packageIdList = new PackageIdentification[returns.length];
		for (int i = 0; i < returns.length; i++) {
			PackageDependenciesDocument.PackageDependencies.Package item = (PackageDependenciesDocument.PackageDependencies.Package) returns[i];
			packageGuid = item.getPackageGuid();
			packageVersion = item.getPackageVersion();

            Iterator<PackageIdentification> ispi = GlobalData.vPackageList.iterator();
            String ver = "";
            while(ispi.hasNext()) {
                PackageIdentification pi = ispi.next();
                if (packageVersion != null) {
                    if (pi.getGuid().equalsIgnoreCase(packageGuid) && pi.getVersion().equals(packageVersion)) {
                        packageIdList[i] = pi;
                        break;
                    } 
                }
                else {
                    if (pi.getGuid().equalsIgnoreCase(packageGuid)) {
                        if (pi.getVersion() != null && pi.getVersion().compareTo(ver) > 0){
                            ver = pi.getVersion();
                            packageIdList[i] = pi;
                        }
                        else if (packageIdList[i] == null){
                            packageIdList[i] = pi;
                        }
                    }
                }
                
            }
		}
		return packageIdList;
	}

	/**
	 * Retrieve LibraryClassDefinitions/LibraryClass for specified usage
	 * 
	 * @param usage
	 *            Library class usage
	 * 
	 * @returns LibraryClass objects list if elements are found at the known
	 *          xpath
	 * @returns null if nothing is there
	 */
	public static Vector<LibraryClassDescriptor> getLibraryClasses(String usage, ModuleIdentification mi){
        ModuleSurfaceAreaDocument.ModuleSurfaceArea msa = (ModuleSurfaceAreaDocument.ModuleSurfaceArea)WorkspaceProfile.getModuleXmlObject(mi);
        Vector<LibraryClassDescriptor> libraryClassInfo = new Vector<LibraryClassDescriptor>();
        if (msa.getLibraryClassDefinitions() == null) {
            return libraryClassInfo;
        }
        
        int size = msa.getLibraryClassDefinitions().getLibraryClassList().size();
		
		for (int i = 0; i < size; i++) {
            LibraryClassDocument.LibraryClass libClass = msa.getLibraryClassDefinitions().getLibraryClassList().get(i);
            if (usage.equals(libClass.getUsage().toString())) {

                libraryClassInfo.add(new LibraryClassDescriptor(libClass.getKeyword(), libClass.getSupArchList()+"", libClass.getSupModuleList()+""));
            }
		}
        
		return libraryClassInfo;
	}

    public static XmlObject[] getSpdPcdDeclarations(PackageIdentification pi) {
        XmlObject[] returns = null;
        PackageSurfaceAreaDocument.PackageSurfaceArea psa = WorkspaceProfile.getPackageXmlObject(pi);
        if (psa.getPcdDeclarations() != null && psa.getPcdDeclarations().getPcdEntryList() != null) {
            int size = psa.getPcdDeclarations().getPcdEntryList().size();
            returns = new XmlObject[size];
            for (int i = 0; i < size; ++i) {
                returns[i] = psa.getPcdDeclarations().getPcdEntryList().get(i);
            }
        }
     
        return returns;
    }

}