2 """
3 This class writes one element. Each tag has its own instance of this class. it contains a function to write the end tag
4 if the element has children, but this function is called from the tree writer. See XmlTreeWriter for the API and other
5 information. This class should only be initialized by XmlTreeWriter.
6 """
7
8
9
10
11 - def __init__ (self, name, attribs, value, hasChildren, hasValue, tabs, output):
12
13 """
14 Initializes the tag writer
15
16 parameters:
17
18 - `name`: A string of the name of the tag
19 - `attribs`: A dictionary of the tag attributes
20 - `value`: A list of the element values or None if there are no values
21 - `hasChildren`: a boolean to indicate if the tag has children
22 - `hasValue`: a boolean to indicate if the tag any value
23 - `tabs`: an integer that indicates the number of tabs over the element is in the document
24 - `output`: the file object to write to
25
26 """
27
28 self.name = name
29
30 self.attribs = attribs
31
32 self.sortedKeyList = self.attribs.keys()
33
34 self.sortedKeyList.sort()
35
36 self.value = value
37
38 self.hasValue = hasValue
39
40 self.hasChildren = hasChildren
41
42 self.tabs = tabs
43
44 self.output = output
45
46 self.writeTag()
47
48
49
51 """
52 Writes the tag. Called from the init function. All its non-necessary formatting is standard
53 and is not dependant apon specifics of the format of the data.
54
55 No parameters
56 """
57
58 self.writeTabs()
59
60 if self.name == '_comment_':
61 self.writeComment()
62 return
63
64 self.output.write('<%s' % self.name)
65
66 openingTagLen = 1 + len(self.name)
67
68 longestNameLen = 0
69
70 for attrKey in self.sortedKeyList:
71
72 nameLen = len(attrKey)
73
74 if nameLen > longestNameLen:
75
76 longestNameLen = nameLen
77
78 longestNameLen+=1
79
80 for key in self.sortedKeyList:
81
82 value = self.attribs[key]
83
84 value = str(value)
85
86 self.output.write('\n')
87
88 self.writeTabs(7)
89
90 self.output.write(key)
91
92 nameLen = len(key)
93
94 spaces = longestNameLen - nameLen
95
96 self.writeTabs(spaces, 0)
97
98 self.output.write('= "%s"' % value)
99
100 if not self.hasChildren and not self.hasValue:
101
102 self.output.write('/>\n')
103
104 return None
105
106 self.output.write('>\n')
107
108 if self.hasValue:
109
110 if not isinstance(self.value, list):
111
112 self.value = list(self.value)
113
114 for line in self.value:
115
116 self.writeTabs(3)
117
118 self.output.write('%s\n' % line)
119
120 if not self.hasChildren:
121
122 self.writeEndTag()
123
124 return None
125
126
127
129 """
130 If `name` is set to '_comment_' this function is called. A comment can be in the tree only
131 if it is included in a transform or the writer is used by another program.
132
133 No parameters
134 """
135
136 self.output.write('<!--%s-->' % self.value)
137
138
140 """
141 Writes the ending tag for an element. Called by the tree writer if the element has children.
142 It is called only after the other children have been written.
143 An end tag appear as follows::
144 </TagName>
145
146 no parameters
147 """
148 self.writeTabs()
149 self.output.write('</%s>\n' % self.name)
150
151
152
153 - def writeTabs (self, tabSpec=None, tabs = None):
154
155 """
156 Writes out the tabs before an element. Can also write a certain number
157 of spaces after the tabs have been written, if `tabSpec` is specified.
158
159 parameters:
160
161 - `tabSpec`: An integer. By default, it is a NoneType. If specified, the program will make the specified number of spaces after the tab it writes.
162 - `tabs`: n integer that indicates the number of tabs over the element is in the document. By default, it is set to self.tabs, which is the `tabs`
163 value provided in the initialization.
164
165 """
166
167 if not tabs:
168
169 tabs = self.tabs
170
171 tab = ""
172
173 x = 0
174
175 while x < tabs:
176
177 tab = tab + ' '
178
179 x+=1
180
181 continue
182
183 if tabSpec:
184
185 y = 0
186
187 while y < tabSpec:
188
189 tab = tab + ' '
190
191 y+=1
192
193 continue
194
195 self.output.write(tab)
196