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: OOXMLStreamImpl.cxx,v $
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"
36 #ifndef _COM_SUN_STAR_CONTAINER_XHIERARCHICALSTORAGEACCESS_HPP_
37 #include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
40 //#define DEBUG_STREAM
42 namespace writerfilter
{
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
)
54 (comphelper::OStorageHelper::GetStorageOfFormatFromInputStream
55 (OFOPXML_STORAGE_FORMAT_STRING
, mxStorageStream
));
56 mxRelationshipAccess
.set(mxStorage
, uno::UNO_QUERY_THROW
);
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
);
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
),
83 (comphelper::OStorageHelper::GetStorageOfFormatFromInputStream
84 (OFOPXML_STORAGE_FORMAT_STRING
, mxStorageStream
));
85 mxRelationshipAccess
.set(mxStorage
, uno::UNO_QUERY_THROW
);
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
),
97 msPath(rOOXMLStream
.msPath
)
99 mxRelationshipAccess
.set(rOOXMLStream
.mxDocumentStream
, uno::UNO_QUERY_THROW
);
104 OOXMLStreamImpl::~OOXMLStreamImpl()
107 debug_logger
->endElement("stream");
111 const ::rtl::OUString
& OOXMLStreamImpl::getTarget() const
116 bool OOXMLStreamImpl::lcl_getTarget(uno::Reference
<embed::XRelationshipAccess
>
118 StreamType_t nStreamType
,
119 const ::rtl::OUString
& rId
,
120 ::rtl::OUString
& rDocumentTarget
)
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
;
144 sStreamType
= sDocumentType
;
147 sStreamType
= sStylesType
;
150 sStreamType
= sNumberingType
;
153 sStreamType
= sFonttableType
;
156 sStreamType
= sFootnotesType
;
159 sStreamType
= sEndnotesType
;
162 sStreamType
= sCommentsType
;
165 sStreamType
= sThemeType
;
168 sStreamType
= sSettingsType
;
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)
192 else if (aPair
.First
.compareTo(sId
) == 0 &&
193 aPair
.Second
.compareTo(rId
) == 0)
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;
206 rDocumentTarget
= sMyTarget
;
209 rDocumentTarget
= msPath
;
210 rDocumentTarget
+= sMyTarget
;
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
))
231 return ::rtl::OUString();
234 void OOXMLStreamImpl::init()
236 bool bFound
= lcl_getTarget(mxRelationshipAccess
,
237 mnStreamType
, msId
, msTarget
);
239 debug_logger
->startElement("stream");
240 debug_logger
->attribute("target", msTarget
);
245 sal_Int32 nLastIndex
= msTarget
.lastIndexOf('/');
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();
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" ),
292 uno::Reference
<uno::XComponentContext
> OOXMLStreamImpl::getContext()
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
,
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()),
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()),