testsubobjects.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007-2010 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import unittest, os, zipfile, re
  21. from odf.opendocument import OpenDocumentText
  22. from odf import draw, text
  23. from odf.element import IllegalChild
  24. from elementparser import ElementParser
  25. def _getxmlpart(odffile, xmlfile):
  26. """ Get the content out of the ODT file"""
  27. z = zipfile.ZipFile(odffile)
  28. content = z.read(xmlfile)
  29. z.close()
  30. return content
  31. def element_has_attributes(s, tag, attribs):
  32. """
  33. checks that at least one of elements in an XML string with a given tag
  34. has the requested attributes, independently of their order
  35. @param s: an XML string
  36. @param tag a tag
  37. @param attributes an attribute string; attributes are separated by spaces
  38. @return True if attributes are there, independently of their order
  39. """
  40. pattern=re.compile(r'<'+tag+' [^>]*/?>')
  41. found=pattern.findall(s)
  42. if not found:
  43. return False
  44. # removes parts of the string which are not attributes
  45. found1=map(lambda s: s.replace('<'+tag+' ', '').replace('/>','').replace('>',''), found)
  46. attribPattern=re.compile(r'[-a-z0-9:]*="[^"]*"')
  47. foundAttribMap = map(lambda s: attribPattern.findall(s), found1)
  48. attribs=attribPattern.findall(attribs)
  49. for foundAttribs in foundAttribMap:
  50. if set(foundAttribs) == set(attribs):
  51. return True
  52. return False
  53. class TestUnicode(unittest.TestCase):
  54. def setUp(self):
  55. self.textdoc = OpenDocumentText()
  56. self.saved = False
  57. def tearDown(self):
  58. if self.saved:
  59. os.unlink("TEST.odt")
  60. def test_subobject(self):
  61. df = draw.Frame(width="476pt", height="404pt", anchortype="paragraph")
  62. self.textdoc.text.addElement(df)
  63. subdoc = OpenDocumentText()
  64. # Here we add the subdocument to the main document. We get back a reference
  65. # to use in the href.
  66. subloc = self.textdoc.addObject(subdoc)
  67. self.assertEqual(subloc,'./Object 1')
  68. do = draw.Object(href=subloc)
  69. df.addElement(do)
  70. subsubdoc = OpenDocumentText()
  71. subsubloc = subdoc.addObject(subsubdoc)
  72. self.assertEqual(subsubloc,'./Object 1/Object 1')
  73. c = self.textdoc.contentxml() # contentxml() is supposed to yeld a bytes
  74. c.index(b'<office:body><office:text><draw:frame ')
  75. e = ElementParser(c.decode("utf-8"), u'draw:frame')
  76. # e = ElementParser('<draw:frame svg:width="476pt" text:anchor-type="paragraph" svg:height="404pt">')
  77. self.assertTrue(e.has_value(u'svg:width',"476pt"))
  78. self.assertTrue(e.has_value(u'svg:height',"404pt"))
  79. self.assertTrue(e.has_value(u'text:anchor-type',"paragraph"))
  80. self.assertFalse(e.has_value(u'svg:height',"476pt"))
  81. c.index(b'<draw:object xlink:href="./Object 1"/></draw:frame></office:text></office:body>')
  82. c.index(b'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
  83. self.textdoc.save(u"TEST.odt")
  84. self.saved = True
  85. m = _getxmlpart(u"TEST.odt", u"META-INF/manifest.xml").decode('utf-8')
  86. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"'))
  87. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/"'))
  88. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="content.xml"'))
  89. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="meta.xml"'))
  90. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/"'))
  91. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/styles.xml"'))
  92. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/content.xml"'))
  93. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="Object 1/Object 1/"'))
  94. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/styles.xml"'))
  95. assert(element_has_attributes(m, u'manifest:file-entry', u'manifest:media-type="text/xml" manifest:full-path="Object 1/Object 1/content.xml"'))
  96. if __name__ == '__main__':
  97. unittest.main()