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 <com/sun/star/xml/sax/XDocumentHandler.hpp>
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/Exception.hpp>
25 #include <com/sun/star/util/MeasureUnit.hpp>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <osl/diagnose.h>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/namespacemap.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmlmetae.hxx>
36 #include <PropertySetMerger.hxx>
37 
39 
40 
41 using namespace ::com::sun::star;
42 using namespace ::xmloff::token;
43 
45  const css::uno::Reference< css::uno::XComponentContext >& xContext,
46  OUString const & implementationName, SvXMLExportFlags nFlags )
47 : SvXMLExport( xContext, implementationName, util::MeasureUnit::CM, XML_TEXT, nFlags )
48 {
49 }
50 
52 {
53 }
54 
55 void SAL_CALL XMLMetaExportComponent::setSourceDocument( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& xDoc )
56 {
57  try
58  {
60  }
61  catch( lang::IllegalArgumentException& )
62  {
63  // allow to use document properties service without model access
64  // this is required for document properties exporter
65  mxDocProps =
66  uno::Reference< document::XDocumentProperties >::query( xDoc );
67  if( !mxDocProps.is() )
68  throw lang::IllegalArgumentException();
69  }
70 }
71 
73 {
74  uno::Reference< xml::sax::XDocumentHandler > xDocHandler = GetDocHandler();
75 
77  {
78  uno::Reference< uno::XComponentContext > xContext = getComponentContext();
79  try
80  {
81  ::comphelper::PropertyMapEntry const aInfoMap[] =
82  {
83  { OUString("Class"), 0,
85  beans::PropertyAttribute::MAYBEVOID, 0},
86  { OUString(), 0, css::uno::Type(), 0, 0 }
87  };
88  uno::Reference< beans::XPropertySet > xConvPropSet(
90  new ::comphelper::PropertySetInfo( aInfoMap ) ) );
91 
92  xConvPropSet->setPropertyValue("Class", uno::Any(GetXMLToken( XML_TEXT )) );
93 
94  uno::Reference< beans::XPropertySet > xPropSet =
95  getExportInfo().is()
97  xConvPropSet )
98  : getExportInfo();
99 
100  uno::Sequence< uno::Any > aArgs{ uno::Any(xDocHandler), uno::Any(xPropSet),
101  uno::Any(GetModel()) };
102 
103  // get filter component
104  xDocHandler.set(
105  xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
106  "com.sun.star.comp.Oasis2OOoTransformer", aArgs, xContext),
107  uno::UNO_QUERY_THROW );
108 
109  SetDocHandler( xDocHandler );
110  }
111  catch( css::uno::Exception& )
112  {
113  OSL_FAIL( "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!");
114  }
115  }
116 
117 
118  xDocHandler->startDocument();
119 
121 
122  {
123 
124  const SvXMLNamespaceMap& rMap = GetNamespaceMap();
125  sal_uInt16 nPos = rMap.GetFirstKey();
126  while( USHRT_MAX != nPos )
127  {
128  GetAttrList().AddAttribute( rMap.GetAttrNameByKey( nPos ), rMap.GetNameByKey( nPos ) );
129  nPos = GetNamespaceMap().GetNextKey( nPos );
130  }
131 
132  const char*const pVersion = GetODFVersionAttributeValue();
133 
134  if( pVersion )
136  OUString::createFromAscii(pVersion) );
137 
139  true, true );
140 
141  // NB: office:meta is now written by _ExportMeta
142  ExportMeta_();
143  }
144  xDocHandler->endDocument();
145  return ERRCODE_NONE;
146 }
147 
149 {
150  if (mxDocProps.is()) {
151  OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
152  // update generator here
153  mxDocProps->setGenerator(generator);
155  pMeta->Export();
156  } else {
158  }
159 }
160 
161 // methods without content:
165 
166 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
168  css::uno::XComponentContext *context,
169  css::uno::Sequence<css::uno::Any> const &)
170 {
171  return cppu::acquire(new XMLMetaExportComponent(context, "XMLMetaExportComponent", SvXMLExportFlags::META|SvXMLExportFlags::OASIS));
172 }
173 
174 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
175 XMLMetaExportOOo_get_implementation(css::uno::XComponentContext* context,
176  css::uno::Sequence<css::uno::Any> const&)
177 {
178  return cppu::acquire(
179  new XMLMetaExportComponent(context, "XMLMetaExportOOo", SvXMLExportFlags::META));
180 }
181 
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
exports com.sun.star.lib. util
virtual void ExportMasterStyles_() override
Override this method to export the contents of .
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
virtual ~XMLMetaExportComponent() override
char const * GetODFVersionAttributeValue() const
returns value of ODF version attribute
Definition: xmlexp.cxx:1200
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:1190
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
SvXMLExportFlags
Definition: xmlexp.hxx:91
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: xmlexp.cxx:574
sal_uInt16 GetFirstKey() const
OUString GetAttrNameByKey(sal_uInt16 nKey) const
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
Definition: xmlexp.hxx:384
SvXMLAttributeList & GetAttrList()
Definition: xmlexp.hxx:379
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:416
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:912
static OUString GetGeneratorString()
const css::uno::Reference< css::uno::XComponentContext > & getComponentContext() const
Definition: xmlexp.hxx:522
COMPHELPER_DLLPUBLIC css::uno::Reference< css::beans::XPropertySet > GenericPropertySet_CreateInstance(PropertySetInfo *pInfo)
export meta data from an XDocumentProperties instance.
Definition: xmlmetae.hxx:49
virtual ErrCode exportDoc(enum::xmloff::token::XMLTokenEnum eClass=xmloff::token::XML_TOKEN_INVALID) override
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
virtual void ExportMeta_()
Override this method to export the content of .
Definition: xmlexp.cxx:1422
void SetDocHandler(const css::uno::Reference< css::xml::sax::XDocumentHandler > &rHandler)
Definition: xmlexp.cxx:286
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:390
const OUString & GetNameByKey(sal_uInt16 nKey) const
const css::uno::Reference< css::beans::XPropertySet > & getExportInfo() const
Definition: xmlexp.hxx:451
virtual void ExportMeta_() override
Override this method to export the content of .
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:3510
Handling of tokens in XML:
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * XMLMetaExportOOo_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void AddAttribute(const OUString &sName, const OUString &sValue)
Definition: attrlist.cxx:110
Reference< XPropertySet > PropertySetMerger_CreateInstance(const Reference< XPropertySet > &rPropSet1, const Reference< XPropertySet > &rPropSet2) noexcept
SvXMLExportFlags getExportFlags() const
Definition: xmlexp.hxx:478
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