load.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007-2008 Søren Roug, European Environment Agency
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # This library 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 GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. #
  19. # Contributor(s):
  20. #
  21. # This script is to be embedded in opendocument.py later
  22. # The purpose is to read an ODT/ODP/ODS file and create the datastructure
  23. # in memory. The user should then be able to make operations and then save
  24. # the structure again.
  25. from defusedxml.sax import make_parser
  26. from xml.sax import handler
  27. from xml.sax.xmlreader import InputSource
  28. import xml.sax.saxutils
  29. from odf.element import Element
  30. from odf.namespaces import OFFICENS
  31. try:
  32. from cStringIO import StringIO
  33. except ImportError:
  34. from io import StringIO
  35. #
  36. # Parse the XML files
  37. #
  38. class LoadParser(handler.ContentHandler):
  39. """ Extract headings from content.xml of an ODT file """
  40. triggers = (
  41. (OFFICENS, 'automatic-styles'), (OFFICENS, 'body'),
  42. (OFFICENS, 'font-face-decls'), (OFFICENS, 'master-styles'),
  43. (OFFICENS, 'meta'), (OFFICENS, 'scripts'),
  44. (OFFICENS, 'settings'), (OFFICENS, 'styles') )
  45. def __init__(self, document):
  46. self.doc = document
  47. self.data = []
  48. self.level = 0
  49. self.parse = False
  50. def characters(self, data):
  51. if self.parse == False:
  52. return
  53. self.data.append(data)
  54. def startElementNS(self, tag, qname, attrs):
  55. if tag in self.triggers:
  56. self.parse = True
  57. if self.doc._parsing != "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
  58. self.parse = False
  59. if self.parse == False:
  60. return
  61. self.level = self.level + 1
  62. # Add any accumulated text content
  63. content = ''.join(self.data)
  64. if content:
  65. self.parent.addText(content, check_grammar=False)
  66. self.data = []
  67. # Create the element
  68. attrdict = {}
  69. for (att,value) in attrs.items():
  70. attrdict[att] = value
  71. try:
  72. e = Element(qname = tag, qattributes=attrdict, check_grammar=False)
  73. self.curr = e
  74. except AttributeError as v:
  75. print ("Error: %s" % v)
  76. if tag == (OFFICENS, 'automatic-styles'):
  77. e = self.doc.automaticstyles
  78. elif tag == (OFFICENS, 'body'):
  79. e = self.doc.body
  80. elif tag == (OFFICENS, 'master-styles'):
  81. e = self.doc.masterstyles
  82. elif tag == (OFFICENS, 'meta'):
  83. e = self.doc.meta
  84. elif tag == (OFFICENS,'scripts'):
  85. e = self.doc.scripts
  86. elif tag == (OFFICENS,'settings'):
  87. e = self.doc.settings
  88. elif tag == (OFFICENS,'styles'):
  89. e = self.doc.styles
  90. elif self.doc._parsing == "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
  91. e = self.doc.fontfacedecls
  92. elif hasattr(self,'parent'):
  93. self.parent.addElement(e, check_grammar=False)
  94. self.parent = e
  95. def endElementNS(self, tag, qname):
  96. if self.parse == False:
  97. return
  98. self.level = self.level - 1
  99. str = ''.join(self.data)
  100. if str:
  101. self.curr.addText(str, check_grammar=False)
  102. self.data = []
  103. self.curr = self.curr.parentNode
  104. self.parent = self.curr
  105. if tag in self.triggers:
  106. self.parse = False