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 #ifndef INCLUDED_OOXML_DOCUMENT_HXX
24 #define INCLUDED_OOXML_DOCUMENT_HXX
25 
26 #include <sal/types.h>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/io/XInputStream.hpp>
29 #ifndef _COM_SUN_STAR_UNO_XCOMPONENTCONTEX_HPP_
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #endif
32 #ifndef INCLUDED_WW8_RESOURCE_MODEL_HXX
33 #include <resourcemodel/WW8ResourceModel.hxx>
34 #endif
35 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HOO_
36 #include <com/sun/star/xml/sax/XParser.hpp>
37 #endif
38 #include <com/sun/star/xml/sax/XFastParser.hpp>
39 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
41 #include <com/sun/star/drawing/XDrawPage.hpp>
42 
43 /**
44    @file OOXMLDocument.hxx
45 
46    <h1>Import of OOXML WordprocessingML Documents</h1>
47 
48    The following picture shows the classes involved in importing OOXML
49    WordprocessingML documents.
50 
51    @image html ooxmlimportchain.png
52 
53    The DOCX consists of parts. Each part is an XML document.  The
54    OOXMLDocument opens the DOCX and creates a SAX parser for the part
55    containing the main document content. The OOXMLDocument creates a
56    SAX handler, too. This handler is set as the handler for the events
57    created by the parser. Finally the OOXMLDocument initiates the
58    parsing process.
59 
60    The SAX handler hosts a stack of contexts. Each context is an
61    instance of a class derived from OOXMLContext. There is a context
62    class for each <define> in the model.xml.
63 
64    For a detailed information about how the contexts are handled see
65    the documentation for OOXMLContext.
66 
67    The contexts know how to convert an element in OOXML to the
68    intermediate format that the domain mapper understands. They
69    enumerate the according entity in OOXML by sending the according
70    events to the domain mapper.
71 
72    The domain mapper knows how to convert the intermediate format to
73    API calls. It takes the events sent by the contexts and uses the
74    core API to insert the according elements to the core.
75  */
76 
77 namespace writerfilter {
78 namespace ooxml
79 {
80 
81 using namespace com::sun::star;
82 
83 class WRITERFILTER_DLLPUBLIC OOXMLStream
84 {
85 public:
86     enum StreamType_t { UNKNOWN, DOCUMENT, STYLES, FONTTABLE, NUMBERING,
87         FOOTNOTES, ENDNOTES, COMMENTS, THEME, SETTINGS };
88     typedef boost::shared_ptr<OOXMLStream> Pointer_t;
89 
~OOXMLStream()90     virtual ~OOXMLStream() {}
91 
92     /**
93        Returns parser for this stream.
94      */
95     virtual uno::Reference<xml::sax::XParser> getParser() = 0;
96 
97     /**
98        Returns fast parser for this stream.
99      */
100     virtual uno::Reference<xml::sax::XFastParser> getFastParser() = 0;
101 
102     virtual uno::Reference<io::XInputStream> getDocumentStream() = 0;
103 
104     virtual uno::Reference<io::XInputStream> getStorageStream() = 0;
105 
106     /**
107        Returns component context for this stream.
108      */
109     virtual uno::Reference<uno::XComponentContext> getContext() = 0;
110 
111     /**
112        Returns target URL from relationships for a given id.
113 
114        @param rId           the id to look for
115 
116        @return the URL found or an empty string
117      */
118     virtual ::rtl::OUString getTargetForId(const ::rtl::OUString & rId) = 0;
119 
120     virtual const ::rtl::OUString & getTarget() const = 0;
121 
122     virtual uno::Reference<xml::sax::XFastTokenHandler>
123     getFastTokenHandler(uno::Reference<uno::XComponentContext> rContext) = 0;
124 
125 };
126 
127 class WRITERFILTER_DLLPUBLIC OOXMLDocument : public writerfilter::Reference<Stream>
128 {
129 public:
130     /**
131        Pointer to this stream.
132     */
133     typedef boost::shared_ptr<OOXMLDocument> Pointer_t;
134 
~OOXMLDocument()135     virtual ~OOXMLDocument() {}
136 
137     /**
138        Resolves this document to a stream handler.
139 
140        @param rStream     stream handler to resolve this document to
141      */
142     virtual void resolve(Stream & rStream) = 0;
143 
144     /**
145        Returns string representation of the type of this reference.
146 
147        DEBUGGING PURPOSE ONLY.
148      */
149     virtual string getType() const = 0;
150 
151     /**
152        Resolves a footnote to a stream handler.
153 
154        A footnote is resolved if either the note type or
155        note id matches.
156 
157        @param rStream       stream handler to resolve to
158        @param rNoteType     type of footnote to resolve
159        @param nIDForXNoteStream  id of the footnote to resolve
160      */
161     virtual void resolveFootnote(Stream & rStream,
162                                  const Id & rNoteType,
163                                  const sal_Int32 nIDForXNoteStream ) = 0;
164     /**
165        Resolves an endnote to a stream handler.
166 
167        An endnote is resolved if either the note type or
168        note id matches.
169 
170        @param rStream       stream handler to resolve to
171        @param rNoteType     type of endnote to resolve
172        @param nIDForXNoteStream  id of the endnote to resolve
173      */
174     virtual void resolveEndnote(Stream & rStream,
175                                 const Id & rNoteType,
176                                 const sal_Int32 nIDForXNoteStream ) = 0;
177 
178     /**
179        Resolves a comment to a stream handler.
180 
181        @param rStream       stream handler to resolve to
182        @param nIDForXNoteStream  id of the comment to resolve
183      */
184     virtual void resolveComment(Stream & rStream,
185                                 const sal_Int32 nIDForXNoteStream ) = 0;
186 
187     /**
188        Resolves a picture to a stream handler.
189 
190        @param rStream       stream handler to resolve to
191        @param rPictureId    id of the picture to resolve
192      */
193     virtual void resolvePicture(Stream & rStream,
194                                 const rtl::OUString & rPictureId) = 0;
195 
196     /**
197        Resolves a header to a stream handler.
198 
199        @param rStream       stream handler to resolve to
200        @param type          type of header to resolve:
201                             NS_ooxml::LN_Value_ST_HrdFtr_even     header on even page
202                             NS_ooxml::LN_Value_ST_HrdFtr_default  header on right page
203                             NS_ooxml::LN_Value_ST_HrdFtr_first    header on first page
204 
205        @param rId           id of the header
206      */
207     virtual void resolveHeader(Stream & rStream,
208                                const sal_Int32 type,
209                                const rtl::OUString & rId) = 0;
210 
211     /**
212        Resolves a footer to a stream handler.
213 
214        @param rStream       stream handler to resolve to
215        @param type          type of footer to resolve:
216                             NS_ooxml::LN_Value_ST_HrdFtr_even     header on even page
217                             NS_ooxml::LN_Value_ST_HrdFtr_default  header on right page
218                             NS_ooxml::LN_Value_ST_HrdFtr_first    header on first page
219 
220        @param rId           id of the header
221     */
222     virtual void resolveFooter(Stream & rStream,
223                                const sal_Int32 type,
224                                const rtl::OUString & rId) = 0;
225 
226 
227     /**
228        Returns target URL from relationships for a given id.
229 
230        @param rId           the id to look for
231 
232        @return the URL found or an empty string
233      */
234     virtual ::rtl::OUString getTargetForId(const ::rtl::OUString & rId) = 0;
235 
236     virtual void setModel(uno::Reference<frame::XModel> xModel) = 0;
237     virtual uno::Reference<frame::XModel> getModel() = 0;
238     virtual void setDrawPage(uno::Reference<drawing::XDrawPage> xDrawPage) = 0;
239     virtual uno::Reference<drawing::XDrawPage> getDrawPage() = 0;
240     virtual uno::Reference<io::XInputStream> getInputStream() = 0;
241     virtual uno::Reference<io::XInputStream> getStorageStream() = 0;
242     virtual uno::Reference<io::XInputStream> getInputStreamForId( const ::rtl::OUString & rId ) = 0;
243 
244     virtual void setIDForXNoteStream( const sal_Int32 nID ) = 0;
245     virtual sal_Int32 getIDForXNoteStream() const = 0;
246 
247     virtual const ::rtl::OUString & getTarget() const = 0;
248 };
249 
250 
251 class WRITERFILTER_DLLPUBLIC OOXMLDocumentFactory
252 {
253 public:
254     static OOXMLStream::Pointer_t
255     createStream(uno::Reference<uno::XComponentContext> rContext,
256                  uno::Reference<io::XInputStream> rStream,
257                  OOXMLStream::StreamType_t nStreamType = OOXMLStream::DOCUMENT);
258 
259     static OOXMLStream::Pointer_t
260     createStream(OOXMLStream::Pointer_t pStream,
261                  OOXMLStream::StreamType_t nStreamType = OOXMLStream::DOCUMENT);
262 
263     static OOXMLStream::Pointer_t
264     createStream(OOXMLStream::Pointer_t pStream, const rtl::OUString & rId);
265 
266     static OOXMLDocument *
267     createDocument(OOXMLStream::Pointer_t pStream);
268 
269 };
270 
271 void ooxmlidsToXML(::std::iostream & out);
272 
273 }}
274 #endif // INCLUDED_OOXML_DOCUMENT_HXX
275