From d492c2fecd9736efa4ff93123fb78218ed54446a Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Sat, 17 Aug 2013 08:37:36 +0100 Subject: [PATCH 1/3] fixes #1213254 by filtering out invalid xml chars --- openlp/plugins/songs/lib/xml.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 1cd349f13..22539b495 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -92,6 +92,27 @@ class SongXML(object): self.song_xml = objectify.fromstring(u'') self.lyrics = etree.SubElement(self.song_xml, u'lyrics') + @staticmethod + def valid_xml_char_ordinal(char): + """ + Control Characters we need to filter from the xml. + Source + """ + return ( + 0x20 <= char <= 0xD7FF + or char in (0x9, 0xA, 0xD) + or 0xE000 <= char <= 0xFFFD + or 0x10000 <= char <= 0x10FFFF + ) + + @staticmethod + def clean_xml_string(xml): + """ + Filter out invalid characters in xml + Source + """ + return ''.join(char for char in xml if SongXML.valid_xml_char_ordinal(ord(char))) + def add_verse_to_lyrics(self, type, number, content, lang=None): """ Add a verse to the ```` tag. From 9f7e32cd31d68485db021f711005245c43f0dd8c Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Sat, 17 Aug 2013 09:10:25 +0100 Subject: [PATCH 2/3] fixes #1213254 by filtering out invalid xml chars --- openlp/plugins/songs/lib/xml.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 22539b495..f4702f207 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -133,6 +133,7 @@ class SongXML(object): The verse's language code (ISO-639). This is not required, but should be added if available. """ + content = self.clean_xml_string(content) verse = etree.Element(u'verse', type=unicode(type), label=unicode(number)) if lang: From 5e6d30f7504d0beab7c0c73c81e412eb73dcc893 Mon Sep 17 00:00:00 2001 From: Philip Ridout Date: Mon, 19 Aug 2013 20:17:09 +0000 Subject: [PATCH 3/3] merged two methods in to one and made them module level --- openlp/plugins/songs/lib/xml.py | 34 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index f4702f207..fc92a9cd9 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -78,6 +78,17 @@ log = logging.getLogger(__name__) NAMESPACE = u'http://openlyrics.info/namespace/2009/song' NSMAP = '{' + NAMESPACE + '}' + '%s' +def clean_xml_string(xml): + """ + Filter out invalid characters in xml + Source + """ + return ''.join(char for char in xml if + 0x20 <= char <= 0xD7FF + or char in (0x9, 0xA, 0xD) + or 0xE000 <= char <= 0xFFFD + or 0x10000 <= char <= 0x10FFFF) + class SongXML(object): """ @@ -92,27 +103,6 @@ class SongXML(object): self.song_xml = objectify.fromstring(u'') self.lyrics = etree.SubElement(self.song_xml, u'lyrics') - @staticmethod - def valid_xml_char_ordinal(char): - """ - Control Characters we need to filter from the xml. - Source - """ - return ( - 0x20 <= char <= 0xD7FF - or char in (0x9, 0xA, 0xD) - or 0xE000 <= char <= 0xFFFD - or 0x10000 <= char <= 0x10FFFF - ) - - @staticmethod - def clean_xml_string(xml): - """ - Filter out invalid characters in xml - Source - """ - return ''.join(char for char in xml if SongXML.valid_xml_char_ordinal(ord(char))) - def add_verse_to_lyrics(self, type, number, content, lang=None): """ Add a verse to the ```` tag. @@ -133,7 +123,7 @@ class SongXML(object): The verse's language code (ISO-639). This is not required, but should be added if available. """ - content = self.clean_xml_string(content) + content = clean_xml_string(content) verse = etree.Element(u'verse', type=unicode(type), label=unicode(number)) if lang: