Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / writerfilter / source / ooxml / OOXMLFastContextHandler.hxx
blobd5de5a778ee09758b9a2e05c5a9eceb61a2c4b38
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 (sal_Int32 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(sal_Int32 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(sal_Int32 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 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 propagateTableProperties();
173 void propagateRowProperties();
174 void propagateCellProperties();
175 void sendPropertiesWithId(Id nId);
176 void sendPropertiesToParent();
177 void sendCellProperties();
178 void sendRowProperties();
179 void sendTableProperties();
180 void clearTableProps();
181 void clearProps();
183 virtual void setDefaultBooleanValue();
184 virtual void setDefaultIntegerValue();
185 virtual void setDefaultHexValue();
186 virtual void setDefaultStringValue();
188 void sendPropertyToParent();
189 OOXMLFastContextHandler* getParent() const { return mpParent; }
190 void setGridAfter(const OOXMLValue::Pointer_t& pGridAfter) { mpGridAfter = pGridAfter; }
192 protected:
193 OOXMLFastContextHandler * mpParent;
194 Id mId;
195 Id mnDefine;
196 Token_t mnToken;
198 // the stream to send the stream events to.
199 Stream * mpStream;
201 // the current global parser state
202 OOXMLParserState::Pointer_t mpParserState;
204 // the table depth of this context
205 unsigned int mnTableDepth;
207 /// @throws css::uno::RuntimeException
208 /// @throws css::xml::sax::SAXException
209 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs);
211 /// @throws css::uno::RuntimeException
212 /// @throws css::xml::sax::SAXException
213 virtual void lcl_endFastElement(Token_t Element);
215 /// @throws css::uno::RuntimeException
216 /// @throws css::xml::sax::SAXException
217 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs);
219 /// @throws css::uno::RuntimeException
220 /// @throws css::xml::sax::SAXException
221 virtual void lcl_characters(const OUString & aChars);
223 void startAction();
224 void endAction();
226 const css::uno::Reference< css::uno::XComponentContext >& getComponentContext() const { return m_xContext;}
228 bool inPositionV;
229 OOXMLValue::Pointer_t mpGridAfter;
231 private:
232 void operator =(OOXMLFastContextHandler const &) = delete;
233 /// Handles AlternateContent. Returns true, if children of the current element should be ignored.
234 bool prepareMceContext(Token_t nElement, const css::uno::Reference<css::xml::sax::XFastAttributeList>& Attribs);
236 // 2.10 of XML 1.0 specification
237 bool IsPreserveSpace() const;
239 css::uno::Reference< css::uno::XComponentContext > m_xContext;
240 bool m_bDiscardChildren;
241 bool m_bTookChoice; ///< Did we take the Choice or want Fallback instead?
242 bool mbPreserveSpace = false;
243 bool mbPreserveSpaceSet = false;
247 class OOXMLFastContextHandlerStream : public OOXMLFastContextHandler
249 public:
250 explicit OOXMLFastContextHandlerStream(OOXMLFastContextHandler * pContext);
251 virtual ~OOXMLFastContextHandlerStream() override;
253 virtual ResourceEnum_t getResource() const override { return STREAM; }
255 const OOXMLPropertySet::Pointer_t& getPropertySetAttrs() const { return mpPropertySetAttrs;}
257 virtual void newProperty(Id aId, const OOXMLValue::Pointer_t& pVal) override;
258 void sendProperty(Id nId);
259 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
261 void handleHyperlink();
263 private:
264 mutable OOXMLPropertySet::Pointer_t mpPropertySetAttrs;
267 class OOXMLFastContextHandlerProperties : public OOXMLFastContextHandler
269 public:
270 explicit OOXMLFastContextHandlerProperties(OOXMLFastContextHandler * pContext);
271 virtual ~OOXMLFastContextHandlerProperties() override;
273 virtual OOXMLValue::Pointer_t getValue() const override;
274 virtual ResourceEnum_t getResource() const override { return PROPERTIES; }
276 virtual void newProperty(Id nId, const OOXMLValue::Pointer_t& pVal) override;
278 void handleXNotes();
279 void handleHdrFtr();
280 void handleComment();
281 void handlePicture();
282 void handleBreak();
283 void handleOutOfOrderBreak();
284 void handleOLE();
285 void handleFontRel();
286 void handleHyperlinkURL();
288 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t& pPropertySet) override;
289 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
291 protected:
292 /// the properties
293 OOXMLPropertySet::Pointer_t mpPropertySet;
295 virtual void lcl_endFastElement(Token_t Element) override;
297 private:
299 bool mbResolve;
302 class OOXMLFastContextHandlerPropertyTable :
303 public OOXMLFastContextHandlerProperties
305 public:
306 explicit OOXMLFastContextHandlerPropertyTable(OOXMLFastContextHandler * pContext);
307 virtual ~OOXMLFastContextHandlerPropertyTable() override;
309 private:
310 OOXMLTable mTable;
312 virtual void lcl_endFastElement(Token_t Element) override;
315 class OOXMLFastContextHandlerValue :
316 public OOXMLFastContextHandler
318 public:
319 explicit OOXMLFastContextHandlerValue(OOXMLFastContextHandler * pContext);
320 virtual ~OOXMLFastContextHandlerValue() override;
322 void setValue(const OOXMLValue::Pointer_t& pValue);
323 virtual OOXMLValue::Pointer_t getValue() const override;
325 virtual void lcl_endFastElement(Token_t Element) override;
327 virtual std::string getType() const override { return "Value"; }
329 virtual void setDefaultBooleanValue() override;
330 virtual void setDefaultIntegerValue() override;
331 virtual void setDefaultHexValue() override;
332 virtual void setDefaultStringValue() override;
334 virtual void pushBiDiEmbedLevel() override;
335 virtual void popBiDiEmbedLevel() override;
337 private:
338 OOXMLValue::Pointer_t mpValue;
341 class OOXMLFastContextHandlerTable : public OOXMLFastContextHandler
343 public:
344 explicit OOXMLFastContextHandlerTable(OOXMLFastContextHandler * pContext);
345 virtual ~OOXMLFastContextHandlerTable() override;
347 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext (sal_Int32 Element,
348 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
350 private:
351 OOXMLTable mTable;
353 css::uno::Reference<css::xml::sax::XFastContextHandler> mCurrentChild;
355 virtual void lcl_endFastElement(Token_t Element) override;
357 virtual ResourceEnum_t getResource() const override { return TABLE; }
359 virtual std::string getType() const override { return "Table"; }
361 void addCurrentChild();
364 class OOXMLFastContextHandlerXNote : public OOXMLFastContextHandlerProperties
366 public:
367 explicit OOXMLFastContextHandlerXNote(OOXMLFastContextHandler * pContext);
368 virtual ~OOXMLFastContextHandlerXNote() override;
370 void checkId(const OOXMLValue::Pointer_t& pValue);
372 void checkType(const OOXMLValue::Pointer_t& pValue);
374 virtual std::string getType() const override { return "XNote"; }
376 private:
377 bool mbForwardEventsSaved;
378 sal_Int32 mnMyXNoteId;
379 sal_Int32 mnMyXNoteType;
381 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
383 virtual void lcl_endFastElement(Token_t Element) override;
385 virtual ResourceEnum_t getResource() const override { return STREAM; }
388 class OOXMLFastContextHandlerTextTableCell : public OOXMLFastContextHandler
390 public:
391 explicit OOXMLFastContextHandlerTextTableCell(OOXMLFastContextHandler * pContext);
392 virtual ~OOXMLFastContextHandlerTextTableCell() override;
394 virtual std::string getType() const override { return "TextTableCell"; }
396 void startCell();
397 void endCell();
400 class OOXMLFastContextHandlerTextTableRow : public OOXMLFastContextHandler
402 public:
403 explicit OOXMLFastContextHandlerTextTableRow(OOXMLFastContextHandler * pContext);
404 virtual ~OOXMLFastContextHandlerTextTableRow() override;
406 virtual std::string getType() const override { return "TextTableRow"; }
408 static void startRow();
409 void endRow();
410 void handleGridBefore( const OOXMLValue::Pointer_t& val );
411 void handleGridAfter(const OOXMLValue::Pointer_t& rValue);
414 class OOXMLFastContextHandlerTextTable : public OOXMLFastContextHandler
416 public:
417 explicit OOXMLFastContextHandlerTextTable(OOXMLFastContextHandler * pContext);
419 virtual ~OOXMLFastContextHandlerTextTable() override;
421 virtual std::string getType() const override { return "TextTable"; }
423 // tdf#111550
424 // when <w:tbl> appears as direct child of <w:p>, we need to rearrange this paragraph
425 // to merge with the table's first paragraph (that's what Word does in this case)
426 void start_P_Tbl();
427 protected:
428 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
430 virtual void lcl_endFastElement(Token_t Element) override;
433 class OOXMLFastContextHandlerShape: public OOXMLFastContextHandlerProperties
435 bool m_bShapeSent;
436 bool m_bShapeStarted;
437 /// Is it necessary to pop the stack in the dtor?
438 bool m_bShapeContextPushed;
439 css::uno::Reference<css::xml::sax::XFastShapeContextHandler> mrShapeContext;
441 public:
442 explicit OOXMLFastContextHandlerShape(OOXMLFastContextHandler * pContext);
443 virtual ~OOXMLFastContextHandlerShape() override;
445 virtual std::string getType() const override { return "Shape"; }
447 // css::xml::sax::XFastContextHandler:
448 virtual void SAL_CALL startUnknownElement (const OUString & Namespace, const OUString & Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
450 virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override;
452 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString & Namespace, const OUString & Name,
453 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
455 virtual void setToken(Token_t nToken) override;
457 virtual ResourceEnum_t getResource() const override { return SHAPE; }
459 void sendShape( Token_t Element );
460 bool isShapeSent( ) const { return m_bShapeSent; }
462 protected:
463 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
465 virtual void lcl_endFastElement(Token_t Element) override;
467 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext (Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
469 virtual void lcl_characters(const OUString & aChars) override;
474 OOXMLFastContextHandlerWrapper wraps an OOXMLFastContextHandler.
476 The method calls for the interface css::xml::sax::XFastContextHandler are
477 forwarded to the wrapped OOXMLFastContextHandler.
479 class OOXMLFastContextHandlerWrapper : public OOXMLFastContextHandler
481 public:
482 OOXMLFastContextHandlerWrapper(OOXMLFastContextHandler * pParent,
483 css::uno::Reference<css::xml::sax::XFastContextHandler> const & xContext,
484 rtl::Reference<OOXMLFastContextHandlerShape> const & xShapeHandler);
485 virtual ~OOXMLFastContextHandlerWrapper() override;
487 // css::xml::sax::XFastContextHandler:
488 virtual void SAL_CALL startUnknownElement(const OUString & Namespace, const OUString & Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
490 virtual void SAL_CALL endUnknownElement(const OUString & Namespace, const OUString & Name) override;
492 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext (const OUString & Namespace, const OUString & Name,
493 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
495 virtual void attributes(const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
497 virtual ResourceEnum_t getResource() const override;
499 void addNamespace(Id nId);
500 void addToken( Token_t Element );
502 virtual void newProperty(Id nId, const OOXMLValue::Pointer_t& pVal) override;
503 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t& pPropertySet) override;
504 virtual OOXMLPropertySet::Pointer_t getPropertySet() const override;
506 virtual std::string getType() const override;
508 protected:
509 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
511 virtual void lcl_endFastElement(Token_t Element) override;
513 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
515 virtual void lcl_characters(const OUString & aChars) override;
517 virtual void setId(Id nId) override;
518 virtual Id getId() const override;
520 virtual void setToken(Token_t nToken) override;
521 virtual Token_t getToken() const override;
523 private:
524 css::uno::Reference<css::xml::sax::XFastContextHandler> mxWrappedContext;
525 rtl::Reference<OOXMLFastContextHandlerShape> mxShapeHandler;
526 std::set<Id> mMyNamespaces;
527 std::set<Token_t> mMyTokens;
528 OOXMLPropertySet::Pointer_t mpPropertySet;
530 OOXMLFastContextHandler * getFastContextHandler() const;
534 A class that converts from XFastParser/XFastContextHandler usage to a liner XML stream of data.
536 The purpose of this class is to convert the rather complex XFastContextHandler-based XML
537 processing that requires context subclasses, callbacks, etc. into a linear stream of XML tokens
538 that can be handled simply by reading the tokens one by one and directly processing them.
539 See the oox::formulaimport::XmlStream class documentation for more information.
541 Usage: Create a subclass of OOXMLFastContextHandlerLinear, reimplemented getType() to provide
542 type of the subclass and process() to actually process the XML stream. Also make sure to
543 add a line like the following to model.xml (for class OOXMLFastContextHandlerMath):
545 <resource name="CT_OMath" resource="Math"/>
547 @since 3.5
549 class OOXMLFastContextHandlerLinear: public OOXMLFastContextHandlerProperties
551 public:
552 explicit OOXMLFastContextHandlerLinear(OOXMLFastContextHandler * pContext);
554 Return the type of the class, as written in model.xml .
556 virtual std::string getType() const override = 0;
558 protected:
560 Called when the tokens for the element, its content and sub-elements have been linearized
561 and should be processed. The data member @ref buffer contains the converted data.
563 virtual void process() = 0;
565 virtual void lcl_startFastElement(Token_t Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
567 virtual void lcl_endFastElement(Token_t Element) override;
569 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > lcl_createFastChildContext(Token_t Element,
570 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
572 virtual void lcl_characters(const OUString & aChars) override;
574 // should be private, but not much point in making deep copies of it
575 oox::formulaimport::XmlStreamBuilder buffer;
577 private:
578 int depthCount;
581 class OOXMLFastContextHandlerMath: public OOXMLFastContextHandlerLinear
583 public:
584 explicit OOXMLFastContextHandlerMath(OOXMLFastContextHandler * pContext);
585 virtual std::string getType() const override { return "Math"; }
586 protected:
587 virtual void process() override;
591 #endif // INCLUDED_WRITERFILTER_SOURCE_OOXML_OOXMLFASTCONTEXTHANDLER_HXX
593 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */