1 /*
2  * This file is part of Office 2007 Filters for Calligra
3  *
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Suresh Chande suresh.chande@nokia.com
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include "DocxXmlFooterReader.h"
25 
26 #include "DocxDebug.h"
27 
28 #include <MsooXmlSchemas.h>
29 #include <MsooXmlUtils.h>
30 
31 #include <KoXmlWriter.h>
32 #include <limits.h>
33 
34 #define MSOOXML_CURRENT_NS "w"
35 #define MSOOXML_CURRENT_CLASS MsooXmlFooterReader
36 #define BIND_READ_CLASS MSOOXML_CURRENT_CLASS
37 
38 #include <MsooXmlReader_p.h>
39 
40 
41 class DocxXmlFooterReader::Private
42 {
43 public:
Private()44     Private() : counter(0) {
45     }
~Private()46     ~Private() {
47     }
48     QString pathAndFile;
49     int counter;
50 };
51 
DocxXmlFooterReader(KoOdfWriters * writers)52 DocxXmlFooterReader::DocxXmlFooterReader(KoOdfWriters *writers)
53     : DocxXmlDocumentReader(writers)
54     , d(new Private)
55 {
56     init();
57     DocxXmlDocumentReader::m_moveToStylesXml = true;
58 }
59 
~DocxXmlFooterReader()60 DocxXmlFooterReader::~DocxXmlFooterReader()
61 {
62     delete d;
63 }
64 
init()65 void DocxXmlFooterReader::init()
66 {
67     d->counter = 0;
68     m_footerActive = true;
69 }
70 
content()71 QString DocxXmlFooterReader::content()
72 {
73     return m_content;
74 }
75 
read(MSOOXML::MsooXmlReaderContext * context)76 KoFilter::ConversionStatus DocxXmlFooterReader::read(MSOOXML::MsooXmlReaderContext* context)
77 {
78     m_context = static_cast<DocxXmlDocumentReaderContext*>(context);
79 
80     debugDocx << "=============================";
81     readNext();
82     if (!isStartDocument()) {
83         return KoFilter::WrongFormat;
84     }
85     readNext();
86 
87     debugDocx << *this << namespaceUri();
88     if (!expectEl(QList<QByteArray>() << "w:ftr")) {
89         return KoFilter::WrongFormat;
90     }
91     if (!expectNS(MSOOXML::Schemas::wordprocessingml)) {
92         return KoFilter::WrongFormat;
93     }
94 
95     QXmlStreamNamespaceDeclarations namespaces(namespaceDeclarations());
96 
97     //! @todo find out whether the namespace returned by namespaceUri()
98     //!       is exactly the same ref as the element of namespaceDeclarations()
99     if (!namespaces.contains(QXmlStreamNamespaceDeclaration("w", MSOOXML::Schemas::wordprocessingml))) {
100         raiseError(i18n("Namespace \"%1\" not found", QLatin1String(MSOOXML::Schemas::wordprocessingml)));
101         return KoFilter::WrongFormat;
102     }
103 
104     const QString qn(qualifiedName().toString());
105 
106     RETURN_IF_ERROR(read_ftr())
107 
108     if (!expectElEnd(qn)) {
109         return KoFilter::WrongFormat;
110     }
111     debugDocx << "===========finished============";
112 
113     return KoFilter::OK;
114 }
115 
116 #undef CURRENT_EL
117 #define CURRENT_EL ftr
118 //! w:ftr handler (Footer)
119 /*!
120 
121  Parent elements:
122  - root element of Wordprocessing Footer part
123 
124  Child elements:
125  - altChunk (Anchor for Imported External Content)                               §17.17.2.1
126  - [done] bookmarkEnd (Bookmark End)                                             §17.13.6.1
127  - [done] bookmarkStart (Bookmark Start)                                         §17.13.6.2
128  - commentRangeEnd (Comment Anchor Range End)                                    §17.13.4.3
129  - commentRangeStart (Comment Anchor Range Start)                                §17.13.4.4
130  - customXml (Block-Level Custom XML Element)                                    §17.5.1.6
131  - customXmlDelRangeEnd (Custom XML Markup Deletion End)                         §17.13.5.4
132  - customXmlDelRangeStart (Custom XML Markup Deletion Start)                     §17.13.5.5
133  - customXmlInsRangeEnd (Custom XML Markup Insertion End)                        §17.13.5.6
134  - customXmlInsRangeStart (Custom XML Markup Insertion Start)                    §17.13.5.7
135  - customXmlMoveFromRangeEnd (Custom XML Markup Move Source End)                 §17.13.5.8
136  - customXmlMoveFromRangeStart (Custom XML Markup Move Source Start)             §17.13.5.9
137  - customXmlMoveToRangeEnd (Custom XML Markup Move Destination Location End)     §17.13.5.10
138  - customXmlMoveToRangeStart (Custom XML Markup Move Destination Location Start) §17.13.5.11
139  - [done] del (Deleted Run Content)                                              §17.13.5.14
140  - [done] ins (Inserted Run Content)                                             §17.13.5.18
141  - moveFrom (Move Source Run Content)                                            §17.13.5.22
142  - moveFromRangeEnd (Move Source Location Container - End)                       §17.13.5.23
143  - moveFromRangeStart (Move Source Location Container - Start)                   §17.13.5.24
144  - moveTo (Move Destination Run Content)                                         §17.13.5.25
145  - moveToRangeEnd (Move Destination Location Container - End)                    §17.13.5.27
146  - moveToRangeStart (Move Destination Location Container - Start)                §17.13.5.28
147  - [done] oMath (Office Math)                                                    §22.1.2.77
148  - oMathPara (Office Math Paragraph)                                             §22.1.2.78
149  - [done] p (Paragraph)                                                          §17.3.1.22
150  - permEnd (Range Permission End)                                                §17.13.7.1
151  - permStart (Range Permission Start)                                            §17.13.7.2
152  - proofErr (Proofing Error Anchor)                                              §17.13.8.1
153  - [done] sdt (Block-Level Structured Document Tag)                              §17.5.2.29
154  - [done] tbl (Table)                                                            §17.4.38
155 //! @todo: Handle all children
156 */
read_ftr()157 KoFilter::ConversionStatus DocxXmlFooterReader::read_ftr()
158 {
159     READ_PROLOGUE
160 
161     QBuffer buffer;
162     buffer.open(QIODevice::WriteOnly);
163     KoXmlWriter *oldBody = body;
164     body = new KoXmlWriter(&buffer);
165 
166     while (!atEnd()) {
167         readNext();
168         BREAK_IF_END_OF(CURRENT_EL)
169         if (isStartElement()) {
170             TRY_READ_IF(p)
171             ELSE_TRY_READ_IF(tbl)
172             ELSE_TRY_READ_IF(bookmarkStart)
173             ELSE_TRY_READ_IF(bookmarkEnd)
174             ELSE_TRY_READ_IF(del)
175             ELSE_TRY_READ_IF(ins)
176             ELSE_TRY_READ_IF(sdt)
177             ELSE_TRY_READ_IF_NS(m, oMath)
178             SKIP_UNKNOWN
179         }
180     }
181 
182     m_content = QString::fromUtf8(buffer.buffer(), buffer.buffer().size());
183 
184     delete body;
185     body = oldBody;
186 
187     READ_EPILOGUE
188 }
189