1 /*
2  * This file is part of Office 2007 Filters for Calligra
3  *
4  * Copyright (C) 2009-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 #ifndef MSOOXMLREADER_H
25 #define MSOOXMLREADER_H
26 
27 #include "komsooxml_export.h"
28 
29 #include <QXmlStreamReader>
30 #include <QStack>
31 #include <QUrl>
32 
33 #include "MsooXmlDebug.h"
34 #include <KoXmlReader.h>
35 #include <KoFilter.h>
36 #include <KoOdfExporter.h>
37 
38 
39 namespace MSOOXML
40 {
41 
42 class MsooXmlRelationships;
43 
44 //! Context for MsooXmlReader::read()
45 class KOMSOOXML_EXPORT MsooXmlReaderContext
46 {
47 protected:
48     MsooXmlReaderContext(MsooXmlRelationships* _relationships = 0);
49 public:
50     virtual ~MsooXmlReaderContext();
51     MSOOXML::MsooXmlRelationships* relationships;
52     QMap<QString, QString> colorMap;
53 
54     // element:graphic - A frame might contain a graphic object that was
55     // generated by an external source and needs a container in which to be
56     // displayed.  A graphic object might represent a group of objects.
57     bool graphicObjectIsGroup;
58 
59 private:
60     Q_DISABLE_COPY(MsooXmlReaderContext)
61 };
62 
63 //! A base class reading MSOOXML parts like document.xml or styles.xml.
64 class KOMSOOXML_EXPORT MsooXmlReader : public QXmlStreamReader, public KoOdfWriters
65 {
66 public:
67     explicit MsooXmlReader(KoOdfWriters *writers);
68 
69     MsooXmlReader(QIODevice* io, KoOdfWriters *writers);
70 
71     virtual ~MsooXmlReader();
72 
73     //! Reads/parses the file
74     virtual KoFilter::ConversionStatus read(MsooXmlReaderContext* context = 0) = 0;
75 
76     //! Sets filename for the document being read.
77     //! Only for error reporting purposes, used in raiseError().
setFileName(const QString & fileName)78     void setFileName(const QString &fileName) {
79         m_fileName = fileName;
80     }
81 
82     //! @return filename for the document being read.
83     //! Only for error reporting purposes, used in raiseError().
fileName()84     QString fileName() const  {
85         return m_fileName;
86     }
87 
88     //! Reimplemented after QXmlStreamReader: adds line, column and filename information
89     void raiseError(const QString & message = QString());
90 
91     // Uncomment if debugging is needed
92     //! Reimplemented after QXmlStreamReader for supporting undo read and for debugging purposes
93     //TokenType readNext();
94 
95     //! Undoes recent readNext(); only one recent readNext() can be undoed
96     //void undoReadNext();
97 
98     // const strings (for optimization)
99     static const char constOn[];
100     static const char constOff[];
101     static const char constTrue[];
102     static const char constFalse[];
103     static const char constNone[];
104     static const char const1[];
105     static const char const0[];
106     static const char constAuto[];
107     static const char constFloat[];
108     static const char constPercentage[];
109     static const char constCurrency[];
110     static const char constDate[];
111     static const char constTime[];
112     static const char constBoolean[];
113     static const char constString[];
114 
115 protected:
116     // -- general
117     bool expectElName(const char* elementName);
118     bool expectElNameEnd(const char* elementName);
119     bool expectEl(const char* qualifiedElementName);
120     bool expectEl(const QString& qualifiedElementName);
121     bool expectEl(const QList<QByteArray>& qualifiedElementNames);
122     bool expectElEnd(const QString& qualifiedElementName);
123     bool expectElEnd(const char* qualifiedElementName);
124     bool expectNS(const char* nsName);
125     void raiseElNotFoundError(const char* elementName);
126     void raiseAttributeNotFoundError(const char* attrName);
127     void raiseNSNotFoundError(const char* nsName);
128     void raiseUnexpectedAttributeValueError(const QString& value, const char* attrName);
129     void raiseUnexpectedSecondOccurenceOfElError(const char* elementName);
130 
131     //! Decodes boolean attribute. Used by read_b(), read_i(), etc.
132     bool readBooleanAttr(const char* attrName, bool defaultValue = false) const;
133 
134     QString m_defaultNamespace; //!< stores namespace (for optimization)
135 
136     QStack<QByteArray> m_callsNames;
137 #ifndef NDEBUG
138     QStack<QByteArray> m_callsNamesDebug;
139 #endif
140 
141 private:
142     Q_DISABLE_COPY(MsooXmlReader)
143 
144     QString m_fileName;
145     bool m_readUndoed;
146     QXmlStreamReader::TokenType m_recentType;
147 
148     void init();
149 };
150 
151 } // namespace MSOOXML
152 
153 //! debugMsooXml stream operator. Writes this reader to the debug output in a nicely formatted way.
154 //! @todo add the same for QXmlStreamWriter
155 KOMSOOXML_EXPORT QDebug operator<<(QDebug dbg, const QXmlStreamReader& reader);
156 
157 #endif //MSOOXMLREADER_H
158