summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/fpd/PlatformBuildFileGenerator.java
blob: ad2ee2cdd24ccf5d1c945c7d2c60757717513d2f (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/** @file
 
 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.build.fpd;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.tianocore.build.global.GlobalData;
import org.tianocore.build.global.SurfaceAreaQuery;
import org.tianocore.build.id.FpdModuleIdentification;
import org.tianocore.build.id.ModuleIdentification;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
  class PlatformBuildFileGenerator is used to generate ${PLATFORM}_build.xml file. 

  @since GenBuild 1.0
**/
public class PlatformBuildFileGenerator {

    ///
    /// Mapping from modules identification to out put file name
    ///
    private Map<FpdModuleIdentification, String> outfiles;
    
    ///
    /// Mapping from FV name to its modules
    ///
    private Map<String, Set<FpdModuleIdentification>> fvs = new HashMap<String, Set<FpdModuleIdentification>>();


    private boolean isUnified = true;
    
    private SurfaceAreaQuery saq = null;
    
    private File platformBuildFile = null;
    
    private Map<String, String> aprioriType = null;
    
    private Project project;
    
    private String info = "DO NOT EDIT \n" 
        + "This file is auto-generated by the build utility\n" 
        + "\n" 
        + "Abstract:\n"
        + "Auto-generated ANT build file for building EFI Modules and Platforms\n";

    public PlatformBuildFileGenerator(Project project, Map<FpdModuleIdentification, String> outfiles, Map<String, Set<FpdModuleIdentification>> fvs, boolean isUnified, SurfaceAreaQuery saq, String platformBuildFile, Map<String, String> aprioriType){
        this.project = project;
        this.outfiles = outfiles;
        this.isUnified = isUnified;
        this.fvs = fvs;
        this.saq = saq;
        this.platformBuildFile = new File(platformBuildFile);
        this.aprioriType = aprioriType;
    }
    
    /**
      Generate build.out.xml file.
     
      @throws BuildException
                  build.out.xml XML document create error
    **/
    public void genBuildFile() throws BuildException {
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder dombuilder = domfac.newDocumentBuilder();
            Document document = dombuilder.newDocument();
            Comment rootComment = document.createComment(info);
            //
            // create root element and its attributes
            //
            Element root = document.createElement("project");
            root.setAttribute("name", project.getProperty("PLATFORM"));
            root.setAttribute("default", "all");
            root.setAttribute("basedir", ".");
            
            //
            // element for External ANT tasks
            //
            root.appendChild(document.createComment("Apply external ANT tasks"));
            Element ele = document.createElement("taskdef");
            ele.setAttribute("resource", "GenBuild.tasks");
            root.appendChild(ele);

            ele = document.createElement("taskdef");
            ele.setAttribute("resource", "frameworktasks.tasks");
            root.appendChild(ele);

            ele = document.createElement("taskdef");
            ele.setAttribute("resource", "net/sf/antcontrib/antlib.xml");
            root.appendChild(ele);
            
            ele = document.createElement("property");
            ele.setAttribute("environment", "env");
            root.appendChild(ele);
            
            //
            // Default Target
            //
            root.appendChild(document.createComment("Default target"));
            ele = document.createElement("target");
            ele.setAttribute("name", "all");
            ele.setAttribute("depends", "prebuild, modules, fvs, postbuild");
            root.appendChild(ele);
            
            //
            // Modules and Fvs Target
            //
            applyModules(document, root);
            
            applyFvs(document, root);

            //
            // Clean Target
            //
            applyClean(document, root);
            
            //
            // Deep Clean Target
            //
            applyCleanall(document, root);
            
            //
            // User Extension pre build
            //
            applyUserExtensionsPreBuild(document, root);
            
            //
            // User Extension Post build
            //
            applyUserExtensionsPostBuild(document, root);
            applyUserExtensions(document, root);

            document.appendChild(rootComment);
            document.appendChild(root);
            //
            // Prepare the DOM document for writing
            //
            Source source = new DOMSource(document);
            //
            // generate all directory path
            //
            (new File(platformBuildFile.getParent())).mkdirs();
            Result result = new StreamResult(platformBuildFile);
            //
            // Write the DOM document to the file
            //
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.transform(source, result);
        } catch (Exception ex) {
            throw new BuildException("Generating platform build file [" + platformBuildFile.getPath() + "_build.xml] failed. \n" + ex.getMessage());
        }
    }
    
    /**
      1. Get All valid Fv Image Names in sequence
      2. For each FV, get modules by sequences
      3. Get other modules
      
      @param document XML document
      @param root Node
    **/
    private void applyModules(Document document, Node root) {
        root.appendChild(document.createComment("Modules target"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "modules");

        //
        // Try to build apriori if necessary
        //
        //
        // For every Target and ToolChain
        //
        String[] targetList = GlobalData.getToolChainInfo().getTargets();
        for (int i = 0; i < targetList.length; i++){
            String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
            for(int j = 0; j < toolchainList.length; j++){
                //
                // Prepare FV_DIR
                //
                String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar 
                                + targetList[i] + "_" 
                                + toolchainList[j];
                File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
                Element fvEle = document.createElement("var");
                fvEle.setAttribute("name", "FV_DIR");
                fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
                ele.appendChild(fvEle);
                
                Iterator<String> aprIter = aprioriType.keySet().iterator();
                while (aprIter.hasNext()) {
                    String fvName = aprIter.next();
                    Element moduleEle = document.createElement("Build_Apriori");
                    moduleEle.setAttribute("FILENAME", fvName);
                    moduleEle.setAttribute("GUID", aprioriType.get(fvName));
                    ele.appendChild(moduleEle);
                }
            }
        }
        
        //
        // Get all valid FV name
        //
        String[] validFv = saq.getFpdValidImageNames();
        
        //
        // For each valid FV, get all modules in sequence
        //
        for (int i = 0; i < validFv.length; i++) {
            if (fvs.containsKey(validFv[i])) {
                Set<FpdModuleIdentification> set = fvs.get(validFv[i]);
                Iterator<FpdModuleIdentification> iter = set.iterator();
                while (iter.hasNext()) {
                    FpdModuleIdentification fpdModuleId = iter.next();
                    applySingleModule(document, ele, fpdModuleId);
                }
            }
        }
        
        //
        // Get all other modules
        //
        Iterator<String> fvsNameIter = fvs.keySet().iterator();
        
        while (fvsNameIter.hasNext()) {
            String fvName = fvsNameIter.next();
            if (!isContain(validFv, fvName)) {
                Set<FpdModuleIdentification> set = fvs.get(fvName);
                Iterator iter = set.iterator();
                while (iter.hasNext()) {
                    FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
                    applySingleModule(document, ele, fpdModuleId);
                }
            }
        }

        root.appendChild(ele);
    }
    
    private void applySingleModule(Document document, Node root, FpdModuleIdentification fpdModuleId) {
        ModuleIdentification moduleId = fpdModuleId.getModule();
        Element moduleEle = document.createElement("GenBuild");
        moduleEle.setAttribute("type", "build");
        //
        // Inherit Properties.
        //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
        //
        
        //
        // ARCH
        //
        Element property = document.createElement("property");
        property.setAttribute("name", "ARCH");
        property.setAttribute("value", fpdModuleId.getArch());
        moduleEle.appendChild(property);

        //
        // MODULE_GUID
        //
        property = document.createElement("property");
        property.setAttribute("name", "MODULE_GUID");
        property.setAttribute("value", moduleId.getGuid());
        moduleEle.appendChild(property);
        
        //
        // MODULE_VERSION
        //
        property = document.createElement("property");
        property.setAttribute("name", "MODULE_VERSION");
        property.setAttribute("value", moduleId.getVersion());
        moduleEle.appendChild(property);
        
        //
        // PACKAGE_GUID
        //
        property = document.createElement("property");
        property.setAttribute("name", "PACKAGE_GUID");
        property.setAttribute("value", moduleId.getPackage().getGuid());
        moduleEle.appendChild(property);
        
        //
        // PACKAGE_VERSION
        //
        property = document.createElement("property");
        property.setAttribute("name", "PACKAGE_VERSION");
        property.setAttribute("value", moduleId.getPackage().getVersion());
        moduleEle.appendChild(property);
        
        root.appendChild(moduleEle);
    }
    
    private boolean isContain(String[] list, String item) {
        for (int i = 0; i < list.length; i++) {
            if (list[i].equalsIgnoreCase(item)) {
                return true;
            }
        }
        return false;
    }
    
    private void applyFvs(Document document, Node root) {
        //
        // FVS Target
        //
        root.appendChild(document.createComment("FVs target"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "fvs");

        //
        // For every Target and ToolChain
        //
        String[] targetList = GlobalData.getToolChainInfo().getTargets();
        for (int i = 0; i < targetList.length; i++){
            String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
            for(int j = 0; j < toolchainList.length; j++){
                String fvOutputDir = project.getProperty("BUILD_DIR") + File.separatorChar 
                                        + targetList[i] + "_" 
                                        + toolchainList[j] + File.separatorChar + "FV";
                String[] validFv = saq.getFpdValidImageNames();
                for (int k = 0; k < validFv.length; k++) {
                    String inputFile = fvOutputDir + "" + File.separatorChar + validFv[k].toUpperCase() + ".inf";
                    Element fvEle = document.createElement("genfvimage");
                    fvEle.setAttribute("infFile", inputFile);
                    fvEle.setAttribute("outputDir", fvOutputDir);
                    ele.appendChild(fvEle);
                }
            }
        }
        root.appendChild(ele);
    }
    
    private void applyClean(Document document, Node root) {
        //
        // Clean Target
        //
        root.appendChild(document.createComment("Clean target"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "clean");

        if (isUnified) {
            Element cleanEle = document.createElement("delete");
            cleanEle.setAttribute("includeemptydirs", "true");
            Element filesetEle = document.createElement("fileset");
            filesetEle.setAttribute("dir", project.getProperty("BUILD_DIR"));
            filesetEle.setAttribute("includes", "**\\OUTPUT\\**");
            cleanEle.appendChild(filesetEle);
            ele.appendChild(cleanEle);
        } else {
            Set set = outfiles.keySet();
            Iterator iter = set.iterator();
            while (iter.hasNext()) {
                FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
                ModuleIdentification moduleId = fpdModuleId.getModule();

                Element ifEle = document.createElement("if");
                Element availableEle = document.createElement("available");
                availableEle.setAttribute("file", moduleId.getMsaFile().getParent() + File.separatorChar
                                                  + "build.xml");
                ifEle.appendChild(availableEle);
                Element elseEle = document.createElement("then");

                Element moduleEle = document.createElement("ant");
                moduleEle.setAttribute("antfile", moduleId.getMsaFile().getParent() + File.separatorChar
                                                  + "build.xml");
                moduleEle.setAttribute("target", "clean");
                //
                // Inherit Properties.
                //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
                //
                
                //
                // ARCH
                //
                Element property = document.createElement("property");
                property.setAttribute("name", "ARCH");
                property.setAttribute("value", fpdModuleId.getArch());
                moduleEle.appendChild(property);

                //
                // PACKAGE
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE");
                property.setAttribute("value", moduleId.getPackage().getName());
                moduleEle.appendChild(property);
                
                //
                // PACKAGE_GUID
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE_GUID");
                property.setAttribute("value", moduleId.getPackage().getGuid());
                moduleEle.appendChild(property);
                
                //
                // PACKAGE_VERSION
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE_VERSION");
                property.setAttribute("value", moduleId.getPackage().getVersion());
                moduleEle.appendChild(property);
                
                //
                // MODULE_DIR
                //
                property = document.createElement("property");
                property.setAttribute("name", "MODULE_DIR");
                property.setAttribute("value", moduleId.getMsaFile().getParent());
                moduleEle.appendChild(property);
                elseEle.appendChild(moduleEle);
                ifEle.appendChild(elseEle);
                ele.appendChild(ifEle);
            }
        }
        root.appendChild(ele);
    }
    
    private void applyCleanall(Document document, Node root) {
        //
        // Deep Clean Target
        //
        root.appendChild(document.createComment("Target: cleanall"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "cleanall");

        if (isUnified) {
            String[] targetList = GlobalData.getToolChainInfo().getTargets();
            for (int i = 0; i < targetList.length; ++i) {
                String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
                for(int j = 0; j < toolchainList.length; j++) {
                    Element cleanAllEle = document.createElement("delete");
                    cleanAllEle.setAttribute("dir", project.getProperty("BUILD_DIR") + File.separatorChar + targetList[i] + "_" + toolchainList[j]);
                    ele.appendChild(cleanAllEle);
                }
            }
        } else {
            Set set = outfiles.keySet();
            Iterator iter = set.iterator();
            while (iter.hasNext()) {
                FpdModuleIdentification fpdModuleId = (FpdModuleIdentification) iter.next();
                ModuleIdentification moduleId = fpdModuleId.getModule();

                Element ifEle = document.createElement("if");
                Element availableEle = document.createElement("available");
                availableEle.setAttribute("file", moduleId.getMsaFile().getParent() + File.separatorChar
                                                  + "build.xml");
                ifEle.appendChild(availableEle);
                Element elseEle = document.createElement("then");

                Element moduleEle = document.createElement("ant");
                moduleEle.setAttribute("antfile", moduleId.getMsaFile().getParent() + File.separatorChar
                                                  + "build.xml");
                moduleEle.setAttribute("target", "cleanall");
                //
                // Inherit Properties.
                //{"ARCH", "PACKAGE", "PACKAGE_GUID", "PACKAGE_VERSION", "MODULE_DIR"}
                //
                
                //
                // ARCH
                //
                Element property = document.createElement("property");
                property.setAttribute("name", "ARCH");
                property.setAttribute("value", fpdModuleId.getArch());
                moduleEle.appendChild(property);

                //
                // PACKAGE
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE");
                property.setAttribute("value", moduleId.getPackage().getName());
                moduleEle.appendChild(property);
                
                //
                // PACKAGE_GUID
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE_GUID");
                property.setAttribute("value", moduleId.getPackage().getGuid());
                moduleEle.appendChild(property);
                
                //
                // PACKAGE_VERSION
                //
                property = document.createElement("property");
                property.setAttribute("name", "PACKAGE_VERSION");
                property.setAttribute("value", moduleId.getPackage().getVersion());
                moduleEle.appendChild(property);
                
                //
                // MODULE_DIR
                //
                property = document.createElement("property");
                property.setAttribute("name", "MODULE_DIR");
                property.setAttribute("value", moduleId.getMsaFile().getParent());
                moduleEle.appendChild(property);
                elseEle.appendChild(moduleEle);
                ifEle.appendChild(elseEle);
                ele.appendChild(ifEle);
            }
        }
        root.appendChild(ele);
    }
    
    private void applyUserExtensionsPreBuild(Document document, Node root) {
        //
        // User Extensions
        //
        root.appendChild(document.createComment("Pre-Build Processing"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "prebuild");
        
        Node node = saq.getFpdUserExtensionPreBuild();
        if (node != null) {
            //
            // For every Target and ToolChain
            //
            String[] targetList = GlobalData.getToolChainInfo().getTargets();
            for (int i = 0; i < targetList.length; i++){
                String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
                for(int j = 0; j < toolchainList.length; j++){
                    //
                    // Prepare FV_DIR
                    //
                    String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar 
                                    + targetList[i] + "_" 
                                    + toolchainList[j];
                    File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
                    Element fvEle = document.createElement("var");
                    fvEle.setAttribute("name", "FV_DIR");
                    fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
                    ele.appendChild(fvEle);
                    
                    Element targetDirEle = document.createElement("var");
                    targetDirEle.setAttribute("name", "TARGET_DIR");
                    targetDirEle.setAttribute("value", ffsCommonDir.replaceAll("(\\\\)", "/"));
                    ele.appendChild(targetDirEle);
                    
                    NodeList childNodes = node.getChildNodes();
                    for (int k = 0; k < childNodes.getLength(); k++) {
                        Node childItem = childNodes.item(k);
                        if (childItem.getNodeType() == Node.ELEMENT_NODE) {
                            ele.appendChild(recursiveNode(childItem, document));
                        }
                    }
                }
            }
        }
        
        root.appendChild(ele);
    }
    
    private void applyUserExtensionsPostBuild(Document document, Node root) {
        //
        // User Extensions
        //
        root.appendChild(document.createComment("Post-Build Processing"));
        Element ele = document.createElement("target");
        ele.setAttribute("name", "postbuild");
        
        Node node = saq.getFpdUserExtensionPostBuild();
        if (node != null) {
            //
            // For every Target and ToolChain
            //
            String[] targetList = GlobalData.getToolChainInfo().getTargets();
            for (int i = 0; i < targetList.length; i++){
                String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
                for(int j = 0; j < toolchainList.length; j++){
                    //
                    // Prepare FV_DIR
                    //
                    String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar 
                                    + targetList[i] + "_" 
                                    + toolchainList[j];
                    File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
                    Element fvEle = document.createElement("var");
                    fvEle.setAttribute("name", "FV_DIR");
                    fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
                    ele.appendChild(fvEle);
                    
                    Element targetDirEle = document.createElement("var");
                    targetDirEle.setAttribute("name", "TARGET_DIR");
                    targetDirEle.setAttribute("value", ffsCommonDir.replaceAll("(\\\\)", "/"));
                    ele.appendChild(targetDirEle);
                    
                    NodeList childNodes = node.getChildNodes();
                    for (int k = 0; k < childNodes.getLength(); k++) {
                        Node childItem = childNodes.item(k);
                        if (childItem.getNodeType() == Node.ELEMENT_NODE) {
                            ele.appendChild(recursiveNode(childItem, document));
                        }
                    }
                
                }
            }
        }
        
        root.appendChild(ele);
    }

    private void applyUserExtensions(Document document, Node root) {
        Node[] nodeList = saq.getFpdUserExtensions();
        for (int nodeIndex = 0; nodeIndex < nodeList.length; ++nodeIndex) {
            Node node = nodeList[nodeIndex];
            //
            // User Extensions
            //
            root.appendChild(document.createComment("User Defined Target"));
            Element ele = document.createElement("target");
            ele.setAttribute("name", node.getAttributes().getNamedItem("Identifier").getNodeValue());

            if (node != null) {
                //
                // For every Target and ToolChain
                //
                String[] targetList = GlobalData.getToolChainInfo().getTargets();
                for (int i = 0; i < targetList.length; i++){
                    String[] toolchainList = GlobalData.getToolChainInfo().getTagnames();
                    for(int j = 0; j < toolchainList.length; j++){
                        //
                        // Prepare FV_DIR
                        //
                        String ffsCommonDir = project.getProperty("BUILD_DIR") + File.separatorChar 
                                        + targetList[i] + "_" 
                                        + toolchainList[j];
                        File fvDir = new File(ffsCommonDir + File.separatorChar + "FV");
                        Element fvEle = document.createElement("var");
                        fvEle.setAttribute("name", "FV_DIR");
                        fvEle.setAttribute("value", fvDir.getPath().replaceAll("(\\\\)", "/"));
                        ele.appendChild(fvEle);

                        Element targetDirEle = document.createElement("var");
                        targetDirEle.setAttribute("name", "TARGET_DIR");
                        targetDirEle.setAttribute("value", ffsCommonDir.replaceAll("(\\\\)", "/"));
                        ele.appendChild(targetDirEle);

                        NodeList childNodes = node.getChildNodes();
                        for (int k = 0; k < childNodes.getLength(); k++) {
                            Node childItem = childNodes.item(k);
                            if (childItem.getNodeType() == Node.ELEMENT_NODE) {
                                ele.appendChild(recursiveNode(childItem, document));
                            }
                        }

                    }
                }
            }

            root.appendChild(ele);
        }
    }

    private Element recursiveNode(Node node, Document document) {
        Element root = document.createElement(node.getNodeName());
        NamedNodeMap attr = node.getAttributes();
        for (int i = 0; i < attr.getLength(); i++) {
            Node attrItem = attr.item(i);
            root.setAttribute(attrItem.getNodeName(), attrItem.getNodeValue());
        }
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childItem = childNodes.item(i);
            if (childItem.getNodeType() == Node.ELEMENT_NODE) {
                root.appendChild(recursiveNode(childItem, document));
            }
            else if (childItem.getNodeType() == Node.TEXT_NODE){
                if (!childItem.getNodeValue().trim().equalsIgnoreCase("")) {
                    root.setTextContent(childItem.getNodeValue());
                }
            }
        }
        return root;
    }
}