MCPcopy Create free account
hub / github.com/aboutcode-org/vulnerablecode / addElement

Method addElement

vulnerabilities/lib_oval.py:461–555  ·  view source on GitHub ↗

Adds the element to the ElementTree for this OVAL document The element argument must be of type OvalElement This method uses the OVALID of the element to determine what type of element it is and if an existing element with that OVALID already exists. This met

(self, element, replace=True)

Source from the content-addressed store, hash-verified

459 return None
460
461 def addElement(self, element, replace=True):
462 """
463 Adds the element to the ElementTree for this OVAL document
464 The element argument must be of type OvalElement
465 This method uses the OVALID of the element to determine what type of element it is
466 and if an existing element with that OVALID already exists.
467 This method will also create the necessary structure (id est, adding <definitions>, <tests>, etc)
468 if the ElementTree does not already contain it.
469 By default this method will replace an existing item with the same OVALID, but this behavior can
470 be overridden by changing the option second argument to a value of "False"
471 Returns True on success, otherwise False
472
473 @rtype: boolean
474 @return: True if the element was added to the document, otherwise False
475 """
476 if not element or element is None:
477 return False
478 if not self.tree or self.tree is None:
479 return False
480
481 ovalid = element.getId()
482 if not ovalid:
483 return False
484
485 root = self.tree.getroot()
486 if not root:
487 root = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}oval_definitions")
488 self.tree._setroot(root)
489
490 # If replace has been set to False, then we want to exit with no changes
491 # when an element with this OVALID already appears in the document
492 if not replace:
493 existing = self.getElementByID(ovalid)
494 if existing:
495 return False
496
497 try:
498 oval_type = OvalElement.getElementTypeFromOvalID(ovalid)
499 except Exception:
500 return False
501
502 # Depending on the ID type, find the parent for it or create that parent if it doesn't exist
503 # Then append the current element
504 if oval_type == OvalDefinition.DEFINITION:
505 parent = root.find("def:definitions", OvalDocument.NS_DEFAULT)
506 if parent is None:
507 parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}definitions")
508 root.append(parent)
509
510 parent.append(element.getElement())
511 self.id_to_definition[ovalid] = element
512 return True
513
514 elif oval_type == OvalDefinition.TEST:
515 parent = root.find("def:tests", OvalDocument.NS_DEFAULT)
516 if parent is None:
517 parent = Element("{" + OvalDocument.NS_DEFAULT.get("def") + "}tests")
518 root.append(parent)

Callers

nothing calls this directly

Calls 5

getElementByIDMethod · 0.95
getIdMethod · 0.80
getElementMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected