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 "MetaExportComponent.hxx"
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/util/MeasureUnit.hpp>
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <comphelper/genericpropertyset.hxx>
31 #include <comphelper/processfactory.hxx>
32 #include <rtl/ustrbuf.hxx>
33 #include <xmloff/xmlnmspe.hxx>
34 #include <xmloff/nmspmap.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlmetae.hxx>
37 #include "PropertySetMerger.hxx"
39 #include <unotools/docinfohelper.hxx>
42 using namespace ::com::sun::star
;
43 using namespace ::xmloff::token
;
45 XMLMetaExportComponent::XMLMetaExportComponent(
46 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
47 OUString
const & implementationName
, SvXMLExportFlags nFlags
)
48 : SvXMLExport( util::MeasureUnit::CM
, xContext
, implementationName
, XML_TEXT
, nFlags
)
52 XMLMetaExportComponent::~XMLMetaExportComponent()
56 void SAL_CALL
XMLMetaExportComponent::setSourceDocument( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
>& xDoc
) throw(::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
60 SvXMLExport::setSourceDocument( xDoc
);
62 catch( lang::IllegalArgumentException
& )
64 // allow to use document properties service without model access
65 // this is required for document properties exporter
67 uno::Reference
< document::XDocumentProperties
>::query( xDoc
);
68 if( !mxDocProps
.is() )
69 throw lang::IllegalArgumentException();
73 sal_uInt32
XMLMetaExportComponent::exportDoc( enum XMLTokenEnum
)
75 uno::Reference
< xml::sax::XDocumentHandler
> xDocHandler
= GetDocHandler();
77 if( !(getExportFlags() & SvXMLExportFlags::OASIS
) )
79 uno::Reference
< uno::XComponentContext
> xContext
= getComponentContext();
82 ::comphelper::PropertyMapEntry
const aInfoMap
[] =
84 { OUString("Class"), 0,
85 ::cppu::UnoType
<OUString
>::get(),
86 beans::PropertyAttribute::MAYBEVOID
, 0},
87 { OUString(), 0, css::uno::Type(), 0, 0 }
89 uno::Reference
< beans::XPropertySet
> xConvPropSet(
90 ::comphelper::GenericPropertySet_CreateInstance(
91 new ::comphelper::PropertySetInfo( aInfoMap
) ) );
93 xConvPropSet
->setPropertyValue("Class", uno::Any(GetXMLToken( XML_TEXT
)) );
95 uno::Reference
< beans::XPropertySet
> xPropSet
=
97 ? PropertySetMerger_CreateInstance( getExportInfo(),
101 uno::Sequence
< uno::Any
> aArgs( 3 );
102 aArgs
[0] <<= xDocHandler
;
103 aArgs
[1] <<= xPropSet
;
104 aArgs
[2] <<= GetModel();
106 // get filter component
108 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
109 "com.sun.star.comp.Oasis2OOoTransformer", aArgs
, xContext
),
110 uno::UNO_QUERY_THROW
);
112 SetDocHandler( xDocHandler
);
114 catch( css::uno::Exception
& )
116 OSL_FAIL( "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!\n");
121 xDocHandler
->startDocument();
123 addChaffWhenEncryptedStorage();
127 const SvXMLNamespaceMap
& rMap
= GetNamespaceMap();
128 sal_uInt16 nPos
= rMap
.GetFirstKey();
129 while( USHRT_MAX
!= nPos
)
131 GetAttrList().AddAttribute( rMap
.GetAttrNameByKey( nPos
), rMap
.GetNameByKey( nPos
) );
132 nPos
= GetNamespaceMap().GetNextKey( nPos
);
135 const sal_Char
* pVersion
= nullptr;
136 switch( getDefaultVersion() )
138 case SvtSaveOptions::ODFVER_LATEST
: pVersion
= "1.2"; break;
139 case SvtSaveOptions::ODFVER_012_EXT_COMPAT
: pVersion
= "1.2"; break;
140 case SvtSaveOptions::ODFVER_012
: pVersion
= "1.2"; break;
141 case SvtSaveOptions::ODFVER_011
: pVersion
= "1.1"; break;
142 case SvtSaveOptions::ODFVER_010
: break;
145 OSL_FAIL("xmloff::XMLMetaExportComponent::exportDoc(), unexpected odf default version!");
149 AddAttribute( XML_NAMESPACE_OFFICE
, XML_VERSION
,
150 OUString::createFromAscii(pVersion
) );
152 SvXMLElementExport
aDocElem( *this, XML_NAMESPACE_OFFICE
, XML_DOCUMENT_META
,
155 // NB: office:meta is now written by _ExportMeta
158 xDocHandler
->endDocument();
162 void XMLMetaExportComponent::ExportMeta_()
164 if (mxDocProps
.is()) {
165 OUString
generator( ::utl::DocInfoHelper::GetGeneratorString() );
166 // update generator here
167 mxDocProps
->setGenerator(generator
);
168 SvXMLMetaExport
* pMeta
= new SvXMLMetaExport(*this, mxDocProps
);
169 uno::Reference
<xml::sax::XDocumentHandler
> xMeta(pMeta
);
172 SvXMLExport::ExportMeta_();
176 // methods without content:
177 void XMLMetaExportComponent::ExportAutoStyles_() {}
178 void XMLMetaExportComponent::ExportMasterStyles_() {}
179 void XMLMetaExportComponent::ExportContent_() {}
181 uno::Sequence
< OUString
> SAL_CALL
XMLMetaExportOOO_getSupportedServiceNames()
184 return uno::Sequence
< OUString
> { "com.sun.star.document.XMLMetaExporter" };
187 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
188 XMLMetaExportComponent_get_implementation(
189 css::uno::XComponentContext
*context
,
190 css::uno::Sequence
<css::uno::Any
> const &)
192 return cppu::acquire(new XMLMetaExportComponent(context
, "XMLMetaExportComponent", SvXMLExportFlags::META
|SvXMLExportFlags::OASIS
));
195 OUString SAL_CALL
XMLMetaExportOOO_getImplementationName() throw()
197 return OUString( "XMLMetaExportOOo" );
200 uno::Reference
< uno::XInterface
> SAL_CALL
XMLMetaExportOOO_createInstance(
201 const uno::Reference
< lang::XMultiServiceFactory
> & rSMgr
)
202 throw( uno::Exception
)
204 return static_cast<cppu::OWeakObject
*>(new XMLMetaExportComponent( comphelper::getComponentContext(rSMgr
), XMLMetaExportOOO_getImplementationName(), SvXMLExportFlags::META
));
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */