lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / writerfilter / source / ooxml / OOXMLFastContextHandler.hxx
blob097214b8e0497398af78739b6a8d088af284efcb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_WRITERFILTER_SOURCE_OOXML_OOXMLFASTCONTEXTHANDLER_HXX
21 #define INCLUDED_WRITERFILTER_SOURCE_OOXML_OOXMLFASTCONTEXTHANDLER_HXX
23 #include <set>
24 #include <cppuhelper/implbase.hxx>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/xml/sax/XFastContextHandler.hpp>
27 #include <com/sun/star/xml/sax/XFastShapeContextHandler.hpp>
28 #include <oox/mathml/import.hxx>
29 #include <oox/mathml/importutils.hxx>
30 #include "OOXMLParserState.hxx"
31 #include "OOXMLPropertySet.hxx"
33 namespace writerfilter {
34 namespace ooxml
36 class OOXMLDocumentImpl;
38 class OOXMLFastContextHandler: public ::cppu::WeakImplHelper<css::xml::sax::XFastContextHandler>
40 public:
41 typedef tools::SvRef<OOXMLFastContextHandler> Pointer_t;
43 enum ResourceEnum_t { UNKNOWN, STREAM, PROPERTIES, TABLE, SHAPE };
45 explicit OOXMLFastContextHandler(css::uno::Reference< css::uno::XComponentContext > const & context);
47 explicit OOXMLFastContextHandler(OOXMLFastContextHandler * pContext);
49 OOXMLFastContextHandler(OOXMLFastContextHandler const &) = default;
51 virtual ~OOXMLFastContextHandler() override;
53 // css::xml::sax::XFastContextHandler:
54 virtual void SAL_CALL startFastElement (Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs) override final;
56 virtual void SAL_CALL startUnknownElement(const OUString & Namespace, const OUString & Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
58 virtual void SAL_CALL endFastElement(Token_t Element) override;
60 virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override;
62 virtual css::uno::Reference<css::xml::sax::XFastContextHandler> SAL_CALL createFastChildContext(Token_t Element,
63 const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs) override;
65 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString & Namespace, const OUString & Name,
66 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
68 virtual void SAL_CALL characters(const OUString & aChars) override;
70 // local
72 void setStream(Stream * pStream);
74 /**
75 Return value of this context(element).
77 @return the value
79 virtual OOXMLValue::Pointer_t getValue() const;
81 /**
82 Returns a string describing the type of the context.
84 This is the name of the define normally.
86 @return type string
88 virtual std::string getType() const { return "??"; }
90 virtual ResourceEnum_t getResource() const { return STREAM; }
92 /// @throws css::uno::RuntimeException
93 /// @throws css::xml::sax::SAXException
94 virtual void attributes(const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs);
96 virtual void newProperty(Id aId, const OOXMLValue::Pointer_t& pVal);
97 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t& pPropertySet);
98 virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
100 virtual void setToken(Token_t nToken);
101 virtual Token_t getToken() const;
103 void resolveFootnote(const sal_Int32 nId);
104 void resolveEndnote(const sal_Int32 nId);
105 void resolveComment(const sal_Int32 nId);
106 void resolvePicture(const OUString & rId);
107 void resolveHeader(const sal_Int32 type,
108 const OUString & rId);
109 void resolveFooter(const sal_Int32 type,
110 const OUString & rId);
111 void resolveData(const OUString & rId);
113 OUString getTargetForId(const OUString & rId);
115 void setDocument(OOXMLDocumentImpl* pDocument);
116 OOXMLDocumentImpl* getDocument();
117 void setXNoteId(const OOXMLValue::Pointer_t& pValue);
118 void setXNoteId(const sal_Int32 nId);
119 sal_Int32 getXNoteId() const;
120 void setForwardEvents(bool bForwardEvents);
121 bool isForwardEvents() const;
122 virtual void setId(Id nId);
123 virtual Id getId() const;
125 void setDefine(Id nDefine);
126 Id getDefine() const { return mnDefine;}
128 const OOXMLParserState::Pointer_t& getParserState() const { return mpParserState;}
130 void sendTableDepth() const;
131 void setHandle();
133 void startSectionGroup();
134 void setLastParagraphInSection();
135 void setLastSectionGroup();
136 void endSectionGroup();
137 void startParagraphGroup();
138 void endParagraphGroup();
139 void startCharacterGroup();
140 void endCharacterGroup();
141 virtual void pushBiDiEmbedLevel();
142 virtual void popBiDiEmbedLevel();
143 void startSdt();
144 void endSdt();
146 void startField();
147 void fieldSeparator();
148 void endField();
149 void lockField();
150 static void ftnednref();
151 void ftnedncont();
152 void ftnednsep();
153 void pgNum();
154 void tab();
155 void symbol();
156 void cr();
157 void noBreakHyphen();
158 void softHyphen();
159 void handleLastParagraphInSection();
160 void endOfParagraph();
161 void text(const OUString & sText);
162 void positionOffset(const OUString & sText);
163 static void ignore();
164 void alignH(const OUString & sText);
165 void alignV(const OUString & sText);
166 void positivePercentage(const OUString& rText);
167 void startGlossaryEntry();
168 void endGlossaryEntry();
169 void startTxbxContent();
170 void endTxbxContent();
171 void propagateCharacterProperties();
172 void propagateCharacterPropertiesAsSet(Id nId);
173 void propagateTableProperties();
174 void propagateRowProperties();
175 void propagateCellProperties();
176 void sendPropertiesWithId(Id nId);
177 void sendPropertiesToParent();
178 void sendCellProperties();
179 void sendRowProperties();
180 void sendTableProperties();
181 void clearTableProps();
182 void clearProps();
184 virtual void setDefaultBooleanValue();
185 virtual void setDefaultIntegerValue();
186 virtual void setDefaultHexValue();
187 virtual void setDefaultStringValue();
189 void sendPropertyToParent();
190 OOXMLFastContextHandler* getParent() const { return mpParent; }
191 void setGridAfter(const OOXMLValue::Pointer_t& pGridAfter) { mpGridAfter = pGridAfter; }
193 protected:
194 OOXMLFastContextHandler * mpParent;
195 Id mId;
196 Id mnDefine;
197 Token_t mnToken;
199 // the stream to send the stream events to.
200 Stream * mpStream;
202 // the current global parser state
203 OOXMLParserState::Pointer_t mpParserState;
205 // the table depth of this context
206 unsigned int mnTableDepth;
208 /// @throws css::uno::RuntimeException
209 /// @throws css::xml::sax::SAXException
210 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs);
212 /// @throws css::uno::RuntimeException
213 /// @throws css::xml::sax::SAXException
214 virtual void lcl_endFastElement(Token_t Element);
216 /// @throws css::uno::RuntimeException
217 /// @throws css::xml::sax::SAXException
218 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs);
220 /// @throws css::uno::RuntimeException
221 /// @throws css::xml::sax::SAXException
222 virtual void lcl_characters(const OUString & aChars);
224 void startAction();
225 void endAction();
227 const css::uno::Reference< css::uno::XComponentContext >& getComponentContext() { return m_xContext;}
229 bool inPositionV;
230 OOXMLValue::Pointer_t mpGridAfter;
232 private:
233 void operator =(OOXMLFastContextHandler const &) = delete;
234 /// Handles AlternateContent. Returns true, if children of the current element should be ignored.
235 bool prepareMceContext(Token_t nElement, const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs);
237 // 2.10 of XML 1.0 specification
238 bool IsPreserveSpace() const;
240 css::uno::Reference< css::uno::XComponentContext > m_xContext;
241 bool m_bDiscardChildren;
242 bool m_bTookChoice; ///< Did we take the Choice or want Fallback instead?
243 bool mbPreserveSpace = false;
244 bool mbPreserveSpaceSet = false;
248 class OOXMLFastContextHandlerStream : public OOXMLFastContextHandler
250 public:
251 explicit OOXMLFastContextHandlerStream(OOXMLFastContextHandler * pContext);
252 virtual ~OOXMLFastContextHandlerStream() override;
254 virtual ResourceEnum_t getResource() const override { return STREAM; }
256 const OOXMLPropertySet::Pointer_t& getPropertySetAttrs() const { return mpPropertySetAttrs;}
258 virtual void newProperty(Id aId, const OOXMLValue::Pointer_t& pVal) override;
259 void sendProperty(Id nId);
260 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
262 void handleHyperlink();
264 private:
265 mutable OOXMLPropertySet::Pointer_t mpPropertySetAttrs;
268 class OOXMLFastContextHandlerProperties : public OOXMLFastContextHandler
270 public:
271 explicit OOXMLFastContextHandlerProperties(OOXMLFastContextHandler * pContext);
272 virtual ~OOXMLFastContextHandlerProperties() override;
274 virtual OOXMLValue::Pointer_t getValue() const override;
275 virtual ResourceEnum_t getResource() const override { return PROPERTIES; }
277 virtual void newProperty(Id nId, const OOXMLValue::Pointer_t& pVal) override;
279 void handleXNotes();
280 void handleHdrFtr();
281 void handleComment();
282 void handlePicture();
283 void handleBreak();
284 void handleOutOfOrderBreak();
285 void handleOLE();
286 void handleFontRel();
287 void handleHyperlinkURL();
289 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t& pPropertySet) override;
290 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
292 protected:
293 /// the properties
294 OOXMLPropertySet::Pointer_t mpPropertySet;
296 virtual void lcl_endFastElement(Token_t Element) override;
298 private:
300 bool mbResolve;
303 class OOXMLFastContextHandlerPropertyTable :
304 public OOXMLFastContextHandlerProperties
306 public:
307 explicit OOXMLFastContextHandlerPropertyTable(OOXMLFastContextHandler * pContext);
308 virtual ~OOXMLFastContextHandlerPropertyTable() override;
310 private:
311 OOXMLTable mTable;
313 virtual void lcl_endFastElement(Token_t Element) override;
316 class OOXMLFastContextHandlerValue :
317 public OOXMLFastContextHandler
319 public:
320 explicit OOXMLFastContextHandlerValue(OOXMLFastContextHandler * pContext);
321 virtual ~OOXMLFastContextHandlerValue() override;
323 void setValue(const OOXMLValue::Pointer_t& pValue);
324 virtual OOXMLValue::Pointer_t getValue() const override;
326 virtual void lcl_endFastElement(Token_t Element) override;
328 virtual std::string getType() const override { return "Value"; }
330 virtual void setDefaultBooleanValue() override;
331 virtual void setDefaultIntegerValue() override;
332 virtual void setDefaultHexValue() override;
333 virtual void setDefaultStringValue() override;
335 virtual void pushBiDiEmbedLevel() override;
336 virtual void popBiDiEmbedLevel() override;
338 private:
339 OOXMLValue::Pointer_t mpValue;
342 class OOXMLFastContextHandlerTable : public OOXMLFastContextHandler
344 public:
345 explicit OOXMLFastContextHandlerTable(OOXMLFastContextHandler * pContext);
346 virtual ~OOXMLFastContextHandlerTable() override;
348 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext (Token_t Element,
349 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
351 private:
352 OOXMLTable mTable;
354 css::uno::Reference<css::xml::sax::XFastContextHandler> mCurrentChild;
356 virtual void lcl_endFastElement(Token_t Element) override;
358 virtual ResourceEnum_t getResource() const override { return TABLE; }
360 virtual std::string getType() const override { return "Table"; }
362 void addCurrentChild();
365 class OOXMLFastContextHandlerXNote : public OOXMLFastContextHandlerProperties
367 public:
368 explicit OOXMLFastContextHandlerXNote(OOXMLFastContextHandler * pContext);
369 virtual ~OOXMLFastContextHandlerXNote() override;
371 void checkId(const OOXMLValue::Pointer_t& pValue);
373 void checkType(const OOXMLValue::Pointer_t& pValue);
375 virtual std::string getType() const override { return "XNote"; }
377 private:
378 bool mbForwardEventsSaved;
379 sal_Int32 mnMyXNoteId;
380 sal_Int32 mnMyXNoteType;
382 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
384 virtual void lcl_endFastElement(Token_t Element) override;
386 virtual ResourceEnum_t getResource() const override { return STREAM; }
389 class OOXMLFastContextHandlerTextTableCell : public OOXMLFastContextHandler
391 public:
392 explicit OOXMLFastContextHandlerTextTableCell(OOXMLFastContextHandler * pContext);
393 virtual ~OOXMLFastContextHandlerTextTableCell() override;
395 virtual std::string getType() const override { return "TextTableCell"; }
397 void startCell();
398 void endCell();
401 class OOXMLFastContextHandlerTextTableRow : public OOXMLFastContextHandler
403 public:
404 explicit OOXMLFastContextHandlerTextTableRow(OOXMLFastContextHandler * pContext);
405 virtual ~OOXMLFastContextHandlerTextTableRow() override;
407 virtual std::string getType() const override { return "TextTableRow"; }
409 static void startRow();
410 void endRow();
411 void handleGridBefore( const OOXMLValue::Pointer_t& val );
412 void handleGridAfter(const OOXMLValue::Pointer_t& rValue);
415 class OOXMLFastContextHandlerTextTable : public OOXMLFastContextHandler
417 public:
418 explicit OOXMLFastContextHandlerTextTable(OOXMLFastContextHandler * pContext);
420 virtual ~OOXMLFastContextHandlerTextTable() override;
422 virtual std::string getType() const override { return "TextTable"; }
424 // tdf#111550
425 // when <w:tbl> appears as direct child of <w:p>, we need to rearrange this paragraph
426 // to merge with the table's first paragraph (that's what Word does in this case)
427 void start_P_Tbl();
428 protected:
429 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
431 virtual void lcl_endFastElement(Token_t Element) override;
434 class OOXMLFastContextHandlerShape: public OOXMLFastContextHandlerProperties
436 bool m_bShapeSent;
437 bool m_bShapeStarted;
438 /// Is it necessary to pop the stack in the dtor?
439 bool m_bShapeContextPushed;
440 css::uno::Reference<css::xml::sax::XFastShapeContextHandler> mrShapeContext;
442 public:
443 explicit OOXMLFastContextHandlerShape(OOXMLFastContextHandler * pContext);
444 virtual ~OOXMLFastContextHandlerShape() override;
446 virtual std::string getType() const override { return "Shape"; }
448 // css::xml::sax::XFastContextHandler:
449 virtual void SAL_CALL startUnknownElement (const OUString & Namespace, const OUString & Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
451 virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override;
453 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString & Namespace, const OUString & Name,
454 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
456 virtual void setToken(Token_t nToken) override;
458 virtual ResourceEnum_t getResource() const override { return SHAPE; }
460 void sendShape( Token_t Element );
461 bool isShapeSent( ) { return m_bShapeSent; }
463 protected:
464 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
466 virtual void lcl_endFastElement(Token_t Element) override;
468 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext (Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
470 virtual void lcl_characters(const OUString & aChars) override;
475 OOXMLFastContextHandlerWrapper wraps an OOXMLFastContextHandler.
477 The method calls for the interface css::xml::sax::XFastContextHandler are
478 forwarded to the wrapped OOXMLFastContextHandler.
480 class OOXMLFastContextHandlerWrapper : public OOXMLFastContextHandler
482 public:
483 explicit OOXMLFastContextHandlerWrapper(OOXMLFastContextHandler * pParent, css::uno::Reference<css::xml::sax::XFastContextHandler> const & xContext);
484 virtual ~OOXMLFastContextHandlerWrapper() override;
486 // css::xml::sax::XFastContextHandler:
487 virtual void SAL_CALL startUnknownElement(const OUString & Namespace, const OUString & Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
489 virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override;
491 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext (const OUString & Namespace, const OUString & Name,
492 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
494 virtual void attributes(const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
496 virtual ResourceEnum_t getResource() const override;
498 void addNamespace(Id nId);
499 void addToken( Token_t Element );
501 virtual void newProperty(Id nId, const OOXMLValue::Pointer_t& pVal) override;
502 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t& pPropertySet) override;
503 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
505 virtual std::string getType() const override;
507 protected:
508 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
510 virtual void lcl_endFastElement(Token_t Element) override;
512 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
514 virtual void lcl_characters(const OUString & aChars) override;
516 virtual void setId(Id nId) override;
517 virtual Id getId() const override;
519 virtual void setToken(Token_t nToken) override;
520 virtual Token_t getToken() const override;
522 private:
523 css::uno::Reference<css::xml::sax::XFastContextHandler> mxContext;
524 std::set<Id> mMyNamespaces;
525 std::set<Token_t> mMyTokens;
526 OOXMLPropertySet::Pointer_t mpPropertySet;
528 OOXMLFastContextHandler * getFastContextHandler() const;
532 A class that converts from XFastParser/XFastContextHandler usage to a liner XML stream of data.
534 The purpose of this class is to convert the rather complex XFastContextHandler-based XML
535 processing that requires context subclasses, callbacks, etc. into a linear stream of XML tokens
536 that can be handled simply by reading the tokens one by one and directly processing them.
537 See the oox::formulaimport::XmlStream class documentation for more information.
539 Usage: Create a subclass of OOXMLFastContextHandlerLinear, reimplemented getType() to provide
540 type of the subclass and process() to actually process the XML stream. Also make sure to
541 add a line like the following to model.xml (for class OOXMLFastContextHandlerMath):
543 <resource name="CT_OMath" resource="Math"/>
545 @since 3.5
547 class OOXMLFastContextHandlerLinear: public OOXMLFastContextHandlerProperties
549 public:
550 explicit OOXMLFastContextHandlerLinear(OOXMLFastContextHandler * pContext);
552 Return the type of the class, as written in model.xml .
554 virtual std::string getType() const override = 0;
556 protected:
558 Called when the tokens for the element, its content and sub-elements have been linearized
559 and should be processed. The data member @ref buffer contains the converted data.
561 virtual void process() = 0;
563 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
565 virtual void lcl_endFastElement(Token_t Element) override;
567 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element,
568 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
570 virtual void lcl_characters(const OUString & aChars) override;
572 // should be private, but not much point in making deep copies of it
573 oox::formulaimport::XmlStreamBuilder buffer;
575 private:
576 int depthCount;
579 class OOXMLFastContextHandlerMath: public OOXMLFastContextHandlerLinear
581 public:
582 explicit OOXMLFastContextHandlerMath(OOXMLFastContextHandler * pContext);
583 virtual std::string getType() const override { return "Math"; }
584 protected:
585 virtual void process() override;
589 #endif // INCLUDED_WRITERFILTER_SOURCE_OOXML_OOXMLFASTCONTEXTHANDLER_HXX
591 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */