summaryrefslogtreecommitdiffstats
path: root/doc/exts/xmlschema.py
blob: 89104c2a64aa549020a1d624daacd698aae33cad (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
""" Sphinx extension to generate documention from XML schemas.  Known
to be woefully imcomplete, probably buggy, terrible error handling,
but it *works* for the subset of XML schema we use in Bcfg2.

Provides the following directives:

* ``.. xml:schema:: <filename>``: Document an XML schema
* ``.. xml:type:: <name>``: Document a complexType or simpleType
* ``.. xml:group:: <name>``: Document an element group
* ``.. xml:attributegroup:: <name>``: Document an attributeGroup
* ``.. xml:element:: <name>``: Document an XML element

Each directive supports the following options:

* ``:namespace: <ns>``: Specify the namespace of the given entity
* ``:nochildren:``: Do not generate documentation for child entities
* ``:noattributegroups:``: Do not generate documentation about
  attribute groups
* ``:nodoc:``: Do not include the documentation included in the entity
  annotation
* ``:notext:``: Do not generate documentation about the text content
  of the entity
* ``:onlyattrs: <attr>,<attr>``: Only generate documentation about the
  comma-separated list of attributes given
* ``:requiredattrs: <attr>,attr>``: Claim that the attributes named in
  the given comma-separated list are required, even if they are not
  flagged as such in the schema.
* ``:linktotype: [<type>,<type>]``: If used as a flag, link to
  documentation on all child types and elements.  If a list is given,
  only link to those types given.  (The default is to generate full
  inline docs for those types.)
* ``:noautodep: [<name>,<name>]``: Do not automatically generate docs
  for any dependent entities.
* ``:inlinetypes: <type>,<type>``: Override a default ``:linktotype:``
  setting for the given types.

Provides the following roles to link to the objects documented above:

* ``:xml:schema:`<name>```: Link to an XML schema
* ``:xml:type:`<name>```: Link to a complexType or simpleType
* ``:xml:group:`<name>```: Link to an element group
* ``:xml:attributegroup:`<name>```: Link to an attributeGroup
* ``:xml:element:`<name>```: Link to an element
* ``:xml:attribute:`<context>:<name>```: Link to the attribute in the
  given context.  The context is the name of the containing object,
  e.g., the parent attributeGroup, element, or complexType.
* ``:xml:datatype:`<name>```: Link to a built-in XML data type.

Note that the entity being linked to does not need to have been
explicitly documented with a directive; e.g., if you document a schema
that contains a complexType, you can link to that type without having
used the ``xml:type::`` directive.

Note also that it's far more reliable to link to a complexType than an
element, since element name collisions are fairly common.  You should
avoid type name collisions whenever possible to maximize usability of
this extension.

There are two configuration items that may be added to conf.py:

* ``xmlschema_path`` gives the base path to all XML schemas.
* ``xmlschema_datatype_url`` gives a string pattern that will be used
  to generate links to built-in XML types.  It must contain a single
  ``%s``, which will be replaced by the name of the type.
"""

import os
import operator
import lxml.etree
from docutils import nodes
from sphinx import addnodes, roles
from docutils.statemachine import ViewList
from docutils.parsers.rst import directives
from sphinx.util.nodes import make_refnode, split_explicit_title, \
    nested_parse_with_titles
from sphinx.util.compat import Directive
from sphinx.domains import ObjType, Domain

try:
    from new import classobj
except ImportError:
    classobj = type

XS = "http://www.w3.org/2001/XMLSchema"
XS_NS = "{%s}" % XS
NSMAP = dict(xs=XS)


def comma_split(opt):
    return opt.split(",")


def flag_or_split(opt):
    try:
        return opt.split(",")
    except AttributeError:
        return True


class _XMLDirective(Directive):
    """ Superclass for the other XML schema directives. """
    required_arguments = 1
    option_spec = dict(namespace=directives.unchanged,
                       nochildren=directives.flag,
                       noattributegroups=directives.flag,
                       nodoc=directives.flag,
                       notext=directives.flag,
                       onlyattrs=comma_split,
                       requiredattrs=comma_split,
                       linktotype=flag_or_split,
                       noautodep=flag_or_split,
                       inlinetypes=comma_split)
    types = []

    def run(self):
        name = self.arguments[0]
        env = self.state.document.settings.env
        reporter = self.state.memo.reporter
        ns_name = self.options.get('namespace')
        try:
            ns_uri = env.xmlschema_namespaces[ns_name]
        except KeyError:
            # URI given as namespace
            ns_uri = ns_name
        etype = None
        for etype in self.types:
            try:
                entity = env.xmlschema_entities[ns_uri][etype][name]
                break
            except KeyError:
                pass
        else:
            reporter.error("No XML %s %s found" %
                           (" or ".join(self.types), name))
            return []
        documentor = XMLDocumentor(entity, env, self.state, name=name,
                                   ns_uri=ns_uri,
                                   include=self.process_include(),
                                   options=self.process_options())
        return documentor.document()

    def process_include(self):
        return dict(children='nochildren' not in self.options,
                    attributegroups='noattributegroups' not in self.options,
                    doc='nodoc' not in self.options,
                    text='notext' not in self.options)

    def process_options(self):
        return dict(onlyattrs=self.options.get('onlyattrs'),
                    requiredattrs=self.options.get('requiredattrs', []),
                    linktotype=self.options.get('linktotype', []),
                    noautodep=self.options.get('noautodep', False),
                    inlinetypes=self.options.get('inlinetypes', []))


def XMLDirective(types):
    class cls(_XMLDirective):
        pass

    cls.__name__ = 'XML%sDirective' % types[0]
    cls.types = types
    return cls


class XMLDocumentor(object):
    def __init__(self, entity, environment, state, name=None, ns_uri=None,
                 parent=None, include=None, options=None):
        self.entity = entity
        self.env = environment
        self.entities = self.env.xmlschema_entities
        self.namespaces = self.env.xmlschema_namespaces
        self.namespaces_by_uri = self.env.xmlschema_namespaces_by_uri
        self.state = state
        self.include = include
        self.options = options
        self.app = self.env.app
        self.reporter = self.state.memo.reporter

        if name is None:
            self.ns_uri = ns_uri
            self.fqname = self.entity.get("name")
            self.ns_name, self.name = self.split_ns(self.fqname)
            if self.ns_uri is None and self.ns_name is not None:
                self.ns_uri = self.namespaces[self.ns_name]
        else:
            self.ns_uri = ns_uri
            self.ns_name = self.namespaces_by_uri[self.ns_uri]
            self.name = name
            if self.ns_name:
                self.fqname = "%s:%s" % (self.ns_name, self.name)
            else:
                self.fqname = name
        self.tname = nodes.strong(self.fqname, self.fqname)
        self.tag = self.entity.tag[len(XS_NS):]
        self.type = tag2type(self.tag)
        self.parent = parent
        if self.parent is None:
            self.dependencies = []
            self.documented = []
        else:
            self.dependencies = self.parent.dependencies
            self.documented = self.parent.documented

    def document(self):
        eid = (self.tag, self.fqname)
        if eid in self.documented:
            return [build_paragraph(get_xref(self.tag, eid[1]))]
        else:
            self.documented.append(eid)

        rv = [self.target_node(self.tag, self.ns_name, self.name)]

        data = addnodes.desc(objtype=self.tag)
        targetid = get_target_id(self.tag, self.ns_name, self.name)
        header = addnodes.desc_signature('', '',
                                         first=True,
                                         ids=[targetid])

        if self.include['doc']:
            header.extend([nodes.emphasis(self.tag, self.tag),
                          text(" "), self.tname])
            data.append(header)
        contents = nodes.definition()
        if self.include['doc']:
            contents.append(self.get_doc(self.entity))
        contents.extend(getattr(self, "document_%s" % self.tag)())
        data.append(contents)
        rv.append(data)

        if self.parent is None:
            # avoid adding duplicate dependencies
            added = [(self.type, self.name)]
            for typ, name, entity in self.dependencies:
                if not name:
                    name = entity.get('name')
                if (typ, name) in added:
                    continue
                ns_name, name = self.split_ns(name)
                ns_uri = self.namespaces[ns_name]
                if not entity:
                    try:
                        entity = self.entities[ns_uri][typ][name]
                    except KeyError:
                        self.app.warn("Dependency %s not found in schemas" %
                                      get_target_id(typ, ns_name, name))
                        continue
                doc = self.get_documentor(entity, name=name, ns_uri=ns_uri)
                rv.extend(doc.document())
                added.append((typ, name))
        return rv

    def document_schema(self):
        try:
            element = self.entity.xpath("xs:element", namespaces=NSMAP)[0]
            ns, name = self.split_ns(element.get("name"))
            doc = self.get_documentor(element, name=name,
                                      ns_uri=self.namespaces[ns])
            return doc.document()
        except IndexError:
            # no top-level element or group -- just a list of
            # (abstract) complexTypes?
            rv = []
            for ctype in self.entity.xpath("xs:complexType", namespaces=NSMAP):
                ns, name = self.split_ns(ctype.get("name"))
                doc = self.get_documentor(ctype, name=name,
                                          ns_uri=self.namespaces[ns])
                rv.extend(doc.document())
            return rv

    def document_group(self):
        rv = nodes.definition_list()
        try:
            (children, groups) = \
                self.get_child_elements(self.entity, nodeclass=nodes.paragraph)
        except TypeError:
            return [build_paragraph(nodes.strong("Any", "Any"),
                                    " arbitrary element allowed")]

        append_node(rv, nodes.term, text("Elements:"))
        append_node(rv, nodes.definition, *children)
        if len(groups):
            append_node(rv, nodes.term, text("Element groups:"))
            append_node(rv, nodes.definition, *groups)
        return rv

    def document_element(self):
        fqtype = self.entity.get("type")
        if fqtype:
            (etype_ns, etype) = self.split_ns(fqtype)
            ns_uri = self.get_namespace_uri(etype_ns)
            values = self.get_values_from_type()
            if values != "Any":
                return [build_paragraph(
                        self.tname,
                        " takes only text content, which may be the ",
                        "following values: ",
                        values)]
            elif etype in self.entities[ns_uri]["complexType"]:
                if ((self.options['linktotype'] is True or
                     self.name in self.options['linktotype'] or
                     etype in self.options['linktotype'] or
                     fqtype in self.options['linktotype']) and
                    self.name not in self.options['inlinetypes'] and
                    etype not in self.options['inlinetypes']):
                    self.add_dep('complexType', fqtype, None)
                    return [build_paragraph("Type: ",
                                            get_xref("type", fqtype))]

                typespec = self.entities[ns_uri]["complexType"][etype]
                doc = self.get_documentor(typespec,
                                          name=self.entity.get("name"))
                rv = [self.target_node("complexType", etype_ns, etype)]
                if self.include['doc'] and not self.get_doc(self.entity):
                    rv.append(self.get_doc(typespec))
                rv.extend(doc.document_complexType())
                return rv
            else:
                self.reporter.error("Unknown element type %s" % fqtype)
                return []
        else:
            rv = []
            typespec = self.entity.xpath("xs:complexType", namespaces=NSMAP)[0]
            if self.include['doc'] and not self.get_doc(self.entity):
                rv.append(self.get_doc(typespec))
            if typespec is not None:
                rv = [self.target_node("complexType", self.ns_name, self.name)]
                doc = self.get_documentor(typespec)
                rv.extend(doc.document_complexType())
            return rv

    def document_complexType(self):
        rv = nodes.definition_list()

        try:
            content = self.entity.xpath("xs:simpleContent",
                                        namespaces=NSMAP)[0]
            base = content.xpath("xs:extension|xs:restriction",
                                 namespaces=NSMAP)[0]
            attr_container = base
        except IndexError:
            base = None
            attr_container = self.entity

        ##### ATTRIBUTES #####
        table, tbody = self.get_attr_table()
        attrs = self.get_attrs(attr_container)
        if attrs:
            tbody.extend(attrs)

        foreign_attr_groups = nodes.bullet_list()
        for agroup in attr_container.xpath("xs:attributeGroup",
                                           namespaces=NSMAP):
            # if the attribute group is in another namespace, just
            # link to it
            ns, name = self.split_ns(agroup.get('ref'))
            if ns != self.ns_name:
                append_node(
                    foreign_attr_groups,
                    nodes.list_item,
                    build_paragraph(get_xref(tag2type("attributeGroup"),
                                             ":".join([ns, name]))))
            else:
                tbody.extend(self.get_attrs(
                        self.entities['attributeGroup'][name]))

        if len(tbody):
            append_node(rv, nodes.term, text("Attributes:"))
            append_node(rv, nodes.definition, table)
        if self.include['attributegroups'] and len(foreign_attr_groups):
            append_node(rv, nodes.term, text("Attribute groups:"))
            append_node(rv, nodes.definition, foreign_attr_groups)

        ##### ELEMENTS #####
        if self.include['children']:
            # todo: distinguish between elements that may occur and
            # elements that must occur
            try:
                (children, groups) = self.get_child_elements(self.entity)
            except TypeError:
                children = None
                groups = None
                rv.append(build_paragraph(nodes.strong("Any", "Any"),
                                          " arbitrary child elements allowed"))
            if children:
                append_node(rv, nodes.term, text("Child elements:"))
                append_node(rv, nodes.definition,
                            build_node(nodes.bullet_list, *children))

            if groups:
                append_node(rv, nodes.term, text("Element groups:"))
                append_node(rv, nodes.definition, *groups)

        ##### TEXT CONTENT #####
        if self.include['text']:
            if self.entity.get("mixed", "false").lower() == "true":
                append_node(rv, nodes.term, text("Text content:"))
                append_node(rv, nodes.definition,
                            build_paragraph(self.get_values_from_simpletype()))
            elif base is not None:
                append_node(rv, nodes.term, text("Text content:"))
                append_node(
                    rv, nodes.definition,
                    build_paragraph(self.get_values_from_simpletype(content)))

        return [rv]

    def document_attributeGroup(self):
        attrs = self.get_attrs(self.entity)
        if attrs:
            table, tbody = self.get_attr_table()
            tbody.extend(attrs)
            return [table]
        else:
            return []

    def get_attr_table(self):
        atable = nodes.table()
        atgroup = build_node(nodes.tgroup('', cols=5),
                             nodes.colspec(colwidth=10),
                             nodes.colspec(colwidth=50),
                             nodes.colspec(colwidth=20),
                             nodes.colspec(colwidth=10),
                             nodes.colspec(colwidth=10),
                             nodes.thead('',
                                         build_table_row("Name", "Description",
                                                         "Values", "Required",
                                                         "Default")))
        atable.append(atgroup)
        atable_body = nodes.tbody()
        atgroup.append(atable_body)
        return (atable, atable_body)

    def get_child_elements(self, el, nodeclass=None):
        """ returns a tuple of (child element nodes, element group
        nodes).  HOWEVER, if _any_ child is allowed, returns True. """
        children = []
        groups = []
        if nodeclass is None:
            nodeclass = nodes.list_item

        if el.xpath("xs:any", namespaces=NSMAP):
            return True

        for child in el.xpath("xs:element", namespaces=NSMAP):
            node = nodeclass()
            if child.get('ref'):
                node.append(build_paragraph(get_xref('element',
                                                     child.get('ref'))))
            else:
                # child element given inline
                doc = self.get_documentor(child, name=child.get('name'))
                node.extend(doc.document())
            children.append(node)

        for group in el.xpath("xs:group", namespaces=NSMAP):
            if group.get('ref'):
                name = group.get('ref')
                node = nodeclass()
                node.append(build_paragraph(get_xref('group', name)))
                self.add_dep('group', name, None)
                groups.append(node)
            else:
                rv = self.get_child_elements(group, nodeclass=nodeclass)
                try:
                    children.extend(rv[0])
                    groups.extend(rv[1])
                except TypeError:
                    return rv

        for container in el.xpath("xs:all|xs:choice|xs:sequence",
                                  namespaces=NSMAP):
            rv = self.get_child_elements(container, nodeclass=nodeclass)
            try:
                children.extend(rv[0])
                groups.extend(rv[1])
            except TypeError:
                return rv
        return (children, groups)

    def get_documentor(self, entity, name=None, ns_uri=None):
        if name is None:
            name = self.name
        if ns_uri is None:
            ns_uri = self.ns_uri
        return XMLDocumentor(entity, self.env, self.state, name=name,
                             ns_uri=ns_uri, parent=self, options=self.options,
                             include=self.include)

    def get_attrs(self, el, context=None):
        cnode = el
        while context is None and cnode is not None:
            context = cnode.get('name')
            cnode = cnode.getparent()

        rows = []
        for attr in el.xpath("xs:attribute[@name]", namespaces=NSMAP):
            name = attr.get("name")
            if self.ns_name:
                fqname = "%s:%s" % (self.ns_name, name)
            else:
                fqname = name
            if (self.options['onlyattrs'] and
                name not in self.options['onlyattrs'] and
                fqname not in self.options['onlyattrs']):
                continue
            tag = attr.tag[len(XS_NS):]
            row = [build_paragraph(self.target_node(tag, self.ns_name, context,
                                                    name),
                                   nodes.literal(fqname, fqname))]
            row.append(self.get_doc(attr))
            if attr.get("type") is not None:
                row.append(build_paragraph(
                        self.get_values_from_type(entity=attr)))
            else:
                try:
                    atype = attr.xpath("xs:simpleType", namespaces=NSMAP)[0]
                    row.append(self.get_values_from_simpletype(atype))
                except IndexError:
                    # todo: warn about no type found
                    pass
            reqd = 0
            if (name in self.options['requiredattrs'] or
                attr.get("use", "optional") == "required"):
                row.append("Yes")
                reqd = 1
            else:
                row.append("No")
            default = attr.get("default")
            if default is None:
                row.append("None")
            else:
                row.append(nodes.literal(default, default))
            # we record name and required separately to make sorting
            # easier
            rows.append((name, reqd, build_table_row(*row)))
        rows.sort(key=operator.itemgetter(0))
        rows.sort(key=operator.itemgetter(1), reverse=True)
        if not self.options['onlyattrs'] or '*' in self.options['onlyattrs']:
            try:
                anyattr = el.xpath("xs:anyAttribute", namespaces=NSMAP)[0]
                rows.append((None, None,
                             build_table_row('*', self.get_doc(anyattr),
                                             "Any", "No", "None")))
            except IndexError:
                pass
        return [r[2] for r in rows]

    def get_values_from_type(self, entity=None, typeattr='type'):
        if entity is None:
            entity = self.entity
        ns_name, name = self.split_ns(entity.get(typeattr))
        ns_uri = self.get_namespace_uri(ns_name, entity=entity)
        if ns_uri == XS:
            return self.get_builtin_type(name)
        elif name in self.entities[ns_uri]['simpleType']:
            return self.get_values_from_simpletype(
                self.entities[ns_uri]['simpleType'][name])
        else:
            return "Any"

    def get_builtin_type(self, vtype):
        if vtype == "boolean":
            return get_value_list(["true", "false"])
        else:
            return get_datatype_ref(vtype, vtype,
                                    self.app.config.xmlschema_datatype_url)

    def get_doc(self, el):
        try:
            return self.parse(el.xpath("xs:annotation/xs:documentation",
                                       namespaces=NSMAP)[0].text)
        except IndexError:
            return build_paragraph('')

    def parse(self, text):
        node = nodes.paragraph()
        vl = ViewList()
        for line in text.splitlines():
            vl.append(line, '<xmlschema>')
        nested_parse_with_titles(self.state, vl, node)
        try:
            return node[0]
        except IndexError:
            return build_paragraph(text)

    def split_ns(self, name):
        try:
            (ns, name) = name.split(":")
        except ValueError:
            ns = self.ns_name
        return (ns, name)

    def get_values_from_simpletype(self, entity=None):
        if entity is None:
            entity = self.entity
        # todo: xs:union, xs:list
        try:
            restriction = entity.xpath("xs:restriction|xs:extension",
                                       namespaces=NSMAP)[0]
        except IndexError:
            return "Any"
        doc = self.get_doc(restriction)
        if len(doc) == 1 and len(doc[0]) == 0:
            # if get_doc returns a paragraph node with an empty Text
            # node
            enum = [e.get("value")
                    for e in restriction.xpath("xs:enumeration",
                                               namespaces=NSMAP)]
            if len(enum):
                return get_value_list(enum)
            else:
                return self.get_values_from_type(entity=restriction,
                                                 typeattr='base')
        else:
            return doc

    def add_dep(self, typ, name, entity):
        try:
            if name in self.options['noautodep']:
                return
        except TypeError:
            if self.options['noautodep']:
                return
        self.dependencies.append((typ, name, entity))

    def target_node(self, tag, ns, *extra):
        targetid = get_target_id(tag, ns, *extra)
        fqname = targetid[len(tag) + 1:]
        rv = nodes.target('', '', ids=[targetid])
        self.add_domain_data(tag2type(tag), fqname,
                             (self.env.docname, targetid))
        return rv

    def add_domain_data(self, typ, key, data):
        if key not in self.env.domaindata['xml'][typ]:
            self.env.domaindata['xml'][typ][key] = data

    def get_namespace_uri(self, ns_name, entity=None):
        if entity is None:
            entity = self.entity
        xs_ns = get_xs_ns(entity)
        if ns_name == xs_ns:
            return XS
        else:
            return self.namespaces[ns_name]


def tag2type(tag):
    if tag in ['complexType', 'simpleType']:
        return 'type'
    elif tag == 'attributeGroup':
        return 'attributegroup'
    return tag


def text(txt):
    return nodes.Text(txt, txt)


def append_node(parent, cls_or_node, *contents):
    parent.append(build_node(cls_or_node, *contents))


def build_node(cls_or_node, *contents):
    if isinstance(cls_or_node, (type, classobj)):
        rv = cls_or_node()
    else:
        rv = cls_or_node
    rv.extend(contents)
    return rv


def get_xref(typ, target, title=None):
    if title is None:
        title = target
    ref = addnodes.pending_xref(title,
                                reftype=typ,
                                refdomain="xml",
                                reftarget=target)
    ref.append(nodes.literal(title, title))
    return ref


def build_table_row(*vals):
    rv = nodes.row('')
    for val in vals:
        if isinstance(val, nodes.Node):
            node = val
        else:
            node = nodes.paragraph(val, val)
        rv.append(nodes.entry(node, node))
    return rv


def build_paragraph(*args):
    """ convenience method to build a paragraph node """
    rv = nodes.paragraph()
    for content in args:
        if isinstance(content, nodes.Node):
            rv.append(content)
        else:
            rv.append(text(content))
    return rv


def get_target_id(etype, ns_name, *extra):
    if ns_name:
        return ":".join([etype, ns_name] + list(extra))
    else:
        return ":".join([etype] + list(extra))


def get_value_list(vals):
    rv = nodes.paragraph()
    if vals:
        rv.append(nodes.literal(vals[0], vals[0]))
        for i in range(1, len(vals)):
            rv.append(text(" | "))
            rv.append(nodes.literal(vals[i], vals[i]))
    return rv


def get_xs_ns(el):
    return get_namespace_name(el, XS)


def get_namespace_name(el, ns_uri):
    for name, ns in el.nsmap.items():
        if ns == ns_uri:
            return name
    return None


def get_datatype_ref(title, target, baseurl):
    return build_node(nodes.reference('', '', refuri=baseurl % target),
                      nodes.literal(title, title))


class XMLDatatypeRole(object):
    def __init__(self, baseurl):
        self.baseurl = baseurl

    def __call__(self, name, rawtext, text, lineno, inliner, options={},
                 content=[]):
        has_explicit_title, title, target = split_explicit_title(text)
        return [get_datatype_ref(title, target, self.baseurl)], []


class XMLXRefRole(roles.XRefRole):
    def __init__(self, typ, **kwargs):
        roles.XRefRole.__init__(self, **kwargs)
        self.type = typ

    def process_link(self, env, refnode, has_explicit_title, title, target):
        if (self.type == 'attribute' and
            not has_explicit_title and
            ':' in title):
            title = title.split(':')[-1]
        return roles.XRefRole.process_link(self, env, refnode,
                                           has_explicit_title, title, target)


class XMLDomain(Domain):
    name = "xml"
    label = "XML"

    types = dict(schema=['schema'],
                 type=['complexType', 'simpleType'],
                 group=['group'],
                 attributegroup=['attributeGroup'],
                 element=['element'],
                 attribute=None)

    object_types = dict([(t, ObjType("XML %s" % t.title(), t))
                         for t in types.keys()])
    directives = dict([(t, XMLDirective(h))
                        for t, h in types.items() if h is not None])
    roles = dict([(t, XMLXRefRole(t)) for t in types.keys()])
    dangling_warnings = dict([(t, "unknown XML %s: %%(target)s" % t)
                              for t in types.keys()])
    initial_data = dict([(t, dict()) for t in types.keys()])
    data_version = 3

    def clear_doc(self, docname):
        to_del = []
        for dtype in self.types.keys():
            for key, (doc, _) in self.data[dtype].items():
                if doc == docname:
                    to_del.append((dtype, key))
        for dtype, key in to_del:
            del self.data[dtype][key]

    def resolve_xref(self, env, fromdocname, builder, typ, target, node,
                     contnode):
        if typ in ['complexType', 'simpleType']:
            typ = 'type'
        if target in self.data[typ]:
            docname, labelid = self.data[typ][target]
        else:
            return None
        return make_refnode(builder, fromdocname, docname,
                            labelid, contnode)

    def get_objects(self):
        for dtype in self.types.keys():
            for name, (docname, tgtid) in self.data[dtype].items():
                yield (name, name, dtype, docname, tgtid,
                       self.object_types[dtype].attrs['searchprio'])


def setup(app):
    app.add_config_value('xmlschema_path', '.', False)
    app.add_config_value('xmlschema_datatype_url',
                         'http://www.w3.org/TR/xmlschema-2/#%s', False)
    app.add_domain(XMLDomain)
    app.connect('builder-inited', load_xml_schemas)
    app.connect('builder-inited', add_xml_datatype_role)


def add_xml_datatype_role(app):
    app.add_role_to_domain('xml', 'datatype',
                           XMLDatatypeRole(app.config.xmlschema_datatype_url))


def load_xml_schemas(app):
    entities = dict()
    entities[None] = dict(schema=dict(),
                          group=dict(),
                          attributeGroup=dict(),
                          element=dict(),
                          simpleType=dict(),
                          complexType=dict())
    namespaces = dict()
    namespaces_by_uri = dict()
    schemapath = os.path.abspath(os.path.join(app.builder.env.srcdir,
                                              app.config.xmlschema_path))
    for root, _, files in os.walk(schemapath):
        for fname in files:
            if not fname.endswith(".xsd"):
                continue
            path = os.path.join(root, fname)
            relpath = path[len(schemapath):].strip("/")
            schema = lxml.etree.parse(path).getroot()

            ns = schema.get("targetNamespace")
            ns_name = get_namespace_name(schema, ns)
            if ns_name not in namespaces:
                namespaces[ns_name] = ns
            if ns not in namespaces_by_uri:
                namespaces_by_uri[ns] = ns_name

            if ns not in entities:
                entities[ns] = dict(schema=dict(),
                                    group=dict(),
                                    attributeGroup=dict(),
                                    element=dict(),
                                    simpleType=dict(),
                                    complexType=dict())
            # schemas don't require namespaces to be identified
            # uniquely, but we let the user identify them either with
            # or without the namespace
            entities[None]['schema'][relpath] = schema
            entities[ns]['schema'][relpath] = schema
            for entity in schema.xpath("//xs:*[@name]", namespaces=NSMAP):
                tag = entity.tag[len(XS_NS):]
                if tag in entities[ns]:
                    entities[ns][tag][entity.get("name")] = entity
    app.builder.env.xmlschema_namespaces = namespaces
    app.builder.env.xmlschema_namespaces_by_uri = namespaces_by_uri
    app.builder.env.xmlschema_entities = entities