root/livinglogic.python.xist/src/ll/xist/scripts/dtd2xsc.py @ 2618:55bea42bc1b2

Revision 2618:55bea42bc1b2, 2.4 KB (checked in by Walter Doerwald <walter@…>, 13 years ago)

Start implementation of XIST 3.0:

Namespace classes are gone now. Instead of them, an element class must define
an xmlns class attribute with the namespace name (it's no longer possible
to assign an element to a namespace after the class has been created).
Processing instructions, entities and charrefs no longer have any connection
to namespaces whatsoever.

For parsing Registry objects are used now, they store the element classes
keyed by Python/XML name and namespace name. There's a defaultregistry where
all classes will be registered (this can be changed by using an alternative
registry in a with block). xsc.NSPool, xsc.Prefixes and the global registry
of namespaces no longer exists (but there's now a global registry of
element classes, pi classes, entity classes, charref classes and attribute
class).

The goal is to be able to use elements without having to define classes for
them. For this the registry would create an Element object directly and set
xmlns on the instance (instead of the element class). The publisher then would
have to use the xmlns on the instance too. xfind has to be extended to support
those classes.

Namespace handling in publishing has finally been fixed. XIST will no longer
use the empty prefix for global attributes or one prefix for two namespace.
Handling the XML namespace (i.e. for xml:lang and xml:space) has been
hardcoded, there's no longer an overwritable xmlprefix() method.

In many spots (e.g. Attrs.iterkeys() etc.) the order of namespace name and
element name has been reversed.

Handling the name of nested classes has been simplyfied. There's no longer and
outerclass attribute (which resulted in cycles), but the fullname will
be set directly.

Virtual dispatching for elements will be implemented later.

Fixed a bug in parser.parseURL() (finalurl must be called!)

A bunch of deprecated methods (all related to attributes) have been removed from
Element.

ll.xist.ns.detox now only has one end pi (if it's used without content the
last block will be closed, if a content is specified (i.e. <?end loop?> it
will check that the innermost block is of the correct type).

The code requires Python 2.5 now (because of with blocks and if-else expressions)

Updated copyright year.

Line 
1#! /usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4## Copyright 1999-2007 by LivingLogic AG, Bayreuth/Germany.
5## Copyright 1999-2007 by Walter Dörwald
6##
7## All Rights Reserved
8##
9## See xist/__init__.py for the license
10
11"""
12<par>Module that helps to create &xist; namespace modules from &dtd;s.
13Needs <app>xmlproc</app> from the <app>PyXML</app> package.
14For usage information type:</par>
15<prog>
16dtd2xsc --help
17</prog>
18"""
19
20
21__version__ = "$Revision$"[11:-2]
22# $Source$
23
24
25import sys, os.path, optparse
26
27from xml.parsers.xmlproc import dtdparser
28
29from ll import url
30from ll.xist import xsc, parsers, xnd
31
32
33def dtd2xsc(dtdurl, outurl, verbose, xmlname, xmlurl, shareattrs, model, defaults):
34    if verbose:
35        print "Parsing DTD %s ..." % dtdurl
36    d = dtdparser.load_dtd(dtdurl.url)
37
38    if verbose:
39        print "Converting ..."
40    data = xnd.fromdtd(d, xmlname, xmlurl)
41
42    if shareattrs=="dupes":
43        data.shareattrs(False)
44    elif shareattrs=="all":
45        data.shareattrs(True)
46
47    if verbose:
48        print "Writing to %s ..." % outurl
49    file = outurl.openwrite()
50    file.write(data.aspy(model=model, defaults=defaults))
51    file.close()
52
53
54def main():
55    p = optparse.OptionParser(usage="usage: %prog [options] inputurl.dtd")
56    p.add_option("-o", "--output", dest="output", metavar="FILE", help="write output to FILE")
57    p.add_option("-v", "--verbose", action="store_true", dest="verbose")
58    p.add_option("-p", "--prefix", dest="xmlname", help="the XML prefix for this namespace", default="prefix", metavar="PREFIX")
59    p.add_option("-u", "--url", dest="xmlurl", help="the XML namespace name", metavar="URL")
60    p.add_option("-a", "--shareattrs", dest="shareattrs", help="Should identical attributes be shared among elements?", choices=("none", "dupes", "all"), default="dupes")
61    p.add_option("-m", "--model", dest="model", default="once", help="Add sims information to the namespace", choices=("no", "all", "once"))
62    p.add_option("-d", "--defaults", action="store_true", dest="defaults", help="Output default values for attributes")
63
64    (options, args) = p.parse_args()
65    if len(args) != 1:
66        p.error("incorrect number of arguments")
67        return 1
68    input = url.URL(args[0])
69    if options.output is None:
70        output = url.File(input.withExt("py").file)
71    else:
72        output = url.URL(options.output)
73    dtd2xsc(input, output, options.verbose, options.xmlname, options.xmlurl, options.shareattrs, options.model, options.defaults)
74
75
76if __name__ == "__main__":
77    sys.exit(main())
Note: See TracBrowser for help on using the browser.