1# Copyright (c) 2017, The MITRE Corporation. All rights reserved.
2# See LICENSE.txt for complete terms.
3
4import sys
5
6from mixbox.binding_utils import *
7from . import cybox_common
8from . import gui_object
9
10
11class GUIDialogboxObjectType(gui_object.GUIObjectType):
12    """The GUIDialogboxObjectType type is intended to characterize GUI
13    dialog boxes."""
14
15    subclass = None
16    superclass = gui_object.GUIObjectType
17    def __init__(self, object_reference=None, Custom_Properties=None, xsi_type=None, Height=None, Width=None, Box_Caption=None, Box_Text=None):
18        super(GUIDialogboxObjectType, self).__init__(object_reference, Custom_Properties, Height, Width, )
19        self.Box_Caption = Box_Caption
20        self.Box_Text = Box_Text
21    def factory(*args_, **kwargs_):
22        if GUIDialogboxObjectType.subclass:
23            return GUIDialogboxObjectType.subclass(*args_, **kwargs_)
24        else:
25            return GUIDialogboxObjectType(*args_, **kwargs_)
26    factory = staticmethod(factory)
27    def get_Box_Caption(self): return self.Box_Caption
28    def set_Box_Caption(self, Box_Caption): self.Box_Caption = Box_Caption
29    def validate_StringObjectPropertyType(self, value):
30        # Validate type cybox_common.StringObjectPropertyType, a restriction on None.
31        pass
32    def get_Box_Text(self): return self.Box_Text
33    def set_Box_Text(self, Box_Text): self.Box_Text = Box_Text
34    def hasContent_(self):
35        if (
36            self.Box_Caption is not None or
37            self.Box_Text is not None or
38            super(GUIDialogboxObjectType, self).hasContent_()
39            ):
40            return True
41        else:
42            return False
43    def export(self, lwrite, level, namespace_='GUIDialogBoxObj:', name_='GUIDialogboxObjectType', namespacedef_='', pretty_print=True):
44        if pretty_print:
45            eol_ = '\n'
46        else:
47            eol_ = ''
48        showIndent(lwrite, level, pretty_print)
49        lwrite('<%s%s%s' % (namespace_, name_, namespacedef_ and ' ' + namespacedef_ or '', ))
50        already_processed = set()
51        self.exportAttributes(lwrite, level, already_processed, namespace_, name_='GUIDialogboxObjectType')
52        if self.hasContent_():
53            lwrite('>%s' % (eol_, ))
54            self.exportChildren(lwrite, level + 1, namespace_, name_, pretty_print=pretty_print)
55            showIndent(lwrite, level, pretty_print)
56            lwrite('</%s%s>%s' % (namespace_, name_, eol_))
57        else:
58            lwrite('/>%s' % (eol_, ))
59    def exportAttributes(self, lwrite, level, already_processed, namespace_='GUIDialogBoxObj:', name_='GUIDialogboxObjectType'):
60        super(GUIDialogboxObjectType, self).exportAttributes(lwrite, level, already_processed, namespace_, name_='GUIDialogboxObjectType')
61    def exportChildren(self, lwrite, level, namespace_='GUIDialogBoxObj:', name_='GUIDialogboxObjectType', fromsubclass_=False, pretty_print=True):
62        super(GUIDialogboxObjectType, self).exportChildren(lwrite, level, 'GUIDialogBoxObj:', name_, True, pretty_print=pretty_print)
63        if pretty_print:
64            eol_ = '\n'
65        else:
66            eol_ = ''
67        if self.Box_Caption is not None:
68            self.Box_Caption.export(lwrite, level, 'GUIDialogBoxObj:', name_='Box_Caption', pretty_print=pretty_print)
69        if self.Box_Text is not None:
70            self.Box_Text.export(lwrite, level, 'GUIDialogBoxObj:', name_='Box_Text', pretty_print=pretty_print)
71    def build(self, node):
72        self.__sourcenode__ = node
73        already_processed = set()
74        self.buildAttributes(node, node.attrib, already_processed)
75        for child in node:
76            nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
77            self.buildChildren(child, node, nodeName_)
78    def buildAttributes(self, node, attrs, already_processed):
79        super(GUIDialogboxObjectType, self).buildAttributes(node, attrs, already_processed)
80    def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
81        if nodeName_ == 'Box_Caption':
82            obj_ = cybox_common.StringObjectPropertyType.factory()
83            obj_.build(child_)
84            self.set_Box_Caption(obj_)
85        elif nodeName_ == 'Box_Text':
86            obj_ = cybox_common.StringObjectPropertyType.factory()
87            obj_.build(child_)
88            self.set_Box_Text(obj_)
89        super(GUIDialogboxObjectType, self).buildChildren(child_, node, nodeName_, True)
90# end class GUIDialogboxObjectType
91
92GDSClassesMapping = {
93    'Build_Utility': cybox_common.BuildUtilityType,
94    'Errors': cybox_common.ErrorsType,
95    'Time': cybox_common.TimeType,
96    'Width': cybox_common.IntegerObjectPropertyType,
97    'Certificate_Issuer': cybox_common.StringObjectPropertyType,
98    'Metadata': cybox_common.MetadataType,
99    'Hash': cybox_common.HashType,
100    'Information_Source_Type': cybox_common.ControlledVocabularyStringType,
101    'Block_Hash_Value': cybox_common.HashValueType,
102    'Fuzzy_Hash_Structure': cybox_common.FuzzyHashStructureType,
103    'SubDatum': cybox_common.MetadataType,
104    'Segment_Hash': cybox_common.HashValueType,
105    'Digital_Signature': cybox_common.DigitalSignatureInfoType,
106    'Code_Snippets': cybox_common.CodeSnippetsType,
107    'Value': cybox_common.StringObjectPropertyType,
108    'Length': cybox_common.IntegerObjectPropertyType,
109    'Certificate_Subject': cybox_common.StringObjectPropertyType,
110    'Encoding': cybox_common.ControlledVocabularyStringType,
111    'Internationalization_Settings': cybox_common.InternationalizationSettingsType,
112    'Tool_Configuration': cybox_common.ToolConfigurationType,
113    'English_Translation': cybox_common.StringObjectPropertyType,
114    'Functions': cybox_common.FunctionsType,
115    'String_Value': cybox_common.StringObjectPropertyType,
116    'Build_Utility_Platform_Specification': cybox_common.PlatformSpecificationType,
117    'Compiler_Informal_Description': cybox_common.CompilerInformalDescriptionType,
118    'System': cybox_common.ObjectPropertiesType,
119    'Platform': cybox_common.PlatformSpecificationType,
120    'Usage_Context_Assumptions': cybox_common.UsageContextAssumptionsType,
121    'Type': cybox_common.ControlledVocabularyStringType,
122    'Compilers': cybox_common.CompilersType,
123    'Tool_Type': cybox_common.ControlledVocabularyStringType,
124    'String': cybox_common.ExtractedStringType,
125    'Tool': cybox_common.ToolInformationType,
126    'Build_Information': cybox_common.BuildInformationType,
127    'Tool_Hashes': cybox_common.HashListType,
128    'Error_Instances': cybox_common.ErrorInstancesType,
129    'Data_Segment': cybox_common.StringObjectPropertyType,
130    'GUI_Object': gui_object.GUIObjectType,
131    'Language': cybox_common.StringObjectPropertyType,
132    'Property': cybox_common.PropertyType,
133    'Strings': cybox_common.ExtractedStringsType,
134    'File_System_Offset': cybox_common.IntegerObjectPropertyType,
135    'Reference_Description': cybox_common.StructuredTextType,
136    'Usage_Context_Assumption': cybox_common.StructuredTextType,
137    'Code_Snippet': cybox_common.ObjectPropertiesType,
138    'Configuration_Settings': cybox_common.ConfigurationSettingsType,
139    'Simple_Hash_Value': cybox_common.SimpleHashValueType,
140    'Byte_String_Value': cybox_common.HexBinaryObjectPropertyType,
141    'Instance': cybox_common.ObjectPropertiesType,
142    'Import': cybox_common.StringObjectPropertyType,
143    'Identifier': cybox_common.PlatformIdentifierType,
144    'Tool_Specific_Data': cybox_common.ToolSpecificDataType,
145    'Execution_Environment': cybox_common.ExecutionEnvironmentType,
146    'Search_Distance': cybox_common.IntegerObjectPropertyType,
147    'Dependencies': cybox_common.DependenciesType,
148    'Offset': cybox_common.IntegerObjectPropertyType,
149    'Date': cybox_common.DateRangeType,
150    'Hashes': cybox_common.HashListType,
151    'Segments': cybox_common.HashSegmentsType,
152    'Segment_Count': cybox_common.IntegerObjectPropertyType,
153    'Box_Caption': cybox_common.StringObjectPropertyType,
154    'Block_Hash': cybox_common.FuzzyHashBlockType,
155    'Dependency': cybox_common.DependencyType,
156    'Error': cybox_common.ErrorType,
157    'Trigger_Point': cybox_common.HexBinaryObjectPropertyType,
158    'Environment_Variable': cybox_common.EnvironmentVariableType,
159    'Byte_Run': cybox_common.ByteRunType,
160    'Contributors': cybox_common.PersonnelType,
161    'Image_Offset': cybox_common.IntegerObjectPropertyType,
162    'Imports': cybox_common.ImportsType,
163    'Library': cybox_common.LibraryType,
164    'Height': cybox_common.IntegerObjectPropertyType,
165    'References': cybox_common.ToolReferencesType,
166    'Internal_Strings': cybox_common.InternalStringsType,
167    'Custom_Properties': cybox_common.CustomPropertiesType,
168    'Configuration_Setting': cybox_common.ConfigurationSettingType,
169    'Libraries': cybox_common.LibrariesType,
170    'Box_Text': cybox_common.StringObjectPropertyType,
171    'Function': cybox_common.StringObjectPropertyType,
172    'Description': cybox_common.StructuredTextType,
173    'User_Account_Info': cybox_common.ObjectPropertiesType,
174    'Build_Configuration': cybox_common.BuildConfigurationType,
175    'Address': cybox_common.HexBinaryObjectPropertyType,
176    'Search_Within': cybox_common.IntegerObjectPropertyType,
177    'Segment': cybox_common.HashSegmentType,
178    'Compiler': cybox_common.CompilerType,
179    'Name': cybox_common.StringObjectPropertyType,
180    'Signature_Description': cybox_common.StringObjectPropertyType,
181    'Block_Size': cybox_common.IntegerObjectPropertyType,
182    'Compiler_Platform_Specification': cybox_common.PlatformSpecificationType,
183    'Fuzzy_Hash_Value': cybox_common.FuzzyHashValueType,
184    'Dependency_Description': cybox_common.StructuredTextType,
185    'Contributor': cybox_common.ContributorType,
186    'Tools': cybox_common.ToolsInformationType,
187    'Data_Size': cybox_common.DataSizeType,
188}
189
190USAGE_TEXT = """
191Usage: python <Parser>.py [ -s ] <in_xml_file>
192"""
193
194def usage():
195    print(USAGE_TEXT)
196    sys.exit(1)
197
198def get_root_tag(node):
199    tag = Tag_pattern_.match(node.tag).groups()[-1]
200    rootClass = GDSClassesMapping.get(tag)
201    if rootClass is None:
202        rootClass = globals().get(tag)
203    return tag, rootClass
204
205def parse(inFileName):
206    doc = parsexml_(inFileName)
207    rootNode = doc.getroot()
208    rootTag, rootClass = get_root_tag(rootNode)
209    if rootClass is None:
210        rootTag = 'GUI_Dialogbox'
211        rootClass = GUIDialogboxObjectType
212    rootObj = rootClass.factory()
213    rootObj.build(rootNode)
214    # Enable Python to collect the space used by the DOM.
215    doc = None
216#    sys.stdout.write('<?xml version="1.0" ?>\n')
217#    rootObj.export(sys.stdout.write, 0, name_=rootTag,
218#        namespacedef_='',
219#        pretty_print=True)
220    return rootObj
221
222def parseEtree(inFileName):
223    doc = parsexml_(inFileName)
224    rootNode = doc.getroot()
225    rootTag, rootClass = get_root_tag(rootNode)
226    if rootClass is None:
227        rootTag = 'GUI_Dialogbox'
228        rootClass = GUIDialogboxObjectType
229    rootObj = rootClass.factory()
230    rootObj.build(rootNode)
231    # Enable Python to collect the space used by the DOM.
232    doc = None
233    rootElement = rootObj.to_etree(None, name_=rootTag)
234    content = etree_.tostring(rootElement, pretty_print=True,
235        xml_declaration=True, encoding="utf-8")
236    sys.stdout.write(content)
237    sys.stdout.write('\n')
238    return rootObj, rootElement
239
240def parseString(inString):
241    from mixbox.vendor.six import StringIO
242    doc = parsexml_(StringIO(inString))
243    rootNode = doc.getroot()
244    rootTag, rootClass = get_root_tag(rootNode)
245    if rootClass is None:
246        rootTag = 'GUI_Dialogbox'
247        rootClass = GUIDialogboxObjectType
248    rootObj = rootClass.factory()
249    rootObj.build(rootNode)
250    # Enable Python to collect the space used by the DOM.
251    doc = None
252#    sys.stdout.write('<?xml version="1.0" ?>\n')
253#    rootObj.export(sys.stdout.write, 0, name_="GUI_Dialogbox",
254#        namespacedef_='')
255    return rootObj
256
257def main():
258    args = sys.argv[1:]
259    if len(args) == 1:
260        parse(args[0])
261    else:
262        usage()
263
264if __name__ == '__main__':
265    #import pdb; pdb.set_trace()
266    main()
267
268__all__ = [
269    "GUIDialogboxObjectType"
270    ]
271