summaryrefslogtreecommitdiff
path: root/python/mllib/transforms.py
blob: 69d99125e36df23444c16a27ee32955cbc2359d9 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# 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
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

"""
Useful transforms for dom objects.
"""

import dom
from cStringIO import StringIO

class Visitor:

  def descend(self, node):
    for child in node.children:
      child.dispatch(self)

  def node(self, node):
    self.descend(node)

  def leaf(self, leaf):
    pass

class Identity:

  def descend(self, node):
    result = []
    for child in node.children:
      result.append(child.dispatch(self))
    return result

  def default(self, tag):
    result = dom.Tag(tag.name, *tag.attrs)
    result.extend(self.descend(tag))
    return result

  def tree(self, tree):
    result = dom.Tree()
    result.extend(self.descend(tree))
    return result

  def tag(self, tag):
    return self.default(tag)

  def leaf(self, leaf):
    return leaf.__class__(leaf.data)

class Sexp(Identity):

  def __init__(self):
    self.stack = []
    self.level = 0
    self.out = ""

  def open(self, s):
    self.out += "(%s" % s
    self.level += len(s) + 1
    self.stack.append(s)

  def line(self, s = ""):
    self.out = self.out.rstrip()
    self.out += "\n" + " "*self.level + s

  def close(self):
    s = self.stack.pop()
    self.level -= len(s) + 1
    self.out = self.out.rstrip()
    self.out += ")"

  def tree(self, tree):
    self.open("+ ")
    for child in tree.children:
      self.line(); child.dispatch(self)
    self.close()

  def tag(self, tag):
    self.open("Node(%s) " % tag.name)
    for child in tag.children:
      self.line(); child.dispatch(self)
    self.close()

  def leaf(self, leaf):
    self.line("%s(%s)" % (leaf.__class__.__name__, leaf.data))

class Output:

  def descend(self, node):
    out = StringIO()
    for child in node.children:
      out.write(child.dispatch(self))
    return out.getvalue()

  def default(self, tag):
    out = StringIO()
    out.write("<%s" % tag.name)
    for k, v in tag.attrs:
      out.write(' %s="%s"' % (k, v))
    out.write(">")
    out.write(self.descend(tag))
    if not tag.singleton:
      out.write("</%s>" % tag.name)
    return out.getvalue()

  def tree(self, tree):
    return self.descend(tree)

  def tag(self, tag):
    return self.default(tag)

  def data(self, leaf):
    return leaf.data

  def entity(self, leaf):
    return "&%s;" % leaf.data

  def character(self, leaf):
    raise Exception("TODO")

  def comment(self, leaf):
    return "<!-- %s -->" % leaf.data

class Empty(Output):

  def tag(self, tag):
    return self.descend(tag)

  def data(self, leaf):
    return ""

  def entity(self, leaf):
    return ""

  def character(self, leaf):
    return ""

  def comment(self, leaf):
    return ""

class Text(Empty):

  def data(self, leaf):
    return leaf.data

  def entity(self, leaf):
    return "&%s;" % leaf.data

  def character(self, leaf):
    # XXX: is this right?
    return "&#%s;" % leaf.data