ODFPY 1.2.0
 
Loading...
Searching...
No Matches
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
27from defusedxml.sax import make_parser
28from xml.sax import handler
29from xml.sax.xmlreader import InputSource
30import xml.sax.saxutils
31from odf.element import Element
32from odf.namespaces import OFFICENS
33try:
34 from cStringIO import StringIO
35except ImportError:
36 from io import StringIO
37
38#
39# Parse the XML files
40#
41
42class 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.datadata = []
52 self.level = 0
53 self.parseparse = False
54
55 def characters(self, data):
56 if self.parseparse == False:
57 return
58 self.datadata.append(data)
59
60 def startElementNS(self, tag, qname, attrs):
61 if tag in self.triggers:
62 self.parseparse = True
63 if self.doc._parsing != "styles.xml" and tag == (OFFICENS, 'font-face-decls'):
64 self.parseparse = False
65 if self.parseparse == False:
66 return
67
68 self.level = self.level + 1
69 # Add any accumulated text content
70 content = ''.join(self.datadata)
71 if content:
72 self.parent.addText(content, check_grammar=False)
73 self.datadata = []
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.parseparse == False:
107 return
108 self.level = self.level - 1
109 str = ''.join(self.datadata)
110 if str:
111 self.curr.addText(str, check_grammar=False)
112 self.datadata = []
113 self.curr = self.curr.parentNode
114 self.parent = self.curr
115 if tag in self.triggers:
116 self.parseparse = False
Creates a arbitrary element and is intended to be subclassed not used on its own.
Definition element.py:361
Extract headings from content.xml of an ODT file.
Definition load.py:42
startElementNS(self, tag, qname, attrs)
Definition load.py:60
__init__(self, document)
Definition load.py:49
endElementNS(self, tag, qname)
Definition load.py:105
characters(self, data)
Definition load.py:55