WKSContentListener.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006, 2007 Andrew Ziem
11 * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
12 * Copyright (C) 2003-2005 William Lachance (william.lachance@sympatico.ca)
13 * Copyright (C) 2003 Marc Maurer (uwog@uwog.net)
14 *
15 * For minor contributions see the git repository.
16 *
17 * Alternatively, the contents of this file may be used under the terms
18 * of the GNU Lesser General Public License Version 2.1 or later
19 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
20 * applicable instead of those above.
21 *
22 * For further information visit http://libwps.sourceforge.net
23 */
24
25 #ifndef WKSCONTENTLISTENER_H
26 #define WKSCONTENTLISTENER_H
27
28 #include <vector>
29
30 #include <librevenge/librevenge.h>
31
32 #include "libwps_internal.h"
33
34 #include "WPSEntry.h"
35 #include "WPSGraphicStyle.h"
36
37 #include "WPSListener.h"
38
39 class WPSCellFormat;
40 class WKSChart;
41 struct WPSColumnFormat;
42 class WPSGraphicShape;
43 class WPSGraphicStyle;
44 class WPSList;
45 class WPSPageSpan;
46 struct WPSParagraph;
47 struct WPSRowFormat;
48 struct WPSTabStop;
49
50 struct WKSContentParsingState;
51 struct WKSDocumentParsingState;
52
53 class WKSContentListener final : public WPSListener
54 {
55 public:
57 struct FormulaInstruction
58 {
59 enum What { F_Operator, F_Function, F_Cell, F_CellList, F_Long, F_Double, F_Text };
61 FormulaInstruction()
62 : m_type(F_Text)
63 , m_content()
64 , m_longValue(0)
65 , m_doubleValue(0)
66 , m_fileName()
67 {
68 for (auto &pos : m_position) pos=Vec2i(0,0);
69 for (auto &pos : m_positionRelative) pos=Vec2b(false,false);
70 for (auto &id : m_sheetId) id=-1;
71 }
73 librevenge::RVNGPropertyList getPropertyList() const;
75 friend std::ostream &operator<<(std::ostream &o, FormulaInstruction const &inst);
77 What m_type;
79 std::string m_content;
81 double m_longValue;
83 double m_doubleValue;
85 Vec2i m_position[2];
87 Vec2b m_positionRelative[2];
89 librevenge::RVNGString m_sheetName[2];
94 int m_sheetId[2];
96 librevenge::RVNGString m_fileName;
97 };
99 struct CellContent
100 {
102 enum ContentType { C_NONE, C_TEXT, C_NUMBER, C_FORMULA, C_UNKNOWN };
104 CellContent()
105 : m_contentType(C_UNKNOWN)
106 , m_value(0.0)
107 , m_valueSet(false)
108 , m_textEntry()
109 , m_formula() { }
110 CellContent(CellContent const &)=default;
111 CellContent &operator=(CellContent const &)=default;
113 ~CellContent() {}
115 friend std::ostream &operator<<(std::ostream &o, CellContent const &cell);
116
118 bool empty() const
119 {
120 if (m_contentType == C_NUMBER) return false;
121 if (m_contentType == C_TEXT && !m_textEntry.valid()) return false;
122 if (m_contentType == C_FORMULA && (m_formula.size() || isValueSet())) return false;
123 return true;
124 }
126 void setValue(double value)
127 {
128 m_value = value;
129 m_valueSet = true;
130 }
132 bool isValueSet() const
133 {
134 return m_valueSet;
135 }
137 bool hasText() const
138 {
139 return m_textEntry.valid();
140 }
142 static bool double2Date(double val, int &Y, int &M, int &D);
144 static bool double2Time(double val, int &H, int &M, int &S);
145
147 ContentType m_contentType;
149 double m_value;
151 bool m_valueSet;
153 WPSEntry m_textEntry;
155 std::vector<FormulaInstruction> m_formula;
156 };
157
158 WKSContentListener(std::vector<WPSPageSpan> const &pageList, librevenge::RVNGSpreadsheetInterface *documentInterface);
159 ~WKSContentListener() final;
160
161 void setDocumentLanguage(int lcid) final;
162 void setMetaData(const librevenge::RVNGPropertyList &list);
163
164 void startDocument();
165 void endDocument();
166 void handleSubDocument(WPSSubDocumentPtr &subDocument, libwps::SubDocumentType subDocumentType);
167
168 // ------ text data -----------
169
171 void insertCharacter(uint8_t character) final;
175 void insertUnicode(uint32_t character) final;
177 void insertUnicodeString(librevenge::RVNGString const &str) final;
178
179 void insertTab() final;
180 void insertEOL(bool softBreak=false) final;
181 void insertBreak(const uint8_t breakType) final;
182
183 // ------ text format -----------
185 void setFont(const WPSFont &font) final;
187 WPSFont const &getFont() const final;
188
189 // ------ paragraph format -----------
191 bool isParagraphOpened() const final;
193 void setParagraph(const WPSParagraph &para) final;
195 WPSParagraph const &getParagraph() const final;
196
197 // ------- fields ----------------
199 void insertField(WPSField const &field) final;
200
201 // ------- subdocument -----------------
203 void insertComment(WPSSubDocumentPtr &subDocument);
205 void insertPicture(WPSPosition const &pos, const librevenge::RVNGBinaryData &binaryData,
206 std::string type="image/pict", WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle());
208 void insertObject(WPSPosition const &pos, const WPSEmbeddedObject &obj,
209 WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle());
211 void insertPicture(WPSPosition const &pos, WPSGraphicShape const &shape, WPSGraphicStyle const &style);
213 void insertTextBox(WPSPosition const &pos, WPSSubDocumentPtr subDocument,
214 WPSGraphicStyle const &frameStyle=WPSGraphicStyle::emptyStyle());
216 bool openGroup(WPSPosition const &pos) final;
218 void closeGroup() final;
219
220 // ------- sheet -----------------
222 void openSheet(std::vector<WPSColumnFormat> const &columns, librevenge::RVNGString const &name="");
224 void closeSheet();
226 void openSheetRow(WPSRowFormat const &f, int numRepeated=1);
228 void closeSheetRow();
233 void openSheetCell(WPSCell const &cell, CellContent const &content, int numRepeated=1);
235 void closeSheetCell();
236
237 // ------- chart -----------------
239 void insertChart(WPSPosition const &pos, WKSChart const &chart,
240 WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle());
241 protected:
242 void _openPageSpan();
243 void _closePageSpan();
244
245 void _handleFrameParameters(librevenge::RVNGPropertyList &propList, WPSPosition const &pos);
246 bool _openFrame(WPSPosition const &pos, WPSGraphicStyle const &style);
247 void _closeFrame();
248
249 void _startSubDocument();
250 void _endSubDocument();
251
252 void _openParagraph();
253 void _closeParagraph();
254 void _appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool isListElement=false);
255 void _resetParagraphState(const bool isListElement=false);
256
257 void _openSpan();
258 void _closeSpan();
259
260 void _flushText();
261 void _flushDeferredTabs();
262
263 void _insertBreakIfNecessary(librevenge::RVNGPropertyList &propList);
264
268 std::shared_ptr<WKSContentParsingState> _pushParsingState();
270 void _popParsingState();
271
272 protected:
273 std::shared_ptr<WKSDocumentParsingState> m_ds; // main parse state
274 std::shared_ptr<WKSContentParsingState> m_ps; // parse state
275 std::vector<std::shared_ptr<WKSContentParsingState> > m_psStack;
276 librevenge::RVNGSpreadsheetInterface *m_documentInterface;
277
278 private:
279 WKSContentListener(const WKSContentListener &) = delete;
280 WKSContentListener &operator=(const WKSContentListener &) = delete;
281 };
282
283 #endif
284 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
WKSContentListener::setFont
void setFont(const WPSFont &font) final
set the actual font
Definition: WKSContentListener.cpp:285
WKSContentListener::insertCharacter
void insertCharacter(uint8_t character) final
adds a basic character, ..
Definition: WKSContentListener.cpp:192
WKSContentListener::closeSheet
void closeSheet()
closes this sheet
Definition: WKSContentListener.cpp:1034
WKSContentListener::CellContent::CellContent
CellContent()
constructor
Definition: WKSContentListener.h:104
WKSContentListener::_openFrame
bool _openFrame(WPSPosition const &pos, WPSGraphicStyle const &style)
Definition: WKSContentListener.cpp:726
WKSContentListener::insertField
void insertField(WPSField const &field) final
adds a field
Definition: WKSContentListener.cpp:325
WKSContentListener::CellContent::m_formula
std::vector< FormulaInstruction > m_formula
the formula list of instruction
Definition: WKSContentListener.h:155
WPSEntry::valid
bool valid(bool checkId=false) const
returns true if the zone length is positive
Definition: WPSEntry.h:87
WPSParagraph
class to store the paragraph properties
Definition: WPSParagraph.h:57
WKSContentListener::insertTextBox
void insertTextBox(WPSPosition const &pos, WPSSubDocumentPtr subDocument, WPSGraphicStyle const &frameStyle=WPSGraphicStyle::emptyStyle())
adds a textbox in given position
Definition: WKSContentListener.cpp:607
WKSContentListener::FormulaInstruction::m_sheetName
librevenge::RVNGString m_sheetName[2]
the sheet name
Definition: WKSContentListener.h:89
WKSContentListener::FormulaInstruction::F_Function
Definition: WKSContentListener.h:59
WKSContentListener::FormulaInstruction::What
What
Definition: WKSContentListener.h:59
WKSContentListener::CellContent::C_NONE
Definition: WKSContentListener.h:102
libwps
Definition: libwps_internal.cpp:38
WKSChart
a class used to store a chart associated to a spreadsheet ....
Definition: WKSChart.h:45
WKSContentListener::CellContent::hasText
bool hasText() const
returns true if the text is set
Definition: WKSContentListener.h:137
WKSContentListener::endDocument
void endDocument()
Definition: WKSContentListener.cpp:381
WKSContentListener::FormulaInstruction::m_fileName
librevenge::RVNGString m_fileName
the file name (external reference)
Definition: WKSContentListener.h:96
WKSContentParsingState
the spreadsheet state
Definition: WKSContentListener.cpp:86
WKSContentListener::FormulaInstruction::m_type
What m_type
the type
Definition: WKSContentListener.h:77
WKSContentListener::FormulaInstruction::m_longValue
double m_longValue
value ( if type==F_Long )
Definition: WKSContentListener.h:81
WKSContentListener::closeSheetRow
void closeSheetRow()
closes this row
Definition: WKSContentListener.cpp:1069
WKSContentListener::_pushParsingState
std::shared_ptr< WKSContentParsingState > _pushParsingState()
creates a new parsing state (copy of the actual state)
Definition: WKSContentListener.cpp:1279
librevenge
Definition: WPSOLEStream.h:33
WKSContentListener::setParagraph
void setParagraph(const WPSParagraph &para) final
sets the actual paragraph
Definition: WKSContentListener.cpp:317
WKSContentListener::insertUnicode
void insertUnicode(uint32_t character) final
adds an unicode character
Definition: WKSContentListener.cpp:204
WKSContentListener::_handleFrameParameters
void _handleFrameParameters(librevenge::RVNGPropertyList &propList, WPSPosition const &pos)
Definition: WKSContentListener.cpp:795
WKSContentListener::FormulaInstruction::F_Cell
Definition: WKSContentListener.h:59
WKSContentListener::_flushDeferredTabs
void _flushDeferredTabs()
Definition: WKSContentListener.cpp:488
WPSFont
define the font properties
Definition: WPSFont.h:36
WKSContentListener::_appendParagraphProperties
void _appendParagraphProperties(librevenge::RVNGPropertyList &propList, const bool isListElement=false)
Definition: WKSContentListener.cpp:446
WPSField
a field
Definition: libwps_internal.h:462
WKSContentListener::openSheetRow
void openSheetRow(WPSRowFormat const &f, int numRepeated=1)
open a row
Definition: WKSContentListener.cpp:1049
WKSContentListener::CellContent::C_TEXT
Definition: WKSContentListener.h:102
WPSListener
virtual class for content listener
Definition: WPSListener.h:36
WKSContentListener::CellContent::double2Time
static bool double2Time(double val, int &H, int &M, int &S)
conversion beetween double: second since 0:00 and time
Definition: WKSContentListener.cpp:1465
WKSContentListener::m_psStack
std::vector< std::shared_ptr< WKSContentParsingState > > m_psStack
Definition: WKSContentListener.h:275
WKSContentListener::handleSubDocument
void handleSubDocument(WPSSubDocumentPtr &subDocument, libwps::SubDocumentType subDocumentType)
Definition: WKSContentListener.cpp:932
WKSContentListener::_openParagraph
void _openParagraph()
Definition: WKSContentListener.cpp:403
WKSContentListener::CellContent::C_UNKNOWN
Definition: WKSContentListener.h:102
WPSCellFormat
a structure used to defined the cell format
Definition: WPSCell.h:40
WPSSubDocumentPtr
std::shared_ptr< WPSSubDocument > WPSSubDocumentPtr
shared pointer to WPSSubDocument
Definition: libwps_internal.h:111
WKSContentListener::FormulaInstruction::operator<<
friend std::ostream & operator<<(std::ostream &o, FormulaInstruction const &inst)
operator<<
Definition: WKSContentListener.cpp:1371
WKSContentListener::_openSpan
void _openSpan()
Definition: WKSContentListener.cpp:456
WKSContentListener::openGroup
bool openGroup(WPSPosition const &pos) final
open a group (not implemented)
Definition: WKSContentListener.cpp:690
WKSContentListener::WKSContentListener
WKSContentListener(std::vector< WPSPageSpan > const &pageList, librevenge::RVNGSpreadsheetInterface *documentInterface)
Definition: WKSContentListener.cpp:177
WKSContentListener::FormulaInstruction::getPropertyList
librevenge::RVNGPropertyList getPropertyList() const
return a proplist corresponding to a instruction
Definition: WKSContentListener.cpp:1312
WPSCell
a structure used to defined the cell position, and a format
Definition: WPSCell.h:290
WKSContentListener::setMetaData
void setMetaData(const librevenge::RVNGPropertyList &list)
Definition: WKSContentListener.cpp:355
WPSGraphicStyle.h
WKSContentListener::CellContent::m_value
double m_value
the cell value
Definition: WKSContentListener.h:149
WKSContentListener::isParagraphOpened
bool isParagraphOpened() const final
returns true if a paragraph or a list is opened
Definition: WKSContentListener.cpp:307
WKSContentListener::insertTab
void insertTab() final
adds an unicode character to a string ( with correct encoding ).
Definition: WKSContentListener.cpp:241
WKSContentListener::_resetParagraphState
void _resetParagraphState(const bool isListElement=false)
Definition: WKSContentListener.cpp:439
WKSContentListener::FormulaInstruction::F_Operator
Definition: WKSContentListener.h:59
WKSContentListener::FormulaInstruction::m_content
std::string m_content
the content ( if type == F_Operator or type = F_Function or type==F_Text)
Definition: WKSContentListener.h:79
WKSContentListener::FormulaInstruction::F_CellList
Definition: WKSContentListener.h:59
WPSGraphicStyle
a structure used to define a picture style
Definition: WPSGraphicStyle.h:37
WKSContentListener::getParagraph
WPSParagraph const & getParagraph() const final
returns the actual paragraph
Definition: WKSContentListener.cpp:312
WKSContentListener::_closeFrame
void _closeFrame()
Definition: WKSContentListener.cpp:783
WPSGraphicShape
a structure used to define a picture shape
Definition: WPSGraphicShape.h:34
WKSContentListener::CellContent::m_valueSet
bool m_valueSet
true if the value has been set
Definition: WKSContentListener.h:151
libwps_internal.h
WKSContentListener::FormulaInstruction::m_doubleValue
double m_doubleValue
value ( if type==F_Double )
Definition: WKSContentListener.h:83
WKSContentListener::setDocumentLanguage
void setDocumentLanguage(int lcid) final
Definition: WKSContentListener.cpp:347
libwps::SubDocumentType
SubDocumentType
Definition: libwps_internal.h:248
WKSContentListener::CellContent::C_FORMULA
Definition: WKSContentListener.h:102
WPSList
a small structure used to store the informations about a list
Definition: WPSList.h:36
WPSEntry.h
libwps_tools_win::Language::name
std::string name(long id)
returns the name given Windows© id
Definition: libwps_tools_win.cpp:7332
WKSContentListener::CellContent::operator=
CellContent & operator=(CellContent const &)=default
WKSContentListener::FormulaInstruction::m_positionRelative
Vec2b m_positionRelative[2]
relative cell position ( if type==F_Cell or F_CellList )
Definition: WKSContentListener.h:87
WKSContentListener::CellContent::double2Date
static bool double2Date(double val, int &Y, int &M, int &D)
conversion beetween double days since 1900 and date
Definition: WKSContentListener.cpp:1409
WKSContentListener::closeSheetCell
void closeSheetCell()
close a cell
Definition: WKSContentListener.cpp:1193
WKSContentListener::FormulaInstruction::m_sheetId
int m_sheetId[2]
the sheet id
Definition: WKSContentListener.h:94
WKSContentListener::CellContent::~CellContent
~CellContent()
destructor
Definition: WKSContentListener.h:113
WKSContentListener::FormulaInstruction::F_Double
Definition: WKSContentListener.h:59
WKSContentListener::insertPicture
void insertPicture(WPSPosition const &pos, const librevenge::RVNGBinaryData &binaryData, std::string type="image/pict", WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle())
adds a picture in given position
Definition: WKSContentListener.cpp:620
WKSContentListener::_openPageSpan
void _openPageSpan()
Definition: WKSContentListener.cpp:1210
WKSContentListener::FormulaInstruction::F_Text
Definition: WKSContentListener.h:59
WKSContentListener::_endSubDocument
void _endSubDocument()
Definition: WKSContentListener.cpp:989
WKSContentListener::closeGroup
void closeGroup() final
close a group (not implemented)
Definition: WKSContentListener.cpp:714
WKSContentListener::insertBreak
void insertBreak(const uint8_t breakType) final
Definition: WKSContentListener.cpp:253
WKSContentListener::FormulaInstruction::m_position
Vec2i m_position[2]
cell position ( if type==F_Cell or F_CellList )
Definition: WKSContentListener.h:85
WPSColumnFormat
Definition: WPSTable.h:39
WKSContentListener::~WKSContentListener
~WKSContentListener() final
Definition: WKSContentListener.cpp:185
WKSContentListener::m_ps
std::shared_ptr< WKSContentParsingState > m_ps
Definition: WKSContentListener.h:274
WKSContentListener::_popParsingState
void _popParsingState()
resets the previous parsing state
Definition: WKSContentListener.cpp:1300
WKSContentListener::CellContent::setValue
void setValue(double value)
sets the double value
Definition: WKSContentListener.h:126
WKSContentListener::startDocument
void startDocument()
Definition: WKSContentListener.cpp:365
WKSContentListener::_insertBreakIfNecessary
void _insertBreakIfNecessary(librevenge::RVNGPropertyList &propList)
Definition: WKSContentListener.cpp:272
WKSDocumentParsingState
the document state
Definition: WKSContentListener.cpp:49
Vec2i
Vec2< int > Vec2i
Vec2 of int.
Definition: libwps_internal.h:702
WKSContentListener::insertObject
void insertObject(WPSPosition const &pos, const WPSEmbeddedObject &obj, WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle())
adds an object with replacement picture in given position
Definition: WKSContentListener.cpp:634
WKSContentListener::openSheet
void openSheet(std::vector< WPSColumnFormat > const &columns, librevenge::RVNGString const &name="")
open a sheet
Definition: WKSContentListener.cpp:1000
WKSContentListener::m_documentInterface
librevenge::RVNGSpreadsheetInterface * m_documentInterface
Definition: WKSContentListener.h:276
WKSContentListener::_closeParagraph
void _closeParagraph()
Definition: WKSContentListener.cpp:425
WKSContentListener::CellContent
small class use to define a sheet cell content
Definition: WKSContentListener.h:99
WKSContentListener::_closePageSpan
void _closePageSpan()
Definition: WKSContentListener.cpp:1262
WKSContentListener::_flushText
void _flushText()
Definition: WKSContentListener.cpp:511
WKSContentListener::_closeSpan
void _closeSpan()
Definition: WKSContentListener.cpp:475
WKSContentListener::CellContent::m_textEntry
WPSEntry m_textEntry
the cell string
Definition: WKSContentListener.h:153
WPSListener.h
WKSContentListener::CellContent::isValueSet
bool isValueSet() const
returns true if the value has been setted
Definition: WKSContentListener.h:132
WKSContentListener::m_ds
std::shared_ptr< WKSDocumentParsingState > m_ds
Definition: WKSContentListener.h:273
WKSContentListener::getFont
WPSFont const & getFont() const final
returns the actual font
Definition: WKSContentListener.cpp:299
WKSContentListener::CellContent::C_NUMBER
Definition: WKSContentListener.h:102
WPSEmbeddedObject
small class use to define a embedded object
Definition: libwps_internal.h:1076
WPSTabStop
Definition: WPSParagraph.h:37
WPSPageSpan
Definition: WPSPageSpan.h:38
WKSContentListener::insertChart
void insertChart(WPSPosition const &pos, WKSChart const &chart, WPSGraphicStyle const &style=WPSGraphicStyle::emptyStyle())
adds a chart in given position
Definition: WKSContentListener.cpp:580
WPSEntry
basic class to store an entry in a file This contained :
Definition: WPSEntry.h:38
Vec2< int >
WKSContentListener::insertComment
void insertComment(WPSSubDocumentPtr &subDocument)
adds comment
Definition: WKSContentListener.cpp:545
WKSContentListener::CellContent::operator<<
friend std::ostream & operator<<(std::ostream &o, CellContent const &cell)
operator<<
Definition: WKSContentListener.cpp:1477
WKSContentListener::insertUnicodeString
void insertUnicodeString(librevenge::RVNGString const &str) final
adds a unicode string
Definition: WKSContentListener.cpp:213
WKSContentListener::CellContent::m_contentType
ContentType m_contentType
the content type ( by default unknown )
Definition: WKSContentListener.h:147
WPSPosition
Class to define the position of an object (textbox, picture, ..) in the document. ...
Definition: WPSPosition.h:39
WKSContentListener::FormulaInstruction::F_Long
Definition: WKSContentListener.h:59
WKSContentListener::FormulaInstruction
small class use to define a formula instruction
Definition: WKSContentListener.h:57
WKSContentListener::_startSubDocument
void _startSubDocument()
Definition: WKSContentListener.cpp:983
WKSContentListener::FormulaInstruction::FormulaInstruction
FormulaInstruction()
constructor
Definition: WKSContentListener.h:61
WKSContentListener::CellContent::ContentType
ContentType
the different types of cell&#39;s field
Definition: WKSContentListener.h:102
WKSContentListener
Definition: WKSContentListener.h:53
WKSContentListener::CellContent::empty
bool empty() const
returns true if the cell has no content
Definition: WKSContentListener.h:118
Vec2b
Vec2< bool > Vec2b
Vec2 of bool.
Definition: libwps_internal.h:700
WKSContentListener::insertEOL
void insertEOL(bool softBreak=false) final
Definition: WKSContentListener.cpp:220
WPSRowFormat
Definition: WPSTable.h:100
WKSContentListener::openSheetCell
void openSheetCell(WPSCell const &cell, CellContent const &content, int numRepeated=1)
low level function to define a cell.
Definition: WKSContentListener.cpp:1080