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_OOXML_FAST_CONTEXT_HANDLER_HXX
25 #define INCLUDED_OOXML_FAST_CONTEXT_HANDLER_HXX
26 
27 #include <com/sun/star/xml/sax/XFastShapeContextHandler.hpp>
28 
29 #include <string>
30 #include <set>
31 #include "sal/config.h"
32 #include "com/sun/star/uno/XComponentContext.hpp"
33 #include "cppuhelper/implbase1.hxx"
34 #include "com/sun/star/xml/sax/XFastContextHandler.hpp"
35 #include "OOXMLParserState.hxx"
36 #include "OOXMLPropertySetImpl.hxx"
37 #include "OOXMLDocumentImpl.hxx"
38 #include "RefAndPointer.hxx"
39 #include <ooxml/OOXMLFastTokens.hxx>
40 
41 namespace writerfilter {
42 namespace ooxml
43 {
44 using namespace ::std;
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::xml::sax;
47 
48 typedef boost::shared_ptr<Stream> StreamPointer_t;
49 
50 class OOXMLFastContextHandler:
51     public ::cppu::WeakImplHelper1<
52         xml::sax::XFastContextHandler>
53 {
54 public:
55     typedef RefAndPointer<XFastContextHandler, OOXMLFastContextHandler>
56     RefAndPointer_t;
57     typedef boost::shared_ptr<OOXMLFastContextHandler> Pointer_t;
58 
59     enum ResourceEnum_t { UNKNOWN, STREAM, PROPERTIES, TABLE, SHAPE };
60 
61     OOXMLFastContextHandler();
62 
63     explicit OOXMLFastContextHandler(
64         uno::Reference< uno::XComponentContext > const & context );
65 
66     explicit OOXMLFastContextHandler(
67         OOXMLFastContextHandler * pContext );
68 
69     virtual ~OOXMLFastContextHandler();
70 
71     // ::com::sun::star::xml::sax::XFastContextHandler:
72     virtual void SAL_CALL startFastElement
73     (sal_Int32 Element,
74      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
75         throw (uno::RuntimeException, xml::sax::SAXException);
76 
77     virtual void SAL_CALL startUnknownElement
78     (const ::rtl::OUString & Namespace,
79      const ::rtl::OUString & Name,
80      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
81         throw (uno::RuntimeException, xml::sax::SAXException);
82 
83     virtual void SAL_CALL endFastElement(sal_Int32 Element)
84         throw (uno::RuntimeException, xml::sax::SAXException);
85 
86     virtual void SAL_CALL endUnknownElement
87     (const ::rtl::OUString & Namespace, const ::rtl::OUString & Name)
88         throw (uno::RuntimeException, xml::sax::SAXException);
89 
90     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
91     createFastChildContext
92     (sal_Int32 Element,
93      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
94         throw (uno::RuntimeException, xml::sax::SAXException);
95 
96     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
97     createUnknownChildContext
98     (const ::rtl::OUString & Namespace,
99      const ::rtl::OUString & Name,
100      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
101         throw (uno::RuntimeException, xml::sax::SAXException);
102 
103     virtual void SAL_CALL characters(const ::rtl::OUString & aChars)
104         throw (uno::RuntimeException, xml::sax::SAXException);
105 
106     static const uno::Sequence< sal_Int8 > & getUnoTunnelId();
107     virtual sal_Int64 SAL_CALL getSomething(const uno::Sequence<sal_Int8> & rId)
108         throw (uno::RuntimeException);
109 
110     // local
111 
112     void setStream(Stream * pStream);
113 
114     /**
115        Return value of this context(element).
116 
117        @return  the value
118      */
119     virtual OOXMLValue::Pointer_t getValue() const;
120 
121     /**
122        Returns a string describing the type of the context.
123 
124        This is the name of the define normally.
125 
126        @return type string
127      */
getType() const128     virtual string getType() const { return "??"; }
129 
getResource() const130     virtual ResourceEnum_t getResource() const { return STREAM; }
131 
132     virtual void attributes
133     (const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
134         throw (uno::RuntimeException, xml::sax::SAXException);
135 
136     virtual void newProperty(const Id & rId, OOXMLValue::Pointer_t pVal);
137     virtual void setPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
138     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
139 
140     virtual void setToken(Token_t nToken);
141     virtual Token_t getToken() const;
142 
143     void mark(const Id & rId, OOXMLValue::Pointer_t pVal);
144 
145     void resolveFootnote( const sal_Int32 nIDForXNoteStream );
146     void resolveEndnote( const sal_Int32 nIDForXNoteStream );
147     void resolveComment( const sal_Int32 nIDForXNoteStream );
148 
149     void resolvePicture(const rtl::OUString & rId);
150     void resolveHeader(const sal_Int32 type,
151                                 const rtl::OUString & rId);
152     void resolveFooter(const sal_Int32 type,
153                                 const rtl::OUString & rId);
154     void resolveOLE(const rtl::OUString & rId);
155 
156     ::rtl::OUString getTargetForId(const ::rtl::OUString & rId);
157 
158     uno::Reference < xml::sax::XFastContextHandler >
159     createFromStart
160     (sal_uInt32 Element,
161      const uno::Reference< xml::sax::XFastAttributeList > & Attribs);
162 
163     void setDocument(OOXMLDocument * pDocument);
164     OOXMLDocument * getDocument();
165 
166     void setIDForXNoteStream(OOXMLValue::Pointer_t pValue);
167 
168     void setForwardEvents(bool bForwardEvents);
169     bool isForwardEvents() const;
170     virtual void setParent(OOXMLFastContextHandler * pParent);
171     virtual void setId(Id nId);
172     virtual Id getId() const;
173 
174     void setDefine(Id nDefine);
175     Id getDefine() const;
176 
177     OOXMLParserState::Pointer_t getParserState() const;
178 
179     void sendTableDepth() const;
180     void setHandle();
181 
182     void startSectionGroup();
183     void setLastParagraphInSection();
184     void endSectionGroup();
185     void startParagraphGroup();
186     void endParagraphGroup();
187     void startCharacterGroup();
188     void endCharacterGroup();
189 
190     void startField();
191     void fieldSeparator();
192     void endField();
193     void ftnednref();
194     void ftnedncont();
195     void ftnednsep();
196     void pgNum();
197     void tab();
198     void cr();
199     void noBreakHyphen();
200     void softHyphen();
201     void handleLastParagraphInSection();
202     void endOfParagraph();
203     void text(const ::rtl::OUString & sText);
204     virtual void propagateCharacterProperties();
205     virtual void propagateCharacterPropertiesAsSet(const Id & rId);
206     virtual void propagateTableProperties();
207     virtual void propagateRowProperties();
208     virtual void propagateCellProperties();
209     virtual bool propagatesProperties() const;
210     void sendPropertiesWithId(const Id & rId);
211     void sendPropertiesToParent();
212     void sendCellProperties();
213     void sendRowProperties();
214     void sendTableProperties();
215     void clearTableProps();
216     void clearProps();
217 
218     virtual void setDefaultBooleanValue();
219     virtual void setDefaultIntegerValue();
220     virtual void setDefaultHexValue();
221     virtual void setDefaultStringValue();
222 
223     void sendPropertyToParent();
224 
225 #ifdef DEBUG
226     static XMLTag::Pointer_t toPropertiesTag(OOXMLPropertySet::Pointer_t);
227     virtual XMLTag::Pointer_t toTag() const;
228     virtual string toString() const;
229 #endif
230 
231 protected:
232     OOXMLFastContextHandler * mpParent;
233     Id mId;
234     Id mnDefine;
235     Token_t mnToken;
236 
237     // the stream to send the stream events to.
238     Stream * mpStream;
239 
240     // the current global parser state
241     OOXMLParserState::Pointer_t mpParserState;
242 
243     // the table depth of this context
244     unsigned int mnTableDepth;
245 
246     virtual void lcl_startFastElement
247     (sal_Int32 Element,
248      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
249         throw (uno::RuntimeException, xml::sax::SAXException);
250 
251     virtual void lcl_endFastElement(sal_Int32 Element)
252         throw (uno::RuntimeException, xml::sax::SAXException);
253 
254     virtual uno::Reference< xml::sax::XFastContextHandler >
255     lcl_createFastChildContext
256     (sal_Int32 Element,
257      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
258         throw (uno::RuntimeException, xml::sax::SAXException);
259 
260     virtual void lcl_characters(const ::rtl::OUString & aChars)
261         throw (uno::RuntimeException, xml::sax::SAXException);
262 
263     void startAction(sal_Int32 Element);
264     virtual void lcl_startAction(sal_Int32 Element);
265     void endAction(sal_Int32 Element);
266     virtual void lcl_endAction(sal_Int32 Element);
267 
268 
269     // Returns string for resource of this context. (debug)
270     string getResourceString() const;
271 
272     virtual OOXMLPropertySet * getPicturePropSet
273     (const ::rtl::OUString & rId);
274     virtual void resolvePropertySetAttrs();
275 
276     uno::Reference< uno::XComponentContext > getComponentContext();
277 
278     sal_uInt32 mnInstanceNumber;
279     sal_uInt32 mnRefCount;
280 private:
281     void operator =(OOXMLFastContextHandler &); // not defined
282 
283     uno::Reference< uno::XComponentContext > m_xContext;
284 
285     static sal_uInt32 mnInstanceCount;
286 
287 };
288 
289 class OOXMLFastContextHandlerStream : public OOXMLFastContextHandler
290 {
291 public:
292     OOXMLFastContextHandlerStream(OOXMLFastContextHandler * pContext);
293     virtual ~OOXMLFastContextHandlerStream();
294 
getResource() const295     virtual ResourceEnum_t getResource() const { return STREAM; }
296 
297     OOXMLPropertySet::Pointer_t getPropertySetAttrs() const;
298 
299     virtual void newProperty(const Id & rId, OOXMLValue::Pointer_t pVal);
300     virtual void sendProperty(Id nId);
301     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
302 
303     void handleHyperlink();
304 
305 protected:
306     virtual void resolvePropertySetAttrs();
307 
308 private:
309     mutable OOXMLPropertySet::Pointer_t mpPropertySetAttrs;
310 };
311 
312 class OOXMLFastContextHandlerProperties : public OOXMLFastContextHandler
313 {
314 public:
315     OOXMLFastContextHandlerProperties(OOXMLFastContextHandler * pContext);
316     virtual ~OOXMLFastContextHandlerProperties();
317 
318     virtual OOXMLValue::Pointer_t getValue() const;
getResource() const319     virtual ResourceEnum_t getResource() const { return PROPERTIES; }
320 
321     virtual void newProperty(const Id & nId, OOXMLValue::Pointer_t pVal);
322 
323     void handleXNotes();
324     void handleHdrFtr();
325     void handleComment();
326     void handlePicture();
327     void handleBreak();
328     void handleOLE();
329 
330     virtual void setPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
331     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
332 
333 #ifdef DEBUG
334     virtual XMLTag::Pointer_t toTag() const;
335 #endif
336 
337 protected:
338     /// the properties
339     OOXMLPropertySet::Pointer_t mpPropertySet;
340 
341     virtual void lcl_endFastElement(sal_Int32 Element)
342         throw (uno::RuntimeException, xml::sax::SAXException);
343     virtual void setParent(OOXMLFastContextHandler * pParent);
344 
345 private:
346 
347     bool mbResolve;
348 };
349 
350 class OOXMLFastContextHandlerPropertyTable :
351     public OOXMLFastContextHandlerProperties
352 {
353 public:
354     OOXMLFastContextHandlerPropertyTable(OOXMLFastContextHandler * pContext);
355     virtual ~OOXMLFastContextHandlerPropertyTable();
356 
357 protected:
358     OOXMLTableImpl mTable;
359 
360     virtual void lcl_endFastElement(sal_Int32 Element)
361         throw (uno::RuntimeException, xml::sax::SAXException);
362  };
363 
364 class OOXMLFastContextHandlerValue :
365     public OOXMLFastContextHandler
366 {
367 public:
368     OOXMLFastContextHandlerValue
369     (OOXMLFastContextHandler * pContext);
370     virtual ~OOXMLFastContextHandlerValue();
371 
372     virtual void setValue(OOXMLValue::Pointer_t pValue);
373     virtual OOXMLValue::Pointer_t getValue() const;
374 
375     virtual void lcl_endFastElement(sal_Int32 Element)
376     throw (uno::RuntimeException, xml::sax::SAXException);
377 
getType() const378     virtual string getType() const { return "Value"; }
379 
380     virtual void setDefaultBooleanValue();
381     virtual void setDefaultIntegerValue();
382     virtual void setDefaultHexValue();
383     virtual void setDefaultStringValue();
384 
385 protected:
386     OOXMLValue::Pointer_t mpValue;
387 };
388 
389 class OOXMLFastContextHandlerTable : public OOXMLFastContextHandler
390 {
391 public:
392     OOXMLFastContextHandlerTable(OOXMLFastContextHandler * pContext);
393     virtual ~OOXMLFastContextHandlerTable();
394 
395     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
396     createFastChildContext
397     (sal_Int32 Element,
398      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
399         throw (uno::RuntimeException, xml::sax::SAXException);
400 
401     virtual void newPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
402 protected:
403     OOXMLTableImpl mTable;
404 
405     RefAndPointer_t mCurrentChild;
406 
407     virtual void lcl_endFastElement(sal_Int32 Element)
408         throw (uno::RuntimeException, xml::sax::SAXException);
409 
getResource() const410     virtual ResourceEnum_t getResource() const { return TABLE; }
411 
getType() const412     virtual string getType() const { return "Table"; }
413 
414     void addCurrentChild();
415 };
416 
417 class OOXMLFastContextHandlerXNote : public OOXMLFastContextHandlerProperties
418 {
419 public:
420     OOXMLFastContextHandlerXNote(OOXMLFastContextHandler * pContext);
421     virtual ~OOXMLFastContextHandlerXNote();
422 
423     void checkId(OOXMLValue::Pointer_t pValue);
424 
getType() const425     virtual string getType() const { return "XNote"; }
426 
427 private:
428     bool mbForwardEventsSaved;
429     sal_Int32 mnMyXNoteId;
430 
431     virtual void lcl_startFastElement
432     (sal_Int32 Element,
433      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
434         throw (uno::RuntimeException, xml::sax::SAXException);
435 
436     virtual void lcl_endFastElement(sal_Int32 Element)
437         throw (uno::RuntimeException, xml::sax::SAXException);
438 
getResource() const439     virtual ResourceEnum_t getResource() const { return STREAM; }
440 };
441 
442 class OOXMLFastContextHandlerTextTableCell : public OOXMLFastContextHandler
443 {
444 public:
445     OOXMLFastContextHandlerTextTableCell
446     (OOXMLFastContextHandler * pContext);
447     virtual ~OOXMLFastContextHandlerTextTableCell();
448 
getType() const449     virtual string getType() const { return "TextTableCell"; }
450 
451     void startCell();
452     void endCell();
453 };
454 
455 class OOXMLFastContextHandlerTextTableRow : public OOXMLFastContextHandler
456 {
457 public:
458     OOXMLFastContextHandlerTextTableRow
459     (OOXMLFastContextHandler * pContext);
460     virtual ~OOXMLFastContextHandlerTextTableRow();
461 
getType() const462     virtual string getType() const { return "TextTableRow"; }
463 
464     void startRow();
465     void endRow();
466 };
467 
468 class OOXMLFastContextHandlerTextTable : public OOXMLFastContextHandler
469 {
470 public:
471     OOXMLFastContextHandlerTextTable
472     (OOXMLFastContextHandler * pContext);
473 
474     virtual ~OOXMLFastContextHandlerTextTable();
475 
getType() const476     virtual string getType() const { return "TextTable"; }
477 
478 protected:
479     virtual void lcl_startFastElement
480     (sal_Int32 Element,
481      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
482         throw (uno::RuntimeException, xml::sax::SAXException);
483 
484     virtual void lcl_endFastElement(sal_Int32 Element)
485         throw (uno::RuntimeException, xml::sax::SAXException);
486 };
487 
488 class OOXMLFastContextHandlerShape: public OOXMLFastContextHandlerProperties
489 {
490 private:
491     bool m_bShapeSent;
492     bool m_bShapeStarted;
493 
494 public:
495     explicit OOXMLFastContextHandlerShape
496     (OOXMLFastContextHandler * pContext);
497     virtual ~OOXMLFastContextHandlerShape();
498 
getType() const499     virtual string getType() const { return "Shape"; }
500 
501     // ::com::sun::star::xml::sax::XFastContextHandler:
502     virtual void SAL_CALL startUnknownElement
503     (const ::rtl::OUString & Namespace,
504      const ::rtl::OUString & Name,
505      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
506         throw (uno::RuntimeException, xml::sax::SAXException);
507 
508     virtual void SAL_CALL endUnknownElement
509     (const ::rtl::OUString & Namespace,
510      const ::rtl::OUString & Name)
511         throw (uno::RuntimeException, xml::sax::SAXException);
512 
513     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
514     createUnknownChildContext
515     (const ::rtl::OUString & Namespace,
516      const ::rtl::OUString & Name,
517      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
518         throw (uno::RuntimeException, xml::sax::SAXException);
519 
520     virtual void setToken(Token_t nToken);
521 
getResource() const522     virtual ResourceEnum_t getResource() const { return SHAPE; }
523 
524     void sendShape( Token_t Element );
525 
526 protected:
527     typedef uno::Reference<XFastShapeContextHandler> ShapeContextRef;
528     ShapeContextRef mrShapeContext;
529 
530     virtual void lcl_startFastElement
531     (sal_Int32 Element,
532      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
533         throw (uno::RuntimeException, xml::sax::SAXException);
534 
535     virtual void lcl_endFastElement(sal_Int32 Element)
536         throw (uno::RuntimeException, xml::sax::SAXException);
537 
538     virtual uno::Reference< xml::sax::XFastContextHandler >
539     lcl_createFastChildContext
540     (sal_Int32 Element,
541      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
542         throw (uno::RuntimeException, xml::sax::SAXException);
543 
544     virtual void lcl_characters(const ::rtl::OUString & aChars)
545                 throw (uno::RuntimeException, xml::sax::SAXException);
546 
547 };
548 
549 /**
550    OOXMLFastContextHandlerWrapper wraps an OOXMLFastContextHandler.
551 
552    The method calls for the interface
553    ::com::sun::star::xml::sax::XFastContextHandler are forwarded to the wrapped
554    OOXMLFastContextHandler.
555  */
556 class OOXMLFastContextHandlerWrapper : public OOXMLFastContextHandler
557 {
558 public:
559     explicit OOXMLFastContextHandlerWrapper
560     (OOXMLFastContextHandler * pParent,
561      uno::Reference<XFastContextHandler>  xContext);
562     virtual ~OOXMLFastContextHandlerWrapper();
563 
564     // ::com::sun::star::xml::sax::XFastContextHandler:
565     virtual void SAL_CALL startUnknownElement
566     (const ::rtl::OUString & Namespace,
567      const ::rtl::OUString & Name,
568      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
569         throw (uno::RuntimeException, xml::sax::SAXException);
570 
571     virtual void SAL_CALL endUnknownElement
572     (const ::rtl::OUString & Namespace,
573      const ::rtl::OUString & Name)
574         throw (uno::RuntimeException, xml::sax::SAXException);
575 
576     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
577     createUnknownChildContext
578     (const ::rtl::OUString & Namespace,
579      const ::rtl::OUString & Name,
580      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
581         throw (uno::RuntimeException, xml::sax::SAXException);
582 
583     virtual void attributes
584     (const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
585     throw (uno::RuntimeException, xml::sax::SAXException);
586 
587     virtual ResourceEnum_t getResource() const;
588 
589     void addNamespace(const Id & nId);
590     void addToken( Token_t Element );
591 
592     virtual void newProperty(const Id & rId, OOXMLValue::Pointer_t pVal);
593     virtual void setPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
594     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
595 
596     virtual string getType() const;
597 
598 protected:
599     virtual void lcl_startFastElement
600     (sal_Int32 Element,
601      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
602         throw (uno::RuntimeException, xml::sax::SAXException);
603 
604     virtual void lcl_endFastElement(sal_Int32 Element)
605         throw (uno::RuntimeException, xml::sax::SAXException);
606 
607     virtual uno::Reference< xml::sax::XFastContextHandler >
608     lcl_createFastChildContext
609     (sal_Int32 Element,
610      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
611         throw (uno::RuntimeException, xml::sax::SAXException);
612 
613     virtual void lcl_characters(const ::rtl::OUString & aChars)
614                 throw (uno::RuntimeException, xml::sax::SAXException);
615 
616     virtual void setId(Id nId);
617     virtual Id getId() const;
618 
619     virtual void setToken(Token_t nToken);
620     virtual Token_t getToken() const;
621 
622 private:
623     uno::Reference<XFastContextHandler> mxContext;
624     set<Id> mMyNamespaces;
625     set<Token_t> mMyTokens;
626     OOXMLPropertySet::Pointer_t mpPropertySet;
627 
628     OOXMLFastContextHandler * getFastContextHandler() const;
629 };
630 }}
631 #endif // INCLUDED_OOXML_FAST_CONTEXT_HANDLER_HXX
632