1 |
# Copyright (c) 2003 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, string |
15 |
from types import UnicodeType |
16 |
|
17 |
# |
18 |
# one level of indention |
19 |
# |
20 |
TAB = " " |
21 |
|
22 |
def escape(data): |
23 |
"""Escape &, \", ', <, and > in a string of data. |
24 |
""" |
25 |
data = string.replace(data, "&", "&") |
26 |
data = string.replace(data, "<", "<") |
27 |
data = string.replace(data, ">", ">") |
28 |
data = string.replace(data, '"', """) |
29 |
data = string.replace(data, "'", "'") |
30 |
return data |
31 |
|
32 |
class XMLWriter: |
33 |
"""Abstract XMLWriter. |
34 |
|
35 |
Should be overridden to provide specific object saving functionality. |
36 |
""" |
37 |
|
38 |
def __init__(self): |
39 |
self.filename = None |
40 |
pass |
41 |
|
42 |
def write(self, file_or_filename): |
43 |
"""Write the session to a file. |
44 |
|
45 |
The argument may be either a file object or a filename. If it's |
46 |
a filename, the file will be opened for writing. Files of |
47 |
shapefiles will be stored as filenames relative to the directory |
48 |
the file is stored in (as given by os.path.dirname(filename)) if |
49 |
they have a common parent directory other than the root |
50 |
directory. |
51 |
|
52 |
If the argument is a file object (which is determined by the |
53 |
presence of a write method) all filenames will be absolute |
54 |
filenames. |
55 |
""" |
56 |
|
57 |
# keep track of how many levels of indentation to write |
58 |
self.indent_level = 0 |
59 |
# track whether an element is currently open. see open_element(). |
60 |
self.element_open = 0 |
61 |
|
62 |
if hasattr(file_or_filename, "write"): |
63 |
# it's a file object |
64 |
self.file = file_or_filename |
65 |
self.dir = "" |
66 |
else: |
67 |
self.filename = file_or_filename |
68 |
self.dir = os.path.dirname(self.filename) |
69 |
self.file = open(self.filename, 'w') |
70 |
|
71 |
def close(self): |
72 |
assert self.indent_level == 0 |
73 |
if self.filename is not None: |
74 |
self.file.close() |
75 |
|
76 |
def write_header(self, doctype, system): |
77 |
"""Write the XML header""" |
78 |
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
79 |
self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system)) |
80 |
|
81 |
def open_element(self, element, attrs = {}): |
82 |
|
83 |
# |
84 |
# we note when an element is opened so that if two open_element() |
85 |
# calls are made successively we can end the currently open |
86 |
# tag and will later write a proper close tag. otherwise, |
87 |
# if a close_element() call is made directly after an open_element() |
88 |
# call we will close the tag with a /> |
89 |
# |
90 |
if self.element_open == 1: |
91 |
self.file.write(">\n") |
92 |
|
93 |
self.element_open = 1 |
94 |
|
95 |
# Helper function to write an element open tag with attributes |
96 |
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
97 |
self.__write_attribs(attrs) |
98 |
|
99 |
self.indent_level += 1 |
100 |
|
101 |
def close_element(self, element): |
102 |
self.indent_level -= 1 |
103 |
assert self.indent_level >= 0 |
104 |
|
105 |
# see open_element() for an explanation |
106 |
if self.element_open == 1: |
107 |
self.element_open = 0 |
108 |
self.file.write("/>\n") |
109 |
else: |
110 |
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
111 |
|
112 |
def write_element(self, element, attrs = {}): |
113 |
"""write an element that won't need a closing tag""" |
114 |
self.open_element(element, attrs) |
115 |
self.close_element(element) |
116 |
|
117 |
def __write_attribs(self, attrs): |
118 |
for name, value in attrs.items(): |
119 |
self.file.write(' %s="%s"' % (self.encode(name), |
120 |
self.encode(value))) |
121 |
|
122 |
def encode(self, str): |
123 |
"""Return an XML-escaped and UTF-8 encoded copy of the string str.""" |
124 |
|
125 |
esc = escape(str) |
126 |
|
127 |
if isinstance(esc, UnicodeType): |
128 |
return esc.encode("utf8") |
129 |
else: |
130 |
return unicode(escape(str),'latin1').encode("utf8") |