summaryrefslogtreecommitdiff
path: root/toolkit/src2xml/source/globals.py
blob: 34d8269402dbbea991569c9c7af4494667193a99 (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
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#

import sys

class ParseError (Exception): 
    pass

def error (msg, exit=0):
    sys.stderr.write (msg)
    if exit:
        sys.exit (exit)

def progress (msg):
    sys.stderr.write(msg)


def removeQuote (text):
    """Remove quotes from a literal.
"""
    if len(text) >= 2 and text[0] == text[len(text)-1] == '"':
        text = text[1:-1]
    return text


class Macro(object):
    def __init__ (self, name):
        self.name = name
        self.tokens = []
        self.vars = {}


class Node(object):
    def __init__ (self):
        self.children = []

    def appendChild (self, childnode):
        self.children.append(childnode)
        childnode.parent = self

    def getChildren (self):
        return self.children


class RootNode(Node):
    def __init__ (self):
        Node.__init__(self)

    def dump (self):
        chars = ''
        for child in self.getChildren():
            chars += child.dump()
        return chars


class Element(Node):

    INDENT = "    "

    def __init__ (self, name, rid = None):
        Node.__init__(self)
        self.name = name
        self.parent = None
#        print "name: " + self.name - stats ...

        # The following attributes are copied when 'clone'ed.
        self.rid = rid
        self.attrs = {}

    def dump (self, level = 0):
        chars = ''
        chars += "\n" + Element.INDENT*level
        chars += '<%s'%self.name

        if self.rid != None:
            self.setAttr("rid", self.rid)

        chars += self.__dumpAttrs()

        if len(self.children) == 0:
            chars += '/>'
        else:
            chars += '>'
            for child in self.getChildren():
                chars += child.dump(level+1)
    
            chars += "\n"+Element.INDENT*level
            chars += "</%s>"%self.name

        return chars

    def hasAttr (self, name):
        return self.attrs.has_key(name)

    def getAttr (self, name):
        return self.attrs[name]

    def setAttr (self, name, value):
        if type(value) == type(0):
            value = "%d"%value
        self.attrs[name] = removeQuote(value)
#        print "attr: " + self.name + "." + name - stats ...
        return

    def clone (self, elem):
        keys = elem.attrs.keys()
        for key in keys:
            self.attrs[key] = elem.attrs[key]
        self.rid = elem.rid

    def __dumpAttrs (self):
        text = ''
        keys = self.attrs.keys()
        keys.sort()
        for key in keys:
            value = self.attrs[key]
            text += ' %s="%s"'%(key, value)
        return text