Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / css / csscharsetrule.py @ 475

History | View | Annotate | Download (5.98 KB)

1
"""CSSCharsetRule implements DOM Level 2 CSS CSSCharsetRule."""
2
__all__ = ['CSSCharsetRule']
3
__docformat__ = 'restructuredtext'
4
__version__ = '$Id$'
5

    
6
import codecs
7
import cssrule
8
import cssutils
9
import xml.dom
10

    
11
class CSSCharsetRule(cssrule.CSSRule):
12
    """
13
    The CSSCharsetRule interface represents an @charset rule in a CSS style
14
    sheet. The value of the encoding attribute does not affect the encoding
15
    of text data in the DOM objects; this encoding is always UTF-16
16
    (also in Python?). After a stylesheet is loaded, the value of the
17
    encoding attribute is the value found in the @charset rule. If there
18
    was no @charset in the original document, then no CSSCharsetRule is
19
    created. The value of the encoding attribute may also be used as a hint
20
    for the encoding used on serialization of the style sheet.
21

22
    The value of the @charset rule (and therefore of the CSSCharsetRule)
23
    may not correspond to the encoding the document actually came in;
24
    character encoding information e.g. in an HTTP header, has priority
25
    (see CSS document representation) but this is not reflected in the
26
    CSSCharsetRule.
27
    
28
    This rule is not really needed anymore as setting 
29
    :attr:`CSSStyleSheet.encoding` is much easier.
30

31
    Format::
32

33
        charsetrule:
34
            CHARSET_SYM S* STRING S* ';'
35

36
    BUT: Only valid format is (single space, double quotes!)::
37
    
38
        @charset "ENCODING";
39
    """
40
    def __init__(self, encoding=None, parentRule=None, 
41
                 parentStyleSheet=None, readonly=False):
42
        """
43
        :param encoding:
44
            a valid character encoding
45
        :param readonly:
46
            defaults to False, not used yet
47
        """
48
        super(CSSCharsetRule, self).__init__(parentRule=parentRule, 
49
                                             parentStyleSheet=parentStyleSheet)
50
        self._atkeyword = '@charset'
51
        
52
        if encoding:
53
            self.encoding = encoding
54
        else:
55
            self._encoding = None
56

    
57
        self._readonly = readonly
58

    
59
    def __repr__(self):
60
        return u"cssutils.css.%s(encoding=%r)" % (
61
                self.__class__.__name__, 
62
                self.encoding)
63

    
64
    def __str__(self):
65
        return u"<cssutils.css.%s object encoding=%r at 0x%x>" % (
66
                self.__class__.__name__, 
67
                self.encoding, 
68
                id(self))
69

    
70
    def _getCssText(self):
71
        """The parsable textual representation."""
72
        return cssutils.ser.do_CSSCharsetRule(self)
73

    
74
    def _setCssText(self, cssText):
75
        """
76
        :param cssText:
77
            A parsable DOMString.
78
        :exceptions:
79
            - :exc:`~xml.dom.SyntaxErr`:
80
              Raised if the specified CSS string value has a syntax error and
81
              is unparsable.
82
            - :exc:`~xml.dom.InvalidModificationErr`:
83
              Raised if the specified CSS string value represents a different
84
              type of rule than the current one.
85
            - :exc:`~xml.dom.HierarchyRequestErr`:
86
              Raised if the rule cannot be inserted at this point in the
87
              style sheet.
88
            - :exc:`~xml.dom.NoModificationAllowedErr`:
89
              Raised if the rule is readonly.
90
        """
91
        super(CSSCharsetRule, self)._setCssText(cssText)
92

    
93
        wellformed = True
94
        tokenizer = self._tokenize2(cssText)
95
        
96
        if self._type(self._nexttoken(tokenizer)) != self._prods.CHARSET_SYM: 
97
            wellformed = False
98
            self._log.error(u'CSSCharsetRule must start with "@charset "',
99
                            error=xml.dom.InvalidModificationErr)
100
        
101
        encodingtoken = self._nexttoken(tokenizer)
102
        encodingtype = self._type(encodingtoken)
103
        encoding = self._stringtokenvalue(encodingtoken)
104
        if self._prods.STRING != encodingtype or not encoding:
105
            wellformed = False
106
            self._log.error(u'CSSCharsetRule: no encoding found; %r.' % 
107
                            self._valuestr(cssText))
108
            
109
        semicolon = self._tokenvalue(self._nexttoken(tokenizer))
110
        EOFtype = self._type(self._nexttoken(tokenizer))
111
        if u';' != semicolon or EOFtype not in ('EOF', None):
112
            wellformed = False
113
            self._log.error(u'CSSCharsetRule: Syntax Error: %r.' % 
114
                            self._valuestr(cssText))
115
        
116
        if wellformed:
117
            self.encoding = encoding
118
            
119
    cssText = property(fget=_getCssText, fset=_setCssText,
120
                       doc=u"(DOM) The parsable textual representation.")
121

    
122
    def _setEncoding(self, encoding):
123
        """
124
        :param encoding:
125
            a valid encoding to be used. Currently only valid Python encodings
126
            are allowed.
127
        :exceptions:
128
            - :exc:`~xml.dom.NoModificationAllowedErr`:
129
              Raised if this encoding rule is readonly.
130
            - :exc:`~xml.dom.SyntaxErr`:
131
              Raised if the specified encoding value has a syntax error and
132
              is unparsable.  
133
        """
134
        self._checkReadonly()
135
        tokenizer = self._tokenize2(encoding)
136
        encodingtoken = self._nexttoken(tokenizer)
137
        unexpected = self._nexttoken(tokenizer)
138

    
139
        if not encodingtoken or unexpected or\
140
           self._prods.IDENT != self._type(encodingtoken):
141
            self._log.error(u'CSSCharsetRule: Syntax Error in encoding value '
142
                            u'%r.' % encoding)
143
        else:
144
            try:
145
                codecs.lookup(encoding)
146
            except LookupError:
147
                self._log.error(u'CSSCharsetRule: Unknown (Python) encoding %r.'
148
                                % encoding)
149
            else:
150
                self._encoding = encoding.lower()
151

    
152
    encoding = property(lambda self: self._encoding, _setEncoding,
153
        doc=u"(DOM)The encoding information used in this @charset rule.")
154

    
155
    type = property(lambda self: self.CHARSET_RULE, 
156
                    doc=u"The type of this rule, as defined by a CSSRule "
157
                        u"type constant.")
158

    
159
    wellformed = property(lambda self: bool(self.encoding))