Update ooo320-m1
[ooovba.git] / writerfilter / source / ooxml / OOXMLDocumentImpl.cxx
blob4ae36eb9dd2e3af63e75581cd4ee4bb10caf92b4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOXMLDocumentImpl.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <com/sun/star/xml/sax/XParser.hpp>
33 #include <doctok/resourceids.hxx>
34 #include <ooxml/resourceids.hxx>
35 #include "OOXMLDocumentImpl.hxx"
36 #include "OOXMLBinaryObjectReference.hxx"
37 #include "OOXMLFastDocumentHandler.hxx"
38 #include "OOXMLPropertySetImpl.hxx"
39 #include "ooxmlLoggers.hxx"
41 #include <iostream>
43 namespace writerfilter {
44 namespace ooxml
46 TagLogger::Pointer_t debug_logger(TagLogger::getInstance("DEBUG"));
47 using namespace ::std;
49 OOXMLDocumentImpl::OOXMLDocumentImpl
50 (OOXMLStream::Pointer_t pStream)
51 : mpStream(pStream), mXNoteType(0), mbIsSubstream( false )
55 OOXMLDocumentImpl::~OOXMLDocumentImpl()
59 void OOXMLDocumentImpl::resolveFastSubStream(Stream & rStreamHandler,
60 OOXMLStream::StreamType_t nType)
62 OOXMLStream::Pointer_t pStream
63 (OOXMLDocumentFactory::createStream(mpStream, nType));
65 uno::Reference< xml::sax::XFastParser > xParser
66 (mpStream->getFastParser());
68 if (xParser.is())
70 uno::Reference<uno::XComponentContext> xContext(mpStream->getContext());
71 OOXMLFastDocumentHandler * pDocHandler =
72 new OOXMLFastDocumentHandler(xContext);
73 pDocHandler->setStream(&rStreamHandler);
74 pDocHandler->setDocument(this);
75 pDocHandler->setXNoteId(msXNoteId);
77 uno::Reference < xml::sax::XFastDocumentHandler > xDocumentHandler
78 (pDocHandler);
79 uno::Reference < xml::sax::XFastTokenHandler > xTokenHandler
80 (mpStream->getFastTokenHandler(xContext));
82 xParser->setFastDocumentHandler(xDocumentHandler);
83 xParser->setTokenHandler(xTokenHandler);
85 uno::Reference<io::XInputStream> xInputStream =
86 pStream->getDocumentStream();
88 if (xInputStream.is())
90 struct xml::sax::InputSource oInputSource;
91 oInputSource.aInputStream = xInputStream;
92 xParser->parseStream(oInputSource);
94 xInputStream->closeInput();
99 void OOXMLDocumentImpl::resolveFastSubStreamWithId(Stream & rStream,
100 writerfilter::Reference<Stream>::Pointer_t pStream,
101 sal_uInt32 nId)
103 rStream.substream(nId, pStream);
106 void OOXMLDocumentImpl::setXNoteId(const rtl::OUString & rId)
108 msXNoteId = rId;
111 const rtl::OUString & OOXMLDocumentImpl::getXNoteId() const
113 return msXNoteId;
116 void OOXMLDocumentImpl::setXNoteType(const Id & nId)
118 mXNoteType = nId;
121 const Id & OOXMLDocumentImpl::getXNoteType() const
123 return mXNoteType;
126 const ::rtl::OUString & OOXMLDocumentImpl::getTarget() const
128 return mpStream->getTarget();
131 writerfilter::Reference<Stream>::Pointer_t
132 OOXMLDocumentImpl::getSubStream(const rtl::OUString & rId)
134 OOXMLStream::Pointer_t pStream
135 (OOXMLDocumentFactory::createStream(mpStream, rId));
137 OOXMLDocumentImpl * pTemp;
138 writerfilter::Reference<Stream>::Pointer_t pRet( pTemp = new OOXMLDocumentImpl(pStream) );
139 pTemp->setModel(mxModel);
140 pTemp->setDrawPage(mxDrawPage);
141 pTemp->setIsSubstream( true );
142 return pRet;
145 writerfilter::Reference<Stream>::Pointer_t
146 OOXMLDocumentImpl::getXNoteStream(OOXMLStream::StreamType_t nType, const Id & rType,
147 const rtl::OUString & rId)
149 #ifdef DEBUG_ELEMENT
150 debug_logger->startElement("getXNoteStream");
151 debug_logger->attribute("id", rId);
152 debug_logger->endElement("getXNoteStream");
153 #endif
155 OOXMLStream::Pointer_t pStream =
156 (OOXMLDocumentFactory::createStream(mpStream, nType));
157 OOXMLDocumentImpl * pDocument = new OOXMLDocumentImpl(pStream);
158 pDocument->setXNoteId(rId);
159 pDocument->setXNoteType(rType);
161 return writerfilter::Reference<Stream>::Pointer_t(pDocument);
164 void OOXMLDocumentImpl::resolveFootnote(Stream & rStream,
165 const Id & rType,
166 const rtl::OUString & rNoteId)
168 writerfilter::Reference<Stream>::Pointer_t pStream =
169 getXNoteStream(OOXMLStream::FOOTNOTES, rType, rNoteId);
171 Id nId;
172 switch (rType)
174 case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_separator:
175 case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_continuationSeparator:
176 nId = rType;
177 break;
178 default:
179 nId = NS_rtf::LN_footnote;
180 break;
183 resolveFastSubStreamWithId(rStream, pStream, nId);
186 void OOXMLDocumentImpl::resolveEndnote(Stream & rStream,
187 const Id & rType,
188 const rtl::OUString & rNoteId)
190 writerfilter::Reference<Stream>::Pointer_t pStream =
191 getXNoteStream(OOXMLStream::ENDNOTES, rType, rNoteId);
193 Id nId;
194 switch (rType)
196 case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_separator:
197 case NS_ooxml::LN_Value_wordprocessingml_ST_FtnEdn_continuationSeparator:
198 nId = rType;
199 break;
200 default:
201 nId = NS_rtf::LN_endnote;
202 break;
205 resolveFastSubStreamWithId(rStream, pStream, nId);
208 void OOXMLDocumentImpl::resolveComment(Stream & rStream,
209 const rtl::OUString & rId)
211 writerfilter::Reference<Stream>::Pointer_t pStream =
212 getXNoteStream(OOXMLStream::COMMENTS, 0, rId);
214 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_annotation);
217 OOXMLPropertySet * OOXMLDocumentImpl::getPicturePropSet
218 (const ::rtl::OUString & rId)
220 OOXMLStream::Pointer_t pStream
221 (OOXMLDocumentFactory::createStream(mpStream, rId));
223 writerfilter::Reference<BinaryObj>::Pointer_t pPicture
224 (new OOXMLBinaryObjectReference(pStream));
226 OOXMLValue::Pointer_t pPayloadValue(new OOXMLBinaryValue(pPicture));
228 OOXMLProperty::Pointer_t pPayloadProperty
229 (new OOXMLPropertyImpl(NS_rtf::LN_payload, pPayloadValue,
230 OOXMLPropertyImpl::ATTRIBUTE));
232 OOXMLPropertySet::Pointer_t pBlipSet(new OOXMLPropertySetImpl());
234 pBlipSet->add(pPayloadProperty);
236 OOXMLValue::Pointer_t pBlipValue(new OOXMLPropertySetValue(pBlipSet));
238 OOXMLProperty::Pointer_t pBlipProperty
239 (new OOXMLPropertyImpl(NS_rtf::LN_blip, pBlipValue,
240 OOXMLPropertyImpl::ATTRIBUTE));
242 OOXMLPropertySet * pProps = new OOXMLPropertySetImpl();
244 pProps->add(pBlipProperty);
246 return pProps;
249 void OOXMLDocumentImpl::resolvePicture(Stream & rStream,
250 const rtl::OUString & rId)
252 OOXMLPropertySet * pProps = getPicturePropSet(rId);
254 rStream.props(writerfilter::Reference<Properties>::Pointer_t(pProps));
257 ::rtl::OUString OOXMLDocumentImpl::getTargetForId(const ::rtl::OUString & rId)
259 return mpStream->getTargetForId(rId);
262 void OOXMLDocumentImpl::resolveHeader(Stream & rStream,
263 const sal_Int32 type,
264 const rtl::OUString & rId)
266 writerfilter::Reference<Stream>::Pointer_t pStream =
267 getSubStream(rId);
268 switch (type)
270 case NS_ooxml::LN_Value_ST_HrdFtr_even:
271 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerl);
272 break;
273 case NS_ooxml::LN_Value_ST_HrdFtr_default: // here we assume that default is right, but not necessarily true :-(
274 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerr);
275 break;
276 case NS_ooxml::LN_Value_ST_HrdFtr_first:
277 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_headerf);
278 break;
279 default:
280 break;
284 void OOXMLDocumentImpl::resolveFooter(Stream & rStream,
285 const sal_Int32 type,
286 const rtl::OUString & rId)
288 writerfilter::Reference<Stream>::Pointer_t pStream =
289 getSubStream(rId);
291 switch (type)
293 case NS_ooxml::LN_Value_ST_HrdFtr_even:
294 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerl);
295 break;
296 case NS_ooxml::LN_Value_ST_HrdFtr_default: // here we assume that default is right, but not necessarily true :-(
297 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerr);
298 break;
299 case NS_ooxml::LN_Value_ST_HrdFtr_first:
300 resolveFastSubStreamWithId(rStream, pStream, NS_rtf::LN_footerf);
301 break;
302 default:
303 break;
307 void OOXMLDocumentImpl::resolve(Stream & rStream)
309 uno::Reference< xml::sax::XFastParser > xParser
310 (mpStream->getFastParser());
312 if (xParser.is())
314 uno::Reference<uno::XComponentContext> xContext(mpStream->getContext());
316 OOXMLFastDocumentHandler * pDocHandler =
317 new OOXMLFastDocumentHandler(xContext);
318 pDocHandler->setStream(&rStream);
319 pDocHandler->setDocument(this);
320 pDocHandler->setXNoteId(msXNoteId);
321 pDocHandler->setIsSubstream( mbIsSubstream );
322 uno::Reference < xml::sax::XFastDocumentHandler > xDocumentHandler
323 (pDocHandler);
324 uno::Reference < xml::sax::XFastTokenHandler > xTokenHandler
325 (mpStream->getFastTokenHandler(xContext));
327 resolveFastSubStream(rStream, OOXMLStream::SETTINGS);
328 resolveFastSubStream(rStream, OOXMLStream::THEME);
329 resolveFastSubStream(rStream, OOXMLStream::FONTTABLE);
330 resolveFastSubStream(rStream, OOXMLStream::STYLES);
331 resolveFastSubStream(rStream, OOXMLStream::NUMBERING);
333 xParser->setFastDocumentHandler( xDocumentHandler );
334 xParser->setTokenHandler( xTokenHandler );
336 xml::sax::InputSource aParserInput;
337 aParserInput.aInputStream = mpStream->getDocumentStream();
338 xParser->parseStream(aParserInput);
342 uno::Reference<io::XInputStream> OOXMLDocumentImpl::getInputStreamForId(const ::rtl::OUString & rId)
344 OOXMLStream::Pointer_t pStream(OOXMLDocumentFactory::createStream(mpStream, rId));
346 return pStream->getDocumentStream();
349 string OOXMLDocumentImpl::getType() const
351 return "OOXMLDocumentImpl";
354 void OOXMLDocumentImpl::setModel(uno::Reference<frame::XModel> xModel)
356 mxModel.set(xModel);
359 uno::Reference<frame::XModel> OOXMLDocumentImpl::getModel()
361 return mxModel;
364 void OOXMLDocumentImpl::setDrawPage(uno::Reference<drawing::XDrawPage> xDrawPage)
366 mxDrawPage.set(xDrawPage);
369 uno::Reference<drawing::XDrawPage> OOXMLDocumentImpl::getDrawPage()
371 return mxDrawPage;
374 uno::Reference<io::XInputStream> OOXMLDocumentImpl::getInputStream()
376 return mpStream->getDocumentStream();
379 uno::Reference<io::XInputStream> OOXMLDocumentImpl::getStorageStream()
381 return mpStream->getStorageStream();
384 OOXMLDocument *
385 OOXMLDocumentFactory::createDocument
386 (OOXMLStream::Pointer_t pStream)
388 return new OOXMLDocumentImpl(pStream);