LibreOffice Module xmloff (master)  1
MetaExportComponent.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <MetaExportComponent.hxx>
21 #include <facreg.hxx>
22 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/uno/Exception.hpp>
27 #include <com/sun/star/util/MeasureUnit.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <osl/diagnose.h>
35 #include <xmloff/xmlnmspe.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/xmlmetae.hxx>
39 #include <PropertySetMerger.hxx>
40 
42 
43 
44 using namespace ::com::sun::star;
45 using namespace ::xmloff::token;
46 
48  const css::uno::Reference< css::uno::XComponentContext >& xContext,
49  OUString const & implementationName, SvXMLExportFlags nFlags )
50 : SvXMLExport( util::MeasureUnit::CM, xContext, implementationName, XML_TEXT, nFlags )
51 {
52 }
53 
55 {
56 }
57 
58 void SAL_CALL XMLMetaExportComponent::setSourceDocument( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& xDoc )
59 {
60  try
61  {
63  }
64  catch( lang::IllegalArgumentException& )
65  {
66  // allow to use document properties service without model access
67  // this is required for document properties exporter
68  mxDocProps =
69  uno::Reference< document::XDocumentProperties >::query( xDoc );
70  if( !mxDocProps.is() )
71  throw lang::IllegalArgumentException();
72  }
73 }
74 
76 {
77  uno::Reference< xml::sax::XDocumentHandler > xDocHandler = GetDocHandler();
78 
80  {
81  uno::Reference< uno::XComponentContext > xContext = getComponentContext();
82  try
83  {
84  ::comphelper::PropertyMapEntry const aInfoMap[] =
85  {
86  { OUString("Class"), 0,
88  beans::PropertyAttribute::MAYBEVOID, 0},
89  { OUString(), 0, css::uno::Type(), 0, 0 }
90  };
91  uno::Reference< beans::XPropertySet > xConvPropSet(
93  new ::comphelper::PropertySetInfo( aInfoMap ) ) );
94 
95  xConvPropSet->setPropertyValue("Class", uno::Any(GetXMLToken( XML_TEXT )) );
96 
97  uno::Reference< beans::XPropertySet > xPropSet =
98  getExportInfo().is()
100  xConvPropSet )
101  : getExportInfo();
102 
103  uno::Sequence< uno::Any > aArgs( 3 );
104  aArgs[0] <<= xDocHandler;
105  aArgs[1] <<= xPropSet;
106  aArgs[2] <<= GetModel();
107 
108  // get filter component
109  xDocHandler.set(
110  xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
111  "com.sun.star.comp.Oasis2OOoTransformer", aArgs, xContext),
112  uno::UNO_QUERY_THROW );
113 
114  SetDocHandler( xDocHandler );
115  }
116  catch( css::uno::Exception& )
117  {
118  OSL_FAIL( "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!");
119  }
120  }
121 
122 
123  xDocHandler->startDocument();
124 
126 
127  {
128 
129  const SvXMLNamespaceMap& rMap = GetNamespaceMap();
130  sal_uInt16 nPos = rMap.GetFirstKey();
131  while( USHRT_MAX != nPos )
132  {
133  GetAttrList().AddAttribute( rMap.GetAttrNameByKey( nPos ), rMap.GetNameByKey( nPos ) );
134  nPos = GetNamespaceMap().GetNextKey( nPos );
135  }
136 
137  const char* pVersion = nullptr;
138  switch( getDefaultVersion() )
139  {
140  case SvtSaveOptions::ODFVER_LATEST: pVersion = "1.2"; break;
141  case SvtSaveOptions::ODFVER_012_EXT_COMPAT: pVersion = "1.2"; break;
142  case SvtSaveOptions::ODFVER_012: pVersion = "1.2"; break;
143  case SvtSaveOptions::ODFVER_011: pVersion = "1.1"; break;
144  case SvtSaveOptions::ODFVER_010: break;
145 
146  default:
147  OSL_FAIL("xmloff::XMLMetaExportComponent::exportDoc(), unexpected odf default version!");
148  }
149 
150  if( pVersion )
152  OUString::createFromAscii(pVersion) );
153 
155  true, true );
156 
157  // NB: office:meta is now written by _ExportMeta
158  ExportMeta_();
159  }
160  xDocHandler->endDocument();
161  return ERRCODE_NONE;
162 }
163 
165 {
166  if (mxDocProps.is()) {
167  OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
168  // update generator here
169  mxDocProps->setGenerator(generator);
171  pMeta->Export();
172  } else {
174  }
175 }
176 
177 // methods without content:
181 
182 uno::Sequence< OUString > XMLMetaExportOOO_getSupportedServiceNames()
183  throw()
184 {
185  return uno::Sequence< OUString > { "com.sun.star.document.XMLMetaExporter" };
186 }
187 
188 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
190  css::uno::XComponentContext *context,
191  css::uno::Sequence<css::uno::Any> const &)
192 {
193  return cppu::acquire(new XMLMetaExportComponent(context, "XMLMetaExportComponent", SvXMLExportFlags::META|SvXMLExportFlags::OASIS));
194 }
195 
197 {
198  return "XMLMetaExportOOo";
199 }
200 
201 uno::Reference< uno::XInterface > XMLMetaExportOOO_createInstance(
202  const uno::Reference< lang::XMultiServiceFactory > & rSMgr)
203 {
205 }
206 
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void ExportMasterStyles_() override
Override this method to export the contents of .
virtual ~XMLMetaExportComponent() override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * XMLMetaExportComponent_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void addChaffWhenEncryptedStorage()
Definition: xmlexp.cxx:1222
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
SvXMLExportFlags
Definition: xmlexp.hxx:89
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: xmlexp.cxx:569
sal_uInt16 GetFirstKey() const
Definition: nmspmap.cxx:360
OUString GetAttrNameByKey(sal_uInt16 nKey) const
Definition: nmspmap.cxx:174
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
Definition: xmlexp.hxx:386
SvXMLAttributeList & GetAttrList()
Definition: xmlexp.hxx:381
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:415
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:926
static OUString GetGeneratorString()
SvtSaveOptions::ODFDefaultVersion getDefaultVersion() const
returns the currently configured default version for odf export
Definition: xmlexp.cxx:2303
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:47
const css::uno::Reference< css::uno::XComponentContext > & getComponentContext() const
Definition: xmlexp.hxx:521
COMPHELPER_DLLPUBLIC css::uno::Reference< css::beans::XPropertySet > GenericPropertySet_CreateInstance(PropertySetInfo *pInfo)
export meta data from an XDocumentProperties instance.
Definition: xmlmetae.hxx:50
virtual ErrCode exportDoc(enum::xmloff::token::XMLTokenEnum eClass=xmloff::token::XML_TOKEN_INVALID) override
uno::Sequence< OUString > XMLMetaExportOOO_getSupportedServiceNames()
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
css::uno::Type const & get()
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
Definition: nmspmap.cxx:365
virtual void ExportMeta_()
Override this method to export the content of .
Definition: xmlexp.cxx:1450
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
void SetDocHandler(const css::uno::Reference< css::xml::sax::XDocumentHandler > &rHandler)
Definition: xmlexp.cxx:303
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:392
const OUString & GetNameByKey(sal_uInt16 nKey) const
Definition: nmspmap.cxx:168
const css::uno::Reference< css::beans::XPropertySet > & getExportInfo() const
Definition: xmlexp.hxx:450
Reference< XPropertySet > PropertySetMerger_CreateInstance(const Reference< XPropertySet > &rPropSet1, const Reference< XPropertySet > &rPropSet2)
virtual void ExportMeta_() override
Override this method to export the content of .
OUString XMLMetaExportOOO_getImplementationName()
virtual void ExportContent_() override
Override this method to export the content of .
#define ERRCODE_NONE
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3366
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
uno::Reference< uno::XInterface > XMLMetaExportOOO_createInstance(const uno::Reference< lang::XMultiServiceFactory > &rSMgr)
Handling of tokens in XML:
void AddAttribute(const OUString &sName, const OUString &sValue)
Definition: attrlist.cxx:143
SvXMLExportFlags getExportFlags() const
Definition: xmlexp.hxx:477
XMLMetaExportComponent(const css::uno::Reference< css::uno::XComponentContext > &xContext, OUString const &implementationName, SvXMLExportFlags nFlags)
virtual void ExportAutoStyles_() override
Override this method to export the contents of .
sal_uInt16 nPos
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
Definition: xmlnmspe.hxx:29