File: test-saxutils.py

package info (click to toggle)
pyxb 1.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 63,792 kB
  • ctags: 48,994
  • sloc: python: 235,928; sh: 803; xml: 657; makefile: 57
file content (124 lines) | stat: -rw-r--r-- 5,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
import logging
if __name__ == '__main__':
    logging.basicConfig()
_log = logging.getLogger(__name__)
import unittest
from pyxb.utils.saxutils import *
from xml.dom import Node
import xml.dom
import pyxb.namespace

class TestState (SAXElementState):
    StateSequence = []

    def __init__ (self, *args, **kw):
        self.StateSequence.append(self)
        super(TestState, self).__init__(*args, **kw)

BogusNamespace = pyxb.namespace.NamespaceInstance('urn:test-saxutils-bogus')
books_ns = pyxb.namespace.NamespaceInstance('urn:loc.gov:books')
isbn_ns = pyxb.namespace.NamespaceInstance('urn:ISBN:0-395-36341-6')
xhtml_ns = pyxb.namespace.NamespaceInstance('http://www.w3.org/1999/xhtml')

class TestInScopeNames (unittest.TestCase):
    def show (self, node):
        xmlns_map = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(node).inScopeNamespaces()
        #print '%s: %s' % (node.nodeName, ' ; '.join([ '%s=%s' % (_k, _v.uri()) for (_k, _v) in xmlns_map.items()]))
        return xmlns_map

    def tearDown (self):
        TestState.StateSequence[:] = []

    def stripUndeclaredNamespaces (self, xmlns_map):
        xmlns_map = xmlns_map.copy()
        self.assertEqual(pyxb.namespace.XML.uri(), xmlns_map.pop('xml').uri())
        self.assertEqual(pyxb.namespace.XMLNamespaces.uri(), xmlns_map.pop('xmlns').uri())
        return xmlns_map

    def test_6_2_2 (self):
        xmld = '''<?xml version="1.0"?>
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
      xmlns:isbn='urn:ISBN:0-395-36341-6'>
    <title>Cheaper by the Dozen</title>
    <isbn:number>1568491379</isbn:number>
    <notes>
      <p xmlns='http://www.w3.org/1999/xhtml'>
          This is a <i>funny</i> book!
      </p>
      <p>another graf without namespace change</p>
    </notes>
</book>'''.encode('utf-8')
        saxer = make_parser(element_state_constructor=TestState, location_base='test_6_2_2', fallback_namespace=BogusNamespace)
        handler = saxer.getContentHandler()
        saxer.parse(io.BytesIO(xmld))

        # First is root context; second is the book element
        book = TestState.StateSequence[1]
        en = book.expandedName()
        self.assertTrue(en is not None)
        self.assertEqual(en.namespace(), books_ns)
        self.assertEqual(en.localName(), 'book')
        self.assertEqual(book.namespaceContext().defaultNamespace(), books_ns)
        self.assertEqual(book.namespaceContext().inScopeNamespaces().get('isbn'), isbn_ns)

        title = TestState.StateSequence[2]
        xmlns_map = self.stripUndeclaredNamespaces(title.namespaceContext().inScopeNamespaces())
        self.assertEqual(2, len(xmlns_map))
        self.assertEqual('urn:loc.gov:books', xmlns_map[None].uri())
        self.assertEqual('urn:ISBN:0-395-36341-6', xmlns_map['isbn'].uri())

        p = TestState.StateSequence[5]
        xmlns_map = self.stripUndeclaredNamespaces(p.namespaceContext().inScopeNamespaces())
        self.assertEqual(p.expandedName().localName(), 'p')
        self.assertEqual(2, len(xmlns_map))
        self.assertEqual('http://www.w3.org/1999/xhtml', xmlns_map[None].uri())
        self.assertEqual('urn:ISBN:0-395-36341-6', xmlns_map['isbn'].uri())

        x = TestState.StateSequence[7]
        xmlns_map = self.stripUndeclaredNamespaces(x.namespaceContext().inScopeNamespaces())
        self.assertEqual(x.expandedName().localName(), 'p')
        self.assertEqual(2, len(xmlns_map))
        self.assertEqual('urn:loc.gov:books', xmlns_map[None].uri())
        self.assertEqual('urn:ISBN:0-395-36341-6', xmlns_map['isbn'].uri())

    def test_6_2_3 (self):
        xmld = '''<?xml version='1.0'?>
<Beers>
  <table xmlns='http://www.w3.org/1999/xhtml'>
   <th><td>Name</td><td>Origin</td><td>Description</td></th>
   <tr>
     <td><brandName xmlns="">Huntsman</brandName></td>
     <td><origin xmlns="">Bath, UK</origin></td>
     <td>
       <details xmlns=""><class>Bitter</class><hop>Fuggles</hop>
         <pro>Wonderful hop, light alcohol, good summer beer</pro>
         <con>Fragile; excessive variance pub to pub</con>
         </details>
        </td>
      </tr>
    </table>
  </Beers>'''.encode('utf-8')

        saxer = make_parser(element_state_constructor=TestState, location_base='test_6_2_3', fallback_namespace=BogusNamespace)
        handler = saxer.getContentHandler()
        saxer.parse(io.BytesIO(xmld))

        Beers = TestState.StateSequence[1]
        xmlns_map = self.stripUndeclaredNamespaces(Beers.namespaceContext().inScopeNamespaces())
        self.assertEqual(0, len(xmlns_map))
        table = TestState.StateSequence[2]
        self.assertEqual(xhtml_ns.createExpandedName('table'), table.expandedName())
        xmlns_map = self.stripUndeclaredNamespaces(table.namespaceContext().inScopeNamespaces())
        self.assertEqual(1, len(xmlns_map))
        self.assertEqual('http://www.w3.org/1999/xhtml', xmlns_map[None].uri())
        brandName = TestState.StateSequence[9]
        xmlns_map = self.stripUndeclaredNamespaces(brandName.namespaceContext().inScopeNamespaces())
        self.assertTrue(brandName.expandedName().namespace() is None)
        self.assertEqual('brandName', brandName.expandedName().localName())
        self.assertEqual(0, len(xmlns_map))

if '__main__' == __name__:
    unittest.main()