Skip to content

Commit 87548d6

Browse files
author
Bryan Worrell
committed
Initial work on recording input namespaces
1 parent 2cab37b commit 87548d6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

stix/utils/parser.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ def _check_root(self, tree):
4949

5050
return True
5151

52+
def _apply_input_namespaces(self, tree, entity):
53+
try:
54+
root = tree.getroot() # is tree an lxml.Element or lxml.ElementTree
55+
except AttributeError:
56+
root = tree
57+
58+
entity.__input_namespaces__ = {}
59+
for alias,ns in root.nsmap.iteritems():
60+
entity.__input_namespaces__[ns] = alias
61+
5262
def parse_xml_to_obj(self, xml_file, check_version=True, check_root=True):
5363
"""Creates a STIX binding object from the supplied xml file.
5464
@@ -83,8 +93,22 @@ def parse_xml(self, xml_file, check_version=True, check_root=True):
8393
check_root -- Inspect the root element before parsing.
8494
8595
"""
96+
tree = None
97+
if check_version or check_root:
98+
tree = etree.parse(xml_file)
99+
100+
if check_version:
101+
self._check_version(tree)
102+
103+
if check_root:
104+
self._check_root(tree)
105+
106+
import stix.bindings.stix_core as stix_core_binding
107+
stix_package_obj = stix_core_binding.STIXType().factory()
108+
stix_package_obj.build(tree.getroot())
109+
86110
from stix.core import STIXPackage # resolve circular dependencies
87-
stix_package_obj = self.parse_xml_to_obj(xml_file, check_version, check_root)
88111
stix_package = STIXPackage().from_obj(stix_package_obj)
112+
self._apply_input_namespaces(tree, stix_package)
89113

90114
return stix_package

0 commit comments

Comments
 (0)