1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_TABLE_DATA
25 #define INCLUDED_TABLE_DATA
26 
27 #ifndef INCLUDED_WW8_RESOURCE_MODEL_HXX
28 #include <resourcemodel/WW8ResourceModel.hxx>
29 #endif
30 
31 #include <vector>
32 #include <boost/shared_ptr.hpp>
33 
34 namespace writerfilter
35 {
36 
37 template <typename T, typename PropertiesPointer>
38 /**
39    Class containing the data to describe a table cell.
40  */
41 class WRITERFILTER_DLLPUBLIC CellData
42 {
43     /**
44        Handle to start of cell.
45     */
46     T mStart;
47 
48     /**
49        Handle to end of cell.
50     */
51     T mEnd;
52 
53     /**
54        Pointer to properties of cell.
55     */
56     PropertiesPointer mpProps;
57 
58     bool mbOpen;
59 
60 public:
61     typedef boost::shared_ptr<CellData> Pointer_t;
62 
CellData(T start,PropertiesPointer pProps)63     CellData(T start, PropertiesPointer pProps)
64     : mStart(start), mEnd(start), mpProps(pProps), mbOpen(true)
65     {
66     }
67 
~CellData()68     virtual ~CellData() {}
69 
70     /**
71        Set the start handle of the cell.
72 
73        @param start    the start handle of the cell
74     */
setStart(T start)75     void setStart(T start) { mStart = start; }
76 
77     /**
78        Set the end handle of a cell.
79 
80        @param end     the end handle of the cell
81     */
setEnd(T end)82     void setEnd(T end) { mEnd = end; mbOpen = false; }
83 
84     /**
85        Set the properties of the cell.
86 
87        @param pProps      the properties of the cell
88     */
setProperties(PropertiesPointer pProps)89     void setProperties(PropertiesPointer pProps) { mpProps = pProps; }
90 
91     /**
92        Adds properties to the cell.
93 
94        @param pProps      the properties to add
95      */
insertProperties(PropertiesPointer pProps)96     void insertProperties(PropertiesPointer pProps)
97     {
98         if( mpProps.get() )
99             mpProps->insert(pProps);
100         else
101             mpProps = pProps;
102     }
103 
104     /**
105        Return start handle of the cell.
106      */
getStart()107     const T & getStart() { return mStart; }
108 
109     /**
110        Return end handle of the cell.
111     */
getEnd()112     const T & getEnd() { return mEnd; }
113 
114     /**
115        Return properties of the cell.
116      */
getProperties()117     PropertiesPointer getProperties() { return mpProps; }
118 
isOpen() const119     bool isOpen() const { return mbOpen; }
120 };
121 
122 template <typename T, typename PropertiesPointer>
123 /**
124    Class to handle data of a table row.
125  */
126 class WRITERFILTER_DLLPUBLIC RowData
127 {
128     typedef typename CellData<T, PropertiesPointer>::Pointer_t
129         CellDataPointer_t;
130     typedef ::std::vector<CellDataPointer_t> Cells;
131 
132     /**
133        the cell data of the row
134      */
135     Cells mCells;
136 
137     /**
138        the properties of the row
139     */
140     mutable PropertiesPointer mpProperties;
141 
142 public:
143     typedef boost::shared_ptr<RowData <T, PropertiesPointer> > Pointer_t;
144 
RowData()145     RowData() {}
146 
RowData(const RowData<T,PropertiesPointer> & rRowData)147     RowData(const RowData<T, PropertiesPointer> & rRowData)
148     : mCells(rRowData.mCells), mpProperties(rRowData.mpProperties)
149     {
150     }
151 
~RowData()152     virtual ~RowData() {}
153 
154     /**
155        Add a cell to the row.
156 
157        @param start     the start handle of the cell
158        @param end       the end handle of the cell
159        @param pProps    the properties of the cell
160      */
addCell(const T & start,PropertiesPointer pProps)161     void addCell(const T & start, PropertiesPointer pProps)
162     {
163         CellDataPointer_t pCellData
164             (new CellData<T, PropertiesPointer>(start, pProps));
165         mCells.push_back(pCellData);
166     }
167 
endCell(const T & end)168     void endCell(const T & end)
169     {
170         if (mCells.size() > 0)
171             mCells.back()->setEnd(end);
172     }
173 
isCellOpen() const174     bool isCellOpen() const
175     {
176         return mCells.size() > 0 && mCells.back()->isOpen();
177     }
178 
179     /**
180        Add properties to the row.
181 
182        @param pProperties   the properties to set
183      */
insertProperties(PropertiesPointer pProperties)184     void insertProperties(PropertiesPointer pProperties)
185     {
186         if( pProperties.get() )
187         {
188             if( !mpProperties.get() )
189                 mpProperties = pProperties;
190             else
191                 mpProperties->insert( pProperties );
192         }
193     }
194 
195     /**
196        Add properties to a cell of the row.
197 
198        @param i          index of the cell
199        @param pProps     the properties to add
200      */
insertCellProperties(unsigned int i,PropertiesPointer pProps)201     void insertCellProperties(unsigned int i, PropertiesPointer pProps)
202     {
203         mCells[i]->insertProperties(pProps);
204     }
205 
206     /**
207         Add properties to the last cell of the row.
208      */
insertCellProperties(PropertiesPointer pProps)209     void insertCellProperties(PropertiesPointer pProps)
210     {
211         if (! mCells.empty())
212             mCells.back()->insertProperties(pProps);
213     }
214 
215     /**
216        Return number of cells in the row.
217     */
getCellCount()218     unsigned int getCellCount()
219     {
220         return mCells.size();
221     }
222 
223     /**
224        Return start handle of a cell in the row.
225 
226        @param i      index of the cell
227      */
getCellStart(unsigned int i) const228     const T & getCellStart(unsigned int i) const
229     {
230         return mCells[i]->getStart();
231     }
232 
233     /**
234         Return end handle of a cell in the row.
235 
236         @param i     index of the cell
237     */
getCellEnd(unsigned int i) const238     const T & getCellEnd(unsigned int i) const
239     {
240         return mCells[i]->getEnd();
241     }
242 
243     /**
244        Return the properties of a cell in the row.
245 
246        @param i      index of the cell
247      */
getCellProperties(unsigned int i) const248     PropertiesPointer getCellProperties(unsigned int i) const
249     {
250         return mCells[i]->getProperties();
251     }
252 
253     /**
254        Return properties of the row.
255      */
getProperties()256     PropertiesPointer getProperties()
257     {
258         return mpProperties;
259     }
260 
261     /**
262        Clear the row data.
263      */
clear()264     void clear()
265     {
266         mCells.clear();
267         mpProperties.reset();
268     }
269 };
270 
271 template <typename T, typename PropertiesPointer>
272 /**
273    Class that holds the data of a table.
274  */
275 class WRITERFILTER_DLLPUBLIC TableData
276 {
277     typedef typename RowData<T, PropertiesPointer>::Pointer_t RowPointer_t;
278     typedef ::std::vector<RowPointer_t> Rows;
279 
280     /**
281        the table properties
282      */
283     PropertiesPointer mpTableProps;
284 
285     /**
286        the data of the rows of the table
287     */
288     Rows mRows;
289 
290     /**
291         pointer to the data of the current row (while building up the table data).
292     */
293     RowPointer_t mpRow;
294 
295     /**
296        depth of the current table in a hierarchy of tables
297      */
298     unsigned int mnDepth;
299 
300     /**
301        initialize mpRow
302      */
newRow()303     void newRow() { mpRow = RowPointer_t(new RowData<T, PropertiesPointer>()); }
304 
305 public:
306     typedef boost::shared_ptr<TableData <T, PropertiesPointer> > Pointer_t;
307 
TableData(unsigned int nDepth)308     TableData(unsigned int nDepth) : mnDepth(nDepth) { newRow(); }
~TableData()309     ~TableData() {}
310 
311     /**
312        End the current row.
313 
314        Sets properties of the current row and pushes the row to the
315        back of the rows currently contained in the table.
316 
317        @param pProperties    properties of the row to be ended
318      */
endRow(PropertiesPointer pProperties)319     void endRow(PropertiesPointer pProperties)
320     {
321         mpRow->insertProperties(pProperties);
322         mRows.push_back(mpRow);
323         newRow();
324     }
325 
326     /**
327        Add a cell to the current row.
328 
329        @param start   start handle of the cell
330        @param end     end handle of the cell
331        @param pProps  properties of the cell
332      */
addCell(const T & start,PropertiesPointer pProps)333     void addCell(const T & start, PropertiesPointer pProps)
334     {
335         mpRow->addCell(start, pProps);
336     }
337 
338     /**
339         End the current cell of the current row.
340 
341         @parm end    end handle of the cell
342      */
endCell(const T & end)343     void endCell(const T & end)
344     {
345         mpRow->endCell(end);
346     }
347 
348     /**
349         Return if the current cell of the current row is open.
350      */
isCellOpen() const351     bool isCellOpen() const
352     {
353         return mpRow->isCellOpen();
354     }
355 
356     /**
357         Insert properties to the current cell of the current row.
358 
359         @param pProps   the properties to add
360      */
insertCellProperties(PropertiesPointer pProps)361     void insertCellProperties(PropertiesPointer pProps)
362     {
363         mpRow->insertCellProperties(pProps);
364     }
365 
366     /**
367        Add properties to a cell of the current row.
368 
369        @param i       index of the cell
370        @param pProps  properties to add
371      */
insertCellProperties(unsigned int i,PropertiesPointer pProps)372     void insertCellProperties(unsigned int i, PropertiesPointer pProps)
373     {
374         mpRow->insertCellProperties(i, pProps);
375     }
376 
insertTableProperties(PropertiesPointer pProps)377     void insertTableProperties( PropertiesPointer pProps )
378     {
379         if ( mpTableProps.get( ) )
380             mpTableProps->insert( pProps );
381         else
382             mpTableProps = pProps;
383     }
384 
385     /**
386       Return the table properties.
387      */
getTableProperties()388     PropertiesPointer getTableProperties( )
389     {
390         return mpTableProps;
391     }
392 
393     /**
394        Return number of rows in the table.
395      */
getRowCount()396     unsigned int getRowCount()
397     {
398         return mRows.size();
399     }
400 
401     /**
402        Return depth of table in surrounding table hierarchy.
403     */
getDepth()404     unsigned int getDepth()
405     {
406         return mnDepth;
407     }
408 
409     /**
410        Return row data of a certain row.
411 
412        @param i     index of the row
413     */
getRow(unsigned int i) const414     const RowPointer_t getRow(unsigned int i) const
415     {
416         return mRows[i];
417     }
418 };
419 
420 }
421 
422 #endif // INCLUDED_TABLE_DATA
423