1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <vcl/errinf.hxx>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <com/sun/star/uri/UriReferenceFactory.hpp>
26 #include <com/sun/star/util/MeasureUnit.hpp>
27 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
28 #include <com/sun/star/packages/WrongPasswordException.hpp>
29 #include <com/sun/star/packages/zip/ZipIOException.hpp>
30 #include <com/sun/star/embed/ElementModes.hpp>
31 #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
32 #include "xmlfilter.hxx"
33 #include <vcl/svapp.hxx>
34 #include <vcl/window.hxx>
35 #include <xmloff/xmlnamespace.hxx>
36 #include <xmloff/xmlscripti.hxx>
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/namespacemap.hxx>
39 #include <com/sun/star/xml/sax/InputSource.hpp>
40 #include <com/sun/star/xml/sax/SAXParseException.hpp>
41 #include <xmloff/ProgressBarHelper.hxx>
42 #include <sfx2/docfile.hxx>
43 #include <com/sun/star/io/XInputStream.hpp>
44 #include "xmlDatabase.hxx"
45 #include "xmlEnums.hxx"
46 #include <strings.hxx>
47 #include <xmloff/DocumentSettingsContext.hxx>
48 #include "xmlStyleImport.hxx"
49 #include <xmloff/xmluconv.hxx>
50 #include "xmlHelper.hxx"
51 #include <com/sun/star/util/XModifiable.hpp>
52 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
53 #include <svtools/sfxecode.hxx>
54 #include <comphelper/diagnose_ex.hxx>
55 #include <osl/diagnose.h>
56 #include <comphelper/sequence.hxx>
57 #include <comphelper/types.hxx>
58 #include <comphelper/namedvaluecollection.hxx>
59 #include <cppuhelper/exc_hlp.hxx>
60 #include <connectivity/DriversConfig.hxx>
61 #include <rtl/uri.hxx>
63 using namespace ::com::sun::star
;
65 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
66 com_sun_star_comp_sdb_DBFilter_get_implementation(
67 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
69 return cppu::acquire(new ::dbaxml::ODBFilter(context
));
75 using namespace ::com::sun::star::util
;
76 /// read a component (file + filter version)
77 static ErrCode
ReadThroughComponent(
78 const uno::Reference
<XInputStream
>& xInputStream
,
79 const uno::Reference
<XComponent
>& xModelComponent
,
80 const uno::Reference
<XComponentContext
> & rxContext
,
83 OSL_ENSURE(xInputStream
.is(), "input stream missing");
84 OSL_ENSURE(xModelComponent
.is(), "document missing");
85 OSL_ENSURE(rxContext
.is(), "factory missing");
87 // prepare ParserInputSource
88 InputSource aParserInput
;
89 aParserInput
.aInputStream
= xInputStream
;
91 // connect model and filter
92 _rFilter
.setTargetDocument( xModelComponent
);
94 // finally, parser the stream
97 _rFilter
.parseStream( aParserInput
);
99 catch (const SAXParseException
&)
101 #if OSL_DEBUG_LEVEL > 0
102 TOOLS_WARN_EXCEPTION("dbaccess", "SAX parse exception caught while importing");
106 catch (const SAXException
&)
110 catch (const packages::zip::ZipIOException
&)
112 return ERRCODE_IO_BROKENPACKAGE
;
114 catch (const Exception
&)
116 DBG_UNHANDLED_EXCEPTION("dbaccess");
124 /// read a component (storage version)
125 static ErrCode
ReadThroughComponent(
126 const uno::Reference
< embed::XStorage
>& xStorage
,
127 const uno::Reference
<XComponent
>& xModelComponent
,
128 const OUString
& sStreamName
,
129 const uno::Reference
<XComponentContext
> & rxContext
,
132 OSL_ENSURE( xStorage
.is(), "Need storage!");
135 // TODO/LATER: better error handling
138 uno::Reference
< io::XStream
> xDocStream
;
142 // open stream (and set parser input)
143 if ( !xStorage
->hasByName( sStreamName
) || !xStorage
->isStreamElement( sStreamName
) )
145 // stream name not found! return immediately with OK signal
150 xDocStream
= xStorage
->openStreamElement( sStreamName
, embed::ElementModes::READ
);
152 catch (const packages::WrongPasswordException
&)
154 return ERRCODE_SFX_WRONGPASSWORD
;
156 catch (const uno::Exception
&)
158 return ErrCode(1); // TODO/LATER: error handling
161 uno::Reference
< XInputStream
> xInputStream
= xDocStream
->getInputStream();
162 // read from the stream
163 return ReadThroughComponent( xInputStream
170 ODBFilter::ODBFilter( const uno::Reference
< XComponentContext
>& _rxContext
)
171 : SvXMLImport(_rxContext
, getImplementationName_Static())
172 , m_bNewFormat(false)
175 GetMM100UnitConverter().SetCoreMeasureUnit(util::MeasureUnit::MM_10TH
);
176 GetMM100UnitConverter().SetXMLMeasureUnit(util::MeasureUnit::CM
);
177 GetNamespaceMap().Add( u
"_db"_ustr
,
178 GetXMLToken(XML_N_DB
),
181 GetNamespaceMap().Add( u
"__db"_ustr
,
182 GetXMLToken(XML_N_DB_OASIS
),
187 ODBFilter::~ODBFilter() noexcept
193 OUString
ODBFilter::getImplementationName_Static()
195 return u
"com.sun.star.comp.sdb.DBFilter"_ustr
;
200 class FocusWindowWaitGuard
203 FocusWindowWaitGuard()
205 SolarMutexGuard aGuard
;
206 mpWindow
.set(Application::GetFocusWindow());
208 mpWindow
->EnterWait();
210 ~FocusWindowWaitGuard()
214 SolarMutexGuard aGuard
;
215 mpWindow
->LeaveWait();
219 VclPtr
<vcl::Window
> mpWindow
;
223 sal_Bool SAL_CALL
ODBFilter::filter( const Sequence
< PropertyValue
>& rDescriptor
)
225 FocusWindowWaitGuard aWindowFocusGuard
;
228 if ( GetModel().is() )
229 bRet
= implImport( rDescriptor
);
235 bool ODBFilter::implImport( const Sequence
< PropertyValue
>& rDescriptor
)
238 ::comphelper::NamedValueCollection
aMediaDescriptor( rDescriptor
);
240 uno::Reference
<embed::XStorage
> xStorage
= GetSourceStorage();
245 if (aMediaDescriptor
.has(u
"URL"_ustr
))
246 sFileName
= aMediaDescriptor
.getOrDefault(u
"URL"_ustr
, OUString());
247 if (sFileName
.isEmpty() && aMediaDescriptor
.has(u
"FileName"_ustr
))
248 sFileName
= aMediaDescriptor
.getOrDefault(u
"FileName"_ustr
, sFileName
);
250 OSL_ENSURE(!sFileName
.isEmpty(), "ODBFilter::implImport: no URL given!");
251 bRet
= !sFileName
.isEmpty();
257 tools::SvRef
<SfxMedium
> pMedium
;
260 OUString sStreamRelPath
;
261 if (sFileName
.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:"))
263 // In this case the authority contains the real path, and the path is the embedded stream name.
264 auto const uri
= css::uri::UriReferenceFactory::create(GetComponentContext())
266 if (uri
.is() && uri
->isAbsolute()
267 && uri
->hasAuthority() && !uri
->hasQuery() && !uri
->hasFragment())
269 auto const auth
= uri
->getAuthority();
270 auto const decAuth
= rtl::Uri::decode(
271 auth
, rtl_UriDecodeStrict
, RTL_TEXTENCODING_UTF8
);
272 auto path
= uri
->getPath();
273 if (!path
.isEmpty()) {
274 assert(path
[0] == '/');
277 auto const decPath
= rtl::Uri::decode(
278 path
, rtl_UriDecodeStrict
, RTL_TEXTENCODING_UTF8
);
279 //TODO: really decode path?
280 if (auth
.isEmpty() == decAuth
.isEmpty() && path
.isEmpty() == decPath
.isEmpty())
282 // Decoding of auth and path to UTF-8 succeeded:
284 sStreamRelPath
= decPath
;
288 "<" << sFileName
<< "> cannot be parse as vnd.sun.star.pkg URL");
293 "<" << sFileName
<< "> cannot be parse as vnd.sun.star.pkg URL");
297 pMedium
= new SfxMedium(sFileName
, (StreamMode::READ
| StreamMode::NOCREATE
));
300 xStorage
.set(pMedium
->GetStorage(false), UNO_SET_THROW
);
302 if (!sStreamRelPath
.isEmpty())
303 xStorage
= xStorage
->openStorageElement(sStreamRelPath
, embed::ElementModes::READ
);
305 catch (const RuntimeException
&)
309 catch (const Exception
&)
311 Any aError
= ::cppu::getCaughtException();
312 throw lang::WrappedTargetRuntimeException(OUString(), *this, aError
);
316 uno::Reference
<sdb::XOfficeDatabaseDocument
> xOfficeDoc(GetModel(),UNO_QUERY_THROW
);
317 m_xDataSource
.set(xOfficeDoc
->getDataSource(),UNO_QUERY_THROW
);
318 uno::Reference
< XNumberFormatsSupplier
> xNum(m_xDataSource
->getPropertyValue(PROPERTY_NUMBERFORMATSSUPPLIER
),UNO_QUERY
);
319 SetNumberFormatsSupplier(xNum
);
321 uno::Reference
<XComponent
> xModel(GetModel());
322 ErrCode nRet
= ReadThroughComponent( xStorage
324 ,u
"settings.xml"_ustr
325 ,GetComponentContext()
329 if ( nRet
== ERRCODE_NONE
)
330 nRet
= ReadThroughComponent( xStorage
333 ,GetComponentContext()
337 bRet
= nRet
== ERRCODE_NONE
;
341 uno::Reference
< XModifiable
> xModi(GetModel(),UNO_QUERY
);
343 xModi
->setModified(false);
347 if ( nRet
== ERRCODE_IO_BROKENPACKAGE
)
348 ;// TODO/LATER: no way to transport the error outside from the filter!
351 // TODO/LATER: this is completely wrong! Filter code should never call ErrorHandler directly! But for now this is the only way!
352 ErrorHandler::HandleError( nRet
);
353 if( nRet
.IsWarning() )
364 class DBXMLDocumentSettingsContext
: public SvXMLImportContext
367 DBXMLDocumentSettingsContext(SvXMLImport
& rImport
)
368 : SvXMLImportContext(rImport
)
372 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
373 sal_Int32 nElement
, const uno::Reference
< xml::sax::XFastAttributeList
>& /*xAttrList*/ ) override
375 if (nElement
== XML_ELEMENT(OFFICE
, XML_SETTINGS
))
377 return new XMLDocumentSettingsContext(GetImport());
383 class DBXMLDocumentStylesContext
: public SvXMLImportContext
386 DBXMLDocumentStylesContext(SvXMLImport
& rImport
)
387 : SvXMLImportContext(rImport
)
391 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
392 sal_Int32 nElement
, const uno::Reference
< xml::sax::XFastAttributeList
>& /*xAttrList*/ ) override
394 ODBFilter
& rImport(static_cast<ODBFilter
&>(GetImport()));
397 case XML_ELEMENT(OFFICE
, XML_STYLES
):
398 case XML_ELEMENT(OOO
, XML_STYLES
):
399 rImport
.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
400 return rImport
.CreateStylesContext(false);
401 case XML_ELEMENT(OFFICE
, XML_AUTOMATIC_STYLES
):
402 case XML_ELEMENT(OOO
, XML_AUTOMATIC_STYLES
):
403 rImport
.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
404 return rImport
.CreateStylesContext(true);
410 class DBXMLDocumentBodyContext
: public SvXMLImportContext
413 DBXMLDocumentBodyContext(SvXMLImport
& rImport
)
414 : SvXMLImportContext(rImport
)
418 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
419 sal_Int32 nElement
, const uno::Reference
< xml::sax::XFastAttributeList
>& /*xAttrList*/ ) override
421 ODBFilter
& rImport(static_cast<ODBFilter
&>(GetImport()));
424 case XML_ELEMENT(OFFICE
, XML_DATABASE
):
425 case XML_ELEMENT(OOO
, XML_DATABASE
):
426 rImport
.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
427 return new OXMLDatabase(rImport
);
434 class DBXMLDocumentContentContext
: public SvXMLImportContext
437 DBXMLDocumentContentContext(SvXMLImport
& rImport
)
438 : SvXMLImportContext(rImport
)
442 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
443 sal_Int32 nElement
, const uno::Reference
< xml::sax::XFastAttributeList
>& /*xAttrList*/ ) override
445 ODBFilter
& rImport(static_cast<ODBFilter
&>(GetImport()));
448 case XML_ELEMENT(OFFICE
, XML_BODY
):
449 case XML_ELEMENT(OOO
, XML_BODY
):
450 return new DBXMLDocumentBodyContext(rImport
);
451 case XML_ELEMENT(OFFICE
, XML_SCRIPTS
):
452 return new XMLScriptContext(GetImport(), rImport
.GetModel());
453 case XML_ELEMENT(OFFICE
, XML_AUTOMATIC_STYLES
):
454 case XML_ELEMENT(OOO
, XML_AUTOMATIC_STYLES
):
455 rImport
.GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
456 return rImport
.CreateStylesContext(true);
465 SvXMLImportContext
* ODBFilter::CreateFastContext(sal_Int32 nElement
,
466 const ::css::uno::Reference
< ::css::xml::sax::XFastAttributeList
>& /*xAttrList*/ )
468 SvXMLImportContext
*pContext
= nullptr;
472 case XML_ELEMENT(OFFICE
, XML_DOCUMENT_SETTINGS
):
473 case XML_ELEMENT(OOO
, XML_DOCUMENT_SETTINGS
):
474 GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
475 pContext
= new DBXMLDocumentSettingsContext(*this);
477 case XML_ELEMENT(OFFICE
, XML_DOCUMENT_STYLES
):
478 case XML_ELEMENT(OOO
, XML_DOCUMENT_STYLES
):
479 pContext
= new DBXMLDocumentStylesContext(*this);
481 case XML_ELEMENT(OFFICE
, XML_DOCUMENT_CONTENT
):
482 case XML_ELEMENT(OOO
, XML_DOCUMENT_CONTENT
):
483 pContext
= new DBXMLDocumentContentContext(*this);
491 void ODBFilter::SetViewSettings(const Sequence
<PropertyValue
>& aViewProps
)
493 for (auto& prop
: aViewProps
)
495 if (prop
.Name
== "Queries")
497 fillPropertyMap(prop
.Value
, m_aQuerySettings
);
499 else if (prop
.Name
== "Tables")
501 fillPropertyMap(prop
.Value
, m_aTablesSettings
);
507 void ODBFilter::SetConfigurationSettings(const Sequence
<PropertyValue
>& aConfigProps
)
509 for (auto& prop
: aConfigProps
)
511 if (prop
.Name
== "layout-settings")
513 Sequence
<PropertyValue
> aWindows
;
514 prop
.Value
>>= aWindows
;
515 uno::Reference
<XPropertySet
> xProp(getDataSource());
517 xProp
->setPropertyValue(PROPERTY_LAYOUTINFORMATION
,Any(aWindows
));
523 void ODBFilter::fillPropertyMap(const Any
& _rValue
,TPropertyNameMap
& _rMap
)
525 Sequence
<PropertyValue
> aWindows
;
526 _rValue
>>= aWindows
;
527 for (auto& window
: aWindows
)
529 Sequence
<PropertyValue
> aValue
;
530 window
.Value
>>= aValue
;
531 _rMap
.emplace(window
.Name
, aValue
);
536 SvXMLImportContext
* ODBFilter::CreateStylesContext( bool bIsAutoStyle
)
538 SvXMLImportContext
*pContext
= new OTableStylesContext(*this, bIsAutoStyle
);
540 SetAutoStyles(static_cast<SvXMLStylesContext
*>(pContext
));
542 SetStyles(static_cast<SvXMLStylesContext
*>(pContext
));
548 rtl::Reference
< XMLPropertySetMapper
> const & ODBFilter::GetTableStylesPropertySetMapper() const
550 if ( !m_xTableStylesPropertySetMapper
.is() )
552 m_xTableStylesPropertySetMapper
= OXMLHelper::GetTableStylesPropertySetMapper( false);
554 return m_xTableStylesPropertySetMapper
;
558 rtl::Reference
< XMLPropertySetMapper
> const & ODBFilter::GetColumnStylesPropertySetMapper() const
560 if ( !m_xColumnStylesPropertySetMapper
.is() )
562 m_xColumnStylesPropertySetMapper
= OXMLHelper::GetColumnStylesPropertySetMapper( false);
564 return m_xColumnStylesPropertySetMapper
;
568 rtl::Reference
< XMLPropertySetMapper
> const & ODBFilter::GetCellStylesPropertySetMapper() const
570 if ( !m_xCellStylesPropertySetMapper
.is() )
572 m_xCellStylesPropertySetMapper
= OXMLHelper::GetCellStylesPropertySetMapper( false);
574 return m_xCellStylesPropertySetMapper
;
578 void ODBFilter::setPropertyInfo()
580 Reference
<XPropertySet
> xDataSource(getDataSource());
581 if ( !xDataSource
.is() )
584 ::connectivity::DriversConfig
aDriverConfig(GetComponentContext());
585 const OUString sURL
= ::comphelper::getString(xDataSource
->getPropertyValue(PROPERTY_URL
));
586 ::comphelper::NamedValueCollection aDataSourceSettings
= aDriverConfig
.getProperties( sURL
);
588 Sequence
<PropertyValue
> aInfo
;
589 if ( !m_aInfoSequence
.empty() )
590 aInfo
= comphelper::containerToSequence(m_aInfoSequence
);
591 aDataSourceSettings
.merge( ::comphelper::NamedValueCollection( aInfo
), true );
593 aDataSourceSettings
>>= aInfo
;
594 if ( aInfo
.hasElements() )
598 xDataSource
->setPropertyValue(PROPERTY_INFO
,Any(aInfo
));
600 catch (const Exception
&)
602 DBG_UNHANDLED_EXCEPTION("dbaccess");
607 } // namespace dbaxml
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */