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( 3 );
101  aArgs[0] <<= xDocHandler;
102  aArgs[1] <<= xPropSet;
103  aArgs[2] <<= GetModel();
104 
105  // get filter component
106  xDocHandler.set(
107  xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
108  "com.sun.star.comp.Oasis2OOoTransformer", aArgs, xContext),
109  uno::UNO_QUERY_THROW );
110 
111  SetDocHandler( xDocHandler );
112  }
113  catch( css::uno::Exception& )
114  {
115  OSL_FAIL( "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!");
116  }
117  }
118 
119 
120  xDocHandler->startDocument();
121 
123 
124  {
125 
126  const SvXMLNamespaceMap& rMap = GetNamespaceMap();
127  sal_uInt16 nPos = rMap.GetFirstKey();
128  while( USHRT_MAX != nPos )
129  {
130  GetAttrList().AddAttribute( rMap.GetAttrNameByKey( nPos ), rMap.GetNameByKey( nPos ) );
131  nPos = GetNamespaceMap().GetNextKey( nPos );
132  }
133 
134  const char*const pVersion = GetODFVersionAttributeValue();
135 
136  if( pVersion )
138  OUString::createFromAscii(pVersion) );
139 
141  true, true );
142 
143  // NB: office:meta is now written by _ExportMeta
144  ExportMeta_();
145  }
146  xDocHandler->endDocument();
147  return ERRCODE_NONE;
148 }
149 
151 {
152  if (mxDocProps.is()) {
153  OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
154  // update generator here
155  mxDocProps->setGenerator(generator);
157  pMeta->Export();
158  } else {
160  }
161 }
162 
163 // methods without content:
167 
168 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
170  css::uno::XComponentContext *context,
171  css::uno::Sequence<css::uno::Any> const &)
172 {
173  return cppu::acquire(new XMLMetaExportComponent(context, "XMLMetaExportComponent", SvXMLExportFlags::META|SvXMLExportFlags::OASIS));
174 }
175 
176 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
177 XMLMetaExportOOo_get_implementation(css::uno::XComponentContext* context,
178  css::uno::Sequence<css::uno::Any> const&)
179 {
180  return cppu::acquire(
181  new XMLMetaExportComponent(context, "XMLMetaExportOOo", SvXMLExportFlags::META));
182 }
183 
184 /* 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:1215
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:1205
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:557
sal_uInt16 GetFirstKey() const
OUString GetAttrNameByKey(sal_uInt16 nKey) const
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
Definition: xmlexp.hxx:385
SvXMLAttributeList & GetAttrList()
Definition: xmlexp.hxx:380
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:414
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:909
static OUString GetGeneratorString()
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
const css::uno::Reference< css::uno::XComponentContext > & getComponentContext() const
Definition: xmlexp.hxx:520
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:1441
void SetDocHandler(const css::uno::Reference< css::xml::sax::XDocumentHandler > &rHandler)
Definition: xmlexp.cxx:299
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:391
const OUString & GetNameByKey(sal_uInt16 nKey) const
const css::uno::Reference< css::beans::XPropertySet > & getExportInfo() const
Definition: xmlexp.hxx:449
Reference< XPropertySet > PropertySetMerger_CreateInstance(const Reference< XPropertySet > &rPropSet1, const Reference< XPropertySet > &rPropSet2)
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:3378
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Handling of tokens in XML:
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:143
SvXMLExportFlags getExportFlags() const
Definition: xmlexp.hxx:476
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