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     (Token_t 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(Token_t 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     (Token_t 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     (Token_t 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 #ifdef DEBUG_CONTEXT_HANDLER
238     string msTokenString;
239 #endif
240 
241     // the stream to send the stream events to.
242     Stream * mpStream;
243 
244     // the current global parser state
245     OOXMLParserState::Pointer_t mpParserState;
246 
247     // the table depth of this context
248     unsigned int mnTableDepth;
249 
250     virtual void lcl_startFastElement
251     (Token_t Element,
252      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
253         throw (uno::RuntimeException, xml::sax::SAXException);
254 
255     virtual void lcl_endFastElement(Token_t Element)
256         throw (uno::RuntimeException, xml::sax::SAXException);
257 
258     virtual uno::Reference< xml::sax::XFastContextHandler >
259     lcl_createFastChildContext
260     (Token_t Element,
261      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
262         throw (uno::RuntimeException, xml::sax::SAXException);
263 
264     virtual void lcl_characters(const ::rtl::OUString & aChars)
265         throw (uno::RuntimeException, xml::sax::SAXException);
266 
267     void startAction(Token_t Element);
268     virtual void lcl_startAction(Token_t Element);
269     void endAction(Token_t Element);
270     virtual void lcl_endAction(Token_t Element);
271 
272 
273     // Returns string for resource of this context. (debug)
274     string getResourceString() const;
275 
276     virtual OOXMLPropertySet * getPicturePropSet
277     (const ::rtl::OUString & rId);
278     virtual void resolvePropertySetAttrs();
279 
280     uno::Reference< uno::XComponentContext > getComponentContext();
281 
282     sal_uInt32 mnInstanceNumber;
283     sal_uInt32 mnRefCount;
284 private:
285     void operator =(OOXMLFastContextHandler &); // not defined
286 
287     uno::Reference< uno::XComponentContext > m_xContext;
288 
289     static sal_uInt32 mnInstanceCount;
290 
291 };
292 
293 class OOXMLFastContextHandlerStream : public OOXMLFastContextHandler
294 {
295 public:
296     OOXMLFastContextHandlerStream(OOXMLFastContextHandler * pContext);
297     virtual ~OOXMLFastContextHandlerStream();
298 
getResource() const299     virtual ResourceEnum_t getResource() const { return STREAM; }
300 
301     OOXMLPropertySet::Pointer_t getPropertySetAttrs() const;
302 
303     virtual void newProperty(const Id & rId, OOXMLValue::Pointer_t pVal);
304     virtual void sendProperty(Id nId);
305     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
306 
307     void handleHyperlink();
308 
309 protected:
310     virtual void resolvePropertySetAttrs();
311     virtual void lcl_characters(const ::rtl::OUString & aChars)
312                 throw (uno::RuntimeException, xml::sax::SAXException);
313 
314 private:
315     mutable OOXMLPropertySet::Pointer_t mpPropertySetAttrs;
316 };
317 
318 class OOXMLFastContextHandlerProperties : public OOXMLFastContextHandler
319 {
320 public:
321     OOXMLFastContextHandlerProperties(OOXMLFastContextHandler * pContext);
322     virtual ~OOXMLFastContextHandlerProperties();
323 
324     virtual OOXMLValue::Pointer_t getValue() const;
getResource() const325     virtual ResourceEnum_t getResource() const { return PROPERTIES; }
326 
327     virtual void newProperty(const Id & nId, OOXMLValue::Pointer_t pVal);
328 
329     void handleXNotes();
330     void handleHdrFtr();
331     void handleComment();
332     void handlePicture();
333     void handleBreak();
334     void handleOLE();
335 
336     virtual void setPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
337     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
338 
339 #ifdef DEBUG
340     virtual XMLTag::Pointer_t toTag() const;
341 #endif
342 
343 protected:
344     /// the properties
345     OOXMLPropertySet::Pointer_t mpPropertySet;
346 
347     virtual void lcl_endFastElement(Token_t Element)
348         throw (uno::RuntimeException, xml::sax::SAXException);
349     virtual void setParent(OOXMLFastContextHandler * pParent);
350 
351 private:
352 
353     bool mbResolve;
354 };
355 
356 class OOXMLFastContextHandlerPropertyTable :
357     public OOXMLFastContextHandlerProperties
358 {
359 public:
360     OOXMLFastContextHandlerPropertyTable(OOXMLFastContextHandler * pContext);
361     virtual ~OOXMLFastContextHandlerPropertyTable();
362 
363 protected:
364     OOXMLTableImpl mTable;
365 
366     virtual void lcl_endFastElement(Token_t Element)
367         throw (uno::RuntimeException, xml::sax::SAXException);
368  };
369 
370 class OOXMLFastContextHandlerValue :
371     public OOXMLFastContextHandler
372 {
373 public:
374     OOXMLFastContextHandlerValue
375     (OOXMLFastContextHandler * pContext);
376     virtual ~OOXMLFastContextHandlerValue();
377 
378     virtual void setValue(OOXMLValue::Pointer_t pValue);
379     virtual OOXMLValue::Pointer_t getValue() const;
380 
381     virtual void lcl_endFastElement(Token_t Element)
382     throw (uno::RuntimeException, xml::sax::SAXException);
383 
getType() const384     virtual string getType() const { return "Value"; }
385 
386     virtual void setDefaultBooleanValue();
387     virtual void setDefaultIntegerValue();
388     virtual void setDefaultHexValue();
389     virtual void setDefaultStringValue();
390 
391 protected:
392     OOXMLValue::Pointer_t mpValue;
393 };
394 
395 class OOXMLFastContextHandlerTable : public OOXMLFastContextHandler
396 {
397 public:
398     OOXMLFastContextHandlerTable(OOXMLFastContextHandler * pContext);
399     virtual ~OOXMLFastContextHandlerTable();
400 
401     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
402     createFastChildContext
403     (Token_t Element,
404      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
405         throw (uno::RuntimeException, xml::sax::SAXException);
406 
407     virtual void newPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
408 protected:
409     OOXMLTableImpl mTable;
410 
411     RefAndPointer_t mCurrentChild;
412 
413     virtual void lcl_endFastElement(Token_t Element)
414         throw (uno::RuntimeException, xml::sax::SAXException);
415 
getResource() const416     virtual ResourceEnum_t getResource() const { return TABLE; }
417 
getType() const418     virtual string getType() const { return "Table"; }
419 
420     void addCurrentChild();
421 };
422 
423 class OOXMLFastContextHandlerXNote : public OOXMLFastContextHandlerProperties
424 {
425 public:
426     OOXMLFastContextHandlerXNote(OOXMLFastContextHandler * pContext);
427     virtual ~OOXMLFastContextHandlerXNote();
428 
429     void checkId(OOXMLValue::Pointer_t pValue);
430 
getType() const431     virtual string getType() const { return "XNote"; }
432 
433 private:
434     bool mbForwardEventsSaved;
435     sal_Int32 mnMyXNoteId;
436 
437     virtual void lcl_startFastElement
438     (Token_t Element,
439      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
440         throw (uno::RuntimeException, xml::sax::SAXException);
441 
442     virtual void lcl_endFastElement(Token_t Element)
443         throw (uno::RuntimeException, xml::sax::SAXException);
444 
getResource() const445     virtual ResourceEnum_t getResource() const { return STREAM; }
446 };
447 
448 class OOXMLFastContextHandlerTextTableCell : public OOXMLFastContextHandler
449 {
450 public:
451     OOXMLFastContextHandlerTextTableCell
452     (OOXMLFastContextHandler * pContext);
453     virtual ~OOXMLFastContextHandlerTextTableCell();
454 
getType() const455     virtual string getType() const { return "TextTableCell"; }
456 
457     void startCell();
458     void endCell();
459 };
460 
461 class OOXMLFastContextHandlerTextTableRow : public OOXMLFastContextHandler
462 {
463 public:
464     OOXMLFastContextHandlerTextTableRow
465     (OOXMLFastContextHandler * pContext);
466     virtual ~OOXMLFastContextHandlerTextTableRow();
467 
getType() const468     virtual string getType() const { return "TextTableRow"; }
469 
470     void startRow();
471     void endRow();
472 };
473 
474 class OOXMLFastContextHandlerTextTable : public OOXMLFastContextHandler
475 {
476 public:
477     OOXMLFastContextHandlerTextTable
478     (OOXMLFastContextHandler * pContext);
479 
480     virtual ~OOXMLFastContextHandlerTextTable();
481 
getType() const482     virtual string getType() const { return "TextTable"; }
483 
484 protected:
485     virtual void lcl_startFastElement
486     (Token_t Element,
487      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
488         throw (uno::RuntimeException, xml::sax::SAXException);
489 
490     virtual void lcl_endFastElement(Token_t Element)
491         throw (uno::RuntimeException, xml::sax::SAXException);
492 };
493 
494 class OOXMLFastContextHandlerShape: public OOXMLFastContextHandlerProperties
495 {
496 private:
497     bool m_bShapeSent;
498     bool m_bShapeStarted;
499 
500 public:
501     explicit OOXMLFastContextHandlerShape
502     (OOXMLFastContextHandler * pContext);
503     virtual ~OOXMLFastContextHandlerShape();
504 
getType() const505     virtual string getType() const { return "Shape"; }
506 
507     // ::com::sun::star::xml::sax::XFastContextHandler:
508     virtual void SAL_CALL startUnknownElement
509     (const ::rtl::OUString & Namespace,
510      const ::rtl::OUString & Name,
511      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
512         throw (uno::RuntimeException, xml::sax::SAXException);
513 
514     virtual void SAL_CALL endUnknownElement
515     (const ::rtl::OUString & Namespace,
516      const ::rtl::OUString & Name)
517         throw (uno::RuntimeException, xml::sax::SAXException);
518 
519     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
520     createUnknownChildContext
521     (const ::rtl::OUString & Namespace,
522      const ::rtl::OUString & Name,
523      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
524         throw (uno::RuntimeException, xml::sax::SAXException);
525 
526     virtual void setToken(Token_t nToken);
527 
getResource() const528     virtual ResourceEnum_t getResource() const { return SHAPE; }
529 
530     void sendShape( Token_t Element );
531 
532 protected:
533     typedef uno::Reference<XFastShapeContextHandler> ShapeContextRef;
534     ShapeContextRef mrShapeContext;
535 
536     virtual void lcl_startFastElement
537     (Token_t Element,
538      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
539         throw (uno::RuntimeException, xml::sax::SAXException);
540 
541     virtual void lcl_endFastElement(Token_t Element)
542         throw (uno::RuntimeException, xml::sax::SAXException);
543 
544     virtual uno::Reference< xml::sax::XFastContextHandler >
545     lcl_createFastChildContext
546     (Token_t Element,
547      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
548         throw (uno::RuntimeException, xml::sax::SAXException);
549 
550     virtual void lcl_characters(const ::rtl::OUString & aChars)
551                 throw (uno::RuntimeException, xml::sax::SAXException);
552 
553 };
554 
555 /**
556    OOXMLFastContextHandlerWrapper wraps an OOXMLFastContextHandler.
557 
558    The method calls for the interface
559    ::com::sun::star::xml::sax::XFastContextHandler are forwarded to the wrapped
560    OOXMLFastContextHandler.
561  */
562 class OOXMLFastContextHandlerWrapper : public OOXMLFastContextHandler
563 {
564 public:
565     explicit OOXMLFastContextHandlerWrapper
566     (OOXMLFastContextHandler * pParent,
567      uno::Reference<XFastContextHandler>  xContext);
568     virtual ~OOXMLFastContextHandlerWrapper();
569 
570     // ::com::sun::star::xml::sax::XFastContextHandler:
571     virtual void SAL_CALL startUnknownElement
572     (const ::rtl::OUString & Namespace,
573      const ::rtl::OUString & Name,
574      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
575         throw (uno::RuntimeException, xml::sax::SAXException);
576 
577     virtual void SAL_CALL endUnknownElement
578     (const ::rtl::OUString & Namespace,
579      const ::rtl::OUString & Name)
580         throw (uno::RuntimeException, xml::sax::SAXException);
581 
582     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
583     createUnknownChildContext
584     (const ::rtl::OUString & Namespace,
585      const ::rtl::OUString & Name,
586      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
587         throw (uno::RuntimeException, xml::sax::SAXException);
588 
589     virtual void attributes
590     (const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
591     throw (uno::RuntimeException, xml::sax::SAXException);
592 
593     virtual ResourceEnum_t getResource() const;
594 
595     void addNamespace(const Id & nId);
596     void addToken( Token_t Element );
597 
598     virtual void newProperty(const Id & rId, OOXMLValue::Pointer_t pVal);
599     virtual void setPropertySet(OOXMLPropertySet::Pointer_t pPropertySet);
600     virtual OOXMLPropertySet::Pointer_t getPropertySet() const;
601 
602     virtual string getType() const;
603 
604 protected:
605     virtual void lcl_startFastElement
606     (Token_t Element,
607      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
608         throw (uno::RuntimeException, xml::sax::SAXException);
609 
610     virtual void lcl_endFastElement(Token_t Element)
611         throw (uno::RuntimeException, xml::sax::SAXException);
612 
613     virtual uno::Reference< xml::sax::XFastContextHandler >
614     lcl_createFastChildContext
615     (Token_t Element,
616      const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
617         throw (uno::RuntimeException, xml::sax::SAXException);
618 
619     virtual void lcl_characters(const ::rtl::OUString & aChars)
620                 throw (uno::RuntimeException, xml::sax::SAXException);
621 
622     virtual void setId(Id nId);
623     virtual Id getId() const;
624 
625     virtual void setToken(Token_t nToken);
626     virtual Token_t getToken() const;
627 
628 private:
629     uno::Reference<XFastContextHandler> mxContext;
630     set<Id> mMyNamespaces;
631     set<Token_t> mMyTokens;
632     OOXMLPropertySet::Pointer_t mpPropertySet;
633 
634     OOXMLFastContextHandler * getFastContextHandler() const;
635 };
636 }}
637 #endif // INCLUDED_OOXML_FAST_CONTEXT_HANDLER_HXX
638