ODFPY  1.2.0
 All Classes Namespaces Files Functions Variables
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 xml.sax import make_parser,handler
28 from xml.sax.xmlreader import InputSource
29 import xml.sax.saxutils
30 from element import Element
31 from namespaces import OFFICENS
32 try:
33  from cStringIO import StringIO
34 except ImportError:
35  from io import StringIO
36 
37 #
38 # Parse the XML files
39 #
40 ##
41 # Extract headings from content.xml of an ODT file
42 class LoadParser(handler.ContentHandler):
43  triggers = (
44  (OFFICENS, 'automatic-styles'), (OFFICENS, 'body'),
45  (OFFICENS, 'font-face-decls'), (OFFICENS, 'master-styles'),
46  (OFFICENS, 'meta'), (OFFICENS, 'scripts'),
47  (OFFICENS, 'settings'), (OFFICENS, 'styles') )
48 
49  def __init__(self, document):
50  self.doc = document
51  self.data = []
52  self.level = 0
53  self.parse = False
54 
55  def characters(self, data):
56  if self.parse == False:
57  return
58  self.data.append(data)
59 
60  def startElementNS(self, tag, qname, attrs):
61  if tag in self.triggers:
62  self.parse = True
63  if self.doc._parsing != "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
64  self.parse = False
65  if self.parse == False:
66  return
67 
68  self.level = self.level + 1
69  # Add any accumulated text content
70  content = ''.join(self.data)
71  if len(content.strip()) > 0:
72  self.parent.addText(content, check_grammar=False)
73  self.data = []
74  # Create the element
75  attrdict = {}
76  for (att,value) in attrs.items():
77  attrdict[att] = value
78  try:
79  e = Element(qname = tag, qattributes=attrdict, check_grammar=False)
80  self.curr = e
81  except AttributeError as v:
82  print ("Error: %s" % v)
83 
84  if tag == (OFFICENS, 'automatic-styles'):
85  e = self.doc.automaticstyles
86  elif tag == (OFFICENS, 'body'):
87  e = self.doc.body
88  elif tag == (OFFICENS, 'master-styles'):
89  e = self.doc.masterstyles
90  elif tag == (OFFICENS, 'meta'):
91  e = self.doc.meta
92  elif tag == (OFFICENS,'scripts'):
93  e = self.doc.scripts
94  elif tag == (OFFICENS,'settings'):
95  e = self.doc.settings
96  elif tag == (OFFICENS,'styles'):
97  e = self.doc.styles
98  elif self.doc._parsing == "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
99  e = self.doc.fontfacedecls
100  elif hasattr(self,'parent'):
101  self.parent.addElement(e, check_grammar=False)
102  self.parent = e
103 
104 
105  def endElementNS(self, tag, qname):
106  if self.parse == False:
107  return
108  self.level = self.level - 1
109  str = ''.join(self.data)
110  if len(str.strip()) > 0:
111  self.curr.addText(str, check_grammar=False)
112  self.data = []
113  self.curr = self.curr.parentNode
114  self.parent = self.curr
115  if tag in self.triggers:
116  self.parse = False
tuple triggers
Definition: load.py:43
def startElementNS
Definition: load.py:60
Extract headings from content.xml of an ODT file.
Definition: load.py:42