File: RtfWriter.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (83 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (3)
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
########################################################################
#
# File Name:            RtfWriter.py
#
#

"""
A special, simple writer for capturing result-tree fragments
WWW: http://4suite.com/4XSLT        e-mail: support@4suite.com

Copyright (c) 1999-2001 Fourthought Inc., USA.   All Rights Reserved.
See  http://4suite.com/COPYRIGHT  for license and copyright information
"""

import string, os
import NullWriter
from xml.dom import EMPTY_NAMESPACE
from xml.xslt import XSL_NAMESPACE
from xml.xpath import Util
from xml.dom.ext import SplitQName
from xml.dom import XMLNS_NAMESPACE, Node

class RtfWriter(NullWriter.NullWriter):
    def __init__(self, outputParams, ownerDoc):
        self._ownerDoc = ownerDoc
        self._root = ownerDoc.createDocumentFragment()
        self._root.stringValue = ""
        self._nodeStack = [self._root]
        self._currElement = None
        self._outputParams = outputParams

    def getResult(self):
        return self._root

    def startElement(self, name, namespace=EMPTY_NAMESPACE, extraNss=None):
        extraNss = extraNss or {}
        prefix, localName = SplitQName(name)
        new_element = self._ownerDoc.createElementNS(namespace, name)
        self._nodeStack.append(new_element)
        for prefix in extraNss.keys():
            if prefix:
                new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:'+prefix,
                                           extraNss[prefix])
            else:
                new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns',
                                           extraNss[prefix])
        new_element.stringValue = ""
        return

    def endElement(self, name):
        new_element = self._nodeStack[-1]
        del self._nodeStack[-1]
        self._nodeStack[-1].appendChild(new_element)
        return

    def text(self, text, escapeOutput=1):
        new_text = self._ownerDoc.createTextNode(text)
        top_node = self._nodeStack[-1]
        top_node.appendChild(new_text)
        top_node.stringValue = top_node.stringValue + text
        return

    def attribute(self, name, value, namespace=EMPTY_NAMESPACE):
        prefix, localName = SplitQName(name)
        attr = self._ownerDoc.createAttributeNS(namespace, name)
        attr.value = value
        if self._nodeStack[-1].nodeType == Node.ELEMENT_NODE:
            self._nodeStack[-1].attributes[(namespace, localName)] = attr
        else:
            #Document-fragment parent
            self._nodeStack[-1].appendChild(attr)
        return

    def processingInstruction(self, target, data):
        pi = self._ownerDoc.createProcessingInstruction(target, data)
        self._nodeStack[-1].appendChild(pi)
        return

    def comment(self, data):
        comment = self._ownerDoc.createComment(data)
        self._nodeStack[-1].appendChild(comment)
        return