root/livinglogic.python.xist/src/ll/xist/ns/jsp.py @ 3180:c414962d9e75

Revision 3180:c414962d9e75, 4.1 KB (checked in by Walter Doerwald <walter@…>, 12 years ago)

Convert all docstrings to ReST.

Line 
1# -*- coding: utf-8 -*-
2
3## Copyright 1999-2008 by LivingLogic AG, Bayreuth/Germany
4## Copyright 1999-2008 by Walter Dörwald
5##
6## All Rights Reserved
7##
8## See xist/__init__.py for the license
9
10
11"""
12A module that allows you to embed JSP content as processing instructions.
13"""
14
15
16import cgi # for parse_header
17
18from ll.xist import xsc, sims
19
20
21__docformat__ = "reStructuredText"
22
23
24xmlns = "http://java.sun.com/JSP/Page"
25
26
27class directive(xsc.Element):
28    xmlns = xmlns
29    model = sims.Empty()
30    register = False # only serves as a base class
31
32    def publish(self, publisher):
33        yield publisher.encode(u"<%@ ")
34        yield publisher.encode(self._publishname(publisher))
35        for part in self.attrs.publish(publisher):
36            yield part
37        yield publisher.encode(u"%>")
38
39
40class scriptlet(xsc.ProcInst):
41    """
42    Will be published as ``<% content %>``.
43    """
44
45    def publish(self, publisher):
46        yield publisher.encode(u"<% ")
47        yield publisher.encode(self.content)
48        yield publisher.encode(u" %>")
49
50
51class expression(xsc.ProcInst):
52    """
53    Will be published as ``<%= content %>``.
54    """
55
56    def publish(self, publisher):
57        yield publisher.encode(u"<%= ")
58        yield publisher.encode(self.content)
59        yield publisher.encode(u" %>")
60
61
62class declaration(xsc.ProcInst):
63    """
64    Will be published as ``<%! content %>``.
65    """
66
67    def publish(self, publisher):
68        yield publisher.encode(u"<%! ")
69        yield publisher.encode(self.content)
70        yield publisher.encode(u" %>")
71
72
73class If(scriptlet):
74    xmlns = xmlns
75    xmlname = "if"
76
77    def convert(self, converter):
78        return scriptlet(u"if(", self.content, u"){")
79
80
81class Else(scriptlet):
82    xmlns = xmlns
83    xmlname = "else"
84
85    def convert(self, converter):
86        return scriptlet(u"}else{")
87
88
89class ElIf(scriptlet):
90    xmlns = xmlns
91    xmlname = "elif"
92
93    def convert(self, converter):
94        return scriptlet(u"}else if (", self.content, u"){")
95
96
97class End(scriptlet):
98    xmlns = xmlns
99    xmlname = "end"
100
101    def convert(self, converter):
102        return scriptlet(u"}")
103
104
105class block(xsc.Element):
106    """
107    This element embeds its content in ``{}`` brackets. Note that the content
108    itself will not be turned into a scriptlet automatically but will be used
109    as-is.
110    """
111    xmlns = xmlns
112    model = sims.Any()
113
114    def convert(self, converter):
115        e = xsc.Frag(
116            scriptlet(u"{"),
117            self.content,
118            scriptlet(u"}")
119        )
120        return e.convert(converter)
121
122
123class directive_include(directive):
124    xmlns = xmlns
125    xmlname = "include"
126    class Attrs(directive.Attrs):
127        class file(xsc.TextAttr): pass
128
129
130class directive_taglib(directive):
131    xmlns = xmlns
132    xmlname = "taglib"
133    class Attrs(directive.Attrs):
134        class uri(xsc.TextAttr): pass
135        class prefix(xsc.TextAttr): pass
136
137
138class directive_page(directive):
139    xmlns = xmlns
140    xmlname = "page"
141    class Attrs(directive.Attrs):
142        class language(xsc.TextAttr):
143            values = ("java",)
144        class extends(xsc.TextAttr): pass
145        class import_(xsc.TextAttr): xmlname = "import"
146        class session(xsc.TextAttr): values = (u"true", u"false")
147        class buffer(xsc.TextAttr): pass
148        class autoFlush(xsc.TextAttr): values = (u"true", u"false")
149        class isThreadSafe(xsc.TextAttr): values = (u"true", u"false")
150        class info(xsc.TextAttr): pass
151        class errorPage(xsc.URLAttr): pass
152        class contentType(xsc.TextAttr): pass
153        class isErrorPage(xsc.TextAttr): values = (u"true", u"false")
154        class pageEncoding(xsc.TextAttr): pass
155
156    def publish(self, publisher):
157        # Only a contentType attribute triggers the special code
158        if u"contentType" in self.attrs and not self.attrs.contentType.isfancy() and not self.attrs.pageEncoding.isfancy():
159            (contenttype, options) = cgi.parse_header(unicode(self.attrs.contentType))
160            pageencoding = unicode(self.attrs.pageEncoding)
161            encoding = publisher.encoding
162            if encoding is None:
163                encoding = "utf-8"
164            if u"charset" not in options or not (options[u"charset"] == pageencoding == encoding):
165                options[u"charset"] = encoding
166                node = self.__class__(
167                    self.attrs,
168                    contentType=(contenttype, u"; ", u"; ".join("%s=%s" % option for option in options.items())),
169                    pageEncoding=encoding
170                )
171                return node.publish(publisher) # return a generator-iterator
172        return super(directive_page, self).publish(publisher) # return a generator-iterator
Note: See TracBrowser for help on using the browser.