1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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/importutils.hxx>
29 #include <rtl/ref.hxx>
30 #include "OOXMLParserState.hxx"
31 #include "OOXMLPropertySet.hxx"
33 namespace writerfilter::ooxml
35 class OOXMLDocumentImpl
;
37 class OOXMLFastContextHandler
: public ::cppu::WeakImplHelper
<css::xml::sax::XFastContextHandler
>
40 typedef tools::SvRef
<OOXMLFastContextHandler
> Pointer_t
;
42 enum ResourceEnum_t
{ UNKNOWN
, STREAM
, PROPERTIES
, TABLE
, SHAPE
};
44 explicit OOXMLFastContextHandler(css::uno::Reference
< css::uno::XComponentContext
> const & context
);
46 explicit OOXMLFastContextHandler(OOXMLFastContextHandler
* pContext
);
48 OOXMLFastContextHandler(OOXMLFastContextHandler
const &) = default;
50 virtual ~OOXMLFastContextHandler() override
;
52 // css::xml::sax::XFastContextHandler:
53 virtual void SAL_CALL
startFastElement (sal_Int32 Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& Attribs
) override final
;
55 virtual void SAL_CALL
startUnknownElement(const OUString
& Namespace
, const OUString
& Name
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
57 virtual void SAL_CALL
endFastElement(sal_Int32 Element
) override
;
59 virtual void SAL_CALL
endUnknownElement(const OUString
& Namespace
, const OUString
& Name
) override
;
61 virtual css::uno::Reference
<css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(sal_Int32 Element
,
62 const css::uno::Reference
<css::xml::sax::XFastAttributeList
>& Attribs
) override
;
64 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext(const OUString
& Namespace
, const OUString
& Name
,
65 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
67 virtual void SAL_CALL
characters(const OUString
& aChars
) override
;
71 void setStream(Stream
* pStream
);
74 Return value of this context(element).
78 virtual OOXMLValue::Pointer_t
getValue() const;
81 Returns a string describing the type of the context.
83 This is the name of the define normally.
87 virtual std::string
getType() const { return "??"; }
89 virtual ResourceEnum_t
getResource() const { return STREAM
; }
91 /// @throws css::uno::RuntimeException
92 /// @throws css::xml::sax::SAXException
93 virtual void attributes(const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
);
95 virtual void newProperty(Id aId
, const OOXMLValue::Pointer_t
& pVal
);
96 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t
& pPropertySet
);
97 virtual OOXMLPropertySet::Pointer_t
getPropertySet() const;
99 virtual void setToken(Token_t nToken
);
100 virtual Token_t
getToken() const;
102 void resolveFootnote(const sal_Int32 nId
);
103 void resolveEndnote(const sal_Int32 nId
);
104 void resolveComment(const sal_Int32 nId
);
105 void resolvePicture(const OUString
& rId
);
106 void resolveHeader(const sal_Int32 type
,
107 const OUString
& rId
);
108 void resolveFooter(const sal_Int32 type
,
109 const OUString
& rId
);
110 void resolveData(const OUString
& rId
);
112 OUString
getTargetForId(const OUString
& rId
);
114 void setDocument(OOXMLDocumentImpl
* pDocument
);
115 OOXMLDocumentImpl
* getDocument();
116 void setXNoteId(const OOXMLValue::Pointer_t
& pValue
);
117 void setXNoteId(const sal_Int32 nId
);
118 sal_Int32
getXNoteId() const;
119 void setForwardEvents(bool bForwardEvents
);
120 bool isForwardEvents() const;
121 virtual void setId(Id nId
);
122 virtual Id
getId() const;
124 void setDefine(Id nDefine
);
125 Id
getDefine() const { return mnDefine
;}
127 const OOXMLParserState::Pointer_t
& getParserState() const { return mpParserState
;}
129 void sendTableDepth() const;
132 void startSectionGroup();
133 void setLastParagraphInSection();
134 void setLastSectionGroup();
135 void endSectionGroup();
136 void startParagraphGroup();
137 void endParagraphGroup();
138 void startCharacterGroup();
139 void endCharacterGroup();
140 virtual void pushBiDiEmbedLevel();
141 virtual void popBiDiEmbedLevel();
146 void fieldSeparator();
156 void noBreakHyphen();
158 void handleLastParagraphInSection();
159 void endOfParagraph();
160 void text(const OUString
& sText
);
161 void positionOffset(const OUString
& sText
);
162 static void ignore();
163 void alignH(const OUString
& sText
);
164 void alignV(const OUString
& sText
);
165 void positivePercentage(const OUString
& rText
);
166 void startGlossaryEntry();
167 void endGlossaryEntry();
168 void startTxbxContent();
169 void endTxbxContent();
170 void propagateCharacterProperties();
171 void propagateTableProperties();
172 void propagateRowProperties();
173 void propagateCellProperties();
174 void sendPropertiesWithId(Id nId
);
175 void sendPropertiesToParent();
176 void sendCellProperties();
177 void sendRowProperties();
178 void sendTableProperties();
179 void clearTableProps();
182 virtual void setDefaultBooleanValue();
183 virtual void setDefaultIntegerValue();
184 virtual void setDefaultHexValue();
185 virtual void setDefaultStringValue();
187 void sendPropertyToParent();
188 OOXMLFastContextHandler
* getParent() const { return mpParent
; }
189 void setGridAfter(const OOXMLValue::Pointer_t
& pGridAfter
) { mpGridAfter
= pGridAfter
; }
192 OOXMLFastContextHandler
* mpParent
;
197 // the formula insertion mode: inline/newline(left, center, right)
198 sal_Int8 mnMathJcVal
;
202 INLINE
, //The equation is anchored as inline to the text
203 CENTER
, //The equation is center aligned
204 LEFT
, //The equation is left aligned
205 RIGHT
//The equation is right aligned
207 // the stream to send the stream events to.
210 // the current global parser state
211 OOXMLParserState::Pointer_t mpParserState
;
213 // the table depth of this context
214 unsigned int mnTableDepth
;
216 /// @throws css::uno::RuntimeException
217 /// @throws css::xml::sax::SAXException
218 virtual void lcl_startFastElement(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_endFastElement(Token_t Element
);
224 /// @throws css::uno::RuntimeException
225 /// @throws css::xml::sax::SAXException
226 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> lcl_createFastChildContext(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
);
228 /// @throws css::uno::RuntimeException
229 /// @throws css::xml::sax::SAXException
230 virtual void lcl_characters(const OUString
& aChars
);
235 const css::uno::Reference
< css::uno::XComponentContext
>& getComponentContext() const { return m_xContext
;}
238 bool mbAllowInCell
; // o:allowincell
240 OOXMLValue::Pointer_t mpGridAfter
;
243 void operator =(OOXMLFastContextHandler
const &) = delete;
244 /// Handles AlternateContent. Returns true, if children of the current element should be ignored.
245 bool prepareMceContext(Token_t nElement
, const css::uno::Reference
<css::xml::sax::XFastAttributeList
>& Attribs
);
247 // 2.10 of XML 1.0 specification
248 bool IsPreserveSpace() const;
250 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
251 bool m_bDiscardChildren
;
252 bool m_bTookChoice
; ///< Did we take the Choice or want Fallback instead?
253 bool mbPreserveSpace
= false;
254 bool mbPreserveSpaceSet
= false;
258 class OOXMLFastContextHandlerStream
: public OOXMLFastContextHandler
261 explicit OOXMLFastContextHandlerStream(OOXMLFastContextHandler
* pContext
);
262 virtual ~OOXMLFastContextHandlerStream() override
;
264 virtual ResourceEnum_t
getResource() const override
{ return STREAM
; }
266 const OOXMLPropertySet::Pointer_t
& getPropertySetAttrs() const { return mpPropertySetAttrs
;}
268 virtual void newProperty(Id aId
, const OOXMLValue::Pointer_t
& pVal
) override
;
269 void sendProperty(Id nId
);
270 virtual OOXMLPropertySet::Pointer_t
getPropertySet() const override
;
272 void handleHyperlink();
275 mutable OOXMLPropertySet::Pointer_t mpPropertySetAttrs
;
278 class OOXMLFastContextHandlerProperties
: public OOXMLFastContextHandler
281 explicit OOXMLFastContextHandlerProperties(OOXMLFastContextHandler
* pContext
);
282 virtual ~OOXMLFastContextHandlerProperties() override
;
284 virtual OOXMLValue::Pointer_t
getValue() const override
;
285 virtual ResourceEnum_t
getResource() const override
{ return PROPERTIES
; }
287 virtual void newProperty(Id nId
, const OOXMLValue::Pointer_t
& pVal
) override
;
291 void handleComment();
292 void handlePicture();
294 void handleOutOfOrderBreak();
296 void handleFontRel();
297 void handleHyperlinkURL();
298 void handleAltChunk();
300 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t
& pPropertySet
) override
;
301 virtual OOXMLPropertySet::Pointer_t
getPropertySet() const override
;
305 OOXMLPropertySet::Pointer_t mpPropertySet
;
307 virtual void lcl_endFastElement(Token_t Element
) override
;
314 class OOXMLFastContextHandlerPropertyTable
:
315 public OOXMLFastContextHandlerProperties
318 explicit OOXMLFastContextHandlerPropertyTable(OOXMLFastContextHandler
* pContext
);
319 virtual ~OOXMLFastContextHandlerPropertyTable() override
;
324 virtual void lcl_endFastElement(Token_t Element
) override
;
327 class OOXMLFastContextHandlerValue
:
328 public OOXMLFastContextHandler
331 explicit OOXMLFastContextHandlerValue(OOXMLFastContextHandler
* pContext
);
332 virtual ~OOXMLFastContextHandlerValue() override
;
334 void setValue(const OOXMLValue::Pointer_t
& pValue
);
335 virtual OOXMLValue::Pointer_t
getValue() const override
;
337 virtual void lcl_endFastElement(Token_t Element
) override
;
339 virtual std::string
getType() const override
{ return "Value"; }
341 virtual void setDefaultBooleanValue() override
;
342 virtual void setDefaultIntegerValue() override
;
343 virtual void setDefaultHexValue() override
;
344 virtual void setDefaultStringValue() override
;
346 virtual void pushBiDiEmbedLevel() override
;
347 virtual void popBiDiEmbedLevel() override
;
349 void handleGridAfter();
352 OOXMLValue::Pointer_t mpValue
;
355 class OOXMLFastContextHandlerTable
: public OOXMLFastContextHandler
358 explicit OOXMLFastContextHandlerTable(OOXMLFastContextHandler
* pContext
);
359 virtual ~OOXMLFastContextHandlerTable() override
;
361 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext (sal_Int32 Element
,
362 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
367 css::uno::Reference
<css::xml::sax::XFastContextHandler
> mCurrentChild
;
369 virtual void lcl_endFastElement(Token_t Element
) override
;
371 virtual ResourceEnum_t
getResource() const override
{ return TABLE
; }
373 virtual std::string
getType() const override
{ return "Table"; }
375 void addCurrentChild();
378 class OOXMLFastContextHandlerXNote
: public OOXMLFastContextHandlerProperties
381 explicit OOXMLFastContextHandlerXNote(OOXMLFastContextHandler
* pContext
);
382 virtual ~OOXMLFastContextHandlerXNote() override
;
384 void checkId(const OOXMLValue::Pointer_t
& pValue
);
386 void checkType(const OOXMLValue::Pointer_t
& pValue
);
388 virtual std::string
getType() const override
{ return "XNote"; }
391 bool mbForwardEventsSaved
;
392 sal_Int32 mnMyXNoteId
;
393 sal_Int32 mnMyXNoteType
;
395 virtual void lcl_startFastElement(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
397 virtual void lcl_endFastElement(Token_t Element
) override
;
399 virtual ResourceEnum_t
getResource() const override
{ return STREAM
; }
402 class OOXMLFastContextHandlerTextTableCell
: public OOXMLFastContextHandler
405 explicit OOXMLFastContextHandlerTextTableCell(OOXMLFastContextHandler
* pContext
);
406 virtual ~OOXMLFastContextHandlerTextTableCell() override
;
408 virtual std::string
getType() const override
{ return "TextTableCell"; }
414 class OOXMLFastContextHandlerTextTableRow
: public OOXMLFastContextHandler
417 explicit OOXMLFastContextHandlerTextTableRow(OOXMLFastContextHandler
* pContext
);
418 virtual ~OOXMLFastContextHandlerTextTableRow() override
;
420 virtual std::string
getType() const override
{ return "TextTableRow"; }
422 static void startRow();
424 void handleGridBefore( const OOXMLValue::Pointer_t
& val
);
427 class OOXMLFastContextHandlerTextTable
: public OOXMLFastContextHandler
430 explicit OOXMLFastContextHandlerTextTable(OOXMLFastContextHandler
* pContext
);
432 virtual ~OOXMLFastContextHandlerTextTable() override
;
434 virtual std::string
getType() const override
{ return "TextTable"; }
437 // when <w:tbl> appears as direct child of <w:p>, we need to rearrange this paragraph
438 // to merge with the table's first paragraph (that's what Word does in this case)
441 virtual void lcl_startFastElement(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
443 virtual void lcl_endFastElement(Token_t Element
) override
;
446 class OOXMLFastContextHandlerShape
: public OOXMLFastContextHandlerProperties
449 bool m_bShapeStarted
;
450 /// Is it necessary to pop the stack in the dtor?
451 bool m_bShapeContextPushed
;
452 css::uno::Reference
<css::xml::sax::XFastShapeContextHandler
> mrShapeContext
;
455 explicit OOXMLFastContextHandlerShape(OOXMLFastContextHandler
* pContext
);
456 virtual ~OOXMLFastContextHandlerShape() override
;
458 virtual std::string
getType() const override
{ return "Shape"; }
460 // css::xml::sax::XFastContextHandler:
461 virtual void SAL_CALL
startUnknownElement (const OUString
& Namespace
, const OUString
& Name
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
463 virtual void SAL_CALL
endUnknownElement(const OUString
& Namespace
, const OUString
& Name
) override
;
465 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext(const OUString
& Namespace
, const OUString
& Name
,
466 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
468 virtual void setToken(Token_t nToken
) override
;
470 virtual ResourceEnum_t
getResource() const override
{ return SHAPE
; }
472 void sendShape( Token_t Element
);
473 bool isShapeSent( ) const { return m_bShapeSent
; }
476 virtual void lcl_startFastElement(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
478 virtual void lcl_endFastElement(Token_t Element
) override
;
480 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> lcl_createFastChildContext (Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
482 virtual void lcl_characters(const OUString
& aChars
) override
;
487 OOXMLFastContextHandlerWrapper wraps an OOXMLFastContextHandler.
489 The method calls for the interface css::xml::sax::XFastContextHandler are
490 forwarded to the wrapped OOXMLFastContextHandler.
492 class OOXMLFastContextHandlerWrapper
: public OOXMLFastContextHandler
495 OOXMLFastContextHandlerWrapper(OOXMLFastContextHandler
* pParent
,
496 css::uno::Reference
<css::xml::sax::XFastContextHandler
> const & xContext
,
497 rtl::Reference
<OOXMLFastContextHandlerShape
> const & xShapeHandler
);
498 virtual ~OOXMLFastContextHandlerWrapper() override
;
500 // css::xml::sax::XFastContextHandler:
501 virtual void SAL_CALL
startUnknownElement(const OUString
& Namespace
, const OUString
& Name
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
503 virtual void SAL_CALL
endUnknownElement(const OUString
& Namespace
, const OUString
& Name
) override
;
505 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext (const OUString
& Namespace
, const OUString
& Name
,
506 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
508 virtual void attributes(const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
510 virtual ResourceEnum_t
getResource() const override
;
512 void addNamespace(Id nId
);
513 void addToken( Token_t Element
);
515 virtual void newProperty(Id nId
, const OOXMLValue::Pointer_t
& pVal
) override
;
516 virtual void setPropertySet(const OOXMLPropertySet::Pointer_t
& pPropertySet
) override
;
517 virtual OOXMLPropertySet::Pointer_t
getPropertySet() const override
;
519 virtual std::string
getType() const override
;
522 virtual void lcl_startFastElement(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
524 virtual void lcl_endFastElement(Token_t Element
) override
;
526 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> lcl_createFastChildContext(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
528 virtual void lcl_characters(const OUString
& aChars
) override
;
530 virtual void setId(Id nId
) override
;
531 virtual Id
getId() const override
;
533 virtual void setToken(Token_t nToken
) override
;
534 virtual Token_t
getToken() const override
;
537 css::uno::Reference
<css::xml::sax::XFastContextHandler
> mxWrappedContext
;
538 rtl::Reference
<OOXMLFastContextHandlerShape
> mxShapeHandler
;
539 std::set
<Id
> mMyNamespaces
;
540 std::set
<Token_t
> mMyTokens
;
541 OOXMLPropertySet::Pointer_t mpPropertySet
;
543 OOXMLFastContextHandler
* getFastContextHandler() const;
547 A class that converts from XFastParser/XFastContextHandler usage to a liner XML stream of data.
549 The purpose of this class is to convert the rather complex XFastContextHandler-based XML
550 processing that requires context subclasses, callbacks, etc. into a linear stream of XML tokens
551 that can be handled simply by reading the tokens one by one and directly processing them.
552 See the oox::formulaimport::XmlStream class documentation for more information.
554 Usage: Create a subclass of OOXMLFastContextHandlerLinear, reimplemented getType() to provide
555 type of the subclass and process() to actually process the XML stream. Also make sure to
556 add a line like the following to model.xml (for class OOXMLFastContextHandlerMath):
558 <resource name="CT_OMath" resource="Math"/>
562 class OOXMLFastContextHandlerLinear
: public OOXMLFastContextHandlerProperties
565 explicit OOXMLFastContextHandlerLinear(OOXMLFastContextHandler
* pContext
);
567 Return the type of the class, as written in model.xml .
569 virtual std::string
getType() const override
= 0;
573 Called when the tokens for the element, its content and sub-elements have been linearized
574 and should be processed. The data member @ref buffer contains the converted data.
576 virtual void process() = 0;
578 virtual void lcl_startFastElement(Token_t Element
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
580 virtual void lcl_endFastElement(Token_t Element
) override
;
582 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> lcl_createFastChildContext(Token_t Element
,
583 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & Attribs
) override
;
585 virtual void lcl_characters(const OUString
& aChars
) override
;
587 // should be private, but not much point in making deep copies of it
588 oox::formulaimport::XmlStreamBuilder buffer
;
594 class OOXMLFastContextHandlerMath
: public OOXMLFastContextHandlerLinear
597 explicit OOXMLFastContextHandlerMath(OOXMLFastContextHandler
* pContext
);
598 virtual std::string
getType() const override
{ return "Math"; }
600 virtual void process() override
;
604 #endif // INCLUDED_WRITERFILTER_SOURCE_OOXML_OOXMLFASTCONTEXTHANDLER_HXX
606 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */