Update ooo320-m1
[ooovba.git] / writerfilter / source / ooxml / OOXMLStreamImpl.cxx
blobd81829af5d9b84ddf12eadd3d98d78e55060eaf5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOXMLStreamImpl.cxx,v $
10 * $Revision: 1.14 $
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 "OOXMLStreamImpl.hxx"
32 #include "OOXMLFastTokenHandler.hxx"
33 #include "ooxmlLoggers.hxx"
34 #include <iostream>
36 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALSTORAGEACCESS_HPP_
37 #include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
38 #endif
40 //#define DEBUG_STREAM
42 namespace writerfilter {
43 namespace ooxml
46 using namespace ::std;
48 OOXMLStreamImpl::OOXMLStreamImpl
49 (uno::Reference<uno::XComponentContext> xContext,
50 uno::Reference<io::XInputStream> xStorageStream, StreamType_t nType)
51 : mxContext(xContext), mxStorageStream(xStorageStream), mnStreamType(nType)
53 mxStorage.set
54 (comphelper::OStorageHelper::GetStorageOfFormatFromInputStream
55 (OFOPXML_STORAGE_FORMAT_STRING, mxStorageStream));
56 mxRelationshipAccess.set(mxStorage, uno::UNO_QUERY_THROW);
58 init();
61 OOXMLStreamImpl::OOXMLStreamImpl
62 (OOXMLStreamImpl & rOOXMLStream, StreamType_t nStreamType)
63 : mxContext(rOOXMLStream.mxContext),
64 mxStorageStream(rOOXMLStream.mxStorageStream),
65 mxStorage(rOOXMLStream.mxStorage),
66 mnStreamType(nStreamType),
67 msPath(rOOXMLStream.msPath)
69 mxRelationshipAccess.set(rOOXMLStream.mxDocumentStream, uno::UNO_QUERY_THROW);
71 init();
74 OOXMLStreamImpl::OOXMLStreamImpl
75 (uno::Reference<uno::XComponentContext> xContext,
76 uno::Reference<io::XInputStream> xStorageStream, const rtl::OUString & rId)
77 : mxContext(xContext),
78 mxStorageStream(xStorageStream),
79 mnStreamType(UNKNOWN),
80 msId(rId)
82 mxStorage.set
83 (comphelper::OStorageHelper::GetStorageOfFormatFromInputStream
84 (OFOPXML_STORAGE_FORMAT_STRING, mxStorageStream));
85 mxRelationshipAccess.set(mxStorage, uno::UNO_QUERY_THROW);
87 init();
90 OOXMLStreamImpl::OOXMLStreamImpl
91 (OOXMLStreamImpl & rOOXMLStream, const rtl::OUString & rId)
92 : mxContext(rOOXMLStream.mxContext),
93 mxStorageStream(rOOXMLStream.mxStorageStream),
94 mxStorage(rOOXMLStream.mxStorage),
95 mnStreamType(UNKNOWN),
96 msId(rId),
97 msPath(rOOXMLStream.msPath)
99 mxRelationshipAccess.set(rOOXMLStream.mxDocumentStream, uno::UNO_QUERY_THROW);
101 init();
104 OOXMLStreamImpl::~OOXMLStreamImpl()
106 #ifdef DEBUG_STREAM
107 debug_logger->endElement("stream");
108 #endif
111 const ::rtl::OUString & OOXMLStreamImpl::getTarget() const
113 return msTarget;
116 bool OOXMLStreamImpl::lcl_getTarget(uno::Reference<embed::XRelationshipAccess>
117 xRelationshipAccess,
118 StreamType_t nStreamType,
119 const ::rtl::OUString & rId,
120 ::rtl::OUString & rDocumentTarget)
122 bool bFound = false;
124 static rtl::OUString sType(RTL_CONSTASCII_USTRINGPARAM("Type"));
125 static rtl::OUString sId(RTL_CONSTASCII_USTRINGPARAM("Id"));
126 static rtl::OUString sDocumentType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"));
127 static rtl::OUString sStylesType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"));
128 static rtl::OUString sNumberingType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"));
129 static rtl::OUString sFonttableType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"));
130 static rtl::OUString sFootnotesType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"));
131 static rtl::OUString sEndnotesType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"));
132 static rtl::OUString sCommentsType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"));
133 static rtl::OUString sThemeType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"));
134 static rtl::OUString sSettingsType(RTL_CONSTASCII_USTRINGPARAM("http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"));
135 static rtl::OUString sTarget(RTL_CONSTASCII_USTRINGPARAM("Target"));
136 static rtl::OUString sTargetMode(RTL_CONSTASCII_USTRINGPARAM("TargetMode"));
137 static rtl::OUString sExternal(RTL_CONSTASCII_USTRINGPARAM("External"));
139 rtl::OUString sStreamType;
141 switch (nStreamType)
143 case DOCUMENT:
144 sStreamType = sDocumentType;
145 break;
146 case STYLES:
147 sStreamType = sStylesType;
148 break;
149 case NUMBERING:
150 sStreamType = sNumberingType;
151 break;
152 case FONTTABLE:
153 sStreamType = sFonttableType;
154 break;
155 case FOOTNOTES:
156 sStreamType = sFootnotesType;
157 break;
158 case ENDNOTES:
159 sStreamType = sEndnotesType;
160 break;
161 case COMMENTS:
162 sStreamType = sCommentsType;
163 break;
164 case THEME:
165 sStreamType = sThemeType;
166 break;
167 case SETTINGS:
168 sStreamType = sSettingsType;
169 break;
170 default:
171 break;
174 if (xRelationshipAccess.is())
176 uno::Sequence< uno::Sequence< beans::StringPair > >aSeqs =
177 xRelationshipAccess->getAllRelationships();
179 for (sal_Int32 j = 0; j < aSeqs.getLength(); j++)
181 uno::Sequence< beans::StringPair > aSeq = aSeqs[j];
183 bool bExternalTarget = false;
184 ::rtl::OUString sMyTarget;
185 for (sal_Int32 i = 0; i < aSeq.getLength(); i++)
187 beans::StringPair aPair = aSeq[i];
189 if (aPair.First.compareTo(sType) == 0 &&
190 aPair.Second.compareTo(sStreamType) == 0)
191 bFound = true;
192 else if (aPair.First.compareTo(sId) == 0 &&
193 aPair.Second.compareTo(rId) == 0)
194 bFound = true;
195 else if (aPair.First.compareTo(sTarget) == 0)
196 sMyTarget = aPair.Second;
197 else if (aPair.First.compareTo(sTargetMode) == 0 &&
198 aPair.Second.compareTo(sExternal) == 0)
199 bExternalTarget = true;
203 if (bFound)
205 if (bExternalTarget)
206 rDocumentTarget = sMyTarget;
207 else
209 rDocumentTarget = msPath;
210 rDocumentTarget += sMyTarget;
213 break;
218 return bFound;
221 ::rtl::OUString OOXMLStreamImpl::getTargetForId(const ::rtl::OUString & rId)
223 ::rtl::OUString sTarget;
225 uno::Reference<embed::XRelationshipAccess> xRelationshipAccess
226 (mxDocumentStream, uno::UNO_QUERY_THROW);
228 if (lcl_getTarget(xRelationshipAccess, UNKNOWN, rId, sTarget))
229 return sTarget;
231 return ::rtl::OUString();
234 void OOXMLStreamImpl::init()
236 bool bFound = lcl_getTarget(mxRelationshipAccess,
237 mnStreamType, msId, msTarget);
238 #ifdef DEBUG_STREAM
239 debug_logger->startElement("stream");
240 debug_logger->attribute("target", msTarget);
241 #endif
243 if (bFound)
245 sal_Int32 nLastIndex = msTarget.lastIndexOf('/');
246 if (nLastIndex >= 0)
247 msPath = msTarget.copy(0, nLastIndex + 1);
249 uno::Reference<embed::XHierarchicalStorageAccess>
250 xHierarchicalStorageAccess(mxStorage, uno::UNO_QUERY);
252 if (xHierarchicalStorageAccess.is())
254 uno::Any aAny(xHierarchicalStorageAccess->
255 openStreamElementByHierarchicalName
256 (msTarget, embed::ElementModes::SEEKABLEREAD));
257 aAny >>= mxDocumentStream;
262 uno::Reference<io::XInputStream> OOXMLStreamImpl::getDocumentStream()
264 uno::Reference<io::XInputStream> xResult;
266 if (mxDocumentStream.is())
267 xResult = mxDocumentStream->getInputStream();
269 return xResult;
272 uno::Reference<io::XInputStream> OOXMLStreamImpl::getStorageStream()
274 return mxStorageStream;
277 uno::Reference<xml::sax::XParser> OOXMLStreamImpl::getParser()
279 uno::Reference<lang::XMultiComponentFactory> xFactory =
280 uno::Reference<lang::XMultiComponentFactory>
281 (mxContext->getServiceManager());
283 uno::Reference<xml::sax::XParser> xParser
284 (xFactory->createInstanceWithContext
285 ( rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ),
286 mxContext ),
287 uno::UNO_QUERY );
289 return xParser;
292 uno::Reference<uno::XComponentContext> OOXMLStreamImpl::getContext()
294 return mxContext;
297 uno::Reference <xml::sax::XFastTokenHandler>
298 OOXMLStreamImpl::getFastTokenHandler
299 (uno::Reference<uno::XComponentContext> xContext)
301 if (! mxFastTokenHandler.is())
302 mxFastTokenHandler.set(new OOXMLFastTokenHandler(xContext));
304 return mxFastTokenHandler;
307 OOXMLStream::Pointer_t
308 OOXMLDocumentFactory::createStream
309 (uno::Reference<uno::XComponentContext> xContext,
310 uno::Reference<io::XInputStream> rStream,
311 OOXMLStream::StreamType_t nStreamType)
313 OOXMLStreamImpl * pStream = new OOXMLStreamImpl(xContext, rStream,
314 nStreamType);
315 return OOXMLStream::Pointer_t(pStream);
318 OOXMLStream::Pointer_t
319 OOXMLDocumentFactory::createStream
320 (OOXMLStream::Pointer_t pStream, OOXMLStream::StreamType_t nStreamType)
322 return OOXMLStream::Pointer_t
323 (new OOXMLStreamImpl(*dynamic_cast<OOXMLStreamImpl *>(pStream.get()),
324 nStreamType));
327 OOXMLStream::Pointer_t
328 OOXMLDocumentFactory::createStream
329 (OOXMLStream::Pointer_t pStream, const rtl::OUString & rId)
331 return OOXMLStream::Pointer_t
332 (new OOXMLStreamImpl(*dynamic_cast<OOXMLStreamImpl *>(pStream.get()),
333 rId));