1# Copyright (c) 2017, The MITRE Corporation. All rights reserved.
2# See LICENSE.txt for complete terms.
3
4#!/usr/bin/env python
5# -*- coding: utf-8 -*-
6
7#
8# Generated Thu Apr 11 15:07:50 2013 by generateDS.py version 2.9a.
9#
10
11import sys
12
13from mixbox.binding_utils import *
14
15from stix.bindings import register_extension
16import stix.bindings.data_marking as data_marking_binding
17
18XML_NS = "http://data-marking.mitre.org/extensions/MarkingStructure#TLP-1"
19
20#
21# Data representation classes.
22#
23
24@register_extension
25class TLPMarkingStructureType(data_marking_binding.MarkingStructureType):
26    """The TLPMarkingStructureType is an implementation of the data marking
27    schema that allows for a TLP Designation to be attached to an
28    identified XML structure. Information about TLP is available
29    here: http://www.us-cert.gov/tlp.The TLP color designation of
30    the marked structure.
31
32    """
33    xmlns          = XML_NS
34    xmlns_prefix   = "tlpMarking"
35    xml_type       = "TLPMarkingStructureType"
36    xsi_type       = "%s:%s" % (xmlns_prefix, xml_type)
37
38    subclass = None
39    superclass = data_marking_binding.MarkingStructureType
40    def __init__(self, marking_model_ref=None, marking_model_name=None, color=None):
41        super(TLPMarkingStructureType, self).__init__(marking_model_ref=marking_model_ref, marking_model_name=marking_model_name)
42        self.color = _cast(None, color)
43        pass
44    def factory(*args_, **kwargs_):
45        if TLPMarkingStructureType.subclass:
46            return TLPMarkingStructureType.subclass(*args_, **kwargs_)
47        else:
48            return TLPMarkingStructureType(*args_, **kwargs_)
49    factory = staticmethod(factory)
50    def get_color(self): return self.color
51    def set_color(self, color): self.color = color
52    def hasContent_(self):
53        if (
54            super(TLPMarkingStructureType, self).hasContent_()
55            ):
56            return True
57        else:
58            return False
59    def export(self, lwrite, level, nsmap, namespace_=XML_NS, name_='TLPMarkingStructureType', namespacedef_='', pretty_print=True):
60        if pretty_print:
61            eol_ = '\n'
62        else:
63            eol_ = ''
64        showIndent(lwrite, level, pretty_print)
65        lwrite('<%s:%s%s' % (nsmap[namespace_], name_, namespacedef_ and ' ' + namespacedef_ or '', ))
66        already_processed = set()
67        self.exportAttributes(lwrite, level, already_processed, namespace_, name_='TLPMarkingStructureType')
68        if self.hasContent_():
69            lwrite('>%s' % (eol_, ))
70            self.exportChildren(lwrite, level + 1, nsmap, XML_NS, name_, pretty_print=pretty_print)
71            lwrite('</%s:%s>%s' % (nsmap[namespace_], name_, eol_))
72        else:
73            lwrite('/>%s' % (eol_, ))
74    def exportAttributes(self, lwrite, level, already_processed, namespace_='tlpMarking:', name_='TLPMarkingStructureType'):
75        super(TLPMarkingStructureType, self).exportAttributes(lwrite, level, already_processed, namespace_, name_='TLPMarkingStructureType')
76        # if 'xmlns' not in already_processed:
77        #     already_processed.add('xmlns')
78        #     xmlns = " xmlns:%s='%s'" % (self.xmlns_prefix, self.xmlns)
79        #     lwrite(xmlns)
80        if 'xsi:type' not in already_processed:
81            already_processed.add('xsi:type')
82            xsi_type = " xsi:type='%s:%s'" % (self.xmlns_prefix, self.xml_type)
83            lwrite(xsi_type)
84        if self.color is not None and 'color' not in already_processed:
85            already_processed.add('color')
86            lwrite(' color=%s' % (quote_attrib(self.color), ))
87    def exportChildren(self, lwrite, level, nsmap, namespace_=XML_NS, name_='TLPMarkingStructureType', fromsubclass_=False, pretty_print=True):
88        super(TLPMarkingStructureType, self).exportChildren(lwrite, level, nsmap, namespace_, name_, True, pretty_print=pretty_print)
89        pass
90    def build(self, node):
91        self.__sourcenode__ = node
92        already_processed = set()
93        self.buildAttributes(node, node.attrib, already_processed)
94        for child in node:
95            nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
96            self.buildChildren(child, node, nodeName_)
97    def buildAttributes(self, node, attrs, already_processed):
98        value = find_attr_value_('color', node)
99        if value is not None and 'color' not in already_processed:
100            already_processed.add('color')
101            self.color = value
102        super(TLPMarkingStructureType, self).buildAttributes(node, attrs, already_processed)
103    def buildChildren(self, child_, node, nodeName_, fromsubclass_=False):
104        super(TLPMarkingStructureType, self).buildChildren(child_, node, nodeName_, True)
105        pass
106# end class TLPMarkingStructureType
107
108
109GDSClassesMapping = {}
110
111USAGE_TEXT = """
112Usage: python <Parser>.py [ -s ] <in_xml_file>
113"""
114
115def usage():
116    print(USAGE_TEXT)
117    sys.exit(1)
118
119def get_root_tag(node):
120    tag = Tag_pattern_.match(node.tag).groups()[-1]
121    rootClass = GDSClassesMapping.get(tag)
122    if rootClass is None:
123        rootClass = globals().get(tag)
124    return tag, rootClass
125
126def parse(inFileName):
127    doc = parsexml_(inFileName)
128    rootNode = doc.getroot()
129    rootTag, rootClass = get_root_tag(rootNode)
130    if rootClass is None:
131        rootTag = 'TLPMarkingStructureType'
132        rootClass = TLPMarkingStructureType
133    rootObj = rootClass.factory()
134    rootObj.build(rootNode)
135    # Enable Python to collect the space used by the DOM.
136    doc = None
137    # sys.stdout.write('<?xml version="1.0" ?>\n')
138    # rootObj.export(sys.stdout, 0, name_=rootTag,
139    #     namespacedef_='',
140    #     pretty_print=True)
141    return rootObj
142
143def parseEtree(inFileName):
144    doc = parsexml_(inFileName)
145    rootNode = doc.getroot()
146    rootTag, rootClass = get_root_tag(rootNode)
147    if rootClass is None:
148        rootTag = 'TLPMarkingStructureType'
149        rootClass = TLPMarkingStructureType
150    rootObj = rootClass.factory()
151    rootObj.build(rootNode)
152    # Enable Python to collect the space used by the DOM.
153    doc = None
154    rootElement = rootObj.to_etree(None, name_=rootTag)
155    content = etree_.tostring(rootElement, pretty_print=True,
156        xml_declaration=True, encoding="utf-8")
157    sys.stdout.write(content)
158    sys.stdout.write('\n')
159    return rootObj, rootElement
160
161def parseString(inString):
162    from mixbox.vendor.six import StringIO
163    doc = parsexml_(StringIO(inString))
164    rootNode = doc.getroot()
165    rootTag, rootClass = get_root_tag(rootNode)
166    if rootClass is None:
167        rootTag = 'TLPMarkingStructureType'
168        rootClass = TLPMarkingStructureType
169    rootObj = rootClass.factory()
170    rootObj.build(rootNode)
171    # Enable Python to collect the space used by the DOM.
172    doc = None
173    # sys.stdout.write('<?xml version="1.0" ?>\n')
174    # rootObj.export(sys.stdout, 0, name_="TLPMarkingStructureType",
175    #     namespacedef_='')
176    return rootObj
177
178def main():
179    args = sys.argv[1:]
180    if len(args) == 1:
181        parse(args[0])
182    else:
183        usage()
184
185if __name__ == '__main__':
186    #import pdb; pdb.set_trace()
187    main()
188
189__all__ = [
190    "TLPMarkingStructureType"
191    ]
192