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