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_WW8_DOCUMENT_HXX
25 #define INCLUDED_WW8_DOCUMENT_HXX
26 
27 #include <boost/shared_ptr.hpp>
28 #include <sal/types.h>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <resourcemodel/SubSequence.hxx>
31 #include <com/sun/star/io/XInputStream.hpp>
32 #ifndef _COM_SUN_STAR_UNO_XCOMPONENTCONTEX_HPP_
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #endif
35 
36 #ifndef INCLUDED_WW8_RESOURCE_MODEL_HXX
37 #include <resourcemodel/WW8ResourceModel.hxx>
38 #endif
39 #include <resourcemodel/OutputWithDepth.hxx>
40 
41 #include <WriterFilterDllApi.hxx>
42 
43 namespace writerfilter {
44 namespace doctok {
45 
46 using namespace ::com::sun::star;
47 
48 /**
49    A stream containing a WW8 document.
50 
51    The content of the stream is a sequence of unsigned bytes. The
52    stream consists of substreams that are identified by string
53    identifiers.
54 */
55 class WRITERFILTER_DLLPUBLIC WW8Stream
56 {
57 public:
58     /**
59        Pointer to a WW8Stream.
60     */
61     typedef boost::shared_ptr<WW8Stream> Pointer_t;
62 
63     /**
64        Type for the content of the stream
65      */
66     typedef SubSequence<sal_uInt8> Sequence;
67 
68     virtual ~WW8Stream();
69 
70     /**
71        Returns pointer to a substream.
72 
73        @param rSid     identifier of substream to return
74 
75        @return    the substream
76      */
77     virtual Pointer_t getSubStream(const ::rtl::OUString & rSid) = 0;
78 
79     /**
80        Return a continious part of the stream.
81 
82        @param nOffset     offset in the stream where the part starts
83        @param nCount      length of the part (number of bytes)
84 
85        @return    sequence of unsigned bytes
86      */
87     virtual Sequence get(sal_uInt32 nOffset, sal_uInt32 nCount)
88         const = 0;
89 
90     // Returns the names of substreams contained in the stream
91     virtual string getSubStreamNames() const = 0;
92 
93     virtual uno::Sequence<rtl::OUString> getSubStreamUNames() const = 0;
94 
95     /**
96        Dumps content of stream to output.
97 
98        @param o     the target output
99     */
100     virtual void dump(OutputWithDepth<string> & o) const = 0;
101 
102     //virtual bool put(sal_uInt32 nOffset, const Sequence & rSeq) = 0;
103 };
104 
105 /**
106    A property.
107 
108 */
109 class WRITERFILTER_DLLPUBLIC WW8Property
110 {
111 public:
112     /**
113        Ponter to a property.
114     */
115     typedef boost::shared_ptr<WW8Property> Pointer_t;
116 
117     virtual ~WW8Property();
118 
119     virtual sal_uInt32 getId() const = 0;
120     virtual sal_uInt32 getParam() const = 0;
121     virtual WW8Stream::Sequence getParams() const = 0;
122 
123     virtual string toString() const = 0;
124 
125     /**
126        Dumps this object to an output.
127 
128     */
129     virtual void dump(OutputWithDepth<string> & o) const = 0;
130 };
131 
132 /**
133    An iterator for traversal of a set of properties.
134 
135    Sample code for use of iterator:
136 
137    \code
138    Return_t function(WW8PropertySet::tPointer pSet)
139    {
140        do_something;
141 
142        WW8PropertySetIterator::tPointer pIt = pSet->begin();
143        WW8PropertySetIterator::tPointer pItEnd = pSet->end();
144 
145        while ((*pIt) != (*pItEnd))
146        {
147            do_something();
148 
149            ++(*pIt);
150        }
151 
152        do_something;
153    }
154    \endcode
155 */
156 class WRITERFILTER_DLLPUBLIC WW8PropertySetIterator
157 {
158 public:
159     typedef boost::shared_ptr<WW8PropertySetIterator> Pointer_t;
160 
161     virtual ~WW8PropertySetIterator();
162 
163     /**
164        Advance iterator to the next property.
165     */
166     virtual WW8PropertySetIterator & operator++() = 0;
167 
168     /**
169        Returns a pointer to the property the iterator references.
170     */
171     virtual WW8Property::Pointer_t get() const = 0;
172 
173     /**
174        Checks if the iterator is equal to another one.
175     */
176     virtual bool equal(const WW8PropertySetIterator & rIt) const = 0;
177 
178     /**
179        Returns string representation of iterator.
180      */
181     virtual string toString() const = 0;
182 };
183 
184 /**
185    Checks if two property set iterators are not equal.
186 */
187 bool operator != (const WW8PropertySetIterator & rA,
188                   const WW8PropertySetIterator & rB);
189 
190 /**
191    A set of properties.
192 */
193 class WRITERFILTER_DLLPUBLIC WW8PropertySet
194 {
195 public:
196     typedef boost::shared_ptr<WW8PropertySet> Pointer_t;
197 
198     virtual ~WW8PropertySet();
199 
200     /**
201        Returns iterator to the start of the set.
202      */
203     virtual WW8PropertySetIterator::Pointer_t begin() = 0;
204 
205     /**
206        Returns iterator to the end of the set.
207     */
208     virtual WW8PropertySetIterator::Pointer_t end() = 0;
209 
210     /**
211        Dumps property set to output stream.
212 
213        @param o    output stream to dump property set to
214     */
215     virtual void dump(OutputWithDepth<string> & o) const = 0;
216 
217     /**
218        Iterate through property set and for each element dump a dot
219        output stream.
220 
221        @param o      output stream to dump dots to
222      */
223     virtual void dots(ostream & o) = 0;
224 
225     virtual bool isPap() const = 0;
226     virtual sal_uInt32 get_istd() const = 0;
227 
228     /**
229        Insert another property set into this property set.
230 
231        @param pSet   the set to insert
232      */
233     virtual void insert(const WW8PropertySet::Pointer_t pSet) = 0;
234 };
235 
236 enum PropertyType {
237     /** Auxiliary type for character positions defined in piece table */
238     PROP_DOC,
239 
240     /** properties are section properies */
241     PROP_SEC,
242 
243     /** properties are paragraph properties */
244     PROP_PAP,
245 
246     /** properties are character properties */
247     PROP_CHP,
248 
249     /** a footnote reference */
250     PROP_FOOTNOTE,
251 
252     /** an endnote reference */
253     PROP_ENDNOTE,
254 
255     /** an annotaion reference */
256     PROP_ANNOTATION,
257 
258     /** the start of a bookmark */
259     PROP_BOOKMARKSTART,
260 
261     /** the end of a bookmark */
262     PROP_BOOKMARKEND,
263 
264     /** a field character (start, separator or end) */
265     PROP_FLD,
266 
267     /** a shape character */
268     PROP_SHP,
269 
270     /** a break character */
271     PROP_BRK
272 };
273 
274 /**
275    An iterator for traversal of the character positions of a Word
276    document.
277 
278    The use of the iterator is analogous to WW8PropertySetIterator.
279 */
280 class WRITERFILTER_DLLPUBLIC WW8DocumentIterator
281 {
282 public:
283     typedef boost::shared_ptr<WW8DocumentIterator> Pointer_t;
284 
285     virtual ~WW8DocumentIterator();
286 
287     /**
288        Advance iterator to next character position of the document.
289     */
290     virtual WW8DocumentIterator & operator++() = 0;
291 
292     /**
293        Recedes iterator to previous character postion of the document.
294      */
295     virtual WW8DocumentIterator & operator--() = 0;
296 
297     /**
298        Returns properties set at the character position the iterator
299        points to.
300 
301        @return pointer to set of properties
302      */
303     virtual writerfilter::Reference<Properties>::Pointer_t getProperties()
304         const = 0;
305 
306     virtual writerfilter::Reference<Stream>::Pointer_t getSubDocument()
307         const = 0;
308 
309     /**
310        Returns text run at the character position the iterator points
311        to.
312      */
313     virtual WW8Stream::Sequence getText() = 0;
314 
315     /**
316        Return pointer to the shape at character position the iterator
317        is pointing to.
318      */
319     virtual writerfilter::Reference<Properties>::Pointer_t getShape() const = 0;
320 
321     /**
322        Checks if the characters of the entity the iterator points to
323        are complex.
324 
325        Complex characters in a Word document are byte size
326        characters. Non-complex characters are word size characters.
327 
328        @retval true      The characters are complex.
329        @retval false     The characters are non-complex.
330      */
331     virtual bool isComplex() const = 0;
332 
333     /**
334        Returns the property type of the entity the iterator points to.
335      */
336     virtual PropertyType getPropertyType() const = 0;
337 
338     /**
339        Checks is the iterator is equal to another one.
340 
341        @param rIt    iterator to check against
342 
343        @retval true    the iterators are equal
344        @retval false   else
345     */
346     virtual bool equal(const WW8DocumentIterator & rIt) const = 0;
347 
348     /**
349        Returns string representation of the iterator.
350     */
351     virtual string toString() const = 0;
352 
353     /**
354        Dumps the iterator to an output stream.
355 
356        @param o    the output stream to dump the iterator to
357      */
358     virtual void dump(ostream & o) const = 0;
359 };
360 
361 /**
362    Checks if two document iterators are equal.
363 
364    @param rA     first iterator
365    @param rB     second iterator
366 
367    @retval true    the document iterators are equal
368    @retval false   else
369  */
370 bool operator == (const WW8DocumentIterator & rA,
371                   const WW8DocumentIterator & rB);
372 
373 class WRITERFILTER_DLLPUBLIC SubDocumentId
374 {
375 public:
376     enum eType { FOOTNOTES, HEADERS, FOOTERS };
377 
378 private:
379     eType mnType;
380     sal_uInt8 mnIndex;
381 
382 public:
SubDocumentId(eType nType,sal_uInt8 nIndex)383     SubDocumentId(eType nType, sal_uInt8 nIndex)
384     : mnType(nType), mnIndex(nIndex)
385     {
386     }
387 
getType() const388     eType getType() const { return mnType; }
getIndex() const389     sal_uInt8 getIndex() const { return mnIndex; }
390 };
391 
392 /**
393    A Word 8 document.
394 */
395 class WRITERFILTER_DLLPUBLIC WW8Document :
396     public writerfilter::Reference<Stream>
397 {
398 public:
399     typedef boost::shared_ptr<WW8Document> Pointer_t;
400 
401     virtual ~WW8Document();
402 
403     /**
404        Get a subdocument.
405 
406        A subdocument can be
407 
408        - a header
409        - a footer
410        - a footnode
411 
412        @param nId identifier of the subdocumen
413     */
414     virtual Pointer_t getSubDocument(SubDocumentId nId) = 0;
415 
416     /**
417        Returns iterator to beginning of document.
418      */
419     virtual WW8DocumentIterator::Pointer_t begin() = 0;
420 
421     /**
422        Returns iterator to end of document.
423      */
424     virtual WW8DocumentIterator::Pointer_t end() = 0;
425 };
426 
427 class WRITERFILTER_DLLPUBLIC WW8DocumentFactory
428 {
429 public:
430     static WW8Stream::Pointer_t
431     createStream(uno::Reference<uno::XComponentContext> rContext,
432                  uno::Reference<io::XInputStream> rStream);
433 
434     static WW8Document *
435     createDocument(WW8Stream::Pointer_t rpStream);
436 };
437 
438 void sprmidsToXML(::std::iostream & out);
439 
440 void doctokidsToXML(::std::iostream & out);
441 
442 }}
443 
444 #endif // INCLUDED_WW8_DOCUMENT_HXX
445