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