summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting/src/com/motorolamobility/preflighting/internal/help/printer/HelpPrinter.java
blob: a85d8e0591301bb1c310416412faaacb1d742653 (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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.motorolamobility.preflighting.internal.help.printer;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.motorolamobility.preflighting.core.checker.CheckerDescription;
import com.motorolamobility.preflighting.core.checker.CheckerExtension;
import com.motorolamobility.preflighting.core.checker.IChecker;
import com.motorolamobility.preflighting.core.checker.condition.Condition;
import com.motorolamobility.preflighting.core.checker.condition.ICondition;
import com.motorolamobility.preflighting.core.devicespecification.DeviceSpecification;
import com.motorolamobility.preflighting.core.devicespecification.DevicesSpecsContainer;
import com.motorolamobility.preflighting.core.devicespecification.DevicesSpecsContainer.SpecKey;
import com.motorolamobility.preflighting.core.exception.PreflightingExtensionPointException;
import com.motorolamobility.preflighting.core.logging.PreflightingLogger;
import com.motorolamobility.preflighting.core.utils.XmlUtils;
import com.motorolamobility.preflighting.core.validation.ParameterDescription;
import com.motorolamobility.preflighting.core.validation.ValidationManager;
import com.motorolamobility.preflighting.core.validation.ValidationManagerConfiguration;
import com.motorolamobility.preflighting.core.validation.Value;
import com.motorolamobility.preflighting.i18n.PreflightingNLS;
import com.motorolamobility.preflighting.internal.PreflightingPlugin;

/**
 * Class responsible for printing help information to the console / command line.
 *
 */
public class HelpPrinter
{

    private static final String SEPARATOR = ",";

    private static final String XML_TAG_CHECKERS = "Checkers";

    private static final String XML_TAG_SPECS = "Specs";

    private static final String XML_TAG_SPEC = "Spec";

    private static final String XML_TAG_SPEC_DEVICES = "SpecDevices";

    private static final String XML_TAG_DEVICES = "Devices";

    private static final String ATTRIBUTE_LEVEL = "level";

    private static final String XML_TAG_CONDITION = "Condition";

    private static final String XML_TAG_VALUE_DESCRIPTION = "ValueDescription";

    private static final String ATTRIBUTE_NAME = "name";

    private static final String XML_TAG_PARAMETER = "Parameter";

    private static final String XML_TAG_DESCRIPTION = "Description";

    private static final String ATTRIBUTE_ID = "id";

    public static final String XML_TAG_CHECKER = "Checker";

    public static final String XML_TAG_DEVICE = "Device";

    public static Document devicesCheckersSpecsMapDocument = null;

    /**
     * Line separator
     */
    private final static String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$

    /**
     * Tab character
     */
    private final static String TAB = "\t"; //$NON-NLS-1$

    /**
     * I/O Exception message logging.
     */
    private final static String IO_ERROR = PreflightingNLS.HelpPrinter_IOExceptionMessage;

    private static final String XML_TAG_APP_VALIDATOR = "AppValidator";

    private static final String XML_ATTRIBUTE_APPVALIDATOR_VERSION = "version";

    private static final String XML_ATTRIBUTE_MOTODEV_LINK = "description_url";

    private static final HashMap<String, Document> cache = new HashMap<String, Document>();

    /**
     * Print help data.
     * @param parameters Help data
     * @param printSyntax Whether syntax should be printed or not
     * @param printStream 
     * @param checker checker to get the description and parameters returned
     */
    public static void printHelp(PrintStream printStream, List<ParameterDescription> parameters,
            boolean printSyntax)
    {
        if (parameters.size() > 0)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (printSyntax)
            {
                stringBuilder.append(PreflightingNLS.HelpPrinter_Usage + " ");
                stringBuilder.append("\t" + PreflightingNLS.HelpPrinter_ProgramName);
                stringBuilder
                        .append("[OPTION[PRM]]...\n\t" + PreflightingNLS.HelpPrinter_ProgramName + "[OPTION[PRM]]... -input [FILE] [OPTION[PRM]]...\n");//$NON-NLS-1$
                stringBuilder.append(PreflightingNLS.HelpPrinter_ProgramDescritpion + NEWLINE
                        + NEWLINE);
            }

            stringBuilder.append(PreflightingNLS.HelpPrinter_OptionsMessage + NEWLINE);
            // Iterate through the parameters and print the info
            for (ParameterDescription p : parameters)
            {
                /*
                 * The output format will be something like this:
                 * 
                 * Parameter info:
                 *  -{Parameter name} [TAB] Default value: {defaultValue} - {Description}
                 *      [TAB] {value1} Type: {type} - Description
                 */

                // Append parameter name
                if (p.getName() != null)
                {
                    stringBuilder.append("-" + p.getName()); //$NON-NLS-1$
                    int totalLenght = p.getName().length();
                    if (p.getValueDescription() != null)
                    {
                        stringBuilder.append(" " + p.getValueDescription()); //$NON-NLS-1$
                        totalLenght += p.getValueDescription().length() + 1;
                    }
                    if (totalLenght < 25)
                    {
                        int i = 25 - totalLenght;
                        while (i > 0)
                        {
                            stringBuilder.append(" ");
                            i--;
                        }
                    }
                }

                // Append default value
                if (p.getDefaultValue() != null)
                {
                    stringBuilder.append(PreflightingNLS.HelpPrinter_DefaultMessage
                            + p.getDefaultValue().getValue());
                    stringBuilder.append(" "); //$NON-NLS-1$
                }

                // Append description
                if (p.getDescription() != null)
                {
                    stringBuilder.append("- "); //$NON-NLS-1$
                    stringBuilder.append(p.getDescription());
                    stringBuilder.append(NEWLINE);
                }

                // Append values information
                for (Value v : p.getAllowedValues())
                {
                    stringBuilder.append(TAB);

                    // Append value
                    if (v.getValue() != null)
                    {
                        stringBuilder.append(v.getValue());
                        stringBuilder.append(" - "); //$NON-NLS-1$
                    }

                    // Append description
                    if (v.getDescription() != null)
                    {
                        stringBuilder.append(v.getDescription());
                        stringBuilder.append(NEWLINE);
                    }
                }

            }

            printStream.print(stringBuilder.toString());
            printStream.flush();
        }

    }

    /**
     * Prints a XML file which contains a list of devices, their id for App Validator and provider
     * @param stream The stream used to print the XML file
     * @throws ParserConfigurationException 
     * @throws TransformerException 
     * @throws UnsupportedEncodingException 
     */
    public static void printXMLDevicesList(PrintStream stream) throws ParserConfigurationException,
            UnsupportedEncodingException, TransformerException
    {

        ValidationManager validationManager = new ValidationManager();

        DevicesSpecsContainer devicesSpecsContainer = validationManager.getDevicesSpecsContainer();

        Document document =
                DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument();
        Element appvalidatorElement = createAppvalidatorNode(document);

        for (DeviceSpecification deviceInfo : devicesSpecsContainer.getDeviceSpecifications())
        {
            appvalidatorElement.appendChild(createDeviceNode(deviceInfo, document));
        }

        XmlUtils.printXMLFormat(document);

        validationManager = null;
    }

    private static Element createDeviceNode(DeviceSpecification deviceInfo, Document document)
    {
        Element deviceNode = document.createElement(XML_TAG_DEVICE);
        deviceNode.setTextContent(deviceInfo.getName());

        deviceNode.setAttribute("id", deviceInfo.getId());
        deviceNode.setAttribute("provider", deviceInfo.getProvider());

        return deviceNode;
    }

    /**
     * Prints a XML file which contains a list of checkers and all the information regarding each checker
     * @param stream The stream used to print the XML file
     */
    public static void printXMLCheckerList(PrintStream stream)
            throws PreflightingExtensionPointException, ParserConfigurationException,
            UnsupportedEncodingException, TransformerException
    {

        Map<String, CheckerExtension> checkers = ValidationManager.loadCheckers();
        Collection<CheckerExtension> checkerExtensions = checkers.values();

        Document document =
                DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument();
        Element appvalidatorElement = null;

        appvalidatorElement = createAppvalidatorNode(document);

        for (CheckerExtension extension : checkerExtensions)
        {
            Element node = getXMLCheckerNode(extension, document);
            appvalidatorElement.appendChild(node);
        }

        XmlUtils.printXMLFormat(document);

    }

    private static Element createAppvalidatorNode(Document document)
    {

        Element appValElem = document.createElement(XML_TAG_APP_VALIDATOR);
        document.appendChild(appValElem);
        String appValidatorVersion = PreflightingPlugin.getInstance().getAppValidatorVersion();
        ValidationManagerConfiguration valManagerConfiguration =
                ValidationManagerConfiguration.getInstance();
        String motodevLink =
                valManagerConfiguration
                        .getProperty(ValidationManagerConfiguration.ConfigProperties.BASE_URL_PROPERTY
                                .getName());

        appValElem.setAttribute(XML_ATTRIBUTE_APPVALIDATOR_VERSION, appValidatorVersion);
        appValElem.setAttribute(XML_ATTRIBUTE_MOTODEV_LINK, motodevLink);

        return appValElem;
    }

    /**
     * Generate a XML node for a specific checker
     */
    private static Element getXMLCheckerNode(CheckerExtension extension, Document document)
    {

        Element checkerNode = document.createElement(XML_TAG_CHECKER);
        checkerNode.setAttribute(ATTRIBUTE_ID, extension.getId());

        Element descriptionNode = document.createElement(XML_TAG_DESCRIPTION);
        descriptionNode.setTextContent(extension.getDescription());

        checkerNode.appendChild(descriptionNode);

        IChecker checker = extension.getChecker();
        List<Condition> conditionList = getSortedConditions(checker);

        appendConditions(checkerNode, document, conditionList);
        appendParameters(checkerNode, document, checker);

        return checkerNode;
    }

    private static void appendParameters(Element checkerNode, Document document, IChecker checker)
    {
        List<ParameterDescription> paremeterDescriptionList = checker.getParameterDescriptions();
        if ((paremeterDescriptionList != null) && (paremeterDescriptionList.size() > 0))
        {
            for (ParameterDescription param : paremeterDescriptionList)
            {
                Element parameterNode = document.createElement(XML_TAG_PARAMETER);
                parameterNode.setAttribute(ATTRIBUTE_NAME, param.getName());

                Element valueDescriptionNode = document.createElement(XML_TAG_VALUE_DESCRIPTION);
                valueDescriptionNode.setTextContent(param.getValueDescription());

                Element paramDescrNode = document.createElement(XML_TAG_DESCRIPTION);
                paramDescrNode.setTextContent(param.getDescription());

                parameterNode.appendChild(valueDescriptionNode);
                parameterNode.appendChild(paramDescrNode);
                checkerNode.appendChild(parameterNode);
            }
        }
    }

    private static void appendConditions(Element checkerNode, Document document,
            List<Condition> conditionList)
    {
        for (ICondition condition : conditionList)
        {
            Element conditionNode = document.createElement(XML_TAG_CONDITION);
            conditionNode.setAttribute(ATTRIBUTE_ID, condition.getId());
            conditionNode.setAttribute(ATTRIBUTE_LEVEL, condition.getSeverityLevel().toString());

            Element conditionDescrNode = document.createElement(XML_TAG_DESCRIPTION);
            conditionDescrNode.setTextContent(condition.getDescription());

            conditionNode.appendChild(conditionDescrNode);
            checkerNode.appendChild(conditionNode);
        }
    }

    /**
     * Prints the description from the extension point declaration and 
     * print the parameters returned by the checker itself.
     * @param checkerExtension
     */
    public static void printHelpChecker(PrintStream out, CheckerExtension checkerExtension)
    {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(PreflightingNLS.HelperPrinter_CheckerId + checkerExtension.getId()
                + NEWLINE);
        stringBuilder.append(PreflightingNLS.HelperPrinter_CheckerDescription
                + checkerExtension.getDescription() + NEWLINE);

        IChecker checker = checkerExtension.getChecker();

        // Append condition information
        List<Condition> conditionList = getSortedConditions(checker);

        // Size < 0 should never happen, but you never can be too sure...
        if ((conditionList != null) && (conditionList.size() > 0))
        {
            // Iterate through the condition and print the descriptions
            stringBuilder.append(NEWLINE);
            stringBuilder.append(PreflightingNLS.HelperPrinter_CheckerHasConditions + NEWLINE);

            stringBuilder.append(NEWLINE);
            stringBuilder.append(TAB);
            stringBuilder.append("Default level");
            stringBuilder.append(TAB);

            stringBuilder.append("Condition ID");
            stringBuilder.append(TAB);
            stringBuilder.append(TAB);
            stringBuilder.append(TAB);
            stringBuilder.append("Description");
            stringBuilder.append(NEWLINE);

            for (ICondition c : conditionList)
            {
                stringBuilder.append(TAB);
                stringBuilder.append(c.getSeverityLevel().toString());
                stringBuilder.append(TAB);
                stringBuilder.append(TAB);
                stringBuilder.append(c.getId());
                stringBuilder.append(TAB);
                if (c.getId().length() < 24)
                {
                    stringBuilder.append(TAB);
                }
                if (c.getId().length() < 15)
                {
                    stringBuilder.append(TAB);
                }
                stringBuilder.append(c.getDescription());
                stringBuilder.append(NEWLINE);
            }

            stringBuilder.append(NEWLINE);
        }

        List<ParameterDescription> paremeterDescriptionList = checker.getParameterDescriptions();
        if (paremeterDescriptionList != null)
        {

            if (paremeterDescriptionList.size() > 0)
            {
                stringBuilder.append(PreflightingNLS.HelperPrinter_CheckerUsesParameters + NEWLINE);
            }
            else
            {
                stringBuilder.append(PreflightingNLS.HelperPrinter_CheckerDoesNotUseParameters
                        + NEWLINE);
            }
            for (ParameterDescription returnParam : paremeterDescriptionList)
            {
                String paramMessage =
                        TAB + returnParam.getName() + " = " + returnParam.getValueDescription();
                if ((returnParam.getType() != null) && (returnParam.getType().toString() != null))
                {
                    paramMessage += " [" + returnParam.getType().toString() + "]";
                }
                paramMessage += TAB + "- " + returnParam.getDescription() + NEWLINE;
                stringBuilder.append(paramMessage);
            }
        }

        out.print(stringBuilder.toString());
    }

    /**
     * Return a sorted list of checker conditions according to it default level
     * 
     * @param checker the checker what have the conditions
     * @return the sorted list of conditions
     */
    private static List<Condition> getSortedConditions(IChecker checker)
    {
        List<Condition> conditionList = new ArrayList(checker.getConditions().values());

        // Sort the conditions list
        Collections.sort(conditionList, new Comparator<Condition>()
        {
            public int compare(Condition o1, Condition o2)
            {
                if (o1.getSeverityLevel().ordinal() > o2.getSeverityLevel().ordinal())
                {
                    return +1;
                }
                else
                {
                    return -1;
                }
            }
        });
        return conditionList;
    }

    public static void printDevicesList(List<Value> list, PrintStream printStream)
    {
        if (list.size() > 0)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append(PreflightingNLS.HelpPrinter_Device_Id + " name - [id]");
            stringBuilder.append(NEWLINE);
            for (Value current : list)
            {
                //Append device ID
                stringBuilder.append(TAB);
                stringBuilder.append(current.getValue());
                stringBuilder.append(NEWLINE);
            }
            printStream.print(stringBuilder.toString());
            // Clean string builder
            stringBuilder.delete(0, stringBuilder.length());
        }
    }

    public static void printDevicesDescription(String deviceDescription, String deviceId,
            PrintStream printStream)
    {
        if ((deviceDescription != null) && !deviceDescription.trim().equals(""))
        {
            printStream.print(deviceDescription);
            printStream.println();
        }
        else
        {
            //device id not found
            printStream.print(PreflightingNLS
                    .bind(PreflightingNLS.GlobalInputParamsValidator_NonExistentDeviceIdMessage,
                            deviceId));
        }
    }

    public static void printCheckersList(List<CheckerDescription> list, PrintStream printStream)
    {
        if (list.size() > 0)
        {
            StringBuilder stringBuilder = new StringBuilder();

            /*
             * Header
             */

            // Iterate through the values and print the info                
            for (CheckerDescription checker : list)
            {
                /*
                 * The output format will be something like this:
                 * 
                 * {value} {TAB} {Description}
                 */

                //Append ID
                stringBuilder.append(PreflightingNLS.HelpPrinter_Checker_Id);
                stringBuilder.append(TAB);
                stringBuilder.append(TAB);
                stringBuilder.append(checker.getId());
                stringBuilder.append(NEWLINE);

                //Append Name
                stringBuilder.append(PreflightingNLS.HelpPrinter_Checker_Name);
                stringBuilder.append(TAB);
                stringBuilder.append(TAB);
                if ((checker.getName() != null))
                {
                    stringBuilder.append(checker.getName());
                }
                else
                {
                    stringBuilder.append(PreflightingNLS.HelpPrinter_Checker_NotAvailable);
                }
                stringBuilder.append(NEWLINE);

                //Append Description
                stringBuilder.append(PreflightingNLS.HelpPrinter_Checker_Description);
                stringBuilder.append(TAB);
                if ((checker.getName() != null))
                {
                    stringBuilder.append(checker.getDescription());
                }
                else
                {
                    stringBuilder.append(PreflightingNLS.HelpPrinter_Checker_NotAvailable);
                }
                stringBuilder.append(NEWLINE);
                stringBuilder.append(NEWLINE);

                printStream.print(stringBuilder.toString());

                // Clean string builder
                stringBuilder.delete(0, stringBuilder.length());
            }
        }
    }

    /**
     * Print a list of values.
     * @param values The list of values
     * @param stream The output stream
     */
    public static void printList(List<Value> values, OutputStream stream)
    {
        if (values.size() > 0)
        {
            /*
             * Writer used for output
             */
            BufferedWriter writer = null;
            StringBuilder stringBuilder = new StringBuilder();

            writer = new BufferedWriter(new OutputStreamWriter(stream));

            try
            {
                // Iterate through the values and print the info
                for (Value v : values)
                {
                    /*
                     * The output format will be something like this:
                     * 
                     * {value} {TAB} {Description}
                     */

                    stringBuilder.append(v.getValue());
                    stringBuilder.append(TAB);
                    if (v.getDescription() != null)
                    {
                        stringBuilder.append(v.getDescription());
                    }

                    writer.write(stringBuilder.toString());
                    writer.flush();

                    // Clean string builder
                    stringBuilder.delete(0, stringBuilder.length());

                }
            }
            catch (IOException e)
            {

                PreflightingLogger.error(HelpPrinter.class, IO_ERROR + e.getMessage());

            }
            finally
            {
                try
                {
                    writer.close();
                }
                catch (IOException e)
                {
                    PreflightingLogger.error(HelpPrinter.class, IO_ERROR + e.getMessage());
                }
            }
        }

    }

    private static Element createSpecDevicesMapNode(ValidationManager validationManager,
            Document document, String categories)
    {
        Element specDevicesNode = document.createElement(XML_TAG_SPEC_DEVICES);
        Map<SpecKey, List<DeviceSpecification>> specDevsMap =
                validationManager.getDevicesSpecsContainer().getSpecDevFilterMap();

        //Set<SpecKey> specs = specDevsMap.keySet();
        String[] specs = categories.split(SEPARATOR);

        for (String specStr : specs)
        {
            SpecKey spec = SpecKey.valueOf(specStr);
            Element specNode = document.createElement(XML_TAG_SPEC);
            specNode.setAttribute(ATTRIBUTE_ID, spec.getId());

            List<DeviceSpecification> devs = specDevsMap.get(spec);

            if (devs != null)
            {
                for (DeviceSpecification dev : devs)
                {
                    Element devNode = document.createElement(XML_TAG_DEVICE);
                    devNode.setAttribute(ATTRIBUTE_ID, dev.getId());
                    specNode.appendChild(devNode);
                }
            }

            specDevicesNode.appendChild(specNode);
        }

        return specDevicesNode;
    }

    private static Element creatSpecsNode(ValidationManager validationManager, Document document,
            String categories)
    {

        Element specsNode = document.createElement(XML_TAG_SPECS);
        String[] specList = categories.split(SEPARATOR);

        //SpecKey[] specs = SpecKey.values();
        //for (SpecKey spec : specs)
        for (String spec : specList)
        {
            Element specNode = document.createElement(XML_TAG_SPEC);
            specNode.setAttribute(ATTRIBUTE_ID, spec);//spec.getId());
            specNode.setAttribute(ATTRIBUTE_NAME, spec);//spec.getId());

            specsNode.appendChild(specNode);
        }

        return specsNode;
    }

    private static Element createCheckersNode(ValidationManager validationManager, Document document)
    {

        Element checkersNode = document.createElement(XML_TAG_CHECKERS);

        List<CheckerDescription> checkers = validationManager.getCheckersDescription();

        for (CheckerDescription checker : checkers)
        {
            Element checkerNode = document.createElement(XML_TAG_CHECKER);
            checkerNode.setAttribute(ATTRIBUTE_ID, checker.getId());
            checkerNode.setAttribute(ATTRIBUTE_NAME, checker.getName());

            checkersNode.appendChild(checkerNode);
        }

        return checkersNode;
    }

    private static Element createDevicesNode(ValidationManager validationManager, Document document)
    {

        Element devicesNode = document.createElement(XML_TAG_DEVICES);
        DevicesSpecsContainer devicesSpecsContainer = validationManager.getDevicesSpecsContainer();
        List<DeviceSpecification> devices = devicesSpecsContainer.getDeviceSpecifications();

        // order by device name
        Collections.sort(devices, new Comparator<DeviceSpecification>()
        {
            public int compare(DeviceSpecification d1, DeviceSpecification d2)
            {
                return d1.getName().compareToIgnoreCase(d2.getName());
            }
        });

        for (DeviceSpecification devSpec : devices)
        {

            Element deviceNode = document.createElement(XML_TAG_DEVICE);

            deviceNode.setAttribute(ATTRIBUTE_ID, devSpec.getId());
            deviceNode.setAttribute(ATTRIBUTE_NAME, devSpec.getName().replace("™", "&#8482;"));

            devicesNode.appendChild(deviceNode);
        }

        return devicesNode;
    }

    /**
     * Print an XML file with: list of checkers, list of devices, map of specifications vs devices
     * @param stream The output stream
     * @throws ParserConfigurationException 
     * @throws TransformerException 
     * @throws UnsupportedEncodingException 
     */
    public static void printXMLDevicesCheckersSpecsMap(PrintStream stream)
            throws ParserConfigurationException, UnsupportedEncodingException, TransformerException
    {

        // TODO: implement a configuration file or another way to keep this list
        String categories =
                SpecKey.screenSize_Small.getId() + SEPARATOR + SpecKey.screenSize_XLarge.getId()
                        + SEPARATOR + SpecKey.screenOrientation_Port.getId() + SEPARATOR
                        + SpecKey.screenOrientation_Land.getId() + SEPARATOR
                        + SpecKey.pixelDensity_High.getId() + SEPARATOR
                        + SpecKey.pixelDensity_Low.getId() + SEPARATOR
                        + SpecKey.pixelDensity_Medium.getId() + SEPARATOR
                        + SpecKey.pixelDensity_XHigh.getId();

        if (cache.get(categories) == null)
        {
            Document document =
                    DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().newDocument();
            Element appvalidatorElement = createAppvalidatorNode(document);

            ValidationManager validationManager = new ValidationManager();

            Element checkersNode = createCheckersNode(validationManager, document);
            appvalidatorElement.appendChild(checkersNode);

            Element devicesNode = createDevicesNode(validationManager, document);
            appvalidatorElement.appendChild(devicesNode);

            Element specsNode = creatSpecsNode(validationManager, document, categories);
            appvalidatorElement.appendChild(specsNode);

            Element specDevicesMapNode =
                    createSpecDevicesMapNode(validationManager, document, categories);
            appvalidatorElement.appendChild(specDevicesMapNode);

            cache.put(categories, document);
        }

        XmlUtils.printXMLFormat(cache.get(categories));

        XmlUtils.printXMLFormat(cache.get(categories));

    }

}