/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/xmlwriter.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/Model/xmlwriter.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (show annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 4166 byte(s)
made a copy
1 # Copyright (c) 2003, 2005 by Intevation GmbH
2 # Authors:
3 # Jonathan Coles <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """
9 Functions to save an XML file
10 """
11
12 __version__ = "$Revision$"
13
14 import os
15 from types import UnicodeType
16
17 from Thuban import unicode_from_internal
18
19 #
20 # one level of indention
21 #
22 TAB = " "
23
24 def escape(data):
25 """Escape &, \", ', <, and > in a string of data.
26 """
27 data = data.replace("&", "&amp;")
28 data = data.replace("<", "&lt;")
29 data = data.replace(">", "&gt;")
30 data = data.replace('"', "&quot;")
31 data = data.replace("'", "&apos;")
32 return data
33
34 class XMLWriter:
35 """Abstract XMLWriter.
36
37 Should be overridden to provide specific object saving functionality.
38 """
39
40 def __init__(self):
41 self.filename = None
42 pass
43
44 def write(self, file_or_filename):
45 """Write the session to a file.
46
47 The argument may be either a file object or a filename. If it's
48 a filename, the file will be opened for writing. Files of
49 shapefiles will be stored as filenames relative to the directory
50 the file is stored in (as given by os.path.dirname(filename)) if
51 they have a common parent directory other than the root
52 directory.
53
54 If the argument is a file object (which is determined by the
55 presence of a write method) all filenames will be absolute
56 filenames.
57 """
58
59 # keep track of how many levels of indentation to write
60 self.indent_level = 0
61 # track whether an element is currently open. see open_element().
62 self.element_open = 0
63
64 if hasattr(file_or_filename, "write"):
65 # it's a file object
66 self.file = file_or_filename
67 self.dir = ""
68 else:
69 self.filename = file_or_filename
70 self.dir = os.path.dirname(self.filename)
71 self.file = open(self.filename, 'w')
72
73 def close(self):
74 assert self.indent_level == 0
75 if self.filename is not None:
76 self.file.close()
77
78 def write_header(self, doctype, system):
79 """Write the XML header"""
80 self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
81 self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system))
82
83 def open_element(self, element, attrs = {}):
84
85 #
86 # we note when an element is opened so that if two open_element()
87 # calls are made successively we can end the currently open
88 # tag and will later write a proper close tag. otherwise,
89 # if a close_element() call is made directly after an open_element()
90 # call we will close the tag with a />
91 #
92 if self.element_open == 1:
93 self.file.write(">\n")
94
95 self.element_open = 1
96
97 # Helper function to write an element open tag with attributes
98 self.file.write("%s<%s" % (TAB*self.indent_level, element))
99 self.__write_attribs(attrs)
100
101 self.indent_level += 1
102
103 def close_element(self, element):
104 self.indent_level -= 1
105 assert self.indent_level >= 0
106
107 # see open_element() for an explanation
108 if self.element_open == 1:
109 self.element_open = 0
110 self.file.write("/>\n")
111 else:
112 self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))
113
114 def write_element(self, element, attrs = {}):
115 """write an element that won't need a closing tag"""
116 self.open_element(element, attrs)
117 self.close_element(element)
118
119 def __write_attribs(self, attrs):
120 for name, value in attrs.items():
121 self.file.write(' %s="%s"' % (self.encode(name),
122 self.encode(value)))
123
124 def encode(self, s):
125 """Return an XML-escaped and UTF-8 encoded copy of the string s.
126
127 The parameter must be a string in Thuban's internal string
128 representation or a unicode object.
129 """
130 if not isinstance(s, unicode):
131 s = unicode_from_internal(s)
132 return escape(s).encode("utf8")

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26