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:06:21 2013 by generateDS.py version 2.9a. 9# 10 11import sys 12 13import cybox.bindings.cybox_core as cybox_core_binding 14from mixbox.binding_utils import * 15 16from stix.bindings import lookup_extension, register_extension 17import stix.bindings.stix_common as stix_common_binding 18import stix.bindings.data_marking as data_marking_binding 19 20XML_NS = "http://stix.mitre.org/CourseOfAction-1" 21 22# 23# Data representation classes. 24# 25 26class StructuredCOAType(GeneratedsSuper): 27 """The StructuredCOAType enables the specification of an actionable 28 structured representation for the CourseOfAction potentially for 29 automated consumption and implementation. This type is defined 30 as an abstract type and is intended to be extended using the XML 31 Schema extension mechanism to allow for the expression of a 32 variety of structured COA types. Instance documents representing 33 structured COAs use the xsi:type attribute to specify which 34 implementation of this type they wish to use. STIX has provided 35 one implementation to allow for the passing of proprietary or 36 externally defined structured courses of action in a CDATA 37 block. This implementation is captured in the Generic Structured 38 COA extension, which provides the GenericStructuredCOAType in 39 the http://stix.mitre.org/extensions/StructuredCOA#Generic-1 40 namespace. The extension is defined in the 41 extensions/structured_coa/generic.xsd file or at the URL http:// 42 stix.mitre.org/XMLSchema/extensions/structured_coa/generic/1.0/g 43 eneric.xsd. Specifies a unique ID for this 44 StructuredCOA.Specifies a reference to the ID for this 45 StructuredCOA specified elsewhere.""" 46 subclass = None 47 superclass = None 48 def __init__(self, idref=None, id=None): 49 self.idref = _cast(None, idref) 50 self.id = _cast(None, id) 51 pass 52 def factory(*args_, **kwargs_): 53 if StructuredCOAType.subclass: 54 return StructuredCOAType.subclass(*args_, **kwargs_) 55 else: 56 return StructuredCOAType(*args_, **kwargs_) 57 factory = staticmethod(factory) 58 def get_idref(self): return self.idref 59 def set_idref(self, idref): self.idref = idref 60 def get_id(self): return self.id 61 def set_id(self, id): self.id = id 62 def hasContent_(self): 63 if ( 64 65 ): 66 return True 67 else: 68 return False 69 def export(self, lwrite, level, nsmap, namespace_=XML_NS, name_='StructuredCOAType', namespacedef_='', pretty_print=True): 70 if pretty_print: 71 eol_ = '\n' 72 else: 73 eol_ = '' 74 showIndent(lwrite, level, pretty_print) 75 lwrite('<%s:%s%s' % (nsmap[namespace_], name_, namespacedef_ and ' ' + namespacedef_ or '', )) 76 already_processed = set() 77 self.exportAttributes(lwrite, level, already_processed, namespace_, name_='StructuredCOAType') 78 if self.hasContent_(): 79 lwrite('>%s' % (eol_, )) 80 self.exportChildren(lwrite, level + 1, nsmap, XML_NS, name_, pretty_print=pretty_print) 81 lwrite('</%s:%s>%s' % (nsmap[namespace_], name_, eol_)) 82 else: 83 lwrite('/>%s' % (eol_, )) 84 def exportAttributes(self, lwrite, level, already_processed, namespace_='coa:', name_='StructuredCOAType'): 85 # if 'xmlns' not in already_processed: 86 # already_processed.add('xmlns') 87 # xmlns = " xmlns:%s='%s'" % (self.xmlns_prefix, self.xmlns) 88 # lwrite(xmlns) 89 if 'xsi:type' not in already_processed: 90 already_processed.add('xsi:type') 91 xsi_type = " xsi:type='%s:%s'" % (self.xmlns_prefix, self.xml_type) 92 lwrite(xsi_type) 93 if self.idref is not None and 'idref' not in already_processed: 94 already_processed.add('idref') 95 lwrite(' idref=%s' % (quote_attrib(self.idref), )) 96 if self.id is not None and 'id' not in already_processed: 97 already_processed.add('id') 98 lwrite(' id=%s' % (quote_attrib(self.id), )) 99 def exportChildren(self, lwrite, level, nsmap, namespace_=XML_NS, name_='StructuredCOAType', fromsubclass_=False, pretty_print=True): 100 pass 101 def build(self, node): 102 self.__sourcenode__ = node 103 already_processed = set() 104 self.buildAttributes(node, node.attrib, already_processed) 105 for child in node: 106 nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] 107 self.buildChildren(child, node, nodeName_) 108 def buildAttributes(self, node, attrs, already_processed): 109 value = find_attr_value_('idref', node) 110 if value is not None and 'idref' not in already_processed: 111 already_processed.add('idref') 112 self.idref = value 113 value = find_attr_value_('id', node) 114 if value is not None and 'id' not in already_processed: 115 already_processed.add('id') 116 self.id = value 117 def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): 118 pass 119# end class StructuredCOAType 120 121class ObjectiveType(GeneratedsSuper): 122 """The ObjectiveType characterizes the objective of this 123 CourseOfAction.""" 124 subclass = None 125 superclass = None 126 def __init__(self, Description=None, Short_Description=None, Applicability_Confidence=None): 127 if Description is None: 128 self.Description = [] 129 else: 130 self.Description = Description 131 132 if Short_Description is None: 133 self.Short_Description = [] 134 else: 135 self.Short_Description = Short_Description 136 137 self.Applicability_Confidence = Applicability_Confidence 138 def factory(*args_, **kwargs_): 139 if ObjectiveType.subclass: 140 return ObjectiveType.subclass(*args_, **kwargs_) 141 else: 142 return ObjectiveType(*args_, **kwargs_) 143 factory = staticmethod(factory) 144 def insert_Description(self, index, value): self.Description[index] = value 145 def add_Description(self, Description): self.Description.append(Description) 146 def get_Description(self): return self.Description 147 def set_Description(self, Description): self.Description = Description 148 def insert_Short_Description(self, index, value): self.Short_Description[index] = value 149 def add_Short_Description(self, Short_Description): self.Short_Description.append(Short_Description) 150 def get_Short_Description(self): return self.Short_Description 151 def set_Short_Description(self, Short_Description): self.Short_Description = Short_Description 152 def get_Applicability_Confidence(self): return self.Applicability_Confidence 153 def set_Applicability_Confidence(self, Applicability_Confidence): self.Applicability_Confidence = Applicability_Confidence 154 def hasContent_(self): 155 if ( 156 self.Description or 157 self.Short_Description or 158 self.Applicability_Confidence is not None 159 ): 160 return True 161 else: 162 return False 163 def export(self, lwrite, level, nsmap, namespace_=XML_NS, name_='ObjectiveType', namespacedef_='', pretty_print=True): 164 if pretty_print: 165 eol_ = '\n' 166 else: 167 eol_ = '' 168 showIndent(lwrite, level, pretty_print) 169 lwrite('<%s:%s%s' % (nsmap[namespace_], name_, namespacedef_ and ' ' + namespacedef_ or '', )) 170 already_processed = set() 171 self.exportAttributes(lwrite, level, already_processed, namespace_, name_='ObjectiveType') 172 if self.hasContent_(): 173 lwrite('>%s' % (eol_, )) 174 self.exportChildren(lwrite, level + 1, nsmap, XML_NS, name_, pretty_print=pretty_print) 175 showIndent(lwrite, level, pretty_print) 176 lwrite('</%s:%s>%s' % (nsmap[namespace_], name_, eol_)) 177 else: 178 lwrite('/>%s' % (eol_, )) 179 def exportAttributes(self, lwrite, level, already_processed, namespace_='coa:', name_='ObjectiveType'): 180 pass 181 def exportChildren(self, lwrite, level, nsmap, namespace_=XML_NS, name_='ObjectiveType', fromsubclass_=False, pretty_print=True): 182 if pretty_print: 183 eol_ = '\n' 184 else: 185 eol_ = '' 186 for Description in self.Description: 187 Description.export(lwrite, level, nsmap, namespace_, name_='Description', pretty_print=pretty_print) 188 for Short_Description in self.Short_Description: 189 Short_Description.export(lwrite, level, nsmap, namespace_, name_='Short_Description', pretty_print=pretty_print) 190 if self.Applicability_Confidence is not None: 191 self.Applicability_Confidence.export(lwrite, level, nsmap, namespace_, name_='Applicability_Confidence', pretty_print=pretty_print) 192 def build(self, node): 193 self.__sourcenode__ = node 194 already_processed = set() 195 self.buildAttributes(node, node.attrib, already_processed) 196 for child in node: 197 nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] 198 self.buildChildren(child, node, nodeName_) 199 def buildAttributes(self, node, attrs, already_processed): 200 pass 201 def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): 202 if nodeName_ == 'Description': 203 obj_ = stix_common_binding.StructuredTextType.factory() 204 obj_.build(child_) 205 self.add_Description(obj_) 206 elif nodeName_ == 'Short_Description': 207 obj_ = stix_common_binding.StructuredTextType.factory() 208 obj_.build(child_) 209 self.add_Short_Description(obj_) 210 elif nodeName_ == 'Applicability_Confidence': 211 obj_ = stix_common_binding.ConfidenceType.factory() 212 obj_.build(child_) 213 self.set_Applicability_Confidence(obj_) 214# end class ObjectiveType 215 216 217@register_extension 218class CourseOfActionType(stix_common_binding.CourseOfActionBaseType): 219 """The CourseOfActionType characterizes a Course of Action to be taken 220 in regards to one of more cyber threats. NOTE: This construct is 221 still in its early stages of maturity and will require a good 222 deal of review and refinement.Specifies the relevant STIX-COA 223 schema version for this content.""" 224 subclass = None 225 superclass = stix_common_binding.CourseOfActionBaseType 226 227 xmlns = "http://stix.mitre.org/CourseOfAction-1" 228 xmlns_prefix = "coa" 229 xml_type = "CourseOfActionType" 230 xsi_type = "%s:%s" % (xmlns_prefix, xml_type) 231 232 def __init__(self, idref=None, id=None, timestamp=None, version=None, Title=None, Stage=None, Type=None, Description=None, Short_Description=None, Objective=None, Parameter_Observables=None, Structured_COA=None, Impact=None, Cost=None, Efficacy=None, Information_Source=None, Handling=None, Related_COAs=None, Related_Packages=None): 233 super(CourseOfActionType, self).__init__(idref=idref, id=id, timestamp=timestamp) 234 235 self.version = _cast(None, version) 236 self.Title = Title 237 self.Stage = Stage 238 self.Type = Type 239 if Description is None: 240 self.Description = [] 241 else: 242 self.Description = Description 243 if Short_Description is None: 244 self.Short_Description = [] 245 else: 246 self.Short_Description = Short_Description 247 self.Objective = Objective 248 self.Parameter_Observables = Parameter_Observables 249 self.Structured_COA = Structured_COA 250 self.Impact = Impact 251 self.Cost = Cost 252 self.Efficacy = Efficacy 253 self.Information_Source = Information_Source 254 self.Handling = Handling 255 self.Related_COAs = Related_COAs 256 self.Related_Packages = Related_Packages 257 def factory(*args_, **kwargs_): 258 if CourseOfActionType.subclass: 259 return CourseOfActionType.subclass(*args_, **kwargs_) 260 else: 261 return CourseOfActionType(*args_, **kwargs_) 262 factory = staticmethod(factory) 263 def get_Title(self): return self.Title 264 def set_Title(self, Title): self.Title = Title 265 def get_Stage(self): return self.Stage 266 def set_Stage(self, Stage): self.Stage = Stage 267 def get_Type(self): return self.Type 268 def set_Type(self, Type): self.Type = Type 269 def insert_Description(self, index, value): self.Description[index] = value 270 def add_Description(self, Description): self.Description.append(Description) 271 def get_Description(self): return self.Description 272 def set_Description(self, Description): self.Description = Description 273 def insert_Short_Description(self, index, value): self.Short_Description[index] = value 274 def add_Short_Description(self, Short_Description): self.Short_Description.append(Short_Description) 275 def get_Short_Description(self): return self.Short_Description 276 def set_Short_Description(self, Short_Description): self.Short_Description = Short_Description 277 def get_Objective(self): return self.Objective 278 def set_Objective(self, Objective): self.Objective = Objective 279 def get_Parameter_Observables(self): return self.Parameter_Observables 280 def set_Parameter_Observables(self, Parameter_Observables): self.Parameter_Observables = Parameter_Observables 281 def get_Structured_COA(self): return self.Structured_COA 282 def set_Structured_COA(self, Structured_COA): self.Structured_COA = Structured_COA 283 def get_Impact(self): return self.Impact 284 def set_Impact(self, Impact): self.Impact = Impact 285 def get_Cost(self): return self.Cost 286 def set_Cost(self, Cost): self.Cost = Cost 287 def get_Efficacy(self): return self.Efficacy 288 def set_Efficacy(self, Efficacy): self.Efficacy = Efficacy 289 def get_Information_Source(self): return self.Information_Source 290 def set_Information_Source(self, Information_Source): self.Information_Source = Information_Source 291 def get_Handling(self): return self.Handling 292 def set_Handling(self, Handling): self.Handling = Handling 293 def get_Related_COAs(self): return self.Related_COAs 294 def set_Related_COAs(self, Related_COAs): self.Related_COAs = Related_COAs 295 def get_Related_Packages(self): return self.Related_Packages 296 def set_Related_Packages(self, Related_Packages): self.Related_Packages = Related_Packages 297 def get_version(self): return self.version 298 def set_version(self, version): self.version = version 299 def hasContent_(self): 300 if ( 301 self.Title is not None or 302 self.Stage is not None or 303 self.Type is not None or 304 self.Description or 305 self.Short_Description or 306 self.Objective is not None or 307 self.Parameter_Observables is not None or 308 self.Structured_COA is not None or 309 self.Impact is not None or 310 self.Cost is not None or 311 self.Efficacy is not None or 312 self.Information_Source is not None or 313 self.Handling is not None or 314 self.Related_COAs is not None or 315 self.Related_Packages is not None or 316 super(CourseOfActionType, self).hasContent_() 317 ): 318 return True 319 else: 320 return False 321 def export(self, lwrite, level, nsmap, namespace_=XML_NS, name_='Course_Of_Action', namespacedef_='', pretty_print=True): 322 if pretty_print: 323 eol_ = '\n' 324 else: 325 eol_ = '' 326 showIndent(lwrite, level, pretty_print) 327 lwrite('<%s:%s%s' % (nsmap[namespace_], name_, namespacedef_ and ' ' + namespacedef_ or '', )) 328 already_processed = set() 329 self.exportAttributes(lwrite, level, already_processed, namespace_, name_='Course_Of_Action') 330 if self.hasContent_(): 331 lwrite('>%s' % (eol_, )) 332 self.exportChildren(lwrite, level + 1, nsmap, XML_NS, name_, pretty_print=pretty_print) 333 showIndent(lwrite, level, pretty_print) 334 lwrite('</%s:%s>%s' % (nsmap[namespace_], name_, eol_)) 335 else: 336 lwrite('/>%s' % (eol_, )) 337 def exportAttributes(self, lwrite, level, already_processed, namespace_='coa:', name_='Course_Of_Action'): 338 super(CourseOfActionType, self).exportAttributes(lwrite, level, already_processed, namespace_, name_='Course_Of_Action') 339 #if 'xmlns' not in already_processed: 340 # already_processed.add('xmlns') 341 # xmlns = " xmlns:%s='%s'" % (self.xmlns_prefix, self.xmlns) 342 # lwrite(xmlns) 343 if 'xsi:type' not in already_processed: 344 already_processed.add('xsi:type') 345 xsi_type = " xsi:type='%s:%s'" % (self.xmlns_prefix, self.xml_type) 346 lwrite(xsi_type) 347 if self.version is not None and 'version' not in already_processed: 348 already_processed.add('version') 349 lwrite(' version=%s' % (quote_attrib(self.version), )) 350 def exportChildren(self, lwrite, level, nsmap, namespace_=XML_NS, name_='CourseOfActionType', fromsubclass_=False, pretty_print=True): 351 super(CourseOfActionType, self).exportChildren(lwrite, level, nsmap, namespace_, name_, True, pretty_print=pretty_print) 352 if pretty_print: 353 eol_ = '\n' 354 else: 355 eol_ = '' 356 if self.Title is not None: 357 showIndent(lwrite, level, pretty_print) 358 lwrite('<%s:Title>%s</%s:Title>%s' % (nsmap[namespace_], quote_xml(self.Title), nsmap[namespace_], eol_)) 359 if self.Stage is not None: 360 self.Stage.export(lwrite, level, nsmap, namespace_, name_='Stage', pretty_print=pretty_print) 361 if self.Type is not None: 362 self.Type.export(lwrite, level, nsmap, namespace_, name_='Type', pretty_print=pretty_print) 363 for Description in self.Description: 364 Description.export(lwrite, level, nsmap, namespace_, name_='Description', pretty_print=pretty_print) 365 for Short_Description in self.Short_Description: 366 Short_Description.export(lwrite, level, nsmap, namespace_, name_='Short_Description', pretty_print=pretty_print) 367 if self.Objective is not None: 368 self.Objective.export(lwrite, level, nsmap, namespace_, name_='Objective', pretty_print=pretty_print) 369 if self.Parameter_Observables is not None: 370 self.Parameter_Observables.export(lwrite, level, "%s:" % (nsmap[namespace_]), name_='Parameter_Observables', pretty_print=pretty_print) 371 if self.Structured_COA is not None: 372 self.Structured_COA.export(lwrite, level, nsmap, namespace_, name_='Structured_COA', pretty_print=pretty_print) 373 if self.Impact is not None: 374 self.Impact.export(lwrite, level, nsmap, namespace_, name_='Impact', pretty_print=pretty_print) 375 if self.Cost is not None: 376 self.Cost.export(lwrite, level, nsmap, namespace_, name_='Cost', pretty_print=pretty_print) 377 if self.Efficacy is not None: 378 self.Efficacy.export(lwrite, level, nsmap, namespace_, name_='Efficacy', pretty_print=pretty_print) 379 if self.Information_Source is not None: 380 self.Information_Source.export(lwrite, level, nsmap, namespace_, name_='Information_Source', pretty_print=pretty_print) 381 if self.Handling is not None: 382 self.Handling.export(lwrite, level, nsmap, namespace_, name_='Handling', pretty_print=pretty_print) 383 if self.Related_COAs is not None: 384 self.Related_COAs.export(lwrite, level, nsmap, namespace_, name_='Related_COAs', pretty_print=pretty_print) 385 if self.Related_Packages is not None: 386 self.Related_Packages.export(lwrite, level, nsmap, namespace_, name_='Related_Packages', pretty_print=pretty_print) 387 def build(self, node): 388 self.__sourcenode__ = node 389 already_processed = set() 390 self.buildAttributes(node, node.attrib, already_processed) 391 for child in node: 392 nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] 393 self.buildChildren(child, node, nodeName_) 394 def buildAttributes(self, node, attrs, already_processed): 395 value = find_attr_value_('version', node) 396 if value is not None and 'version' not in already_processed: 397 already_processed.add('version') 398 self.version = value 399 super(CourseOfActionType, self).buildAttributes(node, attrs, already_processed) 400 def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): 401 if nodeName_ == 'Title': 402 Title_ = child_.text 403 Title_ = self.gds_validate_string(Title_, node, 'Title') 404 self.Title = Title_ 405 elif nodeName_ == 'Stage': 406 obj_ = stix_common_binding.ControlledVocabularyStringType.factory() 407 obj_.build(child_) 408 self.set_Stage(obj_) 409 elif nodeName_ == 'Type': 410 obj_ = stix_common_binding.ControlledVocabularyStringType.factory() 411 obj_.build(child_) 412 self.set_Type(obj_) 413 elif nodeName_ == 'Description': 414 obj_ = stix_common_binding.StructuredTextType.factory() 415 obj_.build(child_) 416 self.add_Description(obj_) 417 elif nodeName_ == 'Short_Description': 418 obj_ = stix_common_binding.StructuredTextType.factory() 419 obj_.build(child_) 420 self.add_Short_Description(obj_) 421 elif nodeName_ == 'Objective': 422 obj_ = ObjectiveType.factory() 423 obj_.build(child_) 424 self.set_Objective(obj_) 425 elif nodeName_ == "Parameter_Observables": 426 obj_ = cybox_core_binding.ObservablesType.factory() 427 obj_.build(child_) 428 self.set_Parameter_Observables(obj_) 429 elif nodeName_ == 'Structured_COA': 430 from .extensions.structured_coa import generic 431 obj_ = lookup_extension(child_).factory() 432 obj_.build(child_) 433 self.set_Structured_COA(obj_) 434 elif nodeName_ == 'Impact': 435 obj_ = stix_common_binding.StatementType.factory() 436 obj_.build(child_) 437 self.set_Impact(obj_) 438 elif nodeName_ == 'Cost': 439 obj_ = stix_common_binding.StatementType.factory() 440 obj_.build(child_) 441 self.set_Cost(obj_) 442 elif nodeName_ == 'Efficacy': 443 obj_ = stix_common_binding.StatementType.factory() 444 obj_.build(child_) 445 self.set_Efficacy(obj_) 446 elif nodeName_ == 'Information_Source': 447 obj_ = stix_common_binding.InformationSourceType.factory() 448 obj_.build(child_) 449 self.set_Information_Source(obj_) 450 elif nodeName_ == 'Handling': 451 obj_ = data_marking_binding.MarkingType.factory() 452 obj_.build(child_) 453 self.set_Handling(obj_) 454 elif nodeName_ == 'Related_COAs': 455 obj_ = RelatedCOAsType.factory() 456 obj_.build(child_) 457 self.set_Related_COAs(obj_) 458 elif nodeName_ == 'Related_Packages': 459 obj_ = stix_common_binding.RelatedPackageRefsType.factory() 460 obj_.build(child_) 461 self.set_Related_Packages(obj_) 462 super(CourseOfActionType, self).buildChildren(child_, node, nodeName_, True) 463# end class CourseOfActionType 464 465 466class RelatedCOAsType(stix_common_binding.GenericRelationshipListType): 467 subclass = None 468 superclass = stix_common_binding.GenericRelationshipListType 469 def __init__(self, scope='exclusive', Related_COA=None): 470 super(RelatedCOAsType, self).__init__(scope=scope) 471 if Related_COA is None: 472 self.Related_COA = [] 473 else: 474 self.Related_COA = Related_COA 475 def factory(*args_, **kwargs_): 476 if RelatedCOAsType.subclass: 477 return RelatedCOAsType.subclass(*args_, **kwargs_) 478 else: 479 return RelatedCOAsType(*args_, **kwargs_) 480 factory = staticmethod(factory) 481 def get_Related_COA(self): return self.Related_COA 482 def set_Related_COA(self, Related_COA): self.Related_COA = Related_COA 483 def add_Related_COA(self, value): self.Related_COA.append(value) 484 def insert_Related_COA(self, index, value): self.Related_COA[index] = value 485 def hasContent_(self): 486 if ( 487 self.Related_COA or 488 super(RelatedCOAsType, self).hasContent_() 489 ): 490 return True 491 else: 492 return False 493 def export(self, lwrite, level, nsmap, namespace_=XML_NS, name_='RelatedCOAsType', namespacedef_='', pretty_print=True): 494 if pretty_print: 495 eol_ = '\n' 496 else: 497 eol_ = '' 498 showIndent(lwrite, level, pretty_print) 499 lwrite('<%s:%s%s' % (nsmap[namespace_], name_, namespacedef_ and ' ' + namespacedef_ or '', )) 500 already_processed = set() 501 self.exportAttributes(lwrite, level, already_processed, namespace_, name_='RelatedCOAsType') 502 if self.hasContent_(): 503 lwrite('>%s' % (eol_, )) 504 self.exportChildren(lwrite, level + 1, nsmap, XML_NS, name_, pretty_print=pretty_print) 505 showIndent(lwrite, level, pretty_print) 506 lwrite('</%s:%s>%s' % (nsmap[namespace_], name_, eol_)) 507 else: 508 lwrite('/>%s' % (eol_, )) 509 def exportAttributes(self, lwrite, level, already_processed, namespace_='coa:', name_='RelatedCOAsType'): 510 super(RelatedCOAsType, self).exportAttributes(lwrite, level, already_processed, namespace_, name_='RelatedCOAsType') 511 def exportChildren(self, lwrite, level, nsmap, namespace_=XML_NS, name_='RelatedCOAsType', fromsubclass_=False, pretty_print=True): 512 super(RelatedCOAsType, self).exportChildren(lwrite, level, nsmap, namespace_, name_, True, pretty_print=pretty_print) 513 if pretty_print: 514 eol_ = '\n' 515 else: 516 eol_ = '' 517 for Related_COA_ in self.Related_COA: 518 Related_COA_.export(lwrite, level, nsmap, namespace_, name_='Related_COA', pretty_print=pretty_print) 519 def build(self, node): 520 self.__sourcenode__ = node 521 already_processed = set() 522 self.buildAttributes(node, node.attrib, already_processed) 523 for child in node: 524 nodeName_ = Tag_pattern_.match(child.tag).groups()[-1] 525 self.buildChildren(child, node, nodeName_) 526 def buildAttributes(self, node, attrs, already_processed): 527 super(RelatedCOAsType, self).buildAttributes(node, attrs, already_processed) 528 def buildChildren(self, child_, node, nodeName_, fromsubclass_=False): 529 if nodeName_ == 'Related_COA': 530 obj_ = stix_common_binding.RelatedCourseOfActionType.factory() 531 obj_.build(child_) 532 self.Related_COA.append(obj_) 533 super(RelatedCOAsType, self).buildChildren(child_, node, nodeName_, True) 534# end class RelatedCOAsType 535 536GDSClassesMapping = {} 537 538USAGE_TEXT = """ 539Usage: python <Parser>.py [ -s ] <in_xml_file> 540""" 541 542def usage(): 543 print(USAGE_TEXT) 544 sys.exit(1) 545 546def get_root_tag(node): 547 tag = Tag_pattern_.match(node.tag).groups()[-1] 548 rootClass = GDSClassesMapping.get(tag) 549 if rootClass is None: 550 rootClass = globals().get(tag) 551 return tag, rootClass 552 553def parse(inFileName): 554 doc = parsexml_(inFileName) 555 rootNode = doc.getroot() 556 rootTag, rootClass = get_root_tag(rootNode) 557 if rootClass is None: 558 rootTag = 'Course_Of_Action' 559 rootClass = CourseOfActionType 560 rootObj = rootClass.factory() 561 rootObj.build(rootNode) 562 # Enable Python to collect the space used by the DOM. 563 doc = None 564 # sys.stdout.write('<?xml version="1.0" ?>\n') 565 # rootObj.export(sys.stdout, 0, name_=rootTag, 566 # namespacedef_='', 567 # pretty_print=True) 568 return rootObj 569 570def parseEtree(inFileName): 571 doc = parsexml_(inFileName) 572 rootNode = doc.getroot() 573 rootTag, rootClass = get_root_tag(rootNode) 574 if rootClass is None: 575 rootTag = 'Course_Of_Action' 576 rootClass = CourseOfActionType 577 rootObj = rootClass.factory() 578 rootObj.build(rootNode) 579 # Enable Python to collect the space used by the DOM. 580 doc = None 581 rootElement = rootObj.to_etree(None, name_=rootTag) 582 content = etree_.tostring(rootElement, pretty_print=True, 583 xml_declaration=True, encoding="utf-8") 584 sys.stdout.write(content) 585 sys.stdout.write('\n') 586 return rootObj, rootElement 587 588def parseString(inString): 589 from mixbox.vendor.six import StringIO 590 doc = parsexml_(StringIO(inString)) 591 rootNode = doc.getroot() 592 rootTag, rootClass = get_root_tag(rootNode) 593 if rootClass is None: 594 rootTag = 'Course_Of_Action' 595 rootClass = CourseOfActionType 596 rootObj = rootClass.factory() 597 rootObj.build(rootNode) 598 # Enable Python to collect the space used by the DOM. 599 doc = None 600 # sys.stdout.write('<?xml version="1.0" ?>\n') 601 # rootObj.export(sys.stdout, 0, name_="Course_Of_Action", 602 # namespacedef_='') 603 return rootObj 604 605def main(): 606 args = sys.argv[1:] 607 if len(args) == 1: 608 parse(args[0]) 609 else: 610 usage() 611 612if __name__ == '__main__': 613 #import pdb; pdb.set_trace() 614 main() 615 616__all__ = [ 617 "StructuredCOAType", 618 "ObjectiveType", 619 "CourseOfActionType" 620 ] 621