コード例 #1
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
 def __get_contents_of_xmlns_string(self, xmlns_string):
     """Converts an xml namespace declaration string to a NamespaceSet."""
     ns = NamespaceSet()
     for m in self.XMLNS_RE.finditer(xmlns_string):
         pfx = m.group(1)
         uri = m.group(2)
         if pfx and pfx[0] == ":":  # drop leading colons if any
             pfx = pfx[1:]
         ns.add_namespace_uri(uri, pfx)
     return ns
コード例 #2
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_subset(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc", "abcschema")
        ns.add_namespace_uri("d:e:f", "def", "defschema")

        correct_subns = NamespaceSet()
        correct_subns.add_namespace_uri("a:b:c", "abc", "abcschema")

        subns = ns.subset(("a:b:c", ))
        self.assertEqual(subns, correct_subns)
コード例 #3
0
    def test_set_semantics(self):
        nsset = NamespaceSet()
        self.assertEqual(0, len(nsset))

        ns1 = Namespace("http://example.com/", "example", "")
        nsset.add(ns1)
        self.assertEqual(1, len(nsset))

        # Adding the same namespace doesn't have an effect
        nsset.add(ns1)
        self.assertEqual(1, len(nsset))

        # Neither does adding a new namespace object that is "equal" to the
        # first.
        nsset.add(Namespace("http://example.com/", "example", ""))
        self.assertEqual(1, len(nsset))
コード例 #4
0
ファイル: namespaces_tests.py プロジェクト: cy-fir/mixbox
    def test_set_semantics(self):
        nsset = NamespaceSet()
        self.assertEqual(0, len(nsset))

        ns1 = Namespace("http://example.com/", "example", "")
        nsset.add(ns1)
        self.assertEqual(1, len(nsset))

        # Adding the same namespace doesn't have an effect
        nsset.add(ns1)
        self.assertEqual(1, len(nsset))

        # Neither does adding a new namespace object that is "equal" to the
        # first.
        nsset.add(Namespace("http://example.com/", "example", ""))
        self.assertEqual(1, len(nsset))
コード例 #5
0
ファイル: nsparser.py プロジェクト: CybOXProject/python-cybox
NS_WINHOOK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinHookObject-1', 'WinHookObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Hook/1.0/Win_Hook_Object.xsd')
NS_WINKERNELHOOK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinKernelHookObject-2', 'WinKernelHookObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Kernel_Hook/2.1/Win_Kernel_Hook_Object.xsd')
NS_WINKERNEL_OBJECT = Namespace('http://cybox.mitre.org/objects#WinKernelObject-2', 'WinKernelObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Kernel/2.1/Win_Kernel_Object.xsd')
NS_WINMAILSLOT_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMailslotObject-2', 'WinMailslotObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Mailslot/2.1/Win_Mailslot_Object.xsd')
NS_WINMEMORYPAGEREGION_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMemoryPageRegionObject-2', 'WinMemoryPageRegionObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Memory_Page_Region/2.1/Win_Memory_Page_Region_Object.xsd')
NS_WINMUTEX_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMutexObject-2', 'WinMutexObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Mutex/2.1/Win_Mutex_Object.xsd')
NS_WINNETWORKROUTEENTRY_OBJECT = Namespace('http://cybox.mitre.org/objects#WinNetworkRouteEntryObject-2', 'WinNetworkRouteEntryObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Network_Route_Entry/2.1/Win_Network_Route_Entry_Object.xsd')
NS_WINNETWORKSHARE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinNetworkShareObject-2', 'WinNetworkShareObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Network_Share/2.1/Win_Network_Share_Object.xsd')
NS_WINPIPE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinPipeObject-2', 'WinPipeObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Pipe/2.1/Win_Pipe_Object.xsd')
NS_WINPREFETCH_OBJECT = Namespace('http://cybox.mitre.org/objects#WinPrefetchObject-2', 'WinPrefetchObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Prefetch/2.1/Win_Prefetch_Object.xsd')
NS_WINPROCESS_OBJECT = Namespace('http://cybox.mitre.org/objects#WinProcessObject-2', 'WinProcessObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Process/2.1/Win_Process_Object.xsd')
NS_WINREGISTRYKEY_OBJECT = Namespace('http://cybox.mitre.org/objects#WinRegistryKeyObject-2', 'WinRegistryKeyObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Registry_Key/2.1/Win_Registry_Key_Object.xsd')
NS_WINSEMAPHORE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSemaphoreObject-2', 'WinSemaphoreObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Semaphore/2.1/Win_Semaphore_Object.xsd')
NS_WINSERVICE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinServiceObject-2', 'WinServiceObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Service/2.1/Win_Service_Object.xsd')
NS_WINSYSTEM_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSystemObject-2', 'WinSystemObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_System/2.1/Win_System_Object.xsd')
NS_WINSYSTEMRESTORE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSystemRestoreObject-2', 'WinSystemRestoreObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_System_Restore/2.1/Win_System_Restore_Object.xsd')
NS_WINTASK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinTaskObject-2', 'WinTaskObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Task/2.1/Win_Task_Object.xsd')
NS_WINTHREAD_OBJECT = Namespace('http://cybox.mitre.org/objects#WinThreadObject-2', 'WinThreadObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Thread/2.1/Win_Thread_Object.xsd')
NS_WINUSERACCOUNT_OBJECT = Namespace('http://cybox.mitre.org/objects#WinUserAccountObject-2', 'WinUserAccountObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_User_Account/2.1/Win_User_Account_Object.xsd')
NS_WINVOLUME_OBJECT = Namespace('http://cybox.mitre.org/objects#WinVolumeObject-2', 'WinVolumeObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Volume/2.1/Win_Volume_Object.xsd')
NS_WINWAITABLETIMER_OBJECT = Namespace('http://cybox.mitre.org/objects#WinWaitableTimerObject-2', 'WinWaitableTimerObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Waitable_Timer/2.1/Win_Waitable_Timer_Object.xsd')
NS_X509CERTIFICATE_OBJECT = Namespace('http://cybox.mitre.org/objects#X509CertificateObject-2', 'X509CertificateObj', 'http://cybox.mitre.org/XMLSchema/objects/X509_Certificate/2.1/X509_Certificate_Object.xsd')

CYBOX_NAMESPACES = NamespaceSet()

# Magic to automatically register all Namespaces defined in this module.
for k, v in dict(globals()).items():
    if k.startswith('NS_'):
        register_namespace(v)
        CYBOX_NAMESPACES.add_namespace(v)
コード例 #6
0
ファイル: nsparser.py プロジェクト: kralca/python-cybox
NS_WINHOOK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinHookObject-1', 'WinHookObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Hook/1.0/Win_Hook_Object.xsd')
NS_WINKERNELHOOK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinKernelHookObject-2', 'WinKernelHookObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Kernel_Hook/2.1/Win_Kernel_Hook_Object.xsd')
NS_WINKERNEL_OBJECT = Namespace('http://cybox.mitre.org/objects#WinKernelObject-2', 'WinKernelObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Kernel/2.1/Win_Kernel_Object.xsd')
NS_WINMAILSLOT_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMailslotObject-2', 'WinMailslotObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Mailslot/2.1/Win_Mailslot_Object.xsd')
NS_WINMEMORYPAGEREGION_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMemoryPageRegionObject-2', 'WinMemoryPageRegionObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Memory_Page_Region/2.1/Win_Memory_Page_Region_Object.xsd')
NS_WINMUTEX_OBJECT = Namespace('http://cybox.mitre.org/objects#WinMutexObject-2', 'WinMutexObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Mutex/2.1/Win_Mutex_Object.xsd')
NS_WINNETWORKROUTEENTRY_OBJECT = Namespace('http://cybox.mitre.org/objects#WinNetworkRouteEntryObject-2', 'WinNetworkRouteEntryObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Network_Route_Entry/2.1/Win_Network_Route_Entry_Object.xsd')
NS_WINNETWORKSHARE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinNetworkShareObject-2', 'WinNetworkShareObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Network_Share/2.1/Win_Network_Share_Object.xsd')
NS_WINPIPE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinPipeObject-2', 'WinPipeObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Pipe/2.1/Win_Pipe_Object.xsd')
NS_WINPREFETCH_OBJECT = Namespace('http://cybox.mitre.org/objects#WinPrefetchObject-2', 'WinPrefetchObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Prefetch/2.1/Win_Prefetch_Object.xsd')
NS_WINPROCESS_OBJECT = Namespace('http://cybox.mitre.org/objects#WinProcessObject-2', 'WinProcessObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Process/2.1/Win_Process_Object.xsd')
NS_WINREGISTRYKEY_OBJECT = Namespace('http://cybox.mitre.org/objects#WinRegistryKeyObject-2', 'WinRegistryKeyObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Registry_Key/2.1/Win_Registry_Key_Object.xsd')
NS_WINSEMAPHORE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSemaphoreObject-2', 'WinSemaphoreObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Semaphore/2.1/Win_Semaphore_Object.xsd')
NS_WINSERVICE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinServiceObject-2', 'WinServiceObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Service/2.1/Win_Service_Object.xsd')
NS_WINSYSTEM_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSystemObject-2', 'WinSystemObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_System/2.1/Win_System_Object.xsd')
NS_WINSYSTEMRESTORE_OBJECT = Namespace('http://cybox.mitre.org/objects#WinSystemRestoreObject-2', 'WinSystemRestoreObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_System_Restore/2.1/Win_System_Restore_Object.xsd')
NS_WINTASK_OBJECT = Namespace('http://cybox.mitre.org/objects#WinTaskObject-2', 'WinTaskObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Task/2.1/Win_Task_Object.xsd')
NS_WINTHREAD_OBJECT = Namespace('http://cybox.mitre.org/objects#WinThreadObject-2', 'WinThreadObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Thread/2.1/Win_Thread_Object.xsd')
NS_WINUSERACCOUNT_OBJECT = Namespace('http://cybox.mitre.org/objects#WinUserAccountObject-2', 'WinUserAccountObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_User_Account/2.1/Win_User_Account_Object.xsd')
NS_WINVOLUME_OBJECT = Namespace('http://cybox.mitre.org/objects#WinVolumeObject-2', 'WinVolumeObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Volume/2.1/Win_Volume_Object.xsd')
NS_WINWAITABLETIMER_OBJECT = Namespace('http://cybox.mitre.org/objects#WinWaitableTimerObject-2', 'WinWaitableTimerObj', 'http://cybox.mitre.org/XMLSchema/objects/Win_Waitable_Timer/2.1/Win_Waitable_Timer_Object.xsd')
NS_X509CERTIFICATE_OBJECT = Namespace('http://cybox.mitre.org/objects#X509CertificateObject-2', 'X509CertificateObj', 'http://cybox.mitre.org/XMLSchema/objects/X509_Certificate/2.1/X509_Certificate_Object.xsd')

CYBOX_NAMESPACES = NamespaceSet()

# Magic to automatically register all Namespaces defined in this module.
for k, v in dict(globals()).items():
    if k.startswith('NS_'):
        register_namespace(v)
        CYBOX_NAMESPACES.add(v)
コード例 #7
0
ファイル: nsparser.py プロジェクト: svakkalanka/python-maec
# Copyright (c) 2015, The MITRE Corporation
# All rights reserved

# Compatible with MAEC v4.1

from mixbox.namespaces import Namespace, NamespaceSet, register_namespace

NS_MAEC_BUNDLE = Namespace(
    'http://maec.mitre.org/XMLSchema/maec-bundle-4', 'maecBundle',
    'http://maec.mitre.org/language/version4.1/maec_bundle_schema.xsd')
NS_MAEC_PACKAGE = Namespace(
    'http://maec.mitre.org/XMLSchema/maec-package-2', 'maecPackage',
    'http://maec.mitre.org/language/version4.1/maec_package_schema.xsd')
NS_MAEC_VOCABS = Namespace(
    'http://maec.mitre.org/default_vocabularies-1', 'maecVocabs',
    'http://maec.mitre.org/language/version4.1/maec_default_vocabularies.xsd')

MAEC_NAMESPACES = NamespaceSet()

# Magic to automatically register all Namespaces defined in this module.
for k, v in dict(globals()).items():
    if k.startswith('NS_'):
        register_namespace(v)
        MAEC_NAMESPACES.add_namespace(v)
コード例 #8
0
ファイル: nsparser.py プロジェクト: MAECProject/python-maec
# Copyright (c) 2018, The MITRE Corporation
# All rights reserved

# Compatible with MAEC v4.1

from mixbox.namespaces import Namespace, NamespaceSet, register_namespace

NS_MAEC_BUNDLE = Namespace('http://maec.mitre.org/XMLSchema/maec-bundle-4', 'maecBundle', 'http://maec.mitre.org/language/version4.1/maec_bundle_schema.xsd')
NS_MAEC_PACKAGE = Namespace('http://maec.mitre.org/XMLSchema/maec-package-2', 'maecPackage', 'http://maec.mitre.org/language/version4.1/maec_package_schema.xsd')
NS_MAEC_VOCABS = Namespace('http://maec.mitre.org/default_vocabularies-1', 'maecVocabs', 'http://maec.mitre.org/language/version4.1/maec_default_vocabularies.xsd')


MAEC_NAMESPACES = NamespaceSet()

# Magic to automatically register all Namespaces defined in this module.
for k, v in dict(globals()).items():
    if k.startswith('NS_'):
        register_namespace(v)
        MAEC_NAMESPACES.add_namespace(v)
コード例 #9
0
ファイル: nsparser.py プロジェクト: awest1339/python-maec
# Copyright (c) 2015, The MITRE Corporation
# All rights reserved

# Compatible with MAEC v4.1

from mixbox.namespaces import Namespace, NamespaceSet, register_namespace

NS_MAEC_BUNDLE = Namespace('http://maec.mitre.org/XMLSchema/maec-bundle-4', 'maecBundle', 'http://maec.mitre.org/language/version4.1/maec_bundle_schema.xsd')
NS_MAEC_PACKAGE = Namespace('http://maec.mitre.org/XMLSchema/maec-package-2', 'maecPackage', 'http://maec.mitre.org/language/version4.1/maec_package_schema.xsd')
NS_MAEC_VOCABS = Namespace('http://maec.mitre.org/default_vocabularies-1', 'maecVocabs', 'http://maec.mitre.org/language/version4.1/maec_default_vocabularies.xsd')


MAEC_NAMESPACES = NamespaceSet()

# Magic to automatically register all Namespaces defined in this module.
for k, v in dict(globals()).items():
    if k.startswith('NS_'):
        register_namespace(v)
        MAEC_NAMESPACES.add(v)
コード例 #10
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_prefixes(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "pfx")
        ns.add_namespace_uri("a:b:c", "pfx2")
        ns.add_prefix("a:b:c", "pfx3")
        self.assertEqual(len(ns.get_prefixes("a:b:c")), 3)
        self.assertTrue(ns.is_valid())

        ns.remove_prefix("pfx3")
        self.assertEqual(len(ns.get_prefixes("a:b:c")), 2)
        self.assertTrue(ns.is_valid())

        # Make sure prefix_iter() gives the same prefixes as get_prefixes().
        self.assertEqual(len(ns.get_prefixes("a:b:c")),
                         len(list(ns.prefix_iter("a:b:c"))))
        #self.assertTrue(all(map(operator.eq, ns.get_prefixes("a:b:c"),
        #                        ns.prefix_iter("a:b:c"))))
        # One time, I saw the above test code fail.  I never saw it again.
        # Seems like the prefixes may have come out in a different order or
        # something... guess I'll play it safe and copy them to sorted
        # lists before comparing.
        pfxs1 = sorted(ns.get_prefixes("a:b:c"))
        pfxs2 = sorted(ns.prefix_iter("a:b:c"))
        self.assertTrue(all(map(operator.eq, pfxs1, pfxs2)))

        self.assertEqual(ns.namespace_for_prefix("pfx"), "a:b:c")
        self.assertEqual(ns.namespace_for_prefix("pfx2"), "a:b:c")

        # Should get an exception if we try to map the same prefix to a
        # different namespace.
        self.assertRaises(mixbox.namespaces.DuplicatePrefixError,
                          ns.add_namespace_uri, "x:y:z", "pfx")
        self.assertRaises(mixbox.namespaces.DuplicatePrefixError,
                          ns.add_namespace, Namespace("x:y:z", "pfx", None))

        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.get_prefixes, "does:not:exist")
        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.prefix_iter, "does:not:exist")
        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.add_prefix, "does:not:exist", "dne")

        ns.remove_prefix("does:not:exist")

        self.assertTrue(ns.is_valid())
コード例 #11
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_set_semantics(self):
        nsset = NamespaceSet()
        self.assertEqual(0, len(nsset))

        ns1 = Namespace("http://example.com/", "example", "")
        nsset.add_namespace(ns1)
        self.assertEqual(1, len(nsset))

        # Adding the same namespace doesn't have an effect
        nsset.add_namespace(ns1)
        self.assertEqual(1, len(nsset))

        # Neither does adding a new namespace object that is "equal" to the
        # first.
        nsset.add_namespace(Namespace("http://example.com/", "example", ""))
        self.assertEqual(1, len(nsset))

        # Adding a prefix doesn't change the set size
        nsset.add_namespace_uri("http://example.com/", "example2")
        self.assertEqual(1, len(nsset))

        self.assertTrue(nsset.contains_namespace("http://example.com/"))

        self.assertTrue(nsset.is_valid())
コード例 #12
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_import(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc", "abcschema")

        imported_ns = NamespaceSet()
        imported_ns.add_namespace_uri("d:e:f", "def", "defschema")

        union_ns = NamespaceSet()
        union_ns.add_namespace_uri("a:b:c", "abc", "abcschema")
        union_ns.add_namespace_uri("d:e:f", "def", "defschema")

        ns.import_from(imported_ns)
        self.assertEqual(ns, union_ns)

        imported_ns2 = NamespaceSet()
        imported_ns2.add_namespace_uri("a:b:c", "abc2")
        # a:b:c is already in ns, replace=True, so this replaces ns's a:b:c
        # entry with (a copy of) imported_ns2's entry.
        ns.import_from(imported_ns2, True)

        self.assertEqual(ns.get_prefixes("a:b:c"), set(["abc2"]))
        self.assertEqual(ns.get_schema_location("a:b:c"), None)

        imported_ns3 = NamespaceSet()
        imported_ns3.add_namespace_uri("g:h:i", "def")
        self.assertRaises(mixbox.namespaces.DuplicatePrefixError,
                          ns.import_from, imported_ns3)

        self.assertTrue(ns.is_valid())
コード例 #13
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_strings(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc", "abcschema")
        ns.add_prefix("a:b:c", "abc2")
        ns.add_namespace_uri("d:e:f", "def", "defschema")
        ns.add_namespace_uri("g:h:i", None)

        # Preferred prefixes only; "abc2" should not be a declared prefix for
        # a:b:c.
        xmlns_string = ns.get_xmlns_string()
        m = self.XMLNS_RE_NO_CAP.match(xmlns_string)
        self.assertNotEqual(m, None,
                            "Invalid XML namespace declaration format")

        ns2 = self.__get_contents_of_xmlns_string(xmlns_string)
        self.assertEqual(len(ns2), 3)

        self.assertTrue(ns2.contains_namespace("a:b:c"))
        self.assertTrue(ns2.contains_namespace("d:e:f"))
        self.assertTrue(ns2.contains_namespace("g:h:i"))

        self.assertEqual(ns2.get_prefixes("a:b:c"), set(["abc"]))
        self.assertEqual(ns2.get_prefixes("d:e:f"), set(["def"]))
        self.assertEqual(len(ns2.get_prefixes("g:h:i")), 0)

        self.assertEqual(ns2.preferred_prefix_for_namespace("a:b:c"), "abc")
        self.assertEqual(ns2.preferred_prefix_for_namespace("d:e:f"), "def")
        self.assertEqual(ns2.preferred_prefix_for_namespace("g:h:i"), None)

        # Now, gimme all the prefixes
        xmlns_string = ns.get_xmlns_string(preferred_prefixes_only=False)
        m = self.XMLNS_RE_NO_CAP.match(xmlns_string)
        self.assertNotEqual(
            m, None,
            "Invalid XML namespace declaration format: " + xmlns_string)

        ns2 = self.__get_contents_of_xmlns_string(xmlns_string)
        self.assertTrue(self.__namespaceset_equal_uris_and_prefixes(ns, ns2))

        schemaloc_string = ns.get_schemaloc_string()
        m = self.SCHEMALOC_RE_NO_CAP.match(schemaloc_string)
        self.assertNotEqual(m, None,
                            "Invalid XML schema location declaration format")
        schemaloc_dict = self.__get_schema_location_pairs(schemaloc_string)

        self.assertEqual(schemaloc_dict, {
            "a:b:c": "abcschema",
            "d:e:f": "defschema"
        })

        # Uh oh, another namespace with no prefixes... this NamespaceSet can't
        # yield an xmlns declaration string anymore.
        ns.add_namespace_uri("j:k:l", None)
        self.assertRaises(mixbox.namespaces.TooManyDefaultNamespacesError,
                          ns.get_xmlns_string)
コード例 #14
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_maps(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc", "abcschema")
        ns.add_prefix("a:b:c", "abc2")
        ns.add_namespace_uri("d:e:f", "def", "defschema")
        ns.set_preferred_prefix_for_namespace("d:e:f", None)

        self.assertEqual(ns.get_uri_prefix_map(), {
            "a:b:c": "abc",
            "d:e:f": "def"
        })
        self.assertEqual(ns.get_prefix_uri_map(), {
            "abc": "a:b:c",
            "def": "d:e:f"
        })

        # The map functions require all namespaces to have at least one
        # prefix (non-None).
        ns.add_namespace_uri("g:h:i", None, None)
        self.assertRaises(mixbox.namespaces.NoPrefixesError,
                          ns.get_prefix_uri_map)
        self.assertRaises(mixbox.namespaces.NoPrefixesError,
                          ns.get_uri_prefix_map)

        self.assertEqual(ns.get_uri_schemaloc_map(), {
            "a:b:c": "abcschema",
            "d:e:f": "defschema"
        })

        # If a preferred prefix is None, one of the other prefixes is chosen.
        # The choice is unpredictable.
        ns.add_prefix("g:h:i", "ghi1")
        ns.add_prefix("g:h:i", "ghi2")
        ns.add_prefix("g:h:i", "ghi3")

        uri_pfx_map = ns.get_uri_prefix_map()
        self.assertTrue(uri_pfx_map["g:h:i"] in ("ghi1", "ghi2", "ghi3"))

        pfx_uri_map = ns.get_prefix_uri_map()
        self.assertTrue("ghi1" in pfx_uri_map or "ghi2" in pfx_uri_map
                        or "ghi3" in pfx_uri_map)

        self.assertTrue(ns.is_valid())
コード例 #15
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_schema_locations(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc", "sc:he:ma")

        self.assertEqual(ns.get_schema_location("a:b:c"), "sc:he:ma")

        self.assertRaises(mixbox.namespaces.ConflictingSchemaLocationError,
                          ns.set_schema_location, "a:b:c", "other:schemaloc")
        self.assertRaises(mixbox.namespaces.ConflictingSchemaLocationError,
                          ns.add_namespace,
                          Namespace("a:b:c", "abc", "other:schemaloc"))
        self.assertRaises(mixbox.namespaces.ConflictingSchemaLocationError,
                          ns.add_namespace_uri, "a:b:c", "abc",
                          "other:schemaloc")
        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.set_schema_location, "does:not:exist", "sc:he:ma")
        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.get_schema_location, "does:not:exist")

        ns.set_schema_location("a:b:c", "sc:he:ma2", True)
        self.assertEqual(ns.get_schema_location("a:b:c"), "sc:he:ma2")

        # test schema location merging; these should not raise exceptions
        ns.add_namespace_uri("a:b:c", "abc", None)
        self.assertEqual(ns.get_schema_location("a:b:c"), "sc:he:ma2")

        ns.add_namespace_uri("d:e:f", "def", None)
        ns.add_namespace_uri("d:e:f", "def", "def:schema")
        self.assertEqual(ns.get_schema_location("d:e:f"), "def:schema")

        self.assertTrue(ns.is_valid())
コード例 #16
0
ファイル: namespaces_tests.py プロジェクト: ajnovice/mixbox
    def test_preferred_prefixes(self):
        ns = NamespaceSet()
        ns.add_namespace_uri("a:b:c", "abc")
        ns.add_namespace_uri("a:b:c", "def")
        self.assertEqual(ns.preferred_prefix_for_namespace("a:b:c"), "abc")

        ns.set_preferred_prefix_for_namespace("a:b:c", "def")
        self.assertEquals(ns.preferred_prefix_for_namespace("a:b:c"), "def")

        ns.set_preferred_prefix_for_namespace("a:b:c", None)

        ns.set_preferred_prefix_for_namespace("a:b:c", "ghi", True)
        self.assertEquals(ns.preferred_prefix_for_namespace("a:b:c"), "ghi")

        ns.add_prefix("a:b:c", "jkl", True)
        self.assertEqual(ns.preferred_prefix_for_namespace("a:b:c"), "jkl")

        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.preferred_prefix_for_namespace, "does:not:exist")
        self.assertRaises(mixbox.namespaces.NamespaceNotFoundError,
                          ns.set_preferred_prefix_for_namespace,
                          "does:not:exist", "dne")
        self.assertRaises(mixbox.namespaces.PrefixNotFoundError,
                          ns.set_preferred_prefix_for_namespace, "a:b:c",
                          "notaprefix")

        self.assertTrue(ns.is_valid())