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