Skip to content

Commit 92a1ae6

Browse files
authored
Merge pull request #90 from MAECProject/namespaces_integration
Namespaces integration
2 parents e985cd1 + 676486c commit 92a1ae6

27 files changed

+798
-650
lines changed

examples/package_generation_example.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
from cybox.core import AssociatedObjects, AssociatedObject, Object, AssociationType
99
from cybox.common import Hash, HashList, VocabString
1010
from cybox.objects.file_object import File
11-
from maec.bundle import Bundle, Collections, MalwareAction, Capability
11+
from maec.bundle import Bundle, MalwareAction, Capability
1212
from maec.package import Analysis, MalwareSubject, Package
13-
from cybox.utils import Namespace
14-
import maec.utils
1513

16-
# Instantiate the ID generator class (for automatic ID generation) with our example namespace
17-
NS = Namespace("http://example.com/", "example")
18-
maec.utils.set_id_namespace(NS)
1914
# Instantiate the Bundle, Package, MalwareSubject, and Analysis classes
2015
bundle = Bundle(defined_subject=False)
2116
package = Package()
@@ -58,5 +53,5 @@
5853
# Add the Malware Subject to the Package
5954
package.add_malware_subject(subject)
6055
# Export the Package Bindings Object to an XML file and use the namespaceparser for writing out the namespace definitions
61-
package.to_xml_file('sample_maec_package.xml', {"http://example.com/":"example"})
56+
package.to_xml_file('sample_maec_package.xml')
6257
print "Wrote to sample_maec_package.xml"

maec/__init__.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
from __future__ import absolute_import
55
from mixbox.entities import Entity as cyboxEntity
66
from mixbox.entities import EntityList
7-
from mixbox.namespaces import (Namespace, get_xmlns_string,
8-
get_schemaloc_string, lookup_name, lookup_prefix)
7+
from mixbox.namespaces import ( get_xmlns_string,
8+
make_namespace_subset_from_uris, get_schemaloc_string, lookup_prefix)
99
from mixbox.vendor.six import iteritems, string_types
1010

11-
from .bindings import maec_bundle as bundle_binding
12-
from .bindings import maec_package as package_binding
1311
import maec
1412
from maec.utils import flip_dict, EntityParser
1513

@@ -44,7 +42,7 @@ def to_xml_file(self, file, namespace_dict=None, custom_header=None):
4442
namespace_dict = {}
4543
else:
4644
# Make a copy so we don't pollute the source
47-
namespace_dict = dict(iteritems(namespace_dict))
45+
namespace_dict = namespace_dict.copy()
4846

4947
# Update the namespace dictionary with namespaces found upon import
5048
input_namespaces = self._ns_to_prefix_input_namespaces()
@@ -84,32 +82,23 @@ def _get_namespace_def(self, additional_ns_dict=None):
8482
# if there are any other namepaces, include xsi for "schemaLocation"
8583
# also, include the MAEC default vocabularies schema by default
8684
if namespaces:
87-
namespaces.update([lookup_prefix('xsi')])
88-
namespaces.update([lookup_prefix('maecVocabs')])
85+
namespaces.add(lookup_prefix('xsi'))
86+
namespaces.add(lookup_prefix('maecVocabs'))
8987

90-
if namespaces and additional_ns_dict:
91-
namespace_list = [x.name for x in namespaces if x]
92-
for ns, prefix in iteritems(additional_ns_dict):
93-
if ns not in namespace_list:
94-
namespaces.update([Namespace(ns, prefix, '')])
95-
96-
if not namespaces:
88+
ns_set = make_namespace_subset_from_uris(namespaces)
89+
if additional_ns_dict:
90+
for ns, prefix in iteritems(additional_ns_dict):
91+
ns_set.add_namespace_uri(ns, prefix)
92+
else:
9793
return ""
9894

99-
namespaces = sorted(namespaces, key=str)
100-
101-
return ('\n\t' + get_xmlns_string(namespaces) +
102-
'\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
103-
'"')
95+
return ('\n\t' + ns_set.get_xmlns_string(sort=True, delim='\n\t') +
96+
'\n\t' + ns_set.get_schemaloc_string(sort=True, delim='\n\t'))
10497

10598
def _get_namespaces(self, recurse=True):
106-
nsset = set()
107-
10899
# Get all _namespaces for parent classes
109-
namespaces = [x._namespace for x in self.__class__.__mro__
110-
if hasattr(x, '_namespace')]
111-
112-
nsset.update([lookup_name(ns) for ns in namespaces])
100+
nsset = set(x._namespace for x in self.__class__.__mro__
101+
if hasattr(x, '_namespace'))
113102

114103
#In case of recursive relationships, don't process this item twice
115104
self.touched = True
@@ -122,8 +111,7 @@ def _get_namespaces(self, recurse=True):
122111
# Add any additional namespaces that may be included in the entity
123112
input_ns = self._ns_to_prefix_input_namespaces()
124113
for namespace, alias in iteritems(input_ns):
125-
if not lookup_name(namespace):
126-
nsset.add(Namespace(namespace, alias, ''))
114+
nsset.update(namespace)
127115

128116
return nsset
129117

0 commit comments

Comments
 (0)