ODFPY  1.2.0
load.py
Go to the documentation of this file.
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 
22 # This script is to be embedded in opendocument.py later
23 # The purpose is to read an ODT/ODP/ODS file and create the datastructure
24 # in memory. The user should then be able to make operations and then save
25 # the structure again.
26 
27 from defusedxml.sax import make_parser
28 from xml.sax import handler
29 from xml.sax.xmlreader import InputSource
30 import xml.sax.saxutils
31 from odf.element import Element
32 from odf.namespaces import OFFICENS
33 try:
34  from cStringIO import StringIO
35 except ImportError:
36  from io import StringIO
37 
38 #
39 # Parse the XML files
40 #
41 
43 class LoadParser(handler.ContentHandler):
44  triggers = (
45  (OFFICENS, 'automatic-styles'), (OFFICENS, 'body'),
46  (OFFICENS, 'font-face-decls'), (OFFICENS, 'master-styles'),
47  (OFFICENS, 'meta'), (OFFICENS, 'scripts'),
48  (OFFICENS, 'settings'), (OFFICENS, 'styles') )
49 
50  def __init__(self, document):
51  self.doc = document
52  self.data = []
53  self.level = 0
54  self.parse = False
55 
56  def characters(self, data):
57  if self.parse == False:
58  return
59  self.data.append(data)
60 
61  def startElementNS(self, tag, qname, attrs):
62  if tag in self.triggers:
63  self.parse = True
64  if self.doc._parsing != "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
65  self.parse = False
66  if self.parse == False:
67  return
68 
69  self.level = self.level + 1
70  # Add any accumulated text content
71  content = ''.join(self.data)
72  if content:
73  self.parent.addText(content, check_grammar=False)
74  self.data = []
75  # Create the element
76  attrdict = {}
77  for (att,value) in attrs.items():
78  attrdict[att] = value
79  try:
80  e = Element(qname = tag, qattributes=attrdict, check_grammar=False)
81  self.curr = e
82  except AttributeError as v:
83  print ("Error: %s" % v)
84 
85  if tag == (OFFICENS, 'automatic-styles'):
86  e = self.doc.automaticstyles
87  elif tag == (OFFICENS, 'body'):
88  e = self.doc.body
89  elif tag == (OFFICENS, 'master-styles'):
90  e = self.doc.masterstyles
91  elif tag == (OFFICENS, 'meta'):
92  e = self.doc.meta
93  elif tag == (OFFICENS,'scripts'):
94  e = self.doc.scripts
95  elif tag == (OFFICENS,'settings'):
96  e = self.doc.settings
97  elif tag == (OFFICENS,'styles'):
98  e = self.doc.styles
99  elif self.doc._parsing == "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
100  e = self.doc.fontfacedecls
101  elif hasattr(self,'parent'):
102  self.parent.addElement(e, check_grammar=False)
103  self.parent = e
104 
105 
106  def endElementNS(self, tag, qname):
107  if self.parse == False:
108  return
109  self.level = self.level - 1
110  str = ''.join(self.data)
111  if str:
112  self.curr.addText(str, check_grammar=False)
113  self.data = []
114  self.curr = self.curr.parentNode
115  self.parent = self.curr
116  if tag in self.triggers:
117  self.parse = False
odf.load.LoadParser.doc
doc
Definition: load.py:51
odf.load.LoadParser.parent
parent
Definition: load.py:103
odf.load.LoadParser.startElementNS
def startElementNS(self, tag, qname, attrs)
Definition: load.py:61
odf.load.LoadParser.characters
def characters(self, data)
Definition: load.py:56
odf.load.LoadParser.triggers
tuple triggers
Definition: load.py:44
odf.load.LoadParser
Extract headings from content.xml of an ODT file.
Definition: load.py:43
odf.load.LoadParser.__init__
def __init__(self, document)
Definition: load.py:50
odf.load.LoadParser.level
level
Definition: load.py:53
odf.element.Element
Creates a arbitrary element and is intended to be subclassed not used on its own.
Definition: element.py:357
odf.load.LoadParser.endElementNS
def endElementNS(self, tag, qname)
Definition: load.py:106
odf.load.LoadParser.parse
parse
Definition: load.py:54
odf.element
Definition: element.py:1
odf.load.LoadParser.data
data
Definition: load.py:52
odf.namespaces
Definition: namespaces.py:1
odf.load.LoadParser.curr
curr
Definition: load.py:81