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