1## @file
2# This file is used to parse a xml file of .PKG file
3#
4# Copyright (c) 2011, 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'''
16GuidProtocolPpiXml
17'''
18from Library.String import ConvertNEToNOTEQ
19from Library.String import ConvertNOTEQToNE
20from Library.String import GetStringOfList
21from Library.Xml.XmlRoutines import XmlElement
22from Library.Xml.XmlRoutines import XmlAttribute
23from Library.Xml.XmlRoutines import XmlNode
24from Library.Xml.XmlRoutines import XmlList
25from Library.Xml.XmlRoutines import CreateXmlElement
26
27from Object.POM.CommonObject import GuidObject
28from Object.POM.CommonObject import ProtocolObject
29from Object.POM.CommonObject import PpiObject
30
31from Xml.CommonXml import CommonDefinesXml
32from Xml.CommonXml import HelpTextXml
33
34from Xml.XmlParserMisc import GetHelpTextList
35
36##
37#GUID/Protocol/Ppi Common
38#
39class GuidProtocolPpiXml(object):
40    def __init__(self, Mode):
41        self.UiName = ''
42        self.GuidTypes = ''
43        self.Notify = ''
44        self.CName = ''
45        self.GuidValue = ''
46        self.CommonDefines = CommonDefinesXml()
47        self.HelpText = []
48        #
49        # Guid/Ppi/Library, internal used for indicate return object for
50        # FromXml
51        #
52        self.Type = ''
53        #
54        # there are slightly different field between package and module
55        #
56        self.Mode = Mode
57        self.GuidType = ''
58        self.VariableName = ''
59
60    def FromXml(self, Item, Key):
61        self.UiName = XmlAttribute(XmlNode(Item, '%s' % Key), 'UiName')
62        self.GuidType = XmlAttribute(XmlNode(Item, '%s' % Key), 'GuidType')
63        self.Notify = XmlAttribute(XmlNode(Item, '%s' % Key), 'Notify')
64        self.CName = XmlElement(Item, '%s/CName' % Key)
65        self.GuidValue = XmlElement(Item, '%s/GuidValue' % Key)
66        self.VariableName = XmlElement(Item, '%s/VariableName' % Key)
67        self.CommonDefines.FromXml(XmlNode(Item, '%s' % Key), Key)
68        for HelpTextItem in XmlList(Item, '%s/HelpText' % Key):
69            HelpTextObj = HelpTextXml()
70            HelpTextObj.FromXml(HelpTextItem, '%s/HelpText' % Key)
71            self.HelpText.append(HelpTextObj)
72
73        if self.Type == 'Guid':
74            GuidProtocolPpi = GuidObject()
75        elif self.Type == 'Protocol':
76            GuidProtocolPpi = ProtocolObject()
77        else:
78            GuidProtocolPpi = PpiObject()
79        GuidProtocolPpi.SetHelpTextList(GetHelpTextList(self.HelpText))
80
81        return GuidProtocolPpi
82
83    def ToXml(self, GuidProtocolPpi, Key):
84        if self.GuidValue:
85            pass
86        AttributeList = \
87        [['Usage', GetStringOfList(GuidProtocolPpi.GetUsage())], \
88         ['UiName', GuidProtocolPpi.GetName()], \
89         ['GuidType', GetStringOfList(GuidProtocolPpi.GetGuidTypeList())], \
90         ['Notify', str(GuidProtocolPpi.GetNotify()).lower()], \
91         ['SupArchList', GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
92         ['SupModList', GetStringOfList(GuidProtocolPpi.GetSupModuleList())], \
93         ['FeatureFlag', ConvertNEToNOTEQ(GuidProtocolPpi.GetFeatureFlag())]
94        ]
95        NodeList = [['CName', GuidProtocolPpi.GetCName()],
96                    ['GuidValue', GuidProtocolPpi.GetGuid()],
97                    ['VariableName', GuidProtocolPpi.VariableName]
98                   ]
99        for Item in GuidProtocolPpi.GetHelpTextList():
100            Tmp = HelpTextXml()
101            NodeList.append(Tmp.ToXml(Item))
102        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
103
104        return Root
105
106    def __str__(self):
107        Str = \
108        "UiName = %s Notify = %s GuidTypes = %s CName = %s GuidValue = %s %s" \
109        % (self.UiName, self.Notify, self.GuidTypes, self.CName, \
110           self.GuidValue, self.CommonDefines)
111        for Item in self.HelpText:
112            Str = Str + "\n\t" + str(Item)
113        return Str
114##
115#GUID Xml
116#
117class GuidXml(GuidProtocolPpiXml):
118    def __init__(self, Mode):
119        GuidProtocolPpiXml.__init__(self, Mode)
120        self.Type = 'Guid'
121
122    def FromXml(self, Item, Key):
123        GuidProtocolPpi = GuidProtocolPpiXml.FromXml(self, Item, Key)
124
125        if self.Mode == 'Package':
126
127            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
128            GuidProtocolPpi.SetSupModuleList(self.CommonDefines.SupModList)
129            GuidProtocolPpi.SetCName(self.CName)
130            GuidProtocolPpi.SetGuid(self.GuidValue)
131        else:
132            GuidProtocolPpi.SetUsage(self.CommonDefines.Usage)
133            if self.GuidType:
134                GuidProtocolPpi.SetGuidTypeList([self.GuidType])
135            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
136            GuidProtocolPpi.SetFeatureFlag(ConvertNOTEQToNE(self.CommonDefines.FeatureFlag))
137            GuidProtocolPpi.SetCName(self.CName)
138            GuidProtocolPpi.SetVariableName(self.VariableName)
139        return GuidProtocolPpi
140
141    def ToXml(self, GuidProtocolPpi, Key):
142        if self.Mode == 'Package':
143            AttributeList = \
144            [['GuidType', \
145              GetStringOfList(GuidProtocolPpi.GetGuidTypeList())], \
146              ['SupArchList', \
147               GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
148               ['SupModList', \
149                GetStringOfList(GuidProtocolPpi.GetSupModuleList())],
150            ]
151            NodeList = [['CName', GuidProtocolPpi.GetCName()],
152                        ['GuidValue', GuidProtocolPpi.GetGuid()],
153                       ]
154        else:
155            AttributeList = \
156            [['Usage', GetStringOfList(GuidProtocolPpi.GetUsage())], \
157             ['GuidType', GetStringOfList(GuidProtocolPpi.GetGuidTypeList())],\
158              ['SupArchList', \
159               GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
160               ['FeatureFlag', ConvertNEToNOTEQ(GuidProtocolPpi.GetFeatureFlag())]
161            ]
162            NodeList = [['CName', GuidProtocolPpi.GetCName()],
163                        ['VariableName', GuidProtocolPpi.GetVariableName()]
164                       ]
165
166        for Item in GuidProtocolPpi.GetHelpTextList():
167            Tmp = HelpTextXml()
168            NodeList.append(Tmp.ToXml(Item))
169        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
170
171        return Root
172##
173#Protocol Xml
174#
175class ProtocolXml(GuidProtocolPpiXml):
176    def __init__(self, Mode):
177        GuidProtocolPpiXml.__init__(self, Mode)
178        self.Type = 'Protocol'
179
180    def FromXml(self, Item, Key):
181        GuidProtocolPpi = GuidProtocolPpiXml.FromXml(self, Item, Key)
182        if self.Mode == 'Package':
183            GuidProtocolPpi.SetFeatureFlag(self.CommonDefines.FeatureFlag)
184            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
185            GuidProtocolPpi.SetSupModuleList(self.CommonDefines.SupModList)
186            GuidProtocolPpi.SetCName(self.CName)
187            GuidProtocolPpi.SetGuid(self.GuidValue)
188        else:
189            GuidProtocolPpi.SetUsage(self.CommonDefines.Usage)
190            if self.Notify.upper() == "TRUE":
191                GuidProtocolPpi.SetNotify(True)
192            elif self.Notify.upper() == "FALSE":
193                GuidProtocolPpi.SetNotify(False)
194            else:
195                GuidProtocolPpi.SetNotify('')
196            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
197            GuidProtocolPpi.SetFeatureFlag(ConvertNOTEQToNE(self.CommonDefines.FeatureFlag))
198            GuidProtocolPpi.SetCName(self.CName)
199
200        return GuidProtocolPpi
201
202    def ToXml(self, GuidProtocolPpi, Key):
203        if self.Mode == 'Package':
204            AttributeList = \
205            [['SupArchList', \
206              GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
207              ['SupModList', \
208               GetStringOfList(GuidProtocolPpi.GetSupModuleList())], \
209               ['FeatureFlag', GuidProtocolPpi.GetFeatureFlag()]
210            ]
211            NodeList = [['CName', GuidProtocolPpi.GetCName()],
212                        ['GuidValue', GuidProtocolPpi.GetGuid()],
213                       ]
214        else:
215            AttributeList = \
216            [['Usage', GetStringOfList(GuidProtocolPpi.GetUsage())], \
217             ['Notify', str(GuidProtocolPpi.GetNotify()).lower()], \
218             ['SupArchList', \
219              GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
220              ['FeatureFlag', ConvertNEToNOTEQ(GuidProtocolPpi.GetFeatureFlag())]
221            ]
222            NodeList = [['CName', GuidProtocolPpi.GetCName()],
223                       ]
224
225        for Item in GuidProtocolPpi.GetHelpTextList():
226            Tmp = HelpTextXml()
227            NodeList.append(Tmp.ToXml(Item))
228        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
229
230        return Root
231##
232#Ppi Xml
233#
234class PpiXml(GuidProtocolPpiXml):
235    def __init__(self, Mode):
236        GuidProtocolPpiXml.__init__(self, Mode)
237        self.Type = 'Ppi'
238
239    def FromXml(self, Item, Key):
240        GuidProtocolPpi = GuidProtocolPpiXml.FromXml(self, Item, Key)
241        if self.Mode == 'Package':
242            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
243            GuidProtocolPpi.SetSupModuleList(self.CommonDefines.SupModList)
244            GuidProtocolPpi.SetCName(self.CName)
245            GuidProtocolPpi.SetGuid(self.GuidValue)
246        else:
247            GuidProtocolPpi.SetUsage(self.CommonDefines.Usage)
248            if self.Notify.upper() == "TRUE":
249                GuidProtocolPpi.SetNotify(True)
250            elif self.Notify.upper() == "FALSE":
251                GuidProtocolPpi.SetNotify(False)
252            else:
253                GuidProtocolPpi.SetNotify('')
254            GuidProtocolPpi.SetSupArchList(self.CommonDefines.SupArchList)
255            GuidProtocolPpi.SetFeatureFlag(ConvertNOTEQToNE(self.CommonDefines.FeatureFlag))
256            GuidProtocolPpi.SetCName(self.CName)
257
258        return GuidProtocolPpi
259
260    def ToXml(self, GuidProtocolPpi, Key):
261        if self.Mode == 'Package':
262            AttributeList = \
263            [['SupArchList', \
264              GetStringOfList(GuidProtocolPpi.GetSupArchList())],
265            ]
266            NodeList = [['CName', GuidProtocolPpi.GetCName()],
267                        ['GuidValue', GuidProtocolPpi.GetGuid()],
268                       ]
269        else:
270            AttributeList = \
271            [['Usage', GetStringOfList(GuidProtocolPpi.GetUsage())], \
272             ['Notify', str(GuidProtocolPpi.GetNotify()).lower()], \
273             ['SupArchList', \
274              GetStringOfList(GuidProtocolPpi.GetSupArchList())], \
275              ['FeatureFlag', ConvertNEToNOTEQ(GuidProtocolPpi.GetFeatureFlag())]
276            ]
277            NodeList = [['CName', GuidProtocolPpi.GetCName()],
278                       ]
279
280        for Item in GuidProtocolPpi.GetHelpTextList():
281            Tmp = HelpTextXml()
282            NodeList.append(Tmp.ToXml(Item))
283        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
284        return Root
285