IWAParser.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * This file is part of the libetonyek project.
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 
10 #ifndef IWAPARSER_H_INCLUDED
11 #define IWAPARSER_H_INCLUDED
12 
13 #include <deque>
14 #include <functional>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19 
20 #include <boost/optional.hpp>
21 #include <boost/variant.hpp>
22 
23 #include <mdds/flat_segment_tree.hpp>
24 
25 #include "libetonyek_utils.h"
26 #include "IWAMessage.h"
27 #include "IWORKLanguageManager.h"
28 #include "IWORKStyle_fwd.h"
29 #include "IWORKTypes.h"
30 
31 namespace libetonyek
32 {
33 
34 class IWORKCollector;
35 class IWORKPropertyMap;
36 class IWORKTable;
37 class IWORKText;
38 
39 class IWAParser
40 {
41  // disable copying
42  IWAParser(const IWAParser &);
43  IWAParser &operator=(const IWAParser &);
44 
45 public:
46  struct ObjectRecord
47  {
48  ObjectRecord();
49  ObjectRecord(const RVNGInputStreamPtr_t &stream, unsigned type, long pos, unsigned long headerLen, unsigned long dataLen);
50 
52  unsigned m_type;
53  std::pair<long, long> m_headerRange;
54  std::pair<long, long> m_dataRange;
55  };
56 
57  typedef std::map<unsigned, std::pair<std::string, RVNGInputStreamPtr_t> > FileMap_t;
58  typedef std::map<unsigned, std::pair<unsigned, ObjectRecord> > RecordMap_t;
59 
60 public:
61  IWAParser(const RVNGInputStreamPtr_t &fragments, const RVNGInputStreamPtr_t &package, IWORKCollector &collector);
62 
63  bool parse();
64 
65 protected:
67  {
68  public:
69  ObjectMessage(IWAParser &parser, unsigned id, unsigned type = 0);
70  ~ObjectMessage();
71 
72  operator bool() const;
73  const IWAMessage &get() const;
74 
75  unsigned getType() const;
76 
77  friend const IWAMessage &get(const ObjectMessage &msg)
78  {
79  return msg.get();
80  }
81 
82  private:
84  boost::optional<IWAMessage> m_message;
85  const unsigned m_id;
86  unsigned m_type;
87  };
88  friend class ObjectMessage;
89 
90  typedef std::map<unsigned, IWORKStylePtr_t> StyleMap_t;
91  typedef std::function<void(unsigned, IWORKStylePtr_t &)> StyleParseFun_t;
92 
93 protected:
94  static boost::optional<unsigned> readRef(const IWAMessage &msg, unsigned field);
95  static std::deque<unsigned> readRefs(const IWAMessage &msg, unsigned field);
96  static boost::optional<IWORKPosition> readPosition(const IWAMessage &msg, unsigned field);
97  static boost::optional<IWORKSize> readSize(const IWAMessage &msg, unsigned field);
98  static boost::optional<IWORKColor> readColor(const IWAMessage &msg, unsigned field);
99  static void readStroke(const IWAMessage &msg, IWORKStroke &stroke);
100  bool readFill(const IWAMessage &msg, IWORKFill &fill);
101  static void readGradient(const IWAMessage &msg, IWORKGradient &gradient);
102  static void readShadow(const IWAMessage &msg, IWORKShadow &shadow);
103  static void readPadding(const IWAMessage &msg, IWORKPadding &padding);
104 
105  bool dispatchShape(unsigned id);
106  bool parseText(unsigned id);
107  void parseComment(unsigned id);
108 
110 
111  const IWORKStylePtr_t queryCharacterStyle(unsigned id) const;
112  const IWORKStylePtr_t queryParagraphStyle(unsigned id) const;
113  const IWORKStylePtr_t queryGraphicStyle(unsigned id) const;
114  const IWORKStylePtr_t queryCellStyle(unsigned id) const;
115  const IWORKStylePtr_t queryTableStyle(unsigned id) const;
116  const IWORKStylePtr_t queryListStyle(unsigned id) const;
117 
118  const IWORKStylePtr_t queryStyle(unsigned id, StyleMap_t &styleMap, StyleParseFun_t parse) const;
119 
120 protected:
122  std::shared_ptr<IWORKText> m_currentText;
123 
124 private:
125  typedef std::map<unsigned, boost::variant<std::string, unsigned> > DataList_t;
126 
127  struct TableHeader
128  {
129  TableHeader(unsigned count);
130 
131  mdds::flat_segment_tree<unsigned, float> m_sizes;
132  mdds::flat_segment_tree<unsigned, bool> m_hidden;
133  };
134 
135  struct TableInfo
136  {
137  TableInfo(const std::shared_ptr<IWORKTable> &table, unsigned columns, unsigned rows);
138 
139  std::shared_ptr<IWORKTable> m_table;
140 
141  const unsigned m_columns;
142  const unsigned m_rows;
143 
145 
148 
149  DataList_t m_simpleTextList;
150  DataList_t m_cellStyleList;
152  DataList_t m_commentList;
153  };
154 
155 private:
156  virtual bool parseDocument() = 0;
157 
158 private:
159  void queryObject(unsigned id, unsigned &type, boost::optional<IWAMessage> &msg) const;
160  const RVNGInputStreamPtr_t queryFile(unsigned id) const;
161 
162  void parseObjectIndex();
163 
164  void scanFragment(unsigned id);
165  void scanFragment(unsigned id, const RVNGInputStreamPtr_t &stream);
166 
167  void parseCharacterStyle(unsigned id, IWORKStylePtr_t &style);
168  void parseParagraphStyle(unsigned id, IWORKStylePtr_t &style);
169  void parseGraphicStyle(unsigned id, IWORKStylePtr_t &style);
170  void parseCellStyle(unsigned id, IWORKStylePtr_t &style);
171  void parseTableStyle(unsigned id, IWORKStylePtr_t &style);
172  void parseListStyle(unsigned id, IWORKStylePtr_t &style);
173 
174  void parseTabularModel(unsigned id);
175  void parseDataList(unsigned id, DataList_t &dataList);
176  void parseTile(unsigned id);
177  void parseHeaders(unsigned id, TableHeader &header);
178 
179  void parseLink(unsigned id, std::string &url);
180 
181  bool parseDrawableShape(const IWAMessage &msg);
182  bool parseGroup(const IWAMessage &msg);
183  bool parseShapePlacement(const IWAMessage &msg);
184  void parseCharacterProperties(const IWAMessage &msg, IWORKPropertyMap &props);
185  bool parseImage(const IWAMessage &msg);
186  bool parseTabularInfo(const IWAMessage &msg);
187 
188 private:
192 
193  FileMap_t m_fragmentMap;
194  mutable RecordMap_t m_fragmentObjectMap;
195  mutable FileMap_t m_fileMap;
196 
197  std::deque<unsigned> m_visited;
198 
199  mutable StyleMap_t m_charStyles;
200  mutable StyleMap_t m_paraStyles;
201  mutable StyleMap_t m_graphicStyles;
202  mutable StyleMap_t m_cellStyles;
203  mutable StyleMap_t m_tableStyles;
204  mutable StyleMap_t m_listStyles;
205 
207  std::shared_ptr<TableInfo> m_currentTable;
208 };
209 
210 }
211 
212 #endif
213 
214 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
void parseCharacterStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1075
std::shared_ptr< IWORKGeometry > IWORKGeometryPtr_t
Definition: IWORKTypes_fwd.h:39
Definition: IWORKBezierElement.cpp:18
DataList_t m_commentList
Definition: IWAParser.h:152
std::shared_ptr< IWORKTable > m_table
Definition: IWAParser.h:139
static boost::optional< IWORKColor > readColor(const IWAMessage &msg, unsigned field)
Definition: IWAParser.cpp:317
Definition: IWORKTypes.h:223
std::pair< long, long > m_headerRange
Definition: IWAParser.h:53
const IWORKStylePtr_t queryParagraphStyle(unsigned id) const
Definition: IWAParser.cpp:646
static void readStroke(const IWAMessage &msg, IWORKStroke &stroke)
Definition: IWAParser.cpp:328
static boost::optional< unsigned > readRef(const IWAMessage &msg, unsigned field)
Definition: IWAParser.cpp:273
std::deque< unsigned > m_visited
Definition: IWAParser.h:197
const IWORKStylePtr_t queryStyle(unsigned id, StyleMap_t &styleMap, StyleParseFun_t parse) const
Definition: IWAParser.cpp:628
RVNGInputStreamPtr_t m_stream
Definition: IWAParser.h:51
Definition: IWORKTypes.h:202
const IWORKStylePtr_t queryTableStyle(unsigned id) const
Definition: IWAParser.cpp:661
void parseComment(unsigned id)
Definition: IWAParser.cpp:1552
StyleMap_t m_paraStyles
Definition: IWAParser.h:200
const unsigned m_columns
Definition: IWAParser.h:141
std::shared_ptr< IWORKText > m_currentText
Definition: IWAParser.h:122
IWAParser & m_parser
Definition: IWAParser.h:83
const unsigned m_rows
Definition: IWAParser.h:142
DataList_t m_formattedTextList
Definition: IWAParser.h:151
void parseTabularModel(unsigned id)
Definition: IWAParser.cpp:1579
Represents a (hierarchical) property map.
Definition: IWORKPropertyMap.h:24
IWORKTableNameMapPtr_t m_tableNameMap
Definition: IWAParser.h:206
const RVNGInputStreamPtr_t queryFile(unsigned id) const
Definition: IWAParser.cpp:253
static boost::optional< IWORKSize > readSize(const IWAMessage &msg, unsigned field)
Definition: IWAParser.cpp:306
static std::deque< unsigned > readRefs(const IWAMessage &msg, unsigned field)
Definition: IWAParser.cpp:280
Definition: IWAParser.h:127
const RVNGInputStreamPtr_t m_fragments
Definition: IWAParser.h:189
void parseHeaders(unsigned id, TableHeader &header)
Definition: IWAParser.cpp:1900
void scanFragment(unsigned id)
Definition: IWAParser.cpp:1011
DataList_t m_cellStyleList
Definition: IWAParser.h:150
std::shared_ptr< IWORKStyle > IWORKStylePtr_t
Definition: IWORKStyle_fwd.h:21
void parseCharacterProperties(const IWAMessage &msg, IWORKPropertyMap &props)
Definition: IWAParser.cpp:1481
friend class ObjectMessage
Definition: IWAParser.h:88
bool parseShapePlacement(const IWAMessage &msg, IWORKGeometryPtr_t &geometry)
Definition: IWAParser.cpp:913
static void readShadow(const IWAMessage &msg, IWORKShadow &shadow)
Definition: IWAParser.cpp:472
IWORKCollector & m_collector
Definition: IWAParser.h:191
IWAParser(const IWAParser &)
bool parseImage(const IWAMessage &msg)
Definition: IWAParser.cpp:1512
const RVNGInputStreamPtr_t m_package
Definition: IWAParser.h:190
std::function< void(unsigned, IWORKStylePtr_t &)> StyleParseFun_t
Definition: IWAParser.h:91
IWORKStylePtr_t m_style
Definition: IWAParser.h:144
TableHeader m_columnHeader
Definition: IWAParser.h:146
const IWORKStylePtr_t queryGraphicStyle(unsigned id) const
Definition: IWAParser.cpp:651
const IWORKStylePtr_t queryListStyle(unsigned id) const
Definition: IWAParser.cpp:666
StyleMap_t m_tableStyles
Definition: IWAParser.h:203
boost::optional< IWAMessage > m_message
Definition: IWAParser.h:84
Definition: IWAParser.h:66
bool parseGroup(const IWAMessage &msg)
Definition: IWAParser.cpp:896
std::pair< long, long > m_dataRange
Definition: IWAParser.h:54
bool parseTabularInfo(const IWAMessage &msg)
Definition: IWAParser.cpp:1567
bool parseDrawableShape(const IWAMessage &msg)
Definition: IWAParser.cpp:671
StyleMap_t m_cellStyles
Definition: IWAParser.h:202
static void readGradient(const IWAMessage &msg, IWORKGradient &gradient)
Definition: IWAParser.cpp:439
bool readFill(const IWAMessage &msg, IWORKFill &fill)
Definition: IWAParser.cpp:382
Definition: IWORKTypes.h:82
bool parseText(unsigned id)
Definition: IWAParser.cpp:514
Definition: IWORKToken.h:268
unsigned m_type
Definition: IWAParser.h:86
Definition: IWAParser.h:135
FileMap_t m_fileMap
Definition: IWAParser.h:195
std::map< unsigned, std::pair< unsigned, ObjectRecord > > RecordMap_t
Definition: IWAParser.h:58
Definition: IWORKToken.h:245
RecordMap_t m_fragmentObjectMap
Definition: IWAParser.h:194
Definition: IWORKCollector.h:41
void parseLink(unsigned id, std::string &url)
Definition: IWAParser.cpp:1924
StyleMap_t m_listStyles
Definition: IWAParser.h:204
Definition: IWORKToken.h:218
void parseParagraphStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1099
ObjectRecord()
Definition: IWAParser.cpp:120
Definition: IWORKTypes.h:245
std::map< unsigned, boost::variant< std::string, unsigned > > DataList_t
Definition: IWAParser.h:125
StyleMap_t m_charStyles
Definition: IWAParser.h:199
boost::variant< IWORKColor, IWORKGradient, IWORKFillImage > IWORKFill
Definition: IWORKTypes.h:243
const IWORKStylePtr_t queryCellStyle(unsigned id) const
Definition: IWAParser.cpp:656
mdds::flat_segment_tree< unsigned, bool > m_hidden
Definition: IWAParser.h:132
unsigned m_type
Definition: IWAParser.h:52
FileMap_t m_fragmentMap
Definition: IWAParser.h:193
Definition: IWORKToken.h:87
std::shared_ptr< librevenge::RVNGInputStream > RVNGInputStreamPtr_t
Definition: libetonyek_utils.h:82
Definition: IWORKToken.h:148
IWORKLanguageManager m_langManager
Definition: IWAParser.h:121
Definition: KEY1Token.h:53
void parseCellStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1244
std::shared_ptr< IWORKTableNameMap_t > IWORKTableNameMapPtr_t
Definition: IWORKTypes_fwd.h:32
void parseTile(unsigned id)
Definition: IWAParser.cpp:1733
void parseListStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1355
StyleMap_t m_graphicStyles
Definition: IWAParser.h:201
Definition: IWORKLanguageManager.h:23
void parseObjectIndex()
Definition: IWAParser.cpp:960
void queryObject(unsigned id, unsigned &type, boost::optional< IWAMessage > &msg) const
Definition: IWAParser.cpp:235
Definition: IWORKToken.h:158
void parseGraphicStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1177
Definition: IWORKToken.h:266
DataList_t m_simpleTextList
Definition: IWAParser.h:149
std::map< unsigned, IWORKStylePtr_t > StyleMap_t
Definition: IWAParser.h:90
void parseTableStyle(unsigned id, IWORKStylePtr_t &style)
Definition: IWAParser.cpp:1311
bool dispatchShape(unsigned id)
Definition: IWAParser.cpp:493
IWAParser & operator=(const IWAParser &)
virtual bool parseDocument()=0
Definition: IWAMessage.h:21
TableHeader m_rowHeader
Definition: IWAParser.h:147
Definition: IWAParser.h:39
Definition: IWORKToken.h:295
static void readPadding(const IWAMessage &msg, IWORKPadding &padding)
Definition: IWAParser.cpp:485
void parseDataList(unsigned id, DataList_t &dataList)
Definition: IWAParser.cpp:1692
std::map< unsigned, std::pair< std::string, RVNGInputStreamPtr_t > > FileMap_t
Definition: IWAParser.h:57
Definition: IWORKToken.h:267
const IWORKStylePtr_t queryCharacterStyle(unsigned id) const
Definition: IWAParser.cpp:641
static boost::optional< IWORKPosition > readPosition(const IWAMessage &msg, unsigned field)
Definition: IWAParser.cpp:295
Definition: IWORKToken.h:353
Definition: IWORKToken.h:114
Definition: IWAParser.h:46
const unsigned m_id
Definition: IWAParser.h:85
bool parse()
Definition: IWAParser.cpp:178
std::shared_ptr< TableInfo > m_currentTable
Definition: IWAParser.h:207
mdds::flat_segment_tree< unsigned, float > m_sizes
Definition: IWAParser.h:131

Generated for libetonyek by doxygen 1.8.13