1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TableManager.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_TABLE_MANAGER_HXX
32 #define INCLUDED_TABLE_MANAGER_HXX
34 #ifndef INCLUDED_TABLE_DATA_HXX
35 #include <resourcemodel/TableData.hxx>
38 #ifndef INCLUDED_WW8_RESOURCE_MODEL_HXX
39 #include <resourcemodel/WW8ResourceModel.hxx>
42 #ifndef INCLUDED_SPRMIDS_HXX
43 #include <doctok/sprmids.hxx>
46 #include <boost/shared_ptr.hpp>
49 namespace writerfilter
52 using namespace ::std
;
54 template <typename T
, typename PropertiesPointer
>
56 Class to handle events generated by TableManager::resolveCurrentTable
58 class WRITERFILTER_DLLPUBLIC TableDataHandler
61 typedef boost::shared_ptr
<TableDataHandler
> Pointer_t
;
64 Handle start of table.
66 @param nRows number of rows in the table
67 @param nDepth depth of the table in surrounding table hierarchy
68 @param pProps properties of the table
70 virtual void startTable(unsigned int nRows
, unsigned int nDepth
,
71 PropertiesPointer pProps
) = 0;
76 virtual void endTable() = 0;
81 @param nCols number of columns in the table
82 @param pProps properties of the row
84 virtual void startRow(unsigned int nCols
,
85 PropertiesPointer pProps
) = 0;
90 virtual void endRow() = 0;
95 @param rT start handle of the cell
96 @param pProps properties of the cell
98 virtual void startCell(const T
& rT
, PropertiesPointer pProps
) = 0;
103 @param rT end handle of cell
105 virtual void endCell(const T
& rT
) = 0;
108 template <typename T
, typename PropertiesPointer
>
112 This class gets forwarded events from the tokenizer. It gathers the
113 table data and after ending the table generates events for the
114 table structure. The events have to be handles by a TableDataHandler.
119 typedef boost::shared_ptr
<T
> T_p
;
122 true if at the end of a row
132 true when at the end of a cell
137 depth of the current cell
139 sal_uInt32 mnTableDepthNew
;
142 depth of the previous cell
144 sal_uInt32 mnTableDepth
;
147 properties at the current point in document
149 PropertiesPointer mpProps
;
152 properties of the current cell
154 PropertiesPointer mpCellProps
;
157 properties of the current row
159 PropertiesPointer mpRowProps
;
162 properties of the current table
164 PropertiesPointer mpTableProps
;
167 handle for the current position in document
174 for each level of nested tables there is one frame in the stack
176 stack
<typename TableData
<T
, PropertiesPointer
>::Pointer_t
> mTableDataStack
;
178 typedef typename TableDataHandler
<T
, PropertiesPointer
>::Pointer_t TableDataHandlerPointer_t
;
181 handler for resolveCurrentTable
183 TableDataHandlerPointer_t mpTableDataHandler
;
186 Set flag which indicates the current handle is in a cell.
191 Set flag which indicate the current handle is at the end of a cell.
196 Set the table depth of the current cell.
198 @param nDepth the cell depth
200 void cellDepth(sal_uInt32 nDepth
);
203 Set flag indication the current handle is at the end of a row.
208 Resolve the current table to the TableDataHandler.
210 void resolveCurrentTable();
215 Return current table depth.
217 sal_uInt32
getTableDepthNew() { return mnTableDepthNew
; }
220 Action to be carried out at the end of the last paragraph of a
223 virtual void endOfCellAction();
226 Action to be carried out at the end of the "table row"
229 virtual void endOfRowAction();
230 /** let the derived class clear their table related data
232 virtual void clearData();
237 virtual ~TableManager(){}
240 Set handler for resolveCurrentTable.
242 @param pTableDataHandler the handler
244 void setHandler(TableDataHandlerPointer_t pTableDataHandler
);
247 Set the current handle.
249 @param rHandle the handle
251 virtual void handle(const T
& rHandle
);
254 Start a new table level.
256 A new context is pushed onto the table data stack,
258 virtual void startLevel();
263 The current table is resolved and the context is popped from
266 virtual void endLevel();
269 Handle the start of a paragraph group.
271 virtual void startParagraphGroup();
274 Handle the end of a paragraph group.
276 virtual void endParagraphGroup();
279 Handle an SPRM at curent handle.
281 @param rSprm the SPRM
283 virtual bool sprm(Sprm
& rSprm
);
286 Handle properties at current handle.
288 @param pProps the properites
290 virtual void props(PropertiesPointer pProps
);
293 Handle occurance of character 0x7.
295 virtual void handle0x7();
298 Handle 8 bit text at current handle.
300 @param data array of characters
301 @param len number of characters to handle
303 virtual void text(const sal_uInt8
* data
, size_t len
);
306 Handle 16 bit text at current handle.
308 @param data array of characters
309 @param len number of characters to handle
311 virtual void utext(const sal_uInt8
* data
, size_t len
);
314 Handle properties of the current cell.
316 @param pProps the properties
318 virtual void cellProps(PropertiesPointer pProps
);
321 Handle properties of a certain cell in the current row.
323 @paran i index of the cell in the current row
324 @param pProps the properties
326 virtual void cellPropsByCell(unsigned int i
, PropertiesPointer pProps
);
329 Handle properties of the current row.
331 @param pProps the properties
333 virtual void insertRowProps(PropertiesPointer pProps
);
336 Handle properties of the current table.
338 @param pProps the properties
340 virtual void insertTableProps(PropertiesPointer pProps
);
343 Return if table manager has detected paragraph to ignore.
345 If this function returns true the current paragraph contains
346 only control information, e.g. end of row.
348 virtual bool isIgnore() const;
351 template <typename T
, typename PropertiesPointer
>
352 TableManager
<T
, PropertiesPointer
>::TableManager()
353 : mbRowEnd(false), mbInCell(false), mbCellEnd(false), mnTableDepthNew(0),
358 template <typename T
, typename PropertiesPointer
>
359 void TableManager
<T
, PropertiesPointer
>::cellDepth(sal_uInt32 nDepth
)
361 mnTableDepthNew
= nDepth
;
364 template <typename T
, typename PropertiesPointer
>
365 void TableManager
<T
, PropertiesPointer
>::inCell()
369 if (mnTableDepthNew
< 1)
373 template <typename T
, typename PropertiesPointer
>
374 void TableManager
<T
, PropertiesPointer
>::endCell()
379 template <typename T
, typename PropertiesPointer
>
380 void TableManager
<T
, PropertiesPointer
>::endRow()
385 template <typename T
, typename PropertiesPointer
>
386 void TableManager
<T
, PropertiesPointer
>::setHandler
387 (typename TableDataHandler
<T
, PropertiesPointer
>::Pointer_t pTableDataHandler
)
389 mpTableDataHandler
= pTableDataHandler
;
392 template <typename T
, typename PropertiesPointer
>
393 void TableManager
<T
, PropertiesPointer
>::handle(const T
& rHandle
)
395 mCurHandle
= rHandle
;
398 template <typename T
, typename PropertiesPointer
>
399 void TableManager
<T
, PropertiesPointer
>::startLevel()
401 typename TableData
<T
, PropertiesPointer
>::Pointer_t pTableData
402 (new TableData
<T
, PropertiesPointer
>(mTableDataStack
.size()));
404 mTableDataStack
.push(pTableData
);
407 template <typename T
, typename PropertiesPointer
>
408 void TableManager
<T
, PropertiesPointer
>::endLevel()
410 if (mpTableDataHandler
.get() != NULL
)
411 resolveCurrentTable();
413 mTableDataStack
.pop();
416 template <typename T
, typename PropertiesPointer
>
417 void TableManager
<T
, PropertiesPointer
>::startParagraphGroup()
425 template <typename T
, typename PropertiesPointer
>
426 void TableManager
<T
, PropertiesPointer
>::endParagraphGroup()
428 sal_Int32 nTableDepthDifference
= mnTableDepthNew
- mnTableDepth
;
429 while (nTableDepthDifference
> 0)
433 --nTableDepthDifference
;
435 while (nTableDepthDifference
< 0)
439 ++nTableDepthDifference
;
442 mnTableDepth
= mnTableDepthNew
;
444 typename TableData
<T
, PropertiesPointer
>::Pointer_t pTableData
=
445 mTableDataStack
.top();
450 pTableData
->endRow(mpRowProps
);
456 if (! pTableData
->isCellOpen())
457 pTableData
->addCell(mCurHandle
, mpCellProps
);
462 pTableData
->endCell(mCurHandle
);
468 template <typename T
, typename PropertiesPointer
>
469 bool TableManager
<T
, PropertiesPointer
>::sprm(Sprm
& rSprm
)
472 switch (rSprm
.getId())
474 case NS_sprm::LN_PTableDepth
:
476 Value::Pointer_t pValue
= rSprm
.getValue();
478 cellDepth(pValue
->getInt());
481 case NS_sprm::LN_PFInTable
:
484 case NS_sprm::LN_PCell
:
487 case NS_sprm::LN_PFTtp
:
488 case NS_sprm::LN_PRow
:
496 template <typename T
, typename PropertiesPointer
>
497 void TableManager
<T
, PropertiesPointer
>::props(PropertiesPointer pProps
)
502 template <typename T
, typename PropertiesPointer
>
503 void TableManager
<T
, PropertiesPointer
>::handle0x7()
505 if (mnTableDepthNew
< 1)
514 template <typename T
, typename PropertiesPointer
>
515 void TableManager
<T
, PropertiesPointer
>::text(const sal_uInt8
* data
, size_t len
)
517 // optimization: cell/row end characters are the last characters in a run
520 if (data
[len
- 1] == 0x7)
525 template <typename T
, typename PropertiesPointer
>
526 void TableManager
<T
, PropertiesPointer
>::utext(const sal_uInt8
* data
, size_t len
)
528 // optimization: cell/row end characters are the last characters in a run
532 sal_Unicode nChar
= data
[(len
- 1) * 2] + (data
[(len
- 1) * 2 + 1] << 8);
538 template <typename T
, typename PropertiesPointer
>
539 void TableManager
<T
, PropertiesPointer
>::cellProps(PropertiesPointer pProps
)
541 if(mpCellProps
.get())
542 mpCellProps
->insert( pProps
);
544 mpCellProps
= pProps
;
547 template <typename T
, typename PropertiesPointer
>
548 void TableManager
<T
, PropertiesPointer
>::cellPropsByCell
549 (unsigned int i
, PropertiesPointer pProps
)
551 mTableDataStack
.top()->insertCellProperties(i
, pProps
);
554 template <typename T
, typename PropertiesPointer
>
555 void TableManager
<T
, PropertiesPointer
>::insertRowProps(PropertiesPointer pProps
)
557 if( mpRowProps
.get() )
558 mpRowProps
->insert( pProps
);
563 template <typename T
, typename PropertiesPointer
>
564 void TableManager
<T
, PropertiesPointer
>::insertTableProps(PropertiesPointer pProps
)
566 if( mpTableProps
.get() )
567 mpTableProps
->insert( pProps
);
569 mpTableProps
= pProps
;
572 template <typename T
, typename PropertiesPointer
>
573 void TableManager
<T
, PropertiesPointer
>::resolveCurrentTable()
575 if (mpTableDataHandler
.get() != NULL
)
577 typename TableData
<T
, PropertiesPointer
>::Pointer_t
578 pTableData
= mTableDataStack
.top();
580 unsigned int nRows
= pTableData
->getRowCount();
582 mpTableDataHandler
->startTable(nRows
, pTableData
->getDepth(), mpTableProps
);
584 for (unsigned int nRow
= 0; nRow
< nRows
; ++nRow
)
586 typename RowData
<T
, PropertiesPointer
>::Pointer_t pRowData
= pTableData
->getRow(nRow
);
588 unsigned int nCells
= pRowData
->getCellCount();
590 mpTableDataHandler
->startRow(nCells
, pRowData
->getProperties());
592 for (unsigned int nCell
= 0; nCell
< nCells
; ++nCell
)
594 mpTableDataHandler
->startCell
595 (pRowData
->getCellStart(nCell
),
596 pRowData
->getCellProperties(nCell
));
598 mpTableDataHandler
->endCell(pRowData
->getCellEnd(nCell
));
601 mpTableDataHandler
->endRow();
604 mpTableDataHandler
->endTable();
606 mpTableProps
.reset();
610 template <typename T
, typename PropertiesPointer
>
611 void TableManager
<T
, PropertiesPointer
>::endOfCellAction()
615 template <typename T
, typename PropertiesPointer
>
616 void TableManager
<T
, PropertiesPointer
>::endOfRowAction()
620 template <typename T
, typename PropertiesPointer
>
621 bool TableManager
<T
, PropertiesPointer
>::isIgnore() const
625 template <typename T
, typename PropertiesPointer
>
626 void TableManager
<T
, PropertiesPointer
>::clearData()
632 #endif // INCLUDED_TABLE_MANAGER_HXX