comparison orpg/orpg_xml.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children 551cd440acce
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 # Copyright (C) 2000-2001 The OpenRPG Project
2 #
3 # openrpg-dev@lists.sourceforge.net
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 # --
19 #
20 # File: orpg_xml.py
21 # Author: Chris Davis
22 # Maintainer:
23 # Version:
24 # $Id: orpg_xml.py,v 1.12 2007/07/19 20:33:10 digitalxero Exp $
25 #
26 # Description: xml utilies
27 #
28
29 from orpg import minidom
30 import string
31
32 def toxml(root,pretty=0):
33 return root.toxml(pretty)
34
35 def parseXml(s):
36 "parse and return doc"
37 try:
38 doc = minidom.parseString(s)
39 doc.normalize()
40 return doc
41 except Exception, e:
42 print e
43 return None
44
45 def safe_get_text_node(xml_dom):
46 """ returns the child text node or creates one if doesnt exist """
47 t_node = xml_dom._get_firstChild()
48 if t_node == None:
49 t_node = minidom.Text("")
50 t_node = xml_dom.appendChild(t_node)
51 return t_node
52
53 def strip_unicode(txt):
54 for i in xrange(len(txt)):
55 if txt[i] not in string.printable:
56 try:
57 txt = txt.replace(txt[i], '&#' + str(ord(txt[i])) + ';')
58 except:
59 txt = txt.replace(txt[i], '{?}')
60 return txt
61
62 def strip_text(txt):
63 # The following block strips out 8-bit characters
64 u_txt = ""
65 bad_txt_found = 0
66 txt = strip_unicode(txt)
67 for c in txt:
68 if ord(c) < 128:
69 u_txt += c
70 else:
71 bad_txt_found = 1
72 if bad_txt_found:
73 print "Some non 7-bit ASCII characters found and stripped"
74 return u_txt