1## @file
2# This file is used to parse a xml file of .PKG file
3#
4# Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
5#
6# This program and the accompanying materials are licensed and made available
7# under the terms and conditions of the BSD License which accompanies this
8# distribution. The full text of the license may be found at
9# http://opensource.org/licenses/bsd-license.php
10#
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13#
14
15'''
16XmlParserMisc
17'''
18from Object.POM.CommonObject import TextObject
19from Logger.StringTable import ERR_XML_PARSER_REQUIRED_ITEM_MISSING
20from Logger.ToolError import PARSER_ERROR
21import Logger.Log as Logger
22
23## ConvertVariableName()
24# Convert VariableName to be L"string",
25# input of UCS-2 format Hex Array or L"string" (C style.) could be converted successfully,
26# others will not.
27#
28# @param VariableName: string need to be converted
29# @retval: the L quoted string converted if success, else None will be returned
30#
31def ConvertVariableName(VariableName):
32    VariableName = VariableName.strip()
33    #
34    # check for L quoted string
35    #
36    if VariableName.startswith('L"') and VariableName.endswith('"'):
37        return VariableName
38
39    #
40    # check for Hex Array, it should be little endian even number of hex numbers
41    #
42    ValueList = VariableName.split(' ')
43    if len(ValueList)%2 == 1:
44        return None
45
46    TransferedStr = ''
47
48    Index = 0
49
50    while Index < len(ValueList):
51        FirstByte = int(ValueList[Index], 16)
52        SecondByte = int(ValueList[Index + 1], 16)
53        if SecondByte != 0:
54            return None
55
56        if FirstByte not in xrange(0x20, 0x7F):
57            return None
58        TransferedStr += ('%c')%FirstByte
59        Index = Index + 2
60
61    return 'L"' + TransferedStr + '"'
62
63## IsRequiredItemListNull
64#
65# Check if a required XML section item/attribue is NULL
66#
67# @param ItemList:     The list of items to be checked
68# @param XmlTreeLevel: The error message tree level
69#
70def IsRequiredItemListNull(ItemDict, XmlTreeLevel):
71    for Key in ItemDict:
72        if not ItemDict[Key]:
73            Msg = "->".join(Node for Node in XmlTreeLevel)
74            ErrorMsg = ERR_XML_PARSER_REQUIRED_ITEM_MISSING % (Key, Msg)
75            Logger.Error('\nUPT', PARSER_ERROR, ErrorMsg, RaiseError=True)
76
77## Get help text
78#
79# @param HelpText
80#
81def GetHelpTextList(HelpText):
82    HelpTextList = []
83    for HelT in HelpText:
84        HelpTextObj = TextObject()
85        HelpTextObj.SetLang(HelT.Lang)
86        HelpTextObj.SetString(HelT.HelpText)
87        HelpTextList.append(HelpTextObj)
88    return HelpTextList
89
90## Get Prompt text
91#
92# @param Prompt
93#
94def GetPromptList(Prompt):
95    PromptList = []
96    for SubPrompt in Prompt:
97        PromptObj = TextObject()
98        PromptObj.SetLang(SubPrompt.Lang)
99        PromptObj.SetString(SubPrompt.Prompt)
100        PromptList.append(PromptObj)
101    return PromptList
102