]> gitweb.michael.orlitzky.com - dead/census-tools.git/blobdiff - src/KML.py
Fixed a bug where the Document open/close tags were not output with to_kml().
[dead/census-tools.git] / src / KML.py
index 4b6d12f3fdcfcb47eace8548ea3566b1d7e7b999..3279c858f29d42c69f7e825529031f89b685f177 100644 (file)
@@ -3,6 +3,7 @@ Utility classes for working with the Keyhole Markup Language (KML).
 """
 
 import sys
+from xml.sax.saxutils import escape
 
 
 class KmlObject(object):
@@ -31,17 +32,17 @@ class KmlObject(object):
 
         
     def to_kml(self):
-        return self.render()
+        kml = self.OPEN_TAG
+        kml += self.render()
+        kml += self.CLOSE_TAG + "\n"
+        return kml
 
 
     def render(self):
-        kml = self.OPEN_TAG
-        kml += self.text
+        kml = escape(self.text)
 
         for c in self.children:
             kml += c.to_kml()
-
-        kml += self.CLOSE_TAG + "\n"
         
         return kml
 
@@ -53,8 +54,8 @@ class KmlObject(object):
 
         
     def render_to_stdout(self):
-        if not (len(self.text) == 0):
-            print self.text
+        if (len(self.text) > 0):
+            print escape(self.text)
         
         for c in self.children:
             c.print_kml()
@@ -74,7 +75,7 @@ class Document(KmlObject):
 <Document>"""
 
     CLOSE_TAG = """</Document>
-    </kml>"""
+</kml>"""
     
     def __init__(self, initial_text=''):
         super(Document, self).__init__(initial_text)
@@ -164,4 +165,10 @@ class StyleUrl(KmlObject):
     
         
 class RawText(KmlObject):
-    pass
+
+    def to_kml(self):
+        return self.text
+
+    def print_kml(self):
+        if (len(self.text) > 0):
+            print self.text